Skip to content

Commit

Permalink
Merge pull request #412 from koxudaxi/improve_ruff_config_path_in_wsl
Browse files Browse the repository at this point in the history
Improve ruff config path in wsl
  • Loading branch information
koxudaxi authored Apr 7, 2024
2 parents e22b642 + 382bd80 commit 7e86410
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 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]
- Improve ruff config path in wsl [[#412](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/412)]
- Fix wsl detection on 2024.1 [[#414](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/414)]

## [0.0.32] - 2024-04-01
Expand Down
19 changes: 15 additions & 4 deletions src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ val RUFF_LSP_COMMAND = when {
}
const val WSL_RUFF_LSP_COMMAND = "ruff-lsp"

const val CONFIG_ARG = "--config"

val ruffVersionCache: ConcurrentHashMap<String, RuffVersion> = ConcurrentHashMap()

fun getRuffCommand(lsp: Boolean) = if (lsp) RUFF_LSP_COMMAND else RUFF_COMMAND
Expand Down Expand Up @@ -217,16 +219,25 @@ fun getGeneralCommandLine(command: List<String>, projectPath: String?): GeneralC
GeneralCommandLine(command).withWorkDirectory(projectPath).withCharset(Charsets.UTF_8)

fun runCommand(
executable: File, project: Project, stdin: ByteArray?, vararg args: String
executable: File, project: Project?, stdin: ByteArray?, vararg args: String
): String? {
val projectPath = project.basePath
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)
val commandLine = getGeneralCommandLine(listOf(windowsUncPath.linuxPath) + args, projectPath)
windowsUncPath.distribution.patchCommandLine<GeneralCommandLine>(commandLine, project, options)
} else {
getGeneralCommandLine(listOf(executable.path) + args, projectPath)
Expand Down Expand Up @@ -337,7 +348,7 @@ fun generateCommandArgs(
project, ruffConfigService, false
) ?: return null
val customConfigArgs = if (withoutConfig) null else ruffConfigService.ruffConfigPath?.let {
args + listOf("--config", it)
args + listOf(CONFIG_ARG, it)
}
return CommandArgs(
executable,
Expand Down
2 changes: 1 addition & 1 deletion src/com/koxudaxi/ruff/RuffLspServerSupportProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private class RuffLspServerDescriptor(project: Project, val executable: File, va

override fun createInitializationOptions(): Any? {
val config = ruffConfig.ruffConfigPath?.let { File(it) }?.takeIf { it.exists() } ?: return null
return InitOptions(Settings(listOf("--config", config.absolutePath)))
return InitOptions(Settings(listOf(CONFIG_ARG, config.absolutePath)))
}

override val lspGoToDefinitionSupport: Boolean = false
Expand Down

0 comments on commit 7e86410

Please sign in to comment.