feat: 添加工作区状态检查功能,优化用户体验

- 用户鼠标聚焦到输入框中就弹窗提示用户打开 优化用户体验
This commit is contained in:
Roe-xin
2025-12-30 16:02:36 +08:00
parent 0f458f6299
commit 3f0cc8ae29
4 changed files with 88 additions and 21 deletions

View File

@ -16,7 +16,9 @@ export async function createFile(
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
} else {
throw new Error("没有打开的工作区,无法创建相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您创建文件了"
);
}
}
@ -28,7 +30,7 @@ export async function createFile(
throw new Error(`文件已存在: ${absolutePath}`);
} catch (error: any) {
// 如果文件不存在,继续创建
if (error.code !== 'FileNotFound') {
if (error.code !== "FileNotFound") {
throw error;
}
}
@ -65,7 +67,9 @@ export async function createOrOverwriteFile(
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
} else {
throw new Error("没有打开的工作区,无法创建相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您创建文件了"
);
}
}
@ -99,7 +103,9 @@ export async function createDirectory(dirPath: string): Promise<void> {
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, dirPath);
} else {
throw new Error("没有打开的工作区,无法创建相对路径的目录");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您创建目录了"
);
}
}
@ -115,7 +121,7 @@ export async function createDirectory(dirPath: string): Promise<void> {
}
} catch (error: any) {
// 如果目录不存在,继续创建
if (error.code !== 'FileNotFound') {
if (error.code !== "FileNotFound") {
throw error;
}
}
@ -161,7 +167,9 @@ export async function deleteFile(filePath: string): Promise<void> {
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
} else {
throw new Error("没有打开的工作区,无法删除相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您删除文件了"
);
}
}
@ -197,7 +205,9 @@ export async function updateFile(
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
} else {
throw new Error("没有打开的工作区,无法修改相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您修改文件了"
);
}
}
@ -236,7 +246,9 @@ export async function appendToFile(
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
} else {
throw new Error("没有打开的工作区,无法追加相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您追加文件内容了"
);
}
}
@ -274,7 +286,9 @@ export async function replaceFile(
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
} else {
throw new Error("没有打开的工作区,无法修改相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您修改文件了"
);
}
}
@ -291,14 +305,17 @@ export async function replaceFile(
// 转义特殊字符,将字符串作为字面量处理
const escapeRegExp = (str: string) => {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
// 替换内容 - 如果是字符串,先转义特殊字符
let newContent: string;
if (typeof searchValue === 'string') {
if (typeof searchValue === "string") {
const escapedSearch = escapeRegExp(searchValue);
newContent = fileContent.replace(new RegExp(escapedSearch, "g"), replaceValue);
newContent = fileContent.replace(
new RegExp(escapedSearch, "g"),
replaceValue
);
} else {
newContent = fileContent.replace(searchValue, replaceValue);
}
@ -330,7 +347,9 @@ export async function insertAtLine(
if (workspaceFolders && workspaceFolders.length > 0) {
absolutePath = path.join(workspaceFolders[0].uri.fsPath, filePath);
} else {
throw new Error("没有打开的工作区,无法修改相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您修改文件了"
);
}
}
@ -382,7 +401,9 @@ export async function renameFile(
absoluteNewPath = path.join(workspaceRoot, newPath);
}
} else {
throw new Error("没有打开的工作区,无法重命名相对路径的文件");
throw new Error(
"请先打开一个文件夹作为工作区,这样我就能为您重命名文件了"
);
}
const oldUri = vscode.Uri.file(absoluteOldPath);
@ -401,10 +422,13 @@ export async function renameFile(
throw new Error(`目标文件已存在: ${absoluteNewPath}`);
} catch (error: any) {
// 如果文件不存在,继续重命名
if (error.code !== 'FileNotFound' && !error.message.includes('目标文件已存在')) {
if (
error.code !== "FileNotFound" &&
!error.message.includes("目标文件已存在")
) {
throw error;
}
if (error.message.includes('目标文件已存在')) {
if (error.message.includes("目标文件已存在")) {
throw error;
}
}