Skip to content

Commit

Permalink
Merge pull request #486 from koxudaxi/fix_idea_lsp_server_on_wsl
Browse files Browse the repository at this point in the history
Fix idea lsp server on wsl
  • Loading branch information
koxudaxi committed Sep 2, 2024
2 parents 8ca1fff + 86921ca commit e1bd39d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Unreleased]
- Fix idea lsp server on wsl [[#486](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/#486)]
- Support LSP4IJ, which is a third-party LSP server integration [[#437](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/#437)]
- Use preview only for non-stable server versions (<0.5.3) [[#481](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/#481)]
- Use rule or --explain dependent on version. [[#479](https://github.com/koxudaxi/pydantic-pycharm-plugin/pull/#479)]
Expand Down
46 changes: 25 additions & 21 deletions src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -249,33 +249,37 @@ fun runCommand(
)


fun getGeneralCommandLine(command: List<String>, projectPath: String?): GeneralCommandLine =
private fun getGeneralCommandLine(command: List<String>, projectPath: String?): GeneralCommandLine =
GeneralCommandLine(command).withWorkDirectory(projectPath).withCharset(Charsets.UTF_8)

fun getGeneralCommandLine(executable: File, project: Project?, vararg args: String): GeneralCommandLine? {
val projectPath = project?.basePath ?: return null
if (!WslPath.isWslUncPath(executable.path)) {
return getGeneralCommandLine(listOf(executable.path) + args, projectPath)
}
val windowsUncPath = WslPath.parseWindowsUncPath(executable.path) ?: return null
val configArgIndex = args.indexOf(CONFIG_ARG)
val injectedArgs = if (configArgIndex != -1 && configArgIndex < args.size - 1) {
val configPathIndex = configArgIndex + 1
val configPath = args[configPathIndex]
val windowsUncConfigPath = WslPath.parseWindowsUncPath(configPath)?.linuxPath ?: configPath
args.toMutableList().apply { this[configPathIndex] = windowsUncConfigPath }.toTypedArray()
} else {
args
}
val commandLine = getGeneralCommandLine(listOf(windowsUncPath.linuxPath) + injectedArgs, projectPath)
val options = WSLCommandLineOptions()
options.setExecuteCommandInShell(false)
options.setLaunchWithWslExe(true)
return windowsUncPath.distribution.patchCommandLine<GeneralCommandLine>(commandLine, project, options)
}

fun runCommand(
executable: File, project: Project?, stdin: ByteArray?, vararg args: String
): String? {
val projectPath = project?.basePath
val indicator = ProgressManager.getInstance().progressIndicator
val handler = if (WslPath.isWslUncPath(executable.path)) {
val windowsUncPath = WslPath.parseWindowsUncPath(executable.path) ?: return null
val configArgIndex = args.indexOf(CONFIG_ARG)
val injectedArgs = if (configArgIndex != -1 && configArgIndex < args.size - 1) {
val configPathIndex = configArgIndex + 1
val configPath = args[configPathIndex]
val windowsUncConfigPath = WslPath.parseWindowsUncPath(configPath)?.linuxPath ?: configPath
args.toMutableList().apply { this[configPathIndex] = windowsUncConfigPath }.toTypedArray()
} else {
args
}
val commandLine = getGeneralCommandLine(listOf(windowsUncPath.linuxPath) + injectedArgs, projectPath)
val options = WSLCommandLineOptions()
options.setExecuteCommandInShell(false)
options.setLaunchWithWslExe(true)
windowsUncPath.distribution.patchCommandLine<GeneralCommandLine>(commandLine, project, options)
} else {
getGeneralCommandLine(listOf(executable.path) + args, projectPath)
}.let { CapturingProcessHandler(it) }
val handler = getGeneralCommandLine(executable, project, *args)
?.let { CapturingProcessHandler(it) } ?: return null

val result = with(handler) {
if (stdin is ByteArray) {
Expand Down
34 changes: 24 additions & 10 deletions src/com/koxudaxi/ruff/lsp/intellij/RuffLspServerSupportProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,22 @@ class RuffLspServerSupportProvider : LspServerSupportProvider {
if (ruffCacheService.getVersion() == null) return
val descriptor = when {
ruffConfigService.useRuffServer && ruffCacheService.hasLsp() == true -> {
getRuffExecutable(project, ruffConfigService , false)?.let { RuffServerServerDescriptor(project, it, ruffConfigService) }
getRuffExecutable(project, ruffConfigService, false)?.let {
RuffServerServerDescriptor(
project,
it,
ruffConfigService
)
}
}

else -> getRuffExecutable(project, ruffConfigService, true)?.let {
RuffLspServerDescriptor(
project,
it,
ruffConfigService
)
}
else -> getRuffExecutable(project, ruffConfigService, true)?.let { RuffLspServerDescriptor(project, it, ruffConfigService) }
} ?: return
serverStarter.ensureServerStarted(descriptor)
}
Expand All @@ -38,11 +51,11 @@ class RuffLspServerSupportProvider : LspServerSupportProvider {
@Suppress("UnstableApiUsage")
private class RuffLspServerDescriptor(project: Project, executable: File, ruffConfig: RuffConfigService) :
RuffLspServerDescriptorBase(project, executable, ruffConfig) {
private fun createBaseCommandLine(): GeneralCommandLine = GeneralCommandLine(executable.absolutePath)
private fun createBaseCommandLine(): GeneralCommandLine? =
getGeneralCommandLine(executable, project)

override fun createCommandLine(): GeneralCommandLine {
val commandLine = createBaseCommandLine()
commandLine.withWorkDirectory(project.basePath)
val commandLine = createBaseCommandLine() ?: return GeneralCommandLine()
if (ruffConfig.useRuffFormat) {
return commandLine.withEnvironment("RUFF_EXPERIMENTAL_FORMATTER", "1")
}
Expand All @@ -57,10 +70,7 @@ abstract class RuffLspServerDescriptorBase(project: Project, val executable: Fil

override fun isSupportedFile(file: VirtualFile) = file.extension == "py"
abstract override fun createCommandLine(): GeneralCommandLine
override fun createInitializationOptions(): Any? {
val configArgs = getConfigArgs(ruffConfig) ?: return null
return InitOptions(Settings(configArgs))
}


override val lspGoToDefinitionSupport: Boolean = false
override val lspCompletionSupport: LspCompletionSupport? = null
Expand All @@ -69,7 +79,11 @@ abstract class RuffLspServerDescriptorBase(project: Project, val executable: Fil
@Suppress("UnstableApiUsage")
private class RuffServerServerDescriptor(project: Project, executable: File, ruffConfig: RuffConfigService) :
RuffLspServerDescriptorBase(project, executable, ruffConfig) {
override fun createCommandLine(): GeneralCommandLine = GeneralCommandLine(listOf(executable.absolutePath) + project.LSP_ARGS)

override fun createCommandLine(): GeneralCommandLine {
val args = project.LSP_ARGS + (getConfigArgs(ruffConfig) ?: listOf())
return getGeneralCommandLine(executable, project, *args.toTypedArray()) ?: GeneralCommandLine()
}
}
@Serializable
data class Settings(
Expand Down
20 changes: 10 additions & 10 deletions src/com/koxudaxi/ruff/lsp/lsp4ij/RuffLanguageServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ class RuffLanguageServer(project: Project) : ProcessStreamConnectionProvider() {

val ruffCacheService = RuffCacheService.getInstance(project)
if (ruffCacheService.getVersion() == null) return null
val executable =
when {

val (ruffFile, args) = when {
ruffConfigService.useRuffServer && ruffCacheService.hasLsp() == true -> {
getRuffExecutable(project, ruffConfigService, false)
val executable = getRuffExecutable(project, ruffConfigService, false) ?: return null
executable to project.LSP_ARGS + (getConfigArgs(ruffConfigService) ?: listOf())
}
else -> {
val executable = getRuffExecutable(project, ruffConfigService, true) ?: return null
executable to listOf()
}
}
return getGeneralCommandLine(ruffFile, project, *args.toTypedArray())?.getCommandLineList(null)

else -> getRuffExecutable(project, ruffConfigService, true)
} ?: return null
val executableCommandArgs = listOf(executable.absolutePath) + project.LSP_ARGS

return getConfigArgs(ruffConfigService)?.let {
executableCommandArgs + it
} ?: executableCommandArgs
}
}
8 changes: 6 additions & 2 deletions src/com/koxudaxi/ruff/lsp/lsp4ij/RuffLanguageServerFactory.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.koxudaxi.ruff.lsp.lsp4ij
import com.intellij.execution.wsl.WslPath

Check warning on line 2 in src/com/koxudaxi/ruff/lsp/lsp4ij/RuffLanguageServerFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import com.intellij.openapi.project.Project
import com.koxudaxi.ruff.RuffCacheService
import com.koxudaxi.ruff.RuffConfigService
import com.koxudaxi.ruff.getRuffExecutable
import com.koxudaxi.ruff.isInspectionEnabled
import com.redhat.devtools.lsp4ij.LanguageServerEnablementSupport
import com.redhat.devtools.lsp4ij.LanguageServerFactory
Expand All @@ -26,9 +28,11 @@ class RuffLanguageServerFactory : LanguageServerFactory, LanguageServerEnablemen
if (!ruffConfigService.useRuffLsp && !ruffConfigService.useRuffServer) return false
if (!ruffConfigService.useLsp4ij) return false
if (!isInspectionEnabled(project)) return false

val ruffCacheService = RuffCacheService.getInstance(project)
return ruffCacheService.getVersion() != null
if (ruffCacheService.getVersion() == null) return false
if (ruffConfigService.useRuffServer && ruffCacheService.hasLsp() != true) return false
if (ruffConfigService.useRuffLsp && getRuffExecutable(project, ruffConfigService, true) == null ) return false
return true
}

override fun setEnabled(p0: Boolean, project: Project) {
Expand Down

0 comments on commit e1bd39d

Please sign in to comment.