feat:实现对文件的创建和删除
- 还涵盖了对已存在的文件进行覆盖 - 对不存在的文件创建 - 还可以创建目录 - 可以一次创建多个文件
This commit is contained in:
@ -1,14 +1,21 @@
|
||||
import * as vscode from "vscode";
|
||||
import { readFileContent } from "./readFiles";
|
||||
import { createFile, createOrOverwriteFile, deleteFile } from "./createFiles";
|
||||
|
||||
/**
|
||||
* 处理用户消息
|
||||
*/
|
||||
export function handleUserMessage(panel: vscode.WebviewPanel, text: string) {
|
||||
// 模拟AI回复
|
||||
const reply = getMockReply(text);
|
||||
export async function handleUserMessage(panel: vscode.WebviewPanel, text: string) {
|
||||
// 检查是否是文件操作命令
|
||||
const fileOperation = parseFileOperation(text);
|
||||
|
||||
// 延迟回复,模拟AI思考
|
||||
if (fileOperation) {
|
||||
await handleFileOperation(panel, fileOperation);
|
||||
return;
|
||||
}
|
||||
|
||||
// 普通消息处理
|
||||
const reply = getMockReply(text);
|
||||
setTimeout(() => {
|
||||
panel.webview.postMessage({
|
||||
command: "receiveMessage",
|
||||
@ -17,6 +24,133 @@ export function handleUserMessage(panel: vscode.WebviewPanel, text: string) {
|
||||
}, 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析文件操作命令
|
||||
*/
|
||||
function parseFileOperation(text: string): {
|
||||
type: 'create' | 'delete' | 'read';
|
||||
filePath: string;
|
||||
content?: string;
|
||||
} | null {
|
||||
const lowerText = text.toLowerCase().trim();
|
||||
|
||||
// 匹配创建文件:创建一个 xxx.ts 文件
|
||||
const createMatch = lowerText.match(/创建(?:一个)?(.+?\.\w+)(?:文件)?/);
|
||||
if (createMatch) {
|
||||
const filePath = createMatch[1].trim();
|
||||
return {
|
||||
type: 'create',
|
||||
filePath: filePath,
|
||||
content: getDefaultContent(filePath)
|
||||
};
|
||||
}
|
||||
|
||||
// 匹配删除文件:删除 xxx.ts 文件
|
||||
const deleteMatch = lowerText.match(/删除(.+?\.\w+)(?:文件)?/);
|
||||
if (deleteMatch) {
|
||||
const filePath = deleteMatch[1].trim();
|
||||
return {
|
||||
type: 'delete',
|
||||
filePath: filePath
|
||||
};
|
||||
}
|
||||
|
||||
// 匹配读取文件:读取 xxx.ts 文件 或 打开 xxx.ts
|
||||
const readMatch = lowerText.match(/(?:读取|打开)(.+?\.\w+)(?:文件)?/);
|
||||
if (readMatch) {
|
||||
const filePath = readMatch[1].trim();
|
||||
return {
|
||||
type: 'read',
|
||||
filePath: filePath
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文件操作
|
||||
*/
|
||||
async function handleFileOperation(
|
||||
panel: vscode.WebviewPanel,
|
||||
operation: { type: 'create' | 'delete' | 'read'; filePath: string; content?: string }
|
||||
) {
|
||||
try {
|
||||
switch (operation.type) {
|
||||
case 'create':
|
||||
await createFile(operation.filePath, operation.content || '');
|
||||
panel.webview.postMessage({
|
||||
command: "receiveMessage",
|
||||
text: `✅ 文件创建成功: ${operation.filePath}`,
|
||||
});
|
||||
vscode.window.showInformationMessage(`文件创建成功: ${operation.filePath}`);
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
await deleteFile(operation.filePath);
|
||||
panel.webview.postMessage({
|
||||
command: "receiveMessage",
|
||||
text: `✅ 文件删除成功: ${operation.filePath}`,
|
||||
});
|
||||
vscode.window.showInformationMessage(`文件删除成功: ${operation.filePath}`);
|
||||
break;
|
||||
|
||||
case 'read':
|
||||
const content = await readFileContent(operation.filePath);
|
||||
panel.webview.postMessage({
|
||||
command: "fileContent",
|
||||
content: content,
|
||||
filePath: operation.filePath,
|
||||
});
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : "操作失败";
|
||||
panel.webview.postMessage({
|
||||
command: "receiveMessage",
|
||||
text: `❌ ${errorMsg}`,
|
||||
});
|
||||
vscode.window.showErrorMessage(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件扩展名生成默认内容
|
||||
*/
|
||||
function getDefaultContent(filePath: string): string {
|
||||
const ext = filePath.split('.').pop()?.toLowerCase();
|
||||
|
||||
switch (ext) {
|
||||
case 'ts':
|
||||
return `// ${filePath}\n\nexport {};\n`;
|
||||
case 'js':
|
||||
return `// ${filePath}\n\n`;
|
||||
case 'json':
|
||||
return '{\n \n}\n';
|
||||
case 'md':
|
||||
return `# ${filePath}\n\n`;
|
||||
case 'html':
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>`;
|
||||
case 'css':
|
||||
return `/* ${filePath} */\n\n`;
|
||||
case 'v':
|
||||
case 'sv':
|
||||
return `// ${filePath}\n\nmodule ${filePath.split('.')[0]} (\n \n);\n\nendmodule\n`;
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文件读取请求
|
||||
*/
|
||||
@ -39,6 +173,39 @@ export async function handleReadFile(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文件创建请求
|
||||
*/
|
||||
export async function handleCreateFile(
|
||||
panel: vscode.WebviewPanel,
|
||||
filePath: string,
|
||||
content: string,
|
||||
overwrite: boolean = false //是否覆盖
|
||||
) {
|
||||
try {
|
||||
if (overwrite) {
|
||||
await createOrOverwriteFile(filePath, content);
|
||||
} else {
|
||||
await createFile(filePath, content);
|
||||
}
|
||||
|
||||
panel.webview.postMessage({
|
||||
command: "fileCreated",
|
||||
filePath: filePath,
|
||||
message: " 文件创建成功",
|
||||
});
|
||||
vscode.window.showInformationMessage(`文件创建成功: ${filePath}`);
|
||||
} catch (error) {
|
||||
panel.webview.postMessage({
|
||||
command: "fileCreateError",
|
||||
error: error instanceof Error ? error.message : "创建文件失败",
|
||||
});
|
||||
vscode.window.showErrorMessage(
|
||||
`创建文件失败: ${error instanceof Error ? error.message : "未知错误"}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模拟回复
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user