首页 > 解决方案 > Vscode在指定行使用showTextDocument打开文件

问题描述

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

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

标签: typescriptvisual-studio-codevscode-extensions

解决方案


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

推荐阅读