VSCode 中的动态代码段评估 [英] Dynamic snippet evaluation in VSCode

查看:33
本文介绍了VSCode 中的动态代码段评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

片段是否可以在 Visual Studio Code 中插入动态计算的完成或片段?

Is it possible for a snippet to insert a dynamically computed completion or snippet in Visual Studio Code?

我想要一个用于插入各种格式的日期和时间字符串的片段.例如,如果您键入 date,则 ISO 格式的当前日期将自动展开.

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.

Sublime Text 中有一个工具可以通过 EventListener 类中的 rel="noreferrer">on_query_completions 方法.实现将非常简单:

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.

如果使用代码段 API 公开的功能无法做到这一点,我能否通过较低级别的插件/扩展 API 实现类似的功能?

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.

推荐答案

绝对不可能在片段中执行脚本或类似的东西.

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

您可以改为为 Visual Studio Code 编写扩展.扩展程序必须实现 CompletionItemProvider.

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

它的 provideCompletionItems 方法会返回一个 <代码>完成项.它们的 filterText 属性将设置为建议框中显示的文本(例如日期"或时间"),而它们的 insertText 属性将设置为动态计算的价值.

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.

最后,您需要使用 registerCompletionItemProvider 注册完成提供程序.

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

在开始之前,您绝对应该看看如何创建扩展:https://code.visualstudio.com/docs/extensions/example-hello-world

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

这篇关于VSCode 中的动态代码段评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆