Skip to content

Commit 6cbd49a

Browse files
committed
refactor "suggestion" quick fixes
1 parent 7b916ab commit 6cbd49a

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

src/lsp/providers/codeActionProvider/index.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import {
2424
InvalidApplyDiagnostic,
2525
isUtilityConflictsDiagnostic,
2626
UtilityConflictsDiagnostic,
27+
isInvalidConfigPathDiagnostic,
28+
isInvalidTailwindDirectiveDiagnostic,
29+
isInvalidScreenDiagnostic,
30+
isInvalidVariantDiagnostic,
2731
} from '../diagnostics/types'
2832
import { flatten, dedupeBy } from '../../../util/array'
2933
import { joinWithAnd } from '../../util/joinWithAnd'
@@ -72,32 +76,30 @@ export async function provideCodeActions(
7276
return provideUtilityConflictsCodeActions(state, params, diagnostic)
7377
}
7478

75-
let match = findLast(
76-
/ Did you mean (?:something like )?'(?<replacement>[^']+)'\?$/g,
77-
diagnostic.message
78-
)
79-
80-
if (!match) {
81-
return []
82-
}
83-
84-
return [
85-
{
86-
title: `Replace with '${match.groups.replacement}'`,
79+
if (
80+
isInvalidConfigPathDiagnostic(diagnostic) ||
81+
isInvalidTailwindDirectiveDiagnostic(diagnostic) ||
82+
isInvalidScreenDiagnostic(diagnostic) ||
83+
isInvalidVariantDiagnostic(diagnostic)
84+
) {
85+
return diagnostic.suggestions.map((suggestion) => ({
86+
title: `Replace with '${suggestion}'`,
8787
kind: CodeActionKind.QuickFix,
8888
diagnostics: [diagnostic],
8989
edit: {
9090
changes: {
9191
[params.textDocument.uri]: [
9292
{
9393
range: diagnostic.range,
94-
newText: match.groups.replacement,
94+
newText: suggestion,
9595
},
9696
],
9797
},
9898
},
99-
},
100-
]
99+
}))
100+
}
101+
102+
return []
101103
})
102104

103105
return Promise.all(actions)

src/lsp/providers/diagnostics/diagnosticsProvider.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,11 @@ function getInvalidScreenDiagnostics(
197197
}
198198

199199
let message = `The screen '${match.groups.screen}' does not exist in your theme config.`
200+
let suggestions: string[] = []
200201
let suggestion = closest(match.groups.screen, screens)
202+
201203
if (suggestion) {
204+
suggestions.push(suggestion)
202205
message += ` Did you mean '${suggestion}'?`
203206
}
204207

@@ -219,6 +222,7 @@ function getInvalidScreenDiagnostics(
219222
? DiagnosticSeverity.Error
220223
: DiagnosticSeverity.Warning,
221224
message,
225+
suggestions,
222226
})
223227
})
224228
})
@@ -261,8 +265,11 @@ function getInvalidVariantDiagnostics(
261265
}
262266

263267
let message = `The variant '${variant}' does not exist.`
268+
let suggestions: string[] = []
264269
let suggestion = closest(variant, state.variants)
270+
265271
if (suggestion) {
272+
suggestions.push(suggestion)
266273
message += ` Did you mean '${suggestion}'?`
267274
}
268275

@@ -283,6 +290,7 @@ function getInvalidVariantDiagnostics(
283290
? DiagnosticSeverity.Error
284291
: DiagnosticSeverity.Warning,
285292
message,
293+
suggestions,
286294
})
287295
}
288296
})
@@ -337,6 +345,7 @@ function getInvalidConfigPathDiagnostics(
337345
}, '')
338346

339347
let message: string
348+
let suggestions: string[] = []
340349

341350
if (isValid(value)) {
342351
// The value resolves successfully, but we need to check that there
@@ -374,24 +383,24 @@ function getInvalidConfigPathDiagnostics(
374383
Object.keys(parentValue).filter((key) => isValid(parentValue[key]))
375384
)
376385
if (closestValidKey) {
377-
message += ` Did you mean '${stitch([
378-
...keys.slice(0, keys.length - 1),
379-
closestValidKey,
380-
])}'?`
386+
suggestions.push(
387+
stitch([...keys.slice(0, keys.length - 1), closestValidKey])
388+
)
389+
message += ` Did you mean '${suggestions[0]}'?`
381390
}
382391
}
383392
} else {
384393
message = `'${match.groups.key}' was found but does not resolve to a string.`
385394

386395
if (isObject(value)) {
387-
let firstValidKey = Object.keys(value).find((key) =>
396+
let validKeys = Object.keys(value).filter((key) =>
388397
isValid(value[key])
389398
)
390-
if (firstValidKey) {
391-
message += ` Did you mean something like '${stitch([
392-
...keys,
393-
firstValidKey,
394-
])}'?`
399+
if (validKeys.length) {
400+
suggestions.push(
401+
...validKeys.map((validKey) => stitch([...keys, validKey]))
402+
)
403+
message += ` Did you mean something like '${suggestions[0]}'?`
395404
}
396405
}
397406
}
@@ -421,6 +430,7 @@ function getInvalidConfigPathDiagnostics(
421430
? DiagnosticSeverity.Error
422431
: DiagnosticSeverity.Warning,
423432
message,
433+
suggestions,
424434
})
425435
})
426436
})
@@ -464,11 +474,15 @@ function getInvalidTailwindDirectiveDiagnostics(
464474
}
465475

466476
let message = `'${match.groups.value}' is not a valid group.`
477+
let suggestions: string[] = []
478+
467479
if (match.groups.value === 'preflight') {
480+
suggestions.push('base')
468481
message += ` Did you mean 'base'?`
469482
} else {
470483
let suggestion = closest(match.groups.value, valid)
471484
if (suggestion) {
485+
suggestions.push(suggestion)
472486
message += ` Did you mean '${suggestion}'?`
473487
}
474488
}
@@ -490,6 +504,7 @@ function getInvalidTailwindDirectiveDiagnostics(
490504
? DiagnosticSeverity.Error
491505
: DiagnosticSeverity.Warning,
492506
message,
507+
suggestions,
493508
})
494509
})
495510
})

src/lsp/providers/diagnostics/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export function isInvalidApplyDiagnostic(
3535

3636
export type InvalidScreenDiagnostic = Diagnostic & {
3737
code: DiagnosticKind.InvalidScreen
38+
suggestions: string[]
3839
}
3940

4041
export function isInvalidScreenDiagnostic(
@@ -45,6 +46,7 @@ export function isInvalidScreenDiagnostic(
4546

4647
export type InvalidVariantDiagnostic = Diagnostic & {
4748
code: DiagnosticKind.InvalidVariant
49+
suggestions: string[]
4850
}
4951

5052
export function isInvalidVariantDiagnostic(
@@ -55,6 +57,7 @@ export function isInvalidVariantDiagnostic(
5557

5658
export type InvalidConfigPathDiagnostic = Diagnostic & {
5759
code: DiagnosticKind.InvalidConfigPath
60+
suggestions: string[]
5861
}
5962

6063
export function isInvalidConfigPathDiagnostic(
@@ -65,6 +68,7 @@ export function isInvalidConfigPathDiagnostic(
6568

6669
export type InvalidTailwindDirectiveDiagnostic = Diagnostic & {
6770
code: DiagnosticKind.InvalidTailwindDirective
71+
suggestions: string[]
6872
}
6973

7074
export function isInvalidTailwindDirectiveDiagnostic(

0 commit comments

Comments
 (0)