VSCode snippet last frontier: Completion Items
Views (398)
All AL developers are using snippets, unless you feel an inexplicable urge to write the same code patterns over and over again. If you want a good definition of snippet, here you are: “Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements.” Source Snippets in Visual Studio Code
I already wrote a lot about how to make your own snippets and share them:
The method above don´t need programming background: you only make a json template with the help of a tool https://snippet-generator.app/ and then there are many ways to share it. The most basic is share json file with “code-snippets” file extension, and anyone can paste it in then %APPDATA%\Code\User\snippets folder.
Next step
Instead people copying and pasting your file in a folder to use it, you can package it. You create a new VSCode extension to do it. And in the contribution section of package.json file you can include your snippets:
"snippets": [
{"language": "json",
"path": "./src/Snippets.json"}
Create snippets this way (without code) is the most usual way to do it, but you have some limitations: you only can use in your templates some environ variables and basic regular expression transformation. This is the example of my more complex snippet with this technique:
" {",
" \"Path\": \"${WORKSPACE_FOLDER/[\\\\]/\\//g}/Translations/$WORKSPACE_NAME.g.xlf\"",
" }",
I made here a substitution of tab-stop with workspace folder + literal “translations” + workspace name + literal “.g.xlf”, with a regex substitution of “\” slash by “/”.
Going further: completion Items
In my last extension I need to go further in my snippet Variable naming extension update. - Dynamics 365 Business Central Community
After typing snippet, I needed to activate an event to wait some actions and do variable naming transformations when line editing is done. So, make templates is not enough to do this work. We have to code Completion items: vscode.CompletionItem - Haxe externs for Visual Studio Code - API documentation (vshaxe.github.io) in a VSCODE extension. In previous posts you can see a lot of material about develop a new VSCode extension, but I know, not an everybody job.
First step is to declare your new completion item:
function SnippetVariableAL()
{
const commandName = 'talVarNaming';
const commandCompletion = new vscode.CompletionItem(commandName);
commandCompletion.kind = vscode.CompletionItemKind.Snippet;
commandCompletion.filterText = commandName;
commandCompletion.label = commandName;
commandCompletion.insertText = TextWritevar;
commandCompletion.command = { command: 'JALVarNaming.CatchDocumentChangesSnp', title: 'Begin variable declaration' };
commandCompletion.detail = 'Write type and subtype of the variable and when write semicolon will be renamed';
commandCompletion.documentation = 'Write type and subtype of the variable and when write semicolon will be renamed';
SnippetTrigered = true;
return [commandCompletion];
This is the way to declare a snippet with code. And the reason to do this snippet with coding instead making a template is this line:
commandCompletion.command = { command: 'JALVarNaming.CatchDocumentChangesSnp', title: 'Begin variable declaration' };
When the snippet is triggered we execute this command that subscribe to change document event. All code is public in the repo: JalmarazMartn/JAMALVariablenaming (github.com)
The second step is declare the completion item in extension.js file
context.subscriptions.push(vscode.languages.registerCompletionItemProvider (
{ language: 'al', scheme: 'file' },
{ provideCompletionItems(document, position) {
const rename = require('./src/RenameVars.js');
return rename.SnippetVariableAL();
} }, 'talVarNaming' // trigger ));}
The dark side.
The third step could be controversial: in the package.json we put this declaration:
"activationEvents": [
. . . . . . . . .
We must include activation event “*” or "onStartupFinished" event and Microsoft don´t recommend it due to performance concerns. I tested this and I don´t feel any lack of speed but I would rather to warn about it.

Like
Report
*This post is locked for comments