vscode extension: 如何获取当前行的范围

vscode extension: how to get the range of the current line

我可以使用 RangeSelection 构造函数来获取当前行的 RangeSelection,但我想知道是否有更简单的方法来实现相同?我真正想做的是用其他内容替换当前的文本行。目前我正在使用 activeTextEditoredit 方法。看来 vscode 编辑器 api 水平很低。

我认为这是尽可能直接地替换当前文本编辑器中的当前行:

const editor = vscode.window.activeTextEditor;
const selection = editor.selection;

// get the range of the current line, I don't think there is an easier way in the api
const currentLineRange = editor.document.lineAt(selection.active.line).range;

editor.edit(edit => edit.replace(currentLineRange, "my new text"));