feat: 添加上下文项点击功能

- 文件类型可点击打开文件
   - 代码片段可点击打开文件并选中对应代码
   - 文件夹类型不可点击
This commit is contained in:
Roe-xin
2026-03-06 10:13:27 +08:00
parent 4384ee53c5
commit 45934baf0a
2 changed files with 52 additions and 3 deletions

View File

@ -494,6 +494,27 @@ export async function showICHelperPanel(
// 退出登录
vscode.commands.executeCommand("ic-coder.logout");
break;
case "openFile":
// 打开文件
if (message.filePath) {
vscode.workspace.openTextDocument(message.filePath).then(doc => {
vscode.window.showTextDocument(doc);
});
}
break;
case "openFileWithSelection":
// 打开文件并选中代码
if (message.filePath) {
vscode.workspace.openTextDocument(message.filePath).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));
});
});
}
break;
case "acceptChange":
// 采纳变更
if (message.changeId) {

View File

@ -51,7 +51,11 @@ export function getContextDisplayStyles(): string {
transition: all 0.2s ease;
}
.context-item:hover {
.context-item.clickable {
cursor: pointer;
}
.context-item.clickable:hover {
background: var(--vscode-list-hoverBackground);
}
@ -180,11 +184,14 @@ export function getContextDisplayScript(): string {
case 'code': icon = getCodeIcon(); break;
}
const clickable = item.type !== 'folder' ? 'clickable' : '';
const onclick = item.type !== 'folder' ? \`onclick="window.handleContextItemClick(\${item.id})"\` : '';
return \`
<div class="context-item" title="\${item.path || item.displayPath}">
<div class="context-item \${clickable}" title="\${item.path || item.displayPath}" \${onclick}>
\${icon}
<span class="context-item-name">\${item.displayPath || getFileName(item.path)}</span>
<span class="context-item-remove" onclick="removeContextItem(\${item.id})">
<span class="context-item-remove" onclick="event.stopPropagation(); removeContextItem(\${item.id})">
\${getRemoveIcon()}
</span>
</div>
@ -192,6 +199,27 @@ export function getContextDisplayScript(): string {
}).join('');
}
// 全局访问函数
window.handleContextItemClick = function(id) {
const item = contextItems.find(i => i.id === id);
if (!item || item.type === 'folder') return;
if (item.type === 'code') {
const codeData = JSON.parse(item.path);
vscode.postMessage({
command: 'openFileWithSelection',
filePath: codeData.fileName,
startLine: codeData.startLine,
endLine: codeData.endLine
});
} else {
vscode.postMessage({
command: 'openFile',
filePath: item.path
});
}
};
// 处理后端返回的文件选择结果
window.addEventListener('message', event => {
const message = event.data;