Skip to content

Commit 329a50d

Browse files
committed
add quick fixes for invalid screens/variants/helper keys/@tailwind
1 parent 1de8af5 commit 329a50d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {
2+
CodeAction,
3+
CodeActionParams,
4+
CodeActionKind,
5+
} from 'vscode-languageserver'
6+
import { State } from '../util/state'
7+
import { findLast } from '../util/find'
8+
9+
export function provideCodeActions(
10+
_state: State,
11+
params: CodeActionParams
12+
): CodeAction[] {
13+
if (params.context.diagnostics.length === 0) {
14+
return null
15+
}
16+
17+
return params.context.diagnostics
18+
.map((diagnostic) => {
19+
let match = findLast(
20+
/ Did you mean (?:something like )?'(?<replacement>[^']+)'\?$/g,
21+
diagnostic.message
22+
)
23+
24+
if (!match) {
25+
return null
26+
}
27+
28+
return {
29+
title: `Replace with '${match.groups.replacement}'`,
30+
kind: CodeActionKind.QuickFix,
31+
diagnostics: [diagnostic],
32+
edit: {
33+
changes: {
34+
[params.textDocument.uri]: [
35+
{
36+
range: diagnostic.range,
37+
newText: match.groups.replacement,
38+
},
39+
],
40+
},
41+
},
42+
}
43+
})
44+
.filter(Boolean)
45+
}

src/lsp/server.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
Hover,
1717
TextDocumentPositionParams,
1818
DidChangeConfigurationNotification,
19+
CodeActionParams,
20+
CodeAction,
1921
} from 'vscode-languageserver'
2022
import getTailwindState from '../class-names/index'
2123
import { State, Settings, EditorState } from './util/state'
@@ -32,6 +34,7 @@ import {
3234
clearAllDiagnostics,
3335
} from './providers/diagnosticsProvider'
3436
import { createEmitter } from '../lib/emitter'
37+
import { provideCodeActions } from './providers/codeActionProvider'
3538

3639
let connection = createConnection(ProposedFeatures.all)
3740
let state: State = { enabled: false, emitter: createEmitter(connection) }
@@ -171,6 +174,7 @@ connection.onInitialize(
171174
],
172175
},
173176
hoverProvider: true,
177+
codeActionProvider: true,
174178
},
175179
}
176180
}
@@ -226,4 +230,9 @@ connection.onHover(
226230
}
227231
)
228232

233+
connection.onCodeAction((params: CodeActionParams): CodeAction[] => {
234+
if (!state.enabled) return null
235+
return provideCodeActions(state, params)
236+
})
237+
229238
connection.listen()

0 commit comments

Comments
 (0)