Skip to content

Commit a435df5

Browse files
authored
(fix) better completions for tags (#1642)
In fallback-mode when a parser error happened only show completion suggestions for things that start with an uppercase and therefore are maybe a component
1 parent 98c3c54 commit a435df5

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

packages/language-server/src/plugins/typescript/features/CompletionProvider.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
252252
const existingImports = this.getExistingImports(document);
253253
const wordRangeStartPosition = document.positionAt(wordRange.start);
254254
const completionItems = completions
255-
.filter(isValidCompletion(document, position))
255+
.filter(isValidCompletion(document, position, !!tsDoc.parserError))
256256
.map((comp) =>
257257
this.toCompletionItem(
258258
tsDoc,
@@ -857,10 +857,24 @@ const svelte2tsxTypes = new Set([
857857
'SvelteStore'
858858
]);
859859

860+
const startsWithUppercase = /^[A-Z]/;
861+
860862
function isValidCompletion(
861863
document: Document,
862-
position: Position
864+
position: Position,
865+
hasParserError: boolean
863866
): (value: ts.CompletionEntry) => boolean {
867+
// Make fallback completions for tags inside the template a bit better
868+
const isAtStartTag =
869+
!isInTag(position, document.scriptInfo) &&
870+
/<\w*$/.test(
871+
document.getText(Range.create(position.line, 0, position.line, position.character))
872+
);
873+
const noWrongCompletionAtStartTag =
874+
isAtStartTag && hasParserError
875+
? (value: ts.CompletionEntry) => startsWithUppercase.test(value.name)
876+
: () => true;
877+
864878
const isNoSvelte2tsxCompletion = (value: ts.CompletionEntry) =>
865879
value.kindModifiers !== 'declare' ||
866880
(!value.name.startsWith('__sveltets_') && !svelte2tsxTypes.has(value.name));
@@ -880,5 +894,7 @@ function isValidCompletion(
880894
// Remove jsx attributes on html tags because they are doubled by the HTML
881895
// attribute suggestions, and for events they are wrong (onX instead of on:X).
882896
// Therefore filter them out.
883-
value.kind !== ts.ScriptElementKind.jsxAttribute && isNoSvelte2tsxCompletion(value);
897+
value.kind !== ts.ScriptElementKind.jsxAttribute &&
898+
isNoSvelte2tsxCompletion(value) &&
899+
noWrongCompletionAtStartTag(value);
884900
}

0 commit comments

Comments
 (0)