From d740f4da44f19c0c5fbe8ded1f5266d3093dfaa0 Mon Sep 17 00:00:00 2001 From: Roe-xin Date: Fri, 6 Mar 2026 16:24:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=A0=87=E7=AD=BE=E5=B8=A6=E8=A1=8C=E5=8F=B7?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 前端解析 file.v:1-2 格式,提取文件名和行号 - 新增 openFilePathTag 命令,支持智能文件查找 - 修复模板字符串中正则表达式转义问题 - 不影响现有 openFile 和 diff 功能 --- src/panels/ICHelperPanel.ts | 55 +++++++++++++++++++++++++++++++++++-- src/views/filePathTag.ts | 19 ++++++++++--- 2 files changed, 68 insertions(+), 6 deletions(-) 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 + }); + } } // 创建文件路径标签