将内置函数插入 VS Code 扩展的正确方法 [英] A proper way of inserting built-in functions into VS Code extension

查看:15
本文介绍了将内置函数插入 VS Code 扩展的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介

我目前正在开发基于 Bluegiga BLE-112 模块的应用程序.该模块可以使用 BGScript 脚本语言进行编程.但是,我发现的唯一一个对 BGScript 有某种支持的编辑器是 Notepad++,它不适合我的需要.因此,我开始为 Visual Studio Code 开发自己的扩展(扩展市场上可用的扩展不起作用).我已经通过了着色器和错误解析,现在我正在创建一个带有代码建议的部分.

I'm currently developing an application based on Bluegiga BLE-112 module. The module is programmable in BGScript scripting language. However, only editor I found, that has some kind of support for BGScript, is Notepad++, which does not suit my needs. Therefore, I started development of my own extension for Visual Studio Code (one available in the extension market is not working). I already went past through colorizer and error parsing, now I'm creating a part with code suggestions.

主要问题

我想要 我打字时列出的语言中嵌入的所有函数、枚举和事件.我发现代码片段可能是实现这一目标的方式.JSON 格式的代码片段示例如下所示.

I'd like to have all functions, enumerations and events embedded in the language listed when I'm typing. I found that code snippets could be the way of achieving this. An example of snippet in JSON format that works as I expect looks like below.

"call attclient_indicate_confirm": {
    "prefix": "call attclient_indicate_confirm",
    "body": [
        "call attclient_indicate_confirm(${1:connection})(${2:result})"
    ],
    "description": "Send a acknowledge a received indication from a remote device."
}

call 是一个关键字,表示在这一行中调用了一个函数.当这样的代码片段存储在适当的 JSON 文件中时,一切正常 - 我开始输入 call,VS Code 建议一个代码片段,我可以使用它.

call is a keyword indicating, that in this line a function is envoked. When snippet like this is stored in a proper JSON file, everything works fine - I start typing call, VS Code suggests a code snippets and I can use it.

但是,为枚举 看起来有点矫枉过正,因为枚举没有任何变量 - 它们应该按原样插入.

However, creating a code snippet for enumerations looks like an overkill, as enums have nothing variable - they should be inserted as-is.

我的问题是:除了代码片段之外,还有什么方法可以在 Visual Studio Code 扩展中实现,以创建 BGScript 语言的枚举建议?

My question is: are there any ways other than code snippets, that could be implemented inside an Visual Studio Code extension, for creating suggestions of enumerations in BGScript language?

推荐答案

是的,您可以注册一个 CompletionItemProvider 通过 registerCompletionItemProvider()(参见 languages 命名空间).这应该涵盖您的所有用例,因为完成项还可以通过将它们的 insertText 设为 SnippetString 来使用代码段插入.

Yes, you can register a CompletionItemProvider via registerCompletionItemProvider() (see the languages namespace). This should cover all your use cases, as completion items can also use snippet insertion by making their insertText a SnippetString.

通常这(以及其他提供者)是通过一个名为语言服务器的抽象层实现的协议而不是直接使用 VSCode API,这使得它独立于编辑器.

Often this (along with other providers) is implemented through an abstraction layer called the Language Server Protocol instead of using the VSCode API directly, which makes it editor-independent.

这篇关于将内置函数插入 VS Code 扩展的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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