Skip to content

Commit 54b328b

Browse files
authored
Improve Completions (#368)
Adds an optional insertText to completions, in order to bring more completions flexibility.
1 parent bbbd12f commit 54b328b

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

packages/language-server/src/plugins/svelte/features/getCompletions.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
CompletionList,
55
CompletionItemKind,
66
CompletionItem,
7+
InsertTextFormat,
78
} from 'vscode-languageserver';
89
import { SvelteTag, documentation, getLatestOpeningTag } from './SvelteTags';
910
import { isInTag } from '../../../lib/documents';
10-
1111
/**
1212
* Get completions for special svelte tags within moustache tags.
1313
*/
@@ -54,9 +54,18 @@ function getCompletionsWithRegardToTriggerCharacter(
5454

5555
if (triggerCharacter === '#') {
5656
return createCompletionItems([
57-
{ tag: 'if', label: 'if' },
58-
{ tag: 'each', label: 'each' },
59-
{ tag: 'await', label: 'await' },
57+
{ tag: 'if', label: 'if', insertText: 'if $1}\n\t$2\n{/if' },
58+
{ tag: 'each', label: 'each', insertText: 'each $1 as $2}\n\t$3\n{/each' },
59+
{
60+
tag: 'await',
61+
label: 'await :then',
62+
insertText: 'await $1}\n\t$2\n{:then $3} \n\t$4\n{/await',
63+
},
64+
{
65+
tag: 'await',
66+
label: 'await then',
67+
insertText: 'await $1 then $2}\n\t$3\n{/await',
68+
},
6069
]);
6170
}
6271

@@ -133,12 +142,16 @@ function showCompletionWithRegardsToOpenedTags(
133142
/**
134143
* Create the completion items for given labels and tags.
135144
*/
136-
function createCompletionItems(items: { label: string; tag: SvelteTag }[]): CompletionList {
145+
function createCompletionItems(
146+
items: { label: string; tag: SvelteTag; insertText?: string }[],
147+
): CompletionList {
137148
return CompletionList.create(
138149
// add sortText/preselect so it is ranked higher than other completions and selected first
139150
items.map(
140151
(item) =>
141152
<CompletionItem>{
153+
insertTextFormat: InsertTextFormat.Snippet,
154+
insertText: item.insertText,
142155
label: item.label,
143156
sortText: '-1',
144157
kind: CompletionItemKind.Keyword,

packages/language-server/test/plugins/svelte/features/getCompletions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('SveltePlugin#getCompletions', () => {
4949
});
5050

5151
it('should return completions for #', () => {
52-
expectCompletionsFor('{#').toEqual(['if', 'each', 'await']);
52+
expectCompletionsFor('{#').toEqual(['if', 'each', 'await :then', 'await then']);
5353
});
5454

5555
it('should return completions for @', () => {

0 commit comments

Comments
 (0)