繁体   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