5

I have a specialized WebView extension in VS Code that I use to generate .Net Classes. These files are generated through an external command line tool. One of the features that the command line tool provides is that it writes to a specific file, the location of the generated file in JSON format. I setup a file watcher on this particular file so that anytime it is updated, I run an extension method that parses that json file, extracts the file path from within the json and then opens that file inside VS Code.
While this works, my intent is to open this file inside a split editor, such that on one side I have my WebView (html) showing, and the other to show the file that was just opened (aka, that who's path came from the JSON file as mentioned above).

How do I open a file to be opposite side of a split window, keeping my webview ext. view on one side and the other side showing the newly opened file?

I have this working such that it opens the file, but not in a split-view editor

    // uri points to the file to read JSON from
    let fileUri: vscode.Uri = vscode.Uri.file(uri.fsPath);
    // read JSON from relative path of this file
    fss.readFile(fileUri.fsPath, 'utf8', function (err, data) 
    {
       if(!err) {
          try{
            // parse the data read from file as JSON
            var jsonObj = JSON.parse(data);
            try{
                // create uri from path within json
                let fileToOpenUri: vscode.Uri = vscode.Uri.file(jsonObj.path);
                // open and show the file inside VS code editor
                vscode.window.showTextDocument(fileToOpenUri);   
            }
            catch(ex)
            {
                // handle file Open error
                vscode.window.showErrorMessage(ex);
            }
          }
          catch(ex)
          {
            // handle JSON Parse error
            vscode.window.showErrorMessage(ex);
          }
        }
        else 
        {
            // handle file read error
            vscode.window.showErrorMessage(err.message);
        }
    });

Looking to open the file into the opposite side of a splitview.

John Kears
  • 677
  • 6
  • 20
  • If you want the same, but without writing an extension, see https://stackoverflow.com/questions/72130297/what-is-the-command-to-open-a-file-in-specified-viewcolumn-in-vs-codium – Ashark May 06 '22 at 10:15

1 Answers1

6
vscode.window.showTextDocument(document, {
    viewColumn: vscode.ViewColumn.Beside
});

https://code.visualstudio.com/api/references/vscode-api#TextDocumentShowOptions

https://code.visualstudio.com/api/references/vscode-api#ViewColumn

Alex
  • 59,571
  • 22
  • 137
  • 126
  • Wondering if you have seen any odd behaviour using `vscode.ViewColumn.Beside`? I've noticed that in some cases, a 2nd/3rd etc tab is opened instead of re-using the first opened tab? – user1859465 Jun 25 '20 at 17:29
  • Interesting that there doesn't seem to be a way to open the preview in a panel above the editor. – Alen Siljak Feb 22 '21 at 15:59
  • @user1859465 vscode.ViewColumn.Two However, it is custom editor in my case and it fails to render as such... – Tegiri Nenashi Aug 01 '22 at 22:10