修改保存逻辑:在上传文件前先通过C++编译服务验证脚本

- 修改model.ts的saveFileContent方法,在上传文件前先通过C++服务验证脚本
- 如果C++服务可连接,则先发送脚本进行编译验证,只有在返回200状态码(编译通过)后才上传到服务器
- 如果C++服务返回非200状态码(编译失败),则不允许上传并提示错误
- 如果C++服务不可连接,则直接上传到服务器
- 简化extension.ts中的保存事件监听器,仅执行UI更新和缓存更新
- 保持原有错误处理机制,确保在C++服务故障时也能正常上传文件

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
睿 安
2025-10-03 20:34:01 +08:00
parent 95dfc4397e
commit 859af05c7f
6 changed files with 113 additions and 113 deletions

44
dist/extension.js vendored
View File

@@ -49,7 +49,6 @@ const onTypeFormattingProvider_1 = require("./providers/onTypeFormattingProvider
const documentFormattingProvider_1 = require("./providers/documentFormattingProvider");
const codeErrorProvider_1 = require("./providers/codeErrorProvider");
const apiParser_1 = require("./providers/apiParser");
const localClient_1 = require("./localClient");
function activate(context) {
console.log('Squirrel NUT Explorer 正在激活...');
// 初始化API解析器
@@ -133,51 +132,12 @@ function activate(context) {
const saveListener = vscode.workspace.onDidSaveTextDocument(async (doc) => {
if (doc.uri.scheme === 'squirrel') {
const filePath = doc.uri.path.substring(1); // 去掉开头的 '/'
const fileContent = doc.getText();
// 检查C++服务是否可连接
const isCppServiceConnected = await (0, localClient_1.checkCppServiceConnection)();
if (isCppServiceConnected) {
output.appendLine('[Squirrel] C++服务可连接,尝试发送文件内容');
try {
const sendSuccess = await (0, localClient_1.sendFileToCppService)(filePath, fileContent);
if (sendSuccess) {
output.appendLine(`[Squirrel] 文件 ${filePath} 已发送到C++服务`);
// C++服务返回200后继续执行保存到服务器
const success = await model.saveFileContent(filePath, fileContent);
if (success) {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存成功`);
// 当文件保存成功后执行UI更新和缓存更新
output.appendLine(`[Squirrel] 文件 ${filePath} 已保存到服务器`);
provider.refresh();
// 更新函数缓存
await functionExtractor.updateFileCache(model, filePath);
}
else {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存到服务器失败`);
}
}
else {
output.appendLine(`[Squirrel] 文件 ${filePath} 发送到C++服务失败,跳过保存到服务器`);
}
}
catch (error) {
output.appendLine(`[Squirrel] 发送文件到C++服务时发生错误: ${error}`);
output.appendLine(`[Squirrel] 跳过保存文件 ${filePath} 到服务器`);
}
}
else {
output.appendLine('[Squirrel] C++服务不可连接,直接保存到服务器');
// C++服务不可连接,直接保存到服务器
const success = await model.saveFileContent(filePath, fileContent);
if (success) {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存成功`);
provider.refresh();
// 更新函数缓存
await functionExtractor.updateFileCache(model, filePath);
}
else {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存到服务器失败`);
}
}
}
});
// 注册文件关闭事件以清理缓存
const closeListener = vscode.workspace.onDidCloseTextDocument((doc) => {

File diff suppressed because one or more lines are too long

42
dist/model.js vendored
View File

@@ -36,6 +36,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.FileModel = void 0;
const vscode = __importStar(require("vscode"));
const apiClient_1 = require("./apiClient");
const localClient_1 = require("./localClient");
class FileModel {
constructor() {
this.fileList = new Map();
@@ -185,6 +186,46 @@ class FileModel {
}
// 保存文件内容
async saveFileContent(filePath, content) {
// 先检查C++服务是否可连接
const isCppServiceConnected = await (0, localClient_1.checkCppServiceConnection)();
if (isCppServiceConnected) {
// C++服务可连接,先发送进行编译验证
try {
const sendSuccess = await (0, localClient_1.sendFileToCppService)(filePath, content);
if (sendSuccess) {
// C++编译服务返回200说明脚本编译通过允许上传
const success = await this.apiClient.uploadFileContent(filePath, content);
if (success) {
const entry = this.fileList.get(filePath);
if (entry) {
entry.content = content;
entry.lastLoaded = new Date();
}
return true;
}
}
else {
// C++编译服务返回非200说明脚本存在问题不允许上传
vscode.window.showErrorMessage(`脚本编译失败,无法保存文件 ${filePath}。请检查代码错误后再试。`);
return false;
}
}
catch (error) {
// 向C++服务发送文件过程中出现错误
vscode.window.showErrorMessage(`发送文件到编译服务时出错: ${error},但仍尝试直接保存文件`);
const success = await this.apiClient.uploadFileContent(filePath, content);
if (success) {
const entry = this.fileList.get(filePath);
if (entry) {
entry.content = content;
entry.lastLoaded = new Date();
}
return true;
}
}
}
else {
// C++服务不可连接,直接执行上传
try {
const success = await this.apiClient.uploadFileContent(filePath, content);
if (success) {
@@ -199,6 +240,7 @@ class FileModel {
catch (error) {
vscode.window.showErrorMessage(`保存文件失败: ${error}`);
}
}
return false;
}
// 刷新文件列表

2
dist/model.js.map vendored

File diff suppressed because one or more lines are too long

View File

@@ -141,54 +141,13 @@ export function activate(context: vscode.ExtensionContext) {
const saveListener = vscode.workspace.onDidSaveTextDocument(async (doc) => {
if (doc.uri.scheme === 'squirrel') {
const filePath = doc.uri.path.substring(1); // 去掉开头的 '/'
const fileContent = doc.getText();
// 检查C++服务是否可连接
const isCppServiceConnected = await checkCppServiceConnection();
if (isCppServiceConnected) {
output.appendLine('[Squirrel] C++服务可连接,尝试发送文件内容');
try {
const sendSuccess = await sendFileToCppService(filePath, fileContent);
if (sendSuccess) {
output.appendLine(`[Squirrel] 文件 ${filePath} 已发送到C++服务`);
// C++服务返回200后继续执行保存到服务器
const success = await model.saveFileContent(filePath, fileContent);
if (success) {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存成功`);
// 当文件保存成功后执行UI更新和缓存更新
output.appendLine(`[Squirrel] 文件 ${filePath} 已保存到服务器`);
provider.refresh();
// 更新函数缓存
await functionExtractor.updateFileCache(model, filePath);
} else {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存到服务器失败`);
}
} else {
output.appendLine(`[Squirrel] 文件 ${filePath} 发送到C++服务失败,跳过保存到服务器`);
}
} catch (error) {
output.appendLine(`[Squirrel] 发送文件到C++服务时发生错误: ${error}`);
output.appendLine(`[Squirrel] 跳过保存文件 ${filePath} 到服务器`);
}
} else {
output.appendLine('[Squirrel] C++服务不可连接,直接保存到服务器');
// C++服务不可连接,直接保存到服务器
const success = await model.saveFileContent(filePath, fileContent);
if (success) {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存成功`);
provider.refresh();
// 更新函数缓存
await functionExtractor.updateFileCache(model, filePath);
} else {
output.appendLine(`[Squirrel] 文件 ${filePath} 保存到服务器失败`);
}
}
}
});

View File

@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { ApiClient } from './apiClient';
import { checkCppServiceConnection, sendFileToCppService } from './localClient';
export interface FileEntry {
key: string; // 文件路径
@@ -183,9 +184,46 @@ export class FileModel {
// 保存文件内容
async saveFileContent(filePath: string, content: string): Promise<boolean> {
// 先检查C++服务是否可连接
const isCppServiceConnected = await checkCppServiceConnection();
if (isCppServiceConnected) {
// C++服务可连接,先发送进行编译验证
try {
const sendSuccess = await sendFileToCppService(filePath, content);
if (sendSuccess) {
// C++编译服务返回200说明脚本编译通过允许上传
const success = await this.apiClient.uploadFileContent(filePath, content);
if (success) {
const entry = this.fileList.get(filePath);
if (entry) {
entry.content = content;
entry.lastLoaded = new Date();
}
return true;
}
} else {
// C++编译服务返回非200说明脚本存在问题不允许上传
vscode.window.showErrorMessage(`脚本编译失败,无法保存文件 ${filePath}。请检查代码错误后再试。`);
return false;
}
} catch (error) {
// 向C++服务发送文件过程中出现错误
vscode.window.showErrorMessage(`发送文件到编译服务时出错: ${error},但仍尝试直接保存文件`);
const success = await this.apiClient.uploadFileContent(filePath, content);
if (success) {
const entry = this.fileList.get(filePath);
if (entry) {
entry.content = content;
entry.lastLoaded = new Date();
}
return true;
}
}
} else {
// C++服务不可连接,直接执行上传
try {
const success = await this.apiClient.uploadFileContent(filePath, content);
if (success) {
const entry = this.fileList.get(filePath);
if (entry) {
@@ -197,6 +235,7 @@ export class FileModel {
} catch (error) {
vscode.window.showErrorMessage(`保存文件失败: ${error}`);
}
}
return false;
}