1.0.2 优化打开逻辑
This commit is contained in:
77
dist/commands.js
vendored
77
dist/commands.js
vendored
@@ -35,6 +35,54 @@ var __importStar = (this && this.__importStar) || (function () {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.registerCommands = registerCommands;
|
||||
const vscode = __importStar(require("vscode"));
|
||||
// 查找已打开的文档
|
||||
function findExistingDocument(filePath) {
|
||||
const documents = vscode.workspace.textDocuments;
|
||||
for (const doc of documents) {
|
||||
if (doc.uri.scheme === 'squirrel' && doc.uri.path === `/${filePath}`) {
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
// 打开文件的通用函数
|
||||
async function openFile(model, provider, entry) {
|
||||
if (!model.getIsConnected()) {
|
||||
vscode.window.showErrorMessage('请先连接到 pvfUtility API');
|
||||
return;
|
||||
}
|
||||
// 检查是否已经有相同的文件打开
|
||||
const existingDocument = findExistingDocument(entry.key);
|
||||
if (existingDocument) {
|
||||
// 如果文件已打开,则聚焦到该标签页
|
||||
await vscode.window.showTextDocument(existingDocument);
|
||||
return;
|
||||
}
|
||||
// 如果文件未打开,则在新标签页中打开
|
||||
try {
|
||||
await vscode.window.withProgress({
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
title: `正在加载文件 ${entry.name}...`,
|
||||
cancellable: false
|
||||
}, async (progress) => {
|
||||
const content = await model.getFileContent(entry.key);
|
||||
if (content !== undefined) {
|
||||
// 创建虚拟文档,使用时间戳确保URI唯一以支持多标签页
|
||||
const timestamp = Date.now();
|
||||
const uri = vscode.Uri.parse(`squirrel:/${entry.key}?instance=${timestamp}`);
|
||||
const doc = await vscode.workspace.openTextDocument(uri);
|
||||
await vscode.window.showTextDocument(doc, { preview: false });
|
||||
provider.refresh();
|
||||
}
|
||||
else {
|
||||
vscode.window.showErrorMessage(`加载文件 ${entry.name} 失败`);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
vscode.window.showErrorMessage(`打开文件失败: ${error}`);
|
||||
}
|
||||
}
|
||||
function registerCommands(context, model, provider, functionExtractor, output) {
|
||||
// 连接到 API
|
||||
console.log('注册 squirrel.connectToApi 命令...');
|
||||
@@ -95,34 +143,9 @@ function registerCommands(context, model, provider, functionExtractor, output) {
|
||||
vscode.window.showErrorMessage(`刷新失败: ${error}`);
|
||||
}
|
||||
});
|
||||
// 打开文件
|
||||
// 打开文件(单击或双击文件时调用)
|
||||
const openFileCommand = vscode.commands.registerCommand('squirrel.openFile', async (entry) => {
|
||||
if (!model.getIsConnected()) {
|
||||
vscode.window.showErrorMessage('请先连接到 pvfUtility API');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await vscode.window.withProgress({
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
title: `正在加载文件 ${entry.name}...`,
|
||||
cancellable: false
|
||||
}, async (progress) => {
|
||||
const content = await model.getFileContent(entry.key);
|
||||
if (content !== undefined) {
|
||||
// 创建虚拟文档
|
||||
const uri = vscode.Uri.parse(`squirrel:/${entry.key}`);
|
||||
const doc = await vscode.workspace.openTextDocument(uri);
|
||||
await vscode.window.showTextDocument(doc);
|
||||
provider.refresh();
|
||||
}
|
||||
else {
|
||||
vscode.window.showErrorMessage(`加载文件 ${entry.name} 失败`);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
vscode.window.showErrorMessage(`打开文件失败: ${error}`);
|
||||
}
|
||||
await openFile(model, provider, entry);
|
||||
});
|
||||
// 保存文件(通过 VS Code 的保存事件处理,这个命令主要用于显示)
|
||||
const saveFileCommand = vscode.commands.registerCommand('squirrel.saveFile', async () => {
|
||||
|
||||
Reference in New Issue
Block a user