Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export type ComponentPartInfo = ReturnType<ComponentEvents['getAll']>;
export interface ComponentInfoProvider {
getEvents(): ComponentPartInfo;
getSlotLets(slot?: string): ComponentPartInfo;
getProps(propName: string): ts.CompletionEntry[];
getProps(): ComponentPartInfo;
getProp(propName: string): ts.CompletionEntry[];
}

export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
Expand Down Expand Up @@ -45,7 +46,16 @@ export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
return this.mapPropertiesOfType(slotLetsType);
}

getProps(propName: string): ts.CompletionEntry[] {
getProps() {
const props = this.getType('$$prop_def');
if (!props) {
return [];
}

return this.mapPropertiesOfType(props);
}

getProp(propName: string): ts.CompletionEntry[] {
const props = this.getType('$$prop_def');
if (!props) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CancellationToken,
CompletionContext,
CompletionItem,
CompletionItemKind,
CompletionList,
CompletionTriggerKind,
MarkupContent,
Expand Down Expand Up @@ -197,6 +198,41 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
return tsDoc.parserError ? CompletionList.create([], true) : null;
}

if (
completions.length > 500 &&
svelteNode?.type === 'Element' &&
completions[0].kind !== ts.ScriptElementKind.memberVariableElement
) {
// False global completions inside element start tag
return null;
}

if (
completions.length > 500 &&
svelteNode?.type === 'InlineComponent' &&
[' ', ' >', ' /'].includes(
document.getText().substring(originalOffset - 1, originalOffset + 1)
)
) {
// Very likely false global completions inside component start tag -> narrow
const props =
componentInfo
?.getProps()
.map((entry) =>
this.componentInfoToCompletionEntry(
entry,
'',
CompletionItemKind.Field,
document,
wordRange
)
) || [];
return CompletionList.create(
[...eventAndSlotLetCompletions, ...props],
!!tsDoc.parserError
);
}

const existingImports = this.getExistingImports(document);
const wordRangeStartPosition = document.positionAt(wordRange.start);
const completionItems = completions
Expand Down Expand Up @@ -255,33 +291,59 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn

private getEventAndSlotLetCompletions(
componentInfo: ComponentInfoProvider | null,
doc: Document,
document: Document,
wordRange: { start: number; end: number }
): Array<AppCompletionItem<CompletionEntryWithIdentifier>> {
if (componentInfo === null) {
return [];
}

return [
...componentInfo
.getEvents()
.map((event) =>
this.componentInfoToCompletionEntry(
event,
'on:',
undefined,
document,
wordRange
)
),
...componentInfo
.getSlotLets()
.map((slot) =>
this.componentInfoToCompletionEntry(
slot,
'let:',
undefined,
document,
wordRange
)
)
];
}

private componentInfoToCompletionEntry(
info: ComponentPartInfo[0],
prefix: string,
kind: CompletionItemKind | undefined,
doc: Document,
wordRange: { start: number; end: number }
): AppCompletionItem<CompletionEntryWithIdentifier> {
const { start, end } = wordRange;
const events = componentInfo.getEvents().map((event) => mapToCompletionEntry(event, 'on:'));
const slotLets = componentInfo
.getSlotLets()
.map((slot) => mapToCompletionEntry(slot, 'let:'));
return [...events, ...slotLets];

function mapToCompletionEntry(info: ComponentPartInfo[0], prefix: string) {
const slotName = prefix + info.name;
return {
label: slotName,
sortText: '-1',
detail: info.name + ': ' + info.type,
documentation: info.doc && { kind: MarkupKind.Markdown, value: info.doc },
textEdit:
start !== end
? TextEdit.replace(toRange(doc.getText(), start, end), slotName)
: undefined
};
}
const name = prefix + info.name;
return {
label: name,
kind,
sortText: '-1',
detail: info.name + ': ' + info.type,
documentation: info.doc && { kind: MarkupKind.Markdown, value: info.doc },
textEdit:
start !== end
? TextEdit.replace(toRange(doc.getText(), start, end), name)
: undefined
};
}

private toCompletionItem(
Expand Down Expand Up @@ -677,7 +739,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
}
: undefined;

return componentInfo.getProps(jsxAttribute.name.getText()).map((item) => ({
return componentInfo.getProp(jsxAttribute.name.getText()).map((item) => ({
...item,
replacementSpan
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function test(useNewTransformation: boolean) {
documentation: '',
label: 'on:aa',
sortText: '-1',
kind: undefined,
textEdit: undefined
},
{
Expand All @@ -197,13 +198,15 @@ function test(useNewTransformation: boolean) {
},
label: 'on:ab',
sortText: '-1',
kind: undefined,
textEdit: undefined
},
{
detail: 'ac: any',
documentation: '',
label: 'on:ac',
sortText: '-1',
kind: undefined,
textEdit: undefined
}
]);
Expand Down Expand Up @@ -237,6 +240,7 @@ function test(useNewTransformation: boolean) {
documentation: '',
label: 'on:aa',
sortText: '-1',
kind: undefined,
textEdit: {
newText: 'on:aa',
range: {
Expand All @@ -259,6 +263,7 @@ function test(useNewTransformation: boolean) {
},
label: 'on:ab',
sortText: '-1',
kind: undefined,
textEdit: {
newText: 'on:ab',
range: {
Expand All @@ -278,6 +283,7 @@ function test(useNewTransformation: boolean) {
documentation: '',
label: 'on:ac',
sortText: '-1',
kind: undefined,
textEdit: {
newText: 'on:ac',
range: {
Expand Down Expand Up @@ -319,6 +325,7 @@ function test(useNewTransformation: boolean) {
},
label: 'on:c',
sortText: '-1',
kind: undefined,
textEdit: undefined
}
]);
Expand Down Expand Up @@ -347,6 +354,7 @@ function test(useNewTransformation: boolean) {
documentation: '',
label: 'on:event1',
sortText: '-1',
kind: undefined,
textEdit: {
newText: 'on:event1',
range: {
Expand All @@ -369,6 +377,7 @@ function test(useNewTransformation: boolean) {
},
label: 'on:event2',
sortText: '-1',
kind: undefined,
textEdit: {
newText: 'on:event2',
range: {
Expand Down Expand Up @@ -409,6 +418,7 @@ function test(useNewTransformation: boolean) {
label: 'on:event1',
sortText: '-1',
documentation: '',
kind: undefined,
textEdit: {
newText: 'on:event1',
range: {
Expand Down Expand Up @@ -1063,6 +1073,7 @@ function test(useNewTransformation: boolean) {
documentation: '',
label: 'let:let1',
sortText: '-1',
kind: undefined,
textEdit: {
newText: 'let:let1',
range: {
Expand All @@ -1085,6 +1096,7 @@ function test(useNewTransformation: boolean) {
},
label: 'let:let2',
sortText: '-1',
kind: undefined,
textEdit: {
newText: 'let:let2',
range: {
Expand Down