Skip to content

Commit bb3605c

Browse files
committed
offer suggestions for unknown variants/screens/@tailwind
1 parent 62b30d5 commit bb3605c

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/lsp/providers/diagnosticsProvider.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import semver from 'semver'
2222
import { getLanguageBoundaries } from '../util/getLanguageBoundaries'
2323
import { absoluteRange } from '../util/absoluteRange'
2424
import { isObject } from '../../class-names/isObject'
25-
import levenshtein from 'js-levenshtein'
2625
import { stringToPath } from '../util/stringToPath'
26+
import { closest } from '../util/closest'
2727

2828
function getUnsupportedApplyDiagnostics(
2929
state: State,
@@ -170,6 +170,12 @@ function getUnknownScreenDiagnostics(
170170
return null
171171
}
172172

173+
let message = `The screen '${match.groups.screen}' does not exist in your theme config.`
174+
let suggestion = closest(match.groups.screen, screens)
175+
if (suggestion) {
176+
message += ` Did you mean '${suggestion}'?`
177+
}
178+
173179
diagnostics.push({
174180
range: absoluteRange(
175181
{
@@ -185,7 +191,7 @@ function getUnknownScreenDiagnostics(
185191
severity === 'error'
186192
? DiagnosticSeverity.Error
187193
: DiagnosticSeverity.Warning,
188-
message: 'Unknown screen',
194+
message,
189195
})
190196
})
191197
})
@@ -227,6 +233,12 @@ function getUnknownVariantDiagnostics(
227233
continue
228234
}
229235

236+
let message = `The variant '${variant}' does not exist.`
237+
let suggestion = closest(variant, state.variants)
238+
if (suggestion) {
239+
message += ` Did you mean '${suggestion}'?`
240+
}
241+
230242
let variantStartIndex =
231243
listStartIndex + variants.slice(0, i).join('').length
232244

@@ -242,7 +254,7 @@ function getUnknownVariantDiagnostics(
242254
severity === 'error'
243255
? DiagnosticSeverity.Error
244256
: DiagnosticSeverity.Warning,
245-
message: `Unknown variant: ${variant}`,
257+
message,
246258
})
247259
}
248260
})
@@ -329,17 +341,14 @@ function getInvalidHelperKeyDiagnostics(
329341
...keys.slice(0, keys.length - 1),
330342
])
331343
if (isObject(parentValue)) {
332-
let validKeys = Object.keys(parentValue)
333-
.filter((key) => isValid(parentValue[key]))
334-
.sort(
335-
(a, b) =>
336-
levenshtein(keys[keys.length - 1], a) -
337-
levenshtein(keys[keys.length - 1], b)
338-
)
339-
if (validKeys.length) {
344+
let closestValidKey = closest(
345+
keys[keys.length - 1],
346+
Object.keys(parentValue).filter((key) => isValid(parentValue[key]))
347+
)
348+
if (closestValidKey) {
340349
message += ` Did you mean '${stitch([
341350
...keys.slice(0, keys.length - 1),
342-
validKeys[0],
351+
closestValidKey,
343352
])}'?`
344353
}
345354
}
@@ -422,6 +431,16 @@ function getUnsupportedTailwindDirectiveDiagnostics(
422431
return null
423432
}
424433

434+
let message = `'${match.groups.value}' is not a valid group.`
435+
if (match.groups.value === 'preflight') {
436+
message += ` Did you mean 'base'?`
437+
} else {
438+
let suggestion = closest(match.groups.value, valid)
439+
if (suggestion) {
440+
message += ` Did you mean '${suggestion}'?`
441+
}
442+
}
443+
425444
diagnostics.push({
426445
range: absoluteRange(
427446
{
@@ -437,9 +456,7 @@ function getUnsupportedTailwindDirectiveDiagnostics(
437456
severity === 'error'
438457
? DiagnosticSeverity.Error
439458
: DiagnosticSeverity.Warning,
440-
message: `Unsupported value: ${match.groups.value}${
441-
match.groups.value === 'preflight' ? '. Use base instead.' : ''
442-
}`,
459+
message,
443460
})
444461
})
445462
})

src/lsp/util/closest.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import levenshtein from 'js-levenshtein'
2+
3+
export function closest(input: string, options: string[]): string | undefined {
4+
return options.sort(
5+
(a, b) => levenshtein(input, a) - levenshtein(input, b)
6+
)[0]
7+
}

0 commit comments

Comments
 (0)