feat: 支持文件路径标签带行号点击跳转
- 前端解析 file.v:1-2 格式,提取文件名和行号 - 新增 openFilePathTag 命令,支持智能文件查找 - 修复模板字符串中正则表达式转义问题 - 不影响现有 openFile 和 diff 功能
This commit is contained in:
@ -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) {
|
||||
|
||||
@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 创建文件路径标签
|
||||
|
||||
Reference in New Issue
Block a user