简体   繁体   English

Vscode 在指定行用 showTextDocument 打开文件

[英]Vscode open file with showTextDocument at specified line

I want my extension to be able to open a document at a certain line.我希望我的扩展程序能够在特定行打开文档。 Is there a way to expand the code below to have the file open with the cursor at say line 10, column 4?有没有办法扩展下面的代码以在第 10 行第 4 列使用 cursor 打开文件? I've seen how this is done with a hyperlink, but I was wondering if there's a way to navigate to the file within the extension.我已经看到这是如何使用超链接完成的,但我想知道是否有一种方法可以导航到扩展中的文件。

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

@ddavid456 had most of the answer I needed, and when I added one more line to the block below, I got the entire functionality I needed. @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);
    });
});

Possibly something like this, where this is a fixed value, but you can pass in or calculate a range and reveal the position.可能是这样的,这是一个固定值,但您可以传入或计算一个范围并显示 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