Skip to content

Commit b79dbfc

Browse files
committed
refactor diagnostics and add types
1 parent e79f72b commit b79dbfc

File tree

5 files changed

+253
-111
lines changed

5 files changed

+253
-111
lines changed

src/lsp/providers/codeActionProvider/index.ts

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import {
44
CodeActionKind,
55
Range,
66
TextEdit,
7-
Diagnostic,
87
} from 'vscode-languageserver'
98
import { State } from '../../util/state'
10-
import { findLast, findClassNamesInRange } from '../../util/find'
9+
import { findLast } from '../../util/find'
1110
import { isWithinRange } from '../../util/isWithinRange'
1211
import { getClassNameParts } from '../../util/getClassNameAtPosition'
1312
const dlv = require('dlv')
@@ -16,19 +15,50 @@ import { removeRangeFromString } from '../../util/removeRangeFromString'
1615
import detectIndent from 'detect-indent'
1716
import { cssObjToAst } from '../../util/cssObjToAst'
1817
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'
1926

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(
2145
state: State,
2246
params: CodeActionParams
2347
): 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+
)
2757

2858
return Promise.all(
29-
params.context.diagnostics
59+
diagnostics
3060
.map((diagnostic) => {
31-
if (diagnostic.code === 'invalidApply') {
61+
if (isInvalidApplyDiagnostic(diagnostic)) {
3262
return provideInvalidApplyCodeAction(state, params, diagnostic)
3363
}
3464

@@ -127,31 +157,14 @@ function classNameToAst(
127157
async function provideInvalidApplyCodeAction(
128158
state: State,
129159
params: CodeActionParams,
130-
diagnostic: Diagnostic
160+
diagnostic: InvalidApplyDiagnostic
131161
): Promise<CodeAction> {
132162
let document = state.editor.documents.get(params.textDocument.uri)
133163
let documentText = document.getText()
134164
const { postcss } = state.modules
135165
let change: TextEdit
136166

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(
155168
/\s+/
156169
).length
157170

@@ -184,7 +197,7 @@ async function provideInvalidApplyCodeAction(
184197
state,
185198
className,
186199
rule.selector,
187-
documentClassName.classList.important
200+
diagnostic.className.classList.important
188201
)
189202

190203
if (!ast) {
@@ -250,10 +263,10 @@ async function provideInvalidApplyCodeAction(
250263
...(totalClassNamesInClassList > 1
251264
? [
252265
{
253-
range: documentClassName.classList.range,
266+
range: diagnostic.className.classList.range,
254267
newText: removeRangeFromString(
255-
documentClassName.classList.classList,
256-
documentClassName.relativeRange
268+
diagnostic.className.classList.classList,
269+
diagnostic.className.relativeRange
257270
),
258271
},
259272
]

0 commit comments

Comments
 (0)