Skip to content

Commit fd98d27

Browse files
authored
Merge pull request #548 from koxudaxi/improve-ruff-formatter
fix: improve ruff formatter
2 parents 23ee3c1 + 17df175 commit fd98d27

File tree

4 files changed

+91
-14
lines changed

4 files changed

+91
-14
lines changed

CHANGELOG.md

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

33
## [Unreleased]
4+
- fix: improve ruff formatter [[#548](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/548)]
45
- Add ruff logging console [[#547](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/547)]
56
- Update dependency versions [[#538](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/538)]
67

resources/META-INF/plugin.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
enabledByDefault="true" level="WARNING"
1717
implementationClass="com.koxudaxi.ruff.RuffInspection"/>
1818
<externalAnnotator language="Python" implementationClass="com.koxudaxi.ruff.RuffExternalAnnotator"/>
19-
<formattingService implementation="com.koxudaxi.ruff.RuffAsyncFormatter"/>
19+
<formattingService implementation="com.koxudaxi.ruff.RuffAsyncFormatterFormat"/>
20+
<formattingService implementation="com.koxudaxi.ruff.RuffAsyncFormatterFix"/>
2021
<platform.backend.documentation.targetProvider
2122
implementation="com.koxudaxi.ruff.RuffNoqaDocumentationTargetProvider"/>
2223
<actionOnSave id="BlackFormatterActionOnSave" implementation="com.koxudaxi.ruff.RuffActionOnSave" order="last"/>

src/com/koxudaxi/ruff/RuffAsyncFormatter.kt src/com/koxudaxi/ruff/RuffAsyncFormatterFix.kt

+3-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.intellij.psi.PsiFile
99
import java.util.*
1010

1111

12-
class RuffAsyncFormatter : AsyncDocumentFormattingService() {
12+
class RuffAsyncFormatterFix : AsyncDocumentFormattingService() {
1313
private val FEATURES: MutableSet<FormattingService.Feature> = EnumSet.noneOf(
1414
FormattingService.Feature::class.java
1515
)
@@ -43,17 +43,7 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
4343
request.onTextReady(null)
4444
return@runCatching
4545
}
46-
if (!RuffConfigService.getInstance(formattingContext.project).useRuffFormat) {
47-
updateText(currentText, fixCommandStdout)
48-
return@runCatching
49-
}
50-
val formatCommandArgs = generateCommandArgs(sourceFile, FORMAT_ARGS)
51-
if (formatCommandArgs == null) {
52-
updateText(currentText, fixCommandStdout)
53-
return@runCatching
54-
}
55-
val formatCommandStdout = runRuff(formatCommandArgs, fixCommandStdout.toByteArray())
56-
updateText(currentText, formatCommandStdout)
46+
updateText(currentText, fixCommandStdout)
5747

5848
}.onFailure { exception ->
5949
when (exception) {
@@ -79,6 +69,6 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
7969
}
8070

8171
override fun getName(): String {
82-
return "Ruff Formatter"
72+
return "Ruff Formatter Fix"
8373
}
8474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.koxudaxi.ruff
2+
3+
import com.intellij.formatting.FormattingContext
4+
import com.intellij.formatting.service.AsyncDocumentFormattingService
5+
import com.intellij.formatting.service.AsyncFormattingRequest
6+
import com.intellij.formatting.service.FormattingService
7+
import com.intellij.openapi.progress.ProcessCanceledException
8+
import com.intellij.psi.PsiFile
9+
import java.util.*
10+
11+
12+
class RuffAsyncFormatterFormat : AsyncDocumentFormattingService() {
13+
private val FEATURES: MutableSet<FormattingService.Feature> = EnumSet.noneOf(
14+
FormattingService.Feature::class.java
15+
)
16+
17+
override fun getFeatures(): MutableSet<FormattingService.Feature> {
18+
return FEATURES
19+
}
20+
21+
override fun canFormat(file: PsiFile): Boolean {
22+
return RuffConfigService.getInstance(file.project).runRuffOnReformatCode &&
23+
RuffConfigService.getInstance(file.project).useRuffFormat &&
24+
file.isApplicableTo
25+
}
26+
27+
override fun runAfter(): Class<out FormattingService> {
28+
return RuffAsyncFormatterFix::class.java
29+
}
30+
31+
override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask? {
32+
val formattingContext: FormattingContext = request.context
33+
val ioFile = request.ioFile ?: return null
34+
return object : FormattingTask {
35+
private fun updateText(currentText: String, text: String?) {
36+
when {
37+
text == null -> request.onTextReady(null)
38+
currentText == text -> request.onTextReady(null)
39+
else -> request.onTextReady(text)
40+
}
41+
}
42+
43+
override fun run() {
44+
runCatching {
45+
val sourceFile = formattingContext.containingFile.sourceFile
46+
val currentText = ioFile.readText()
47+
48+
val formatCommandArgs = generateCommandArgs(sourceFile, FORMAT_ARGS)
49+
if (formatCommandArgs == null) {
50+
request.onTextReady(null)
51+
return@runCatching
52+
}
53+
val formatCommandStdout = runRuff(formatCommandArgs, currentText.toByteArray())
54+
updateText(currentText, formatCommandStdout)
55+
56+
}.onFailure { exception ->
57+
when (exception) {
58+
is ProcessCanceledException -> { /* ignore */
59+
}
60+
61+
else -> {
62+
request.onError("Ruff Error", exception.localizedMessage)
63+
}
64+
}
65+
}
66+
}
67+
68+
override fun cancel(): Boolean {
69+
return true
70+
}
71+
72+
override fun isRunUnderProgress(): Boolean {
73+
return true
74+
}
75+
}
76+
}
77+
78+
override fun getNotificationGroupId(): String {
79+
return "Ruff"
80+
}
81+
82+
override fun getName(): String {
83+
return "Ruff Formatter Format"
84+
}
85+
}

0 commit comments

Comments
 (0)