Dynamic snippet evaluation in VSCode

笑着哭i 提交于 2020-01-02 01:25:48

问题


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.


回答1:


It's definitely not possible to execute a script or something similar within a snippet.

You can write an extension for Visual Studio Code instead. The extension must implement a CompletionItemProvider.

Its provideCompletionItems method would return a list of CompletionItems. Their filterText properties would be set to the texts displayed in the suggestion box (for example "date" or "time") and their insertText properties would be set to the dynamically computed values.

Finally you would need to register the completion provider using registerCompletionItemProvider.

You should absolutely take a look on how to create an extension before you start: https://code.visualstudio.com/docs/extensions/example-hello-world




回答2:


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
);


来源:https://stackoverflow.com/questions/39413783/dynamic-snippet-evaluation-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!