簡體   English   中英

Vscode 在指定行用 showTextDocument 打開文件

[英]Vscode open file with showTextDocument at specified line

我希望我的擴展程序能夠在特定行打開文檔。 有沒有辦法擴展下面的代碼以在第 10 行第 4 列使用 cursor 打開文件? 我已經看到這是如何使用超鏈接完成的,但我想知道是否有一種方法可以導航到擴展中的文件。

var openPath = vscode.Uri.file(filePath);
vscode.workspace.openTextDocument(openPath).then(doc => {
     vscode.window.showTextDocument(doc);
});

@ddavid456 提供了我需要的大部分答案,當我在下面的塊中再添加一行時,我得到了我需要的全部功能。

var pos1 = new vscode.Position(10,4);
var openPath = vscode.Uri.file(filePath);
vscode.workspace.openTextDocument(openPath).then(doc => 
{
    vscode.window.showTextDocument(doc).then(editor => 
    {
        // Line added - by having a selection at the same position twice, the cursor jumps there
        editor.selections = [new vscode.Selection(pos1,pos1)]; 

        // And the visible range jumps there too
        var range = new vscode.Range(pos1, pos1);
        editor.revealRange(range);
    });
});

可能是這樣的,這是一個固定值,但您可以傳入或計算一個范圍並顯示 position。

var openPath = vscode.Uri.file(filePath);
vscode.workspace.openTextDocument(openPath).then(doc => {
vscode.window.showTextDocument(doc).then(editor => {
    var range = new vscode.Range(new vscode.Position(10, 4), new vscode.Position(11, 0));
    editor.revealRange(range);
   })
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM