Skip to content

Commit

Permalink
Merge pull request #53 from koxudaxi/fix_system_ruff_detection
Browse files Browse the repository at this point in the history
Fix system ruff detection
koxudaxi authored Jan 17, 2023
2 parents 979fff0 + 13c2a09 commit b465e49
Showing 4 changed files with 8 additions and 15 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 system ruff detection [[#53](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/53)]

## 0.0.4 - 2023-01-05
- Support fix message [[#43](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/43)]
16 changes: 5 additions & 11 deletions src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
@@ -101,16 +101,11 @@ fun runCommand(
}
}

fun runRuff(sdk: Sdk, stdin: ByteArray?, vararg args: String): String {
val projectPath = sdk.associatedModulePath
?: throw PyExecutionException(
"Cannot find the project associated with this Ruff environment",
"Ruff", emptyList(), ProcessOutput()
)
val executable = getRuffExecutableInSDK(sdk) ?: getRuffExecutable()
fun runRuff(module: Module, stdin: ByteArray?, vararg args: String): String {
val executable = module.pythonSdk?.let { getRuffExecutableInSDK(it) } ?: getRuffExecutable()
?: throw PyExecutionException("Cannot find Ruff", "ruff", emptyList(), ProcessOutput())

return runCommand(executable, projectPath, stdin, *args)
return runCommand(executable, module.basePath, stdin, *args)
}

inline fun <reified T> runRuffInBackground(
@@ -122,14 +117,13 @@ inline fun <reified T> runRuffInBackground(
): ProgressIndicator? {
val task = object : Task.Backgroundable(module.project, StringUtil.toTitleCase(description), true) {
override fun run(indicator: ProgressIndicator) {
val sdk = module.pythonSdk ?: return
indicator.text = "$description..."
val result: String? = try {
runRuff(sdk, stdin, *args.toTypedArray())
runRuff(module, stdin, *args.toTypedArray())
} catch (_: RunCanceledByUserException) {
null
} catch (e: ExecutionException) {
showSdkExecutionException(sdk, e, "Error Running Ruff")
showSdkExecutionException(module.pythonSdk, e, "Error Running Ruff")
null
}
callback(result)
3 changes: 1 addition & 2 deletions src/com/koxudaxi/ruff/RuffInspection.kt
Original file line number Diff line number Diff line change
@@ -35,11 +35,10 @@ class RuffInspection : PyInspection() {
private fun inspectFile(pyFile: PyFile) {
val document = PsiDocumentManager.getInstance(pyFile.project).getDocument(pyFile) ?: return
val module = ModuleUtil.findModuleForPsiElement(pyFile) ?: return
val sdk = module.pythonSdk ?: return
val stdin = pyFile.textToCharArray().toByteArrayAndClear()

val response = executeOnPooledThread(null) {
runRuff(sdk, stdin, *args.toTypedArray())
runRuff(module, stdin, *args.toTypedArray())
} ?: return
parseJsonResponse(response).forEach {
val psiElement = getPyElement(it, pyFile, document) ?: return@forEach
3 changes: 1 addition & 2 deletions src/com/koxudaxi/ruff/RuffPostFormatProcessor.kt
Original file line number Diff line number Diff line change
@@ -23,12 +23,11 @@ class RuffPostFormatProcessor : PostFormatProcessor {
val pyFile = source.containingFile
if (!pyFile.isApplicableTo) return TextRange.EMPTY_RANGE
val module = ModuleUtil.findModuleForPsiElement(source) ?: return TextRange.EMPTY_RANGE
val sdk = module.pythonSdk ?: return TextRange.EMPTY_RANGE

val stdin = source.textToCharArray().toByteArrayAndClear()

val formatted = executeOnPooledThread(null) {
runRuff(sdk, stdin, *args.toTypedArray())
runRuff(module, stdin, *args.toTypedArray())
} ?: return TextRange.EMPTY_RANGE
val diffRange = diffRange(source.text, formatted) ?: return TextRange.EMPTY_RANGE

0 comments on commit b465e49

Please sign in to comment.