我正在编写一个扩展,为基于XML的数据格式描述语言(https://daffodil.apache.org/docs/dfdl/)提供自动完成结果。我使用的是VScode扩展API函数provideCompletionItems。我有许多完成项是属性。因为属性可以在单行ex上组合:
<xs:element name="RGBA_Value" type="xs:hexBinary" dfdl:lengthKind="explicit" dfdl:lengthUnits="bytes" dfdl:length="4" />或由linefeed分隔:
<xs:element name="RGBA_Value"
type="xs:hexBinary"
dfdl:lengthKind="explicit"
dfdl:lengthUnits="bytes"
dfdl:length="4" />我使用字符‘’(空格)和'\n‘(行提要)作为provideCompletionItems触发器的触发器,该触发器定义为字符串数组。我尝试过各种方法来组合字符串数组中的字符,但是没有成功。因此,我创建了两个基本相同的provideCompletionItems函数,一个带有‘’(空格)触发器,一个带有'\n‘(linefeed)触发器。这两个函数包含相同的21个完成项定义。我正试图找到一种不需要使用重复完成项的解决方案。如有任何帮助或指导,将不胜感激。
@Mark根据您的建议更改了我的代码,下面是一个简短的版本:
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”,那就有效了。
发布于 2021-12-21 16:51:55
这不是一个数组。这对我来说很管用:
`'"', '$' // trigger intellisense/completion`const configCompletionProvider = vscode.languages.registerCompletionItemProvider (
{ pattern: '**/keybindings.json' }, // your pattern here
{
provideCompletionItems(document, position) {}
},
'"', '$'
)使用"和$触发intellisense。
令人困惑的是,触发器字符的签名是:
...triggerCharacters: string[]
但我认为这意味着,如果您有一个触发器字符的数组,您可以这样使用它:
...['.', '"'],它等价于'.', '"'
https://stackoverflow.com/questions/70437402
复制相似问题