A proper way of inserting built-in functions into

2019-07-22 17:37发布

问题:

Introduction

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.

Main problem

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 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.

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?

回答1:

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.

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.