简体   繁体   中英

VSCode Open a File In a Specific Line Number Using JS

I added a new button to my VSCode, such that when I click it - it compiles the current folder, and shows dialog boxes using vscode.window.showInformationMessage .
Each box shows a compilation error, and has a button in it. Once the user clicks the button - it opens the problematic file in a tab using vscode.workspace.openTextDocument .

I want to make the button to also navigate me to the problematic line in the problematic file.

My question is:
Given a number, is it possible to navigate to a specific line number inside a file?

Sample code of what I achieved so far:

// Bullshit to give some context
const pattern = /(In \w+.jack)/g;
var i = s.search(pattern);
var substring = s.substring(i + 1)
var j = substring.search(pattern);
var s = "bsadsdbla In main.jack (line 55) sqdwqe blasdsd wq qqweq"
let GoToFile = 'Go to File'; 
var k = s.search(/(\w+.jack)/);
var l = s.search(/(.jack)/)
var fileName = s.substring(k, l);

// ---------> This is the important part <----------------
vscode.window.showInformationMessage(s.substring(i, j), GoToFile).then(selection => {
    if (selection === GoToFile) {
        vscode.workspace.openTextDocument(currentDirectory + '\\' + fileName + '.jack')
        .then(document => vscode.window.showTextDocument(document));
    }
});

I am assuming you have some code/regex in place that gives you the line number. Whenever someone clicks your GoToFile method invoke the following code:

activeEditor.selections = [new vscode.Selection(lineToGo, lineToGo)];
var range = new vscode.Range(lineToGo, lineToGo);
activeEditor.revealRange(range);

Some background:
As @rioV8 mentioned, revealRange was the way to go, but the problem was that I couldn't understand how to use it using the VSCode API, so here @Shahriar Hossain came into the picture. @Shahriar Hossain's code works, however, there was an important declaration missing, and I also had to figure out how to run the code when the user clicks the button.

This is the full solution:

       vscode.window.showInformationMessage(s.substring(i), GoToFile).then(selection => {
          if (selection === GoToFile) {

            vscode.workspace.openTextDocument(currentDirectory + '\\' + fileName + '.jack')
            .then(document => vscode.window.showTextDocument(document))

            // Whatever code inside this "then" block 
            // will be executed on button click
            .then(x => {
              let m = s.substring(i, j).search(/\(line \d+\)/);
              let subStr = s.substring(m + 6);
              let n = subStr.search(/\)/);
              subStr = subStr.substring(0, n);
              let lineToGo = parseInt(subStr.match(/\d+/));

              // The missing declaration of the activeEditor
              let activeEditor = vscode.window.activeTextEditor;
              let range = activeEditor.document.lineAt(lineToGo - 1).range;
              activeEditor.selection =  new vscode.Selection(range.start, range.end);
              activeEditor.revealRange(range);
            })

          }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM