@@ -10,7 +10,7 @@ import java.io.FileNotFoundException
10
10
11
11
12
12
class RuffAsyncFormatter : AsyncDocumentFormattingService () {
13
- private val FEATURES : Set <FormattingService .Feature > = setOf (FormattingService .Feature .AD_HOC_FORMATTING )
13
+ private val FEATURES : Set <FormattingService .Feature > = setOf (FormattingService .Feature .FORMAT_FRAGMENTS )
14
14
15
15
override fun getFeatures (): Set <FormattingService .Feature > {
16
16
return FEATURES
@@ -51,13 +51,29 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
51
51
return @runCatching
52
52
}
53
53
val sourceFile = formattingContext.containingFile.sourceFile
54
- val fixCommandArgs =
55
- generateCommandArgs(sourceFile, formattingContext.project.FIX_ARGS , false ) ? : return @runCatching
56
54
val currentText = ioFile.readText()
57
- if (cancelled) {
58
- noUpdate()
55
+
56
+ // Check if a formatting range is specified.
57
+ val formatRange = request.formattingRanges.firstOrNull()?.let { selectedRange ->
58
+ if (selectedRange.endOffset >= currentText.length) null
59
+ else selectedRange
60
+ }
61
+
62
+ if (formatRange != null ) {
63
+ // 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)
59
72
return @runCatching
60
73
}
74
+
75
+ val fixCommandArgs = generateCommandArgs(sourceFile, formattingContext.project.FIX_ARGS , false )
76
+ ? : return @runCatching
61
77
val fixCommandStdout = runRuff(fixCommandArgs, currentText.toByteArray())
62
78
if (fixCommandStdout == null ) {
63
79
request.onTextReady(null )
@@ -68,10 +84,7 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
68
84
return @runCatching
69
85
}
70
86
val formatCommandArgs = generateCommandArgs(sourceFile, FORMAT_ARGS , false )
71
- if (formatCommandArgs == null ) {
72
- updateText(currentText, fixCommandStdout)
73
- return @runCatching
74
- }
87
+ ? : return @runCatching
75
88
if (cancelled) {
76
89
noUpdate()
77
90
return @runCatching
@@ -85,22 +98,14 @@ class RuffAsyncFormatter : AsyncDocumentFormattingService() {
85
98
86
99
}.onFailure { exception ->
87
100
when (exception) {
88
- is ProcessCanceledException -> { /* ignore */
89
- }
90
-
91
- is FileNotFoundException -> {
92
- noUpdate()
93
- }
94
-
95
- else -> {
96
- request.onError(" Ruff Error" , exception.localizedMessage)
97
- }
101
+ is ProcessCanceledException -> { /* ignore */ }
102
+ is FileNotFoundException -> noUpdate()
103
+ else -> request.onError(" Ruff Error" , exception.localizedMessage)
98
104
}
99
105
}
100
106
}
101
107
102
108
override fun cancel (): Boolean {
103
- // Signal cancellation.
104
109
cancelled = true
105
110
return true
106
111
}
0 commit comments