Skip to content

Commit 3841fe7

Browse files
authored
Merge pull request #564 from koxudaxi/fix-invalid-ruff-command-option
fix: fix invalid ruff command option
2 parents 2c20ee3 + f9edf37 commit 3841fe7

File tree

6 files changed

+38
-49
lines changed

6 files changed

+38
-49
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## [Unreleased]
4+
- fix: fix invalid ruff command option [[#564](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/564)]
45

56
## [0.0.44] - 2025-03-04
67

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pluginGroup = com.koxudaxi.ruff
44
pluginName = Ruff
55
pluginRepositoryUrl = https://github.com/koxudaxi/ruff-pycharm-plugin
66
# SemVer format -> https://semver.org
7-
pluginVersion = 0.0.44
7+
pluginVersion = 0.0.45
88

99
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1010
pluginSinceBuild = 243.21565.199

src/com/koxudaxi/ruff/Ruff.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ fun runRuff(sourceFile: SourceFile, args: List<String>): String? =
470470
generateCommandArgs(sourceFile, args, true)?.let { runRuff(it) }
471471

472472
fun runRuff(project: Project, args: List<String>, withoutConfig: Boolean = false): String? =
473-
generateCommandArgs(project, null, args, withoutConfig)?.let { runRuff(it) }
473+
generateCommandArgs(project, null, args, false, withoutConfig)?.let { runRuff(it) }
474474

475475

476476
data class CommandArgs(
@@ -482,13 +482,15 @@ fun generateCommandArgs(sourceFile: SourceFile, args: List<String>, setStdin: Bo
482482
generateCommandArgs(
483483
sourceFile.project,
484484
if (setStdin) sourceFile.asStdin else null,
485-
args + getStdinFileNameArgs(sourceFile)
485+
args + getStdinFileNameArgs(sourceFile),
486+
true
486487
)
487488

488489
fun generateCommandArgs(
489490
project: Project,
490491
stdin: ByteArray?,
491492
args: List<String>,
493+
addStdinOption: Boolean,
492494
withoutConfig: Boolean = false
493495
): CommandArgs? {
494496
val ruffConfigService = project.configService
@@ -502,7 +504,7 @@ fun generateCommandArgs(
502504
executable,
503505
project,
504506
stdin,
505-
(customConfigArgs ?: args) + if (stdin == null) listOf() else listOf("-")
507+
(customConfigArgs ?: args) + if (stdin == null && !addStdinOption) listOf() else listOf("-")
506508
)
507509
}
508510

@@ -545,7 +547,7 @@ inline fun <reified T> runRuffInBackground(
545547
inline fun <reified T> runRuffInBackground(
546548
project: Project, stdin: ByteArray?, args: List<String>, description: String, crossinline callback: (String?) -> T
547549
): ProgressIndicator? {
548-
val commandArgs = generateCommandArgs(project, stdin, args) ?: return null
550+
val commandArgs = generateCommandArgs(project, stdin, args, true) ?: return null
549551
val task = object : Task.Backgroundable(project, StringUtil.toTitleCase(description), true) {
550552
override fun run(indicator: ProgressIndicator) {
551553
indicator.text = "$description..."

src/com/koxudaxi/ruff/RuffApplyService.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ class RuffApplyService(val project: Project) {
8888
val formatCommandArgs = generateCommandArgs(
8989
sourceFile.project,
9090
sourceByte,
91-
FORMAT_ARGS
91+
FORMAT_ARGS,
92+
true
9293
) ?: return
9394

9495
if (projectRef.isDisposed) return

src/com/koxudaxi/ruff/RuffAsyncFormatter.kt

+27-40
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.intellij.formatting.service.AsyncFormattingRequest
66
import com.intellij.formatting.service.FormattingService
77
import com.intellij.openapi.progress.ProcessCanceledException
88
import com.intellij.psi.PsiFile
9+
import com.jetbrains.python.packaging.PyExecutionException
910
import java.io.FileNotFoundException
1011

1112

@@ -26,15 +27,11 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
2627
@Volatile
2728
private var cancelled = false
2829

29-
private fun noUpdate() {
30-
request.onTextReady(null)
31-
}
32-
33-
private fun updateText(currentText: String, text: String?) {
34-
when {
35-
text == null -> noUpdate()
36-
currentText == text -> noUpdate()
37-
else -> request.onTextReady(text)
30+
private fun assertResult(currentText: String, text: String?): String? {
31+
return when {
32+
text == null -> null
33+
currentText == text -> null
34+
else -> text
3835
}
3936
}
4037

@@ -45,11 +42,7 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
4542
}
4643
runCatching {
4744
val formattingContext: FormattingContext = request.context
48-
val ioFile = request.ioFile
49-
if (ioFile == null) {
50-
request.onTextReady(null)
51-
return@runCatching
52-
}
45+
val ioFile = request.ioFile ?: return@runCatching null
5346
val sourceFile = formattingContext.containingFile.sourceFile
5447
val currentText = ioFile.readText()
5548

@@ -61,47 +54,41 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
6154

6255
if (formatRange != null) {
6356
// When a range is specified, only run the format command.
64-
val formatCommandArgs = generateCommandArgs(sourceFile, FORMAT_ARGS + formatRange.formatRangeArgs(currentText), false)
65-
?: return@runCatching
66-
val formatCommandStdout = runRuff(formatCommandArgs, currentText.toByteArray())
67-
if (formatCommandStdout == null) {
68-
request.onTextReady(null)
69-
return@runCatching
70-
}
71-
updateText(currentText, formatCommandStdout)
72-
return@runCatching
57+
val formatCommandArgs = generateCommandArgs(
58+
sourceFile,
59+
FORMAT_ARGS + formatRange.formatRangeArgs(currentText),
60+
false
61+
)
62+
?: return@runCatching null
63+
val formatCommandStdout = runRuff(formatCommandArgs, currentText.toByteArray()) ?: return@runCatching null
64+
return@runCatching assertResult(currentText, formatCommandStdout)
7365
}
7466

7567
val fixCommandArgs = generateCommandArgs(sourceFile, formattingContext.project.FIX_ARGS, false)
76-
?: return@runCatching
77-
val fixCommandStdout = runRuff(fixCommandArgs, currentText.toByteArray())
78-
if (fixCommandStdout == null) {
79-
request.onTextReady(null)
80-
return@runCatching
81-
}
68+
?: return@runCatching null
69+
val fixCommandStdout = runRuff(fixCommandArgs, currentText.toByteArray()) ?: return@runCatching null
8270
if (!RuffConfigService.getInstance(formattingContext.project).useRuffFormat) {
83-
updateText(currentText, fixCommandStdout)
84-
return@runCatching
71+
return@runCatching assertResult(currentText, fixCommandStdout)
8572
}
8673
val formatCommandArgs = generateCommandArgs(sourceFile, FORMAT_ARGS, false)
87-
?: return@runCatching
74+
?: return@runCatching null
8875
if (cancelled) {
89-
noUpdate()
90-
return@runCatching
76+
return@runCatching null
9177
}
9278
val formatCommandStdout = runRuff(formatCommandArgs, fixCommandStdout.toByteArray())
9379
if (cancelled) {
94-
noUpdate()
95-
return@runCatching
80+
return@runCatching null
9681
}
97-
updateText(currentText, formatCommandStdout)
98-
82+
assertResult(currentText, formatCommandStdout)
9983
}.onFailure { exception ->
10084
when (exception) {
101-
is ProcessCanceledException -> { /* ignore */ }
102-
is FileNotFoundException -> noUpdate()
85+
is ProcessCanceledException -> request.onTextReady(null)
86+
is FileNotFoundException -> request.onTextReady(null)
87+
is PyExecutionException -> request.onTextReady(null)
10388
else -> request.onError("Ruff Error", exception.localizedMessage)
10489
}
90+
}.onSuccess {
91+
request.onTextReady(it)
10592
}
10693
}
10794

src/com/koxudaxi/ruff/RuffCacheService.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ class RuffCacheService(val project: Project) {
9292
if (projectRef.isDisposed) return@supplyAsync null
9393
val newVersion = fetchVersionFromCommand()
9494
if (!projectRef.isDisposed) {
95-
lock.withLock {
96-
version = newVersion
97-
}
95+
setVersion(newVersion)
9896
}
9997
newVersion
10098
}, executor)

0 commit comments

Comments
 (0)