@@ -6,6 +6,7 @@ import com.intellij.formatting.service.AsyncFormattingRequest
6
6
import com.intellij.formatting.service.FormattingService
7
7
import com.intellij.openapi.progress.ProcessCanceledException
8
8
import com.intellij.psi.PsiFile
9
+ import com.jetbrains.python.packaging.PyExecutionException
9
10
import java.io.FileNotFoundException
10
11
11
12
@@ -26,15 +27,11 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
26
27
@Volatile
27
28
private var cancelled = false
28
29
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
38
35
}
39
36
}
40
37
@@ -45,11 +42,7 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
45
42
}
46
43
runCatching {
47
44
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
53
46
val sourceFile = formattingContext.containingFile.sourceFile
54
47
val currentText = ioFile.readText()
55
48
@@ -61,47 +54,41 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
61
54
62
55
if (formatRange != null ) {
63
56
// 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)
73
65
}
74
66
75
67
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
82
70
if (! RuffConfigService .getInstance(formattingContext.project).useRuffFormat) {
83
- updateText(currentText, fixCommandStdout)
84
- return @runCatching
71
+ return @runCatching assertResult(currentText, fixCommandStdout)
85
72
}
86
73
val formatCommandArgs = generateCommandArgs(sourceFile, FORMAT_ARGS , false )
87
- ? : return @runCatching
74
+ ? : return @runCatching null
88
75
if (cancelled) {
89
- noUpdate()
90
- return @runCatching
76
+ return @runCatching null
91
77
}
92
78
val formatCommandStdout = runRuff(formatCommandArgs, fixCommandStdout.toByteArray())
93
79
if (cancelled) {
94
- noUpdate()
95
- return @runCatching
80
+ return @runCatching null
96
81
}
97
- updateText(currentText, formatCommandStdout)
98
-
82
+ assertResult(currentText, formatCommandStdout)
99
83
}.onFailure { exception ->
100
84
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 )
103
88
else -> request.onError(" Ruff Error" , exception.localizedMessage)
104
89
}
90
+ }.onSuccess {
91
+ request.onTextReady(it)
105
92
}
106
93
}
107
94
0 commit comments