Skip to content

Commit

Permalink
Improve Completions (sveltejs#368)
Browse files Browse the repository at this point in the history
Adds an optional insertText to completions, in order to bring more completions flexibility.
  • Loading branch information
galkatz373 authored Jul 27, 2020
1 parent bbbd12f commit 54b328b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
CompletionList,
CompletionItemKind,
CompletionItem,
InsertTextFormat,
} from 'vscode-languageserver';
import { SvelteTag, documentation, getLatestOpeningTag } from './SvelteTags';
import { isInTag } from '../../../lib/documents';

/**
* Get completions for special svelte tags within moustache tags.
*/
Expand Down Expand Up @@ -54,9 +54,18 @@ function getCompletionsWithRegardToTriggerCharacter(

if (triggerCharacter === '#') {
return createCompletionItems([
{ tag: 'if', label: 'if' },
{ tag: 'each', label: 'each' },
{ tag: 'await', label: 'await' },
{ tag: 'if', label: 'if', insertText: 'if $1}\n\t$2\n{/if' },
{ tag: 'each', label: 'each', insertText: 'each $1 as $2}\n\t$3\n{/each' },
{
tag: 'await',
label: 'await :then',
insertText: 'await $1}\n\t$2\n{:then $3} \n\t$4\n{/await',
},
{
tag: 'await',
label: 'await then',
insertText: 'await $1 then $2}\n\t$3\n{/await',
},
]);
}

Expand Down Expand Up @@ -133,12 +142,16 @@ function showCompletionWithRegardsToOpenedTags(
/**
* Create the completion items for given labels and tags.
*/
function createCompletionItems(items: { label: string; tag: SvelteTag }[]): CompletionList {
function createCompletionItems(
items: { label: string; tag: SvelteTag; insertText?: string }[],
): CompletionList {
return CompletionList.create(
// add sortText/preselect so it is ranked higher than other completions and selected first
items.map(
(item) =>
<CompletionItem>{
insertTextFormat: InsertTextFormat.Snippet,
insertText: item.insertText,
label: item.label,
sortText: '-1',
kind: CompletionItemKind.Keyword,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('SveltePlugin#getCompletions', () => {
});

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

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

0 comments on commit 54b328b

Please sign in to comment.