feat:将extension文件拆分成不同功能的独立组件
This commit is contained in:
67
src/utils/messageHandler.ts
Normal file
67
src/utils/messageHandler.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import * as vscode from "vscode";
|
||||
|
||||
/**
|
||||
* 处理用户消息
|
||||
*/
|
||||
export function handleUserMessage(panel: vscode.WebviewPanel, text: string) {
|
||||
// 模拟AI回复
|
||||
const reply = getMockReply(text);
|
||||
|
||||
// 延迟回复,模拟AI思考
|
||||
setTimeout(() => {
|
||||
panel.webview.postMessage({
|
||||
command: "receiveMessage",
|
||||
text: reply,
|
||||
});
|
||||
}, 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模拟回复
|
||||
*/
|
||||
function getMockReply(question: string): string {
|
||||
const replies = [
|
||||
`已收到您的问题:"${question}"
|
||||
|
||||
这是一个演示版本,实际需要连接AI服务。
|
||||
|
||||
示例回复:这是一个计数器模板:
|
||||
\`\`\`verilog
|
||||
module counter (
|
||||
input clk,
|
||||
input rst_n,
|
||||
output reg [3:0] count
|
||||
);
|
||||
always @(posedge clk or negedge rst_n) begin
|
||||
if (!rst_n) count <= 0;
|
||||
else count <= count + 1;
|
||||
end
|
||||
endmodule
|
||||
\`\`\``,
|
||||
|
||||
`感谢提问!关于"${question}",在真实版本中我会:
|
||||
1. 分析您的代码上下文
|
||||
2. 提供优化建议
|
||||
3. 生成完整代码
|
||||
4. 解释设计原理
|
||||
|
||||
当前是演示版,请点击侧边栏按钮快速生成代码。`,
|
||||
];
|
||||
|
||||
return replies[Math.floor(Math.random() * replies.length)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 将代码插入到编辑器
|
||||
*/
|
||||
export function insertCodeToEditor(code: string) {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor) {
|
||||
editor.edit((editBuilder) => {
|
||||
editBuilder.insert(editor.selection.active, code);
|
||||
});
|
||||
vscode.window.showInformationMessage("代码已插入");
|
||||
} else {
|
||||
vscode.window.showWarningMessage("请先打开一个编辑器");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user