diff --git a/src/panels/ICHelperPanel.ts b/src/panels/ICHelperPanel.ts index 871a987..ec28085 100644 --- a/src/panels/ICHelperPanel.ts +++ b/src/panels/ICHelperPanel.ts @@ -497,7 +497,12 @@ export async function showICHelperPanel( case "openFile": // 打开文件 if (message.filePath) { - vscode.workspace.openTextDocument(message.filePath).then(doc => { + const path = require('path'); + const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; + const fullPath = path.isAbsolute(message.filePath) || !workspaceFolder + ? message.filePath + : vscode.Uri.joinPath(workspaceFolder.uri, message.filePath).fsPath; + vscode.workspace.openTextDocument(fullPath).then(doc => { vscode.window.showTextDocument(doc); }); } @@ -505,7 +510,12 @@ export async function showICHelperPanel( case "openFileWithSelection": // 打开文件并选中代码 if (message.filePath) { - vscode.workspace.openTextDocument(message.filePath).then(doc => { + const path = require('path'); + const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; + const fullPath = path.isAbsolute(message.filePath) || !workspaceFolder + ? message.filePath + : vscode.Uri.joinPath(workspaceFolder.uri, message.filePath).fsPath; + vscode.workspace.openTextDocument(fullPath).then(doc => { vscode.window.showTextDocument(doc).then(editor => { const start = new vscode.Position(message.startLine - 1, 0); const end = new vscode.Position(message.endLine - 1, doc.lineAt(message.endLine - 1).text.length); @@ -515,6 +525,47 @@ export async function showICHelperPanel( }); } break; + case "openFilePathTag": + // 打开文件路径标签(智能查找) + if (message.filePath) { + const path = require('path'); + const fs = require('fs'); + const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; + + let fullPath = message.filePath; + + // 如果是相对路径且工作区存在 + if (!path.isAbsolute(message.filePath) && workspaceFolder) { + const candidatePath = vscode.Uri.joinPath(workspaceFolder.uri, message.filePath).fsPath; + // 检查文件是否存在 + if (fs.existsSync(candidatePath)) { + fullPath = candidatePath; + } else { + // 尝试在工作区中搜索该文件 + const fileName = path.basename(message.filePath); + const files = await vscode.workspace.findFiles(`**/${fileName}`, '**/node_modules/**', 1); + if (files.length > 0) { + fullPath = files[0].fsPath; + } + } + } + + if (message.startLine && message.endLine) { + vscode.workspace.openTextDocument(fullPath).then(doc => { + vscode.window.showTextDocument(doc).then(editor => { + const start = new vscode.Position(message.startLine - 1, 0); + const end = new vscode.Position(message.endLine - 1, doc.lineAt(message.endLine - 1).text.length); + editor.selection = new vscode.Selection(start, end); + editor.revealRange(new vscode.Range(start, end)); + }); + }); + } else { + vscode.workspace.openTextDocument(fullPath).then(doc => { + vscode.window.showTextDocument(doc); + }); + } + } + break; case "acceptChange": // 采纳变更 if (message.changeId) { diff --git a/src/views/filePathTag.ts b/src/views/filePathTag.ts index 72b14e5..6c3ac51 100644 --- a/src/views/filePathTag.ts +++ b/src/views/filePathTag.ts @@ -46,10 +46,21 @@ export function getFilePathTagScript(): string { return ` // 处理文件路径标签点击 function handleFilePathClick(filePath) { - vscode.postMessage({ - command: 'openFile', - filePath: filePath - }); + // 解析文件路径,支持 file.v:5-8 格式 + const match = filePath.match(/^(.+?):(\\d+)-(\\d+)$/); + if (match) { + vscode.postMessage({ + command: 'openFilePathTag', + filePath: match[1], + startLine: parseInt(match[2]), + endLine: parseInt(match[3]) + }); + } else { + vscode.postMessage({ + command: 'openFilePathTag', + filePath: filePath + }); + } } // 创建文件路径标签