更改 VS Code CompletionItem 的字符位置

问题描述 投票:0回答:1

扩展 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 补全?

intellisense vscode-extensions
1个回答
0
投票

你可以在

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;
  
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.