fix: 修复关闭面板后快捷键无法自动打开面板的问题
- 通过 try-catch 检测 webview 是否真正可用 - 修复 panel._isDisposed 检测不准确的问题 - 增加异常捕获防止发送消息时崩溃 - 延长消息发送延迟至 500ms 确保面板加载完成
This commit is contained in:
@ -286,8 +286,12 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||||||
const addCodeToChat = vscode.commands.registerCommand(
|
const addCodeToChat = vscode.commands.registerCommand(
|
||||||
"ic-coder.addCodeToChat",
|
"ic-coder.addCodeToChat",
|
||||||
async () => {
|
async () => {
|
||||||
|
console.log('[addCodeToChat] 命令触发');
|
||||||
const editor = vscode.window.activeTextEditor;
|
const editor = vscode.window.activeTextEditor;
|
||||||
if (!editor) return;
|
if (!editor) {
|
||||||
|
console.log('[addCodeToChat] 没有活动编辑器');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const selection = editor.selection;
|
const selection = editor.selection;
|
||||||
const selectedText = editor.document.getText(selection);
|
const selectedText = editor.document.getText(selection);
|
||||||
@ -303,14 +307,40 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||||||
|
|
||||||
// 检查是否已有打开的面板
|
// 检查是否已有打开的面板
|
||||||
let panel = (global as any).currentICHelperPanel;
|
let panel = (global as any).currentICHelperPanel;
|
||||||
if (!panel || panel._isDisposed) {
|
let needCreatePanel = false;
|
||||||
|
|
||||||
|
if (!panel) {
|
||||||
|
needCreatePanel = true;
|
||||||
|
} else {
|
||||||
|
// 尝试访问 webview,如果抛出异常说明已销毁
|
||||||
|
try {
|
||||||
|
const _ = panel.webview;
|
||||||
|
} catch (e) {
|
||||||
|
needCreatePanel = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('[addCodeToChat] 需要创建面板:', needCreatePanel);
|
||||||
|
|
||||||
|
if (needCreatePanel) {
|
||||||
|
console.log('[addCodeToChat] 正在打开面板...');
|
||||||
await showICHelperPanel(context);
|
await showICHelperPanel(context);
|
||||||
panel = (global as any).currentICHelperPanel;
|
panel = (global as any).currentICHelperPanel;
|
||||||
|
console.log('[addCodeToChat] 面板打开后状态:', panel ? '成功' : '失败');
|
||||||
|
|
||||||
|
// 如果面板仍未创建(如未登录),直接返回
|
||||||
|
if (!panel) {
|
||||||
|
console.log('[addCodeToChat] 面板创建失败,退出');
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送代码上下文
|
// 发送代码上下文
|
||||||
|
console.log('[addCodeToChat] 准备发送代码到面板');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
try {
|
||||||
if (panel?.webview) {
|
if (panel?.webview) {
|
||||||
|
console.log('[addCodeToChat] 发送 addCodeContext 消息');
|
||||||
panel.webview.postMessage({
|
panel.webview.postMessage({
|
||||||
command: 'addCodeContext',
|
command: 'addCodeContext',
|
||||||
fileName,
|
fileName,
|
||||||
@ -320,7 +350,10 @@ export async function activate(context: vscode.ExtensionContext) {
|
|||||||
languageId: editor.document.languageId
|
languageId: editor.document.languageId
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, 300);
|
} catch (e) {
|
||||||
|
console.log('[addCodeToChat] 发送消息失败:', e);
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user