如何在VS代码拆分窗口中编程打开文件 [英] How to open file into VS Code Split window programatically
本文介绍了如何在VS代码拆分窗口中编程打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在VS代码中有一个专门的WebView扩展,用于生成.Net类。这些文件是通过外部命令行工具生成的。命令行工具提供的功能之一是它写入特定的文件,即JSON格式的生成文件的位置。
我在这个特定的文件上设置了一个文件观察器,以便每当它更新时,我都会运行一个扩展方法来解析该json文件,从json中提取文件路径,然后在VS代码中打开该文件。
虽然这是可行的,但我的目的是在拆分编辑器中打开这个文件,这样一边显示我的WebView(Html),另一边显示刚刚打开的文件(也就是,如上所述,谁的路径来自JSON文件)。
我让它正常工作,以便它打开文件,而不是在拆分视图编辑器中
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假"> // 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);
}
});
希望在拆分视图的另一侧打开文件。
推荐答案
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
这篇关于如何在VS代码拆分窗口中编程打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文