Vscode在指定行使用showTextDocument打开文件

时间:2020-06-10 21:07:14

标签: typescript visual-studio-code vscode-extensions

我希望我的扩展程序能够在某一行打开文档。有没有一种方法可以扩展下面的代码,以使光标在第10行第4列的列中打开文件??我已经看到了如何通过超链接完成此操作,但是我想知道是否存在导航至扩展名内文件的方式。

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

2 个答案:

答案 0 :(得分:1)

可能是这样,这是一个固定值,但是您可以传入或计算范围并显示位置。

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);
   })
});

答案 1 :(得分:1)

@ 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);
    });
});