Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide snippets as completionItems #1

Closed
tahabasri opened this issue Dec 8, 2020 · 2 comments · Fixed by #35
Closed

Provide snippets as completionItems #1

tahabasri opened this issue Dec 8, 2020 · 2 comments · Fixed by #35
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@tahabasri
Copy link
Owner

Provide snippets as completionItems (to be shown next to the built-in snippets using ctrl+space).

  • When initializing the application, we can successfully populate VSCode with snippets prefixed with snp:snippet-label
  • Problem starts to show up when we update existing snippets as the code bellow do not override existing ones but duplicate them:
// see https://code.visualstudio.com/api/references/vscode-api#DocumentSelector
this.context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
    '*', {
    provideCompletionItems() {
        return [
            {
                // see https://code.visualstudio.com/api/references/vscode-api#DocumentFilter
                label: `snp:${element.label.replace('\n', '').replace(' ', '-')}`,
                insertText: new vscode.SnippetString(element.value),
                detail: element.label,
                kind: vscode.CompletionItemKind.Snippet,
            },
        ];
    },
},
    // '' trigger character
));

// list completionItems
const list: any = await vscode.commands.executeCommand('vscode.executeCompletionItemProvider',
    vscode.window.activeTextEditor?.document.uri,
    new vscode.Position(0, 0));
console.log(list.items);
@tahabasri tahabasri added enhancement New feature or request help wanted Extra attention is needed labels Dec 8, 2020
@LucaGabi
Copy link
Contributor

LucaGabi commented Aug 21, 2021

I think you need to keep the value returned by the registration function, it is a disposable, you can call to remove the old provider and recreate the new one as above. (So instead to just push it to the subscriptions array keep a reference to that returned value and then also add it to the subscriptions)

Great work just this small thing missing.

@tahabasri
Copy link
Owner Author

Thanks @LucaGabi for the contribution ! It didn't come to my mind to go with that solution, great tip ! I added some tweaks in order to trigger the suggestions in all file types :

  • because there is no explicit trigger character, VSCode uses default behavior which doesn't show up Snippet completion items for other file types (e.g js or json). VSCode suggestions ranking seems showing more language related candidates (see this for more details)

Thus, I'm setting the character > as main trigger (ctrl+space is always available but ranking may play with the results).

tahabasri added a commit that referenced this issue Nov 2, 2021
enable completion items of defined snippets fixes #1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants