我正在開發一個跳轉到特定file:num的 VS Code 擴展,但是在打開檔案后我被困在將游標移動到特定行的步驟中。我怎樣才能做到這一點:
export const openAndMoveToLine = async (file_line: string) => {
// /home/user/some/path.php:10
let [filename, line_number] = file_line.split(":")
// opening the file => OK
let setting: vscode.Uri = vscode.Uri.parse(filename)
let doc = await vscode.workspace.openTextDocument(setting)
vscode.window.showTextDocument(doc, 1, false);
// FIXME: After being opened, now move to the line X => NOK **/
await vscode.commands.executeCommand("cursorMove", {
to: "down", by:'wrappedLine',
value: parseInt(line_number)
});
}
謝謝
uj5u.com熱心網友回復:
可以使用TextDocumentShowOptions輕松完成:
const showDocOptions = {
preserveFocus: false,
preview: false,
viewColumn: 1,
// replace with your line_number's
selection: new vscode.Range(314, 0, 314, 0)
};
let doc = await vscode.window.showTextDocument(setting, showDocOptions);
uj5u.com熱心網友回復:
您首先需要訪問活動編輯器。這是通過將 a 添加.then到回傳文本編輯器物件的showTextDocument呼叫(這是一個函式)中來完成的。Thenable然后,您將能夠使用textEditor變數(如示例中所示)使用selection屬性設定游標的位置,如下所示:
vscode.window.showTextDocument(doc, 1, false).then((textEditor: TextEditor) => {
const lineNumber = 1;
const characterNumberOnLine = 1;
const position = new vscode.Position(lineNumber, characterNumberOnLine);
const newSelection = new vscode.Selection(position, position);
textEditor.selection = newSelection;
});
selection可以在此處找到對 API 的參考。
您正在探索的用例已在 GitHub 問題中討論,可在此處找到。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/491125.html
標籤:javascript 打字稿 视觉工作室代码 vscode 扩展
