如何以编程方式将文件打开到VS Code Split窗口

时间:2019-07-05 12:06:12

标签: visual-studio-code vscode-extensions

我在VS Code中有一个专门的WebView扩展,用于生成.Net类。这些文件是通过外部命令行工具生成的。命令行工具提供的功能之一是将其写入JSON格式的特定文件,即生成文件的位置。 我在此特定文件上设置了文件监视程序,以便随时对其进行更新,然后运行扩展方法来解析该json文件,从json中提取文件路径,然后在VS Code中打开该文件。
在执行此操作的同时,我的目的是在拆分编辑器中打开此文件,这样一来,我的WebView(html)就会显示出来,而另一侧则是会显示刚刚打开的文件(也就是,谁的路径来自如上所述的JSON文件)。

如何在拆分窗口的对面打开文件,同时保持Webview扩展。视图的一侧和另一侧显示新打开的文件?

我的工作方式是打开文件,但不能在拆分视图编辑器中

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

希望将文件打开到splitview的另一侧。

1 个答案:

答案 0 :(得分:1)