首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VSCode扩展API函数provideCompletionItems()可以从多个触发器字符触发吗?

VSCode扩展API函数provideCompletionItems()可以从多个触发器字符触发吗?
EN

Stack Overflow用户
提问于 2021-12-21 14:54:50
回答 1查看 280关注 0票数 0

我正在编写一个扩展,为基于XML的数据格式描述语言(https://daffodil.apache.org/docs/dfdl/)提供自动完成结果。我使用的是VScode扩展API函数provideCompletionItems。我有许多完成项是属性。因为属性可以在单行ex上组合:

代码语言:javascript
复制
<xs:element name="RGBA_Value" type="xs:hexBinary" dfdl:lengthKind="explicit" dfdl:lengthUnits="bytes" dfdl:length="4" />

或由linefeed分隔:

代码语言:javascript
复制
<xs:element name="RGBA_Value"
type="xs:hexBinary"
dfdl:lengthKind="explicit"
dfdl:lengthUnits="bytes"
dfdl:length="4" />

我使用字符‘’(空格)和'\n‘(行提要)作为provideCompletionItems触发器的触发器,该触发器定义为字符串数组。我尝试过各种方法来组合字符串数组中的字符,但是没有成功。因此,我创建了两个基本相同的provideCompletionItems函数,一个带有‘’(空格)触发器,一个带有'\n‘(linefeed)触发器。这两个函数包含相同的21个完成项定义。我正试图找到一种不需要使用重复完成项的解决方案。如有任何帮助或指导,将不胜感激。

@Mark根据您的建议更改了我的代码,下面是一个简短的版本:

代码语言:javascript
复制
    const elementDfdlAttributeProvider = vscode.languages.registerCompletionItemProvider(
        'plaintext',
        {
            provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {
                const wholeLine = document.lineAt(position).text.substr(0, position.character);
                console.log('in elementspacetrigger: ' + wholeLine);
                if(wholeLine.includes('"{')) {
                    return undefined;                   
                }
                const dfdlDefineFormat = new vscode.CompletionItem("dfdl:defineFormat");
                dfdlDefineFormat.insertText = new vscode.SnippetString('<dfdl:defineFormat name="$1" >\n\t$2\n</dfdl:defineFormat>\n$0');
                dfdlDefineFormat.documentation = new vscode.MarkdownString("dfdl format name and configuration");
            
                const dfdlDefineEscape = new vscode.CompletionItem('dfdl:defineEscapeScheme');
                dfdlDefineEscape.insertText = new vscode.SnippetString('<dfdl:defineEscapeScheme name=$1 >\n\t$0,/dfdl:defineEscapeScheme>\n');
                dfdlDefineEscape.documentation = new vscode.MarkdownString("dfdl escape character definition");
.
.
.
                const dfdlOutputNewLine = new vscode.CompletionItem('dfdl:outputNewLine=');
                dfdlOutputNewLine.insertText = new vscode.SnippetString("dfdl:outputNewLine=\"${1|%CR,%LF,%CR%LF,%NEL,%LS|}\"$0");
                dfdlOutputNewLine.documentation = new vscode.MarkdownString("Specifies the character or characters that are used to replace the %NL; character class entity during unparse");
    
                //const wholeLine = document.lineAt(position).text.substr(0, position.character);
                if (wholeLine.includes('<xs:element name="') || (wholeLine.includes('<xs:element ref="'))) {
                    return [
                      xmlType,
                        minOccurs,
                        maxOccurs,
                        dfdlOccursCount,
                        dfdlOccursCountKind,
                        dfdlLength,
                        dfdlLengthKind,
                        dfdlLengthUnits,
                        dfdlLengthPattern,
                        xmlEncoding,
                        dfdlInputValueCalc,
                        dfdlOutputValueCalc,
                        dfdlAlignment,
                        dfdlAlignmentUnits,
                        dfdlTerminator,
                        dfdlOutputNewLine,
                    ];
                }
            }
        },

        ' ', '\n' // triggered whenever  ' ' or '\n' is typed
    );

完成函数适用于空格字符,而不是新的行/行提要。也许VSCode不喜欢'\n'?

但是,如果我删除了“”而只留下了“\n”,那就有效了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-21 16:51:55

这不是一个数组。这对我来说很管用:

代码语言:javascript
复制
`'"', '$'  // trigger intellisense/completion`
代码语言:javascript
复制
const configCompletionProvider = vscode.languages.registerCompletionItemProvider (
      { pattern: '**/keybindings.json' },     // your pattern here
      {
        provideCompletionItems(document, position) {}
      },
      '"', '$'
)

使用"$触发intellisense。

令人困惑的是,触发器字符的签名是:

...triggerCharacters: string[]

但我认为这意味着,如果您有一个触发器字符的数组,您可以这样使用它:

...['.', '"'],它等价于'.', '"'

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70437402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档