@@ -4,10 +4,9 @@ import {
4
4
CodeActionKind ,
5
5
Range ,
6
6
TextEdit ,
7
- Diagnostic ,
8
7
} from 'vscode-languageserver'
9
8
import { State } from '../../util/state'
10
- import { findLast , findClassNamesInRange } from '../../util/find'
9
+ import { findLast } from '../../util/find'
11
10
import { isWithinRange } from '../../util/isWithinRange'
12
11
import { getClassNameParts } from '../../util/getClassNameAtPosition'
13
12
const dlv = require ( 'dlv' )
@@ -16,19 +15,50 @@ import { removeRangeFromString } from '../../util/removeRangeFromString'
16
15
import detectIndent from 'detect-indent'
17
16
import { cssObjToAst } from '../../util/cssObjToAst'
18
17
import isObject from '../../../util/isObject'
18
+ import { getDiagnostics } from '../diagnostics/diagnosticsProvider'
19
+ import { rangesEqual } from '../../util/rangesEqual'
20
+ import {
21
+ DiagnosticKind ,
22
+ isInvalidApplyDiagnostic ,
23
+ AugmentedDiagnostic ,
24
+ InvalidApplyDiagnostic ,
25
+ } from '../diagnostics/types'
19
26
20
- export function provideCodeActions (
27
+ async function getDiagnosticsFromCodeActionParams (
28
+ state : State ,
29
+ params : CodeActionParams ,
30
+ only ?: DiagnosticKind [ ]
31
+ ) : Promise < AugmentedDiagnostic [ ] > {
32
+ let document = state . editor . documents . get ( params . textDocument . uri )
33
+ let diagnostics = await getDiagnostics ( state , document , only )
34
+
35
+ return params . context . diagnostics
36
+ . map ( ( diagnostic ) => {
37
+ return diagnostics . find ( ( d ) => {
38
+ return rangesEqual ( d . range , diagnostic . range )
39
+ } )
40
+ } )
41
+ . filter ( Boolean )
42
+ }
43
+
44
+ export async function provideCodeActions (
21
45
state : State ,
22
46
params : CodeActionParams
23
47
) : Promise < CodeAction [ ] > {
24
- if ( params . context . diagnostics . length === 0 ) {
25
- return null
26
- }
48
+ let codes = params . context . diagnostics
49
+ . map ( ( diagnostic ) => diagnostic . code )
50
+ . filter ( Boolean ) as DiagnosticKind [ ]
51
+
52
+ let diagnostics = await getDiagnosticsFromCodeActionParams (
53
+ state ,
54
+ params ,
55
+ codes
56
+ )
27
57
28
58
return Promise . all (
29
- params . context . diagnostics
59
+ diagnostics
30
60
. map ( ( diagnostic ) => {
31
- if ( diagnostic . code === 'invalidApply' ) {
61
+ if ( isInvalidApplyDiagnostic ( diagnostic ) ) {
32
62
return provideInvalidApplyCodeAction ( state , params , diagnostic )
33
63
}
34
64
@@ -127,31 +157,14 @@ function classNameToAst(
127
157
async function provideInvalidApplyCodeAction (
128
158
state : State ,
129
159
params : CodeActionParams ,
130
- diagnostic : Diagnostic
160
+ diagnostic : InvalidApplyDiagnostic
131
161
) : Promise < CodeAction > {
132
162
let document = state . editor . documents . get ( params . textDocument . uri )
133
163
let documentText = document . getText ( )
134
164
const { postcss } = state . modules
135
165
let change : TextEdit
136
166
137
- let documentClassNames = findClassNamesInRange (
138
- document ,
139
- {
140
- start : {
141
- line : Math . max ( 0 , diagnostic . range . start . line - 10 ) ,
142
- character : 0 ,
143
- } ,
144
- end : { line : diagnostic . range . start . line + 10 , character : 0 } ,
145
- } ,
146
- 'css'
147
- )
148
- let documentClassName = documentClassNames . find ( ( className ) =>
149
- isWithinRange ( diagnostic . range . start , className . range )
150
- )
151
- if ( ! documentClassName ) {
152
- return null
153
- }
154
- let totalClassNamesInClassList = documentClassName . classList . classList . split (
167
+ let totalClassNamesInClassList = diagnostic . className . classList . classList . split (
155
168
/ \s + /
156
169
) . length
157
170
@@ -184,7 +197,7 @@ async function provideInvalidApplyCodeAction(
184
197
state ,
185
198
className ,
186
199
rule . selector ,
187
- documentClassName . classList . important
200
+ diagnostic . className . classList . important
188
201
)
189
202
190
203
if ( ! ast ) {
@@ -250,10 +263,10 @@ async function provideInvalidApplyCodeAction(
250
263
...( totalClassNamesInClassList > 1
251
264
? [
252
265
{
253
- range : documentClassName . classList . range ,
266
+ range : diagnostic . className . classList . range ,
254
267
newText : removeRangeFromString (
255
- documentClassName . classList . classList ,
256
- documentClassName . relativeRange
268
+ diagnostic . className . classList . classList ,
269
+ diagnostic . className . relativeRange
257
270
) ,
258
271
} ,
259
272
]
0 commit comments