VSCode 扩展:如何设置值?

VSCode Extension: How to set value?

我通读了 API 文档并了解如何获取值,但不太了解如何设置值。

例如,我想更改活动行(光标行),因此尝试按如下方式更新 editor.selection.active 的值。

// get the current value
editor.selection.active
V {_line: 12, _character: 0}

// I tried to update the value
editor.selection.active.line = 3
3

// I was expecting this an updated value but unchanged
editor.selection.active
V {_line: 12, _character: 0}

但正如上面的评论,它不会更新值,不会更改活动行位置。那么我该怎么做呢?谢谢

您必须使用新选择更新 editor.selections 属性:


const newStartPosition = new vscode.Position(3, 0);
const newEndPosition = new vscode.Position(yourLine, yourCharacter);
const newSelections = [new vscode.Selection(newStartPosition, newEndPosition )];


editor.selections = newSelections;