Is it possible for a snippet to insert a dynamically computed completion or snippet in Visual Studio Code?
I would like a snippet for inserting date and time strings of various formats. For example if you type date
, the current date in ISO format would automatically be expanded.
There is a facility in Sublime Text to do this in the python API via the on_query_completions
method in the EventListener
class. There the implementation would be very simple:
def on_query_completions(self, view, prefix, locations):
if prefix == 'date':
val = datetime.now().strftime('%Y-%m-%d')
return [(prefix, prefix, val)] if val else []
I have read the documentation on User Defined Snippets, but it appears that one can only insert pre-defined text with tab-stops and variables that the user fills in.
If this isn't possible with the functionality exposed by the snippet API, would I be able to implement something similar with via a lower-level plugin/extension API?
I do realize that there is an existing extension called Insert Date and Time but this works via the command pallet instead of a dynamic expansion.
To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages.
In order to create a new snippet you need to select the User Snippets option within the File > Preferences menu. You can also open the command palette and search for Preferences: Configure User Snippets. Once you do this you will be presented with 3 main ways to create a snippets file.
You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.) in JavaScript).
For example, in Visual Basic there's a code snippet that inserts a property. To insert the snippet, choose Snippet > Insert Snippet from the right-click or context menu in a Visual Basic code file. Then, choose Code Patterns > Properties, Procedures, Events > Define a Property.
Although, it's possible to achieve simple date-time stuff without extensions:
"⌚ Date Time SNIPPET": {
"prefix": "datetime",
"body": [
"${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}T${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}",
]
}
The question is about dynamic snippets. And here's an example of using CompletionItemProvider
:
const datetimeProvider = vscode.languages.registerCompletionItemProvider(
{
scheme: 'file',
// language: 'typescript',
},
{
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {
const completionItem = new vscode.CompletionItem('datetime ⌚', vscode.CompletionItemKind.Snippet);
completionItem.insertText = new Date(Date.now() - new Date().getTimezoneOffset() * 60000).toISOString().split('.')[0];
return [completionItem];
}
},
// ''// trigger character
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With