扩展 LSP 自动完成修复此答案,我现在尝试获得大括号自动完成,其中完成时光标位于大括号中间。这是我到目前为止所拥有的:
const c = new vscode.CompletionItem('@{}');
c.range = new vscode.Range(position.translate(0, -1), position);
completions.push(c);
return completions;
但这会创建一个自动完成,光标位于
@{}
的末尾。我知道我可以用
c.insertText = new vscode.SnippetString('@{$1}');
但这需要按 Tab 键将光标返回到正常的打字模式,这比我想要的更繁重,因为这些括号的内容是用逗号分隔的小数字,例如
@{0,1}
或 @{1,3}
。
我已经看到简单的非制表符完成光标放置类型的自动完成自动发生在带有括号和大括号的语言的各种 VS Code 扩展中。一个例子是,当您开始在 Go VS Code 扩展中使用
{
编写函数体时,结尾 }
会自动放置在光标后面。
有没有办法让我只需在
@{
和 }
之间移动光标而不需要 Tab 补全?
你可以在
command
后面添加一个CompletionItem
,添加完成后然后运行任何vscode命令。试试这个:
let moveCursorCommand = {};
if (linePrefix.startsWith('@')) {
item = new vscode.CompletionItem("@{}");
// any title will do, but you must have one
moveCursorCommand.title = "Move cursor left between brackets";
// you can also call a command that you have created with registerCommand
moveCursorCommand.command = "cursorLeft"; // the command ID from Keyboard Shortcuts
}
if (item) {
item.range = new vscode.Range(position.translate(0, -1), position);
item.command = moveCursorCommand;
...
}