Skip to content

Commit

Permalink
♻️ refactor(core): 重构命令管理和错误处理
Browse files Browse the repository at this point in the history
- 优化 COMMANDS 常量结构,使用命名空间管理
- 添加 NotificationHandler 统一处理错误通知
- 移除 extension.ts 中重复的初始化代码
- 在命令执行过程中增加错误处理和用户通知
- 规范化命令注册结构和错误捕获流程
  • Loading branch information
littleCareless committed Dec 10, 2024
1 parent 1b36a48 commit cad5b4e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
45 changes: 29 additions & 16 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode";
import { COMMANDS } from "./constants";
import { GenerateCommitCommand } from "./commands/GenerateCommitCommand";
import { SelectModelCommand } from "./commands/SelectModelCommand";
import { NotificationHandler } from "./utils/NotificationHandler";

export class CommandManager implements vscode.Disposable {
private disposables: vscode.Disposable[] = [];
Expand All @@ -11,23 +12,35 @@ export class CommandManager implements vscode.Disposable {
}

private registerCommands() {
const generateCommand = new GenerateCommitCommand(this.context);
const selectModelCommand = new SelectModelCommand(this.context);
try {
const generateCommand = new GenerateCommitCommand(this.context);
const selectModelCommand = new SelectModelCommand(this.context);

this.disposables.push(
vscode.commands.registerCommand(
COMMANDS.GENERATE,
async (...resources: vscode.SourceControlResourceState[]) => {
await generateCommand.execute(resources);
}
),
vscode.commands.registerCommand(
"dish-ai-commit.selectModel",
async () => {
await selectModelCommand.execute();
}
)
);
this.disposables.push(
vscode.commands.registerCommand(
COMMANDS.COMMIT.GENERATE,
async (...resources: vscode.SourceControlResourceState[]) => {
try {
await generateCommand.execute(resources);
} catch (error) {
NotificationHandler.error("command.generate.failed", error instanceof Error ? error.message : String(error));
}
}
),
vscode.commands.registerCommand(
COMMANDS.MODEL.SHOW,
async () => {
try {
await selectModelCommand.execute();
} catch (error) {
NotificationHandler.error("command.select.model.failed", error instanceof Error ? error.message : String(error));
}
}
)
);
} catch (error) {
NotificationHandler.error("command.register.failed", error instanceof Error ? error.message : String(error));
}
}

dispose() {
Expand Down
15 changes: 11 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import * as packageJson from "../package.json";
export const EXTENSION_NAME = packageJson.name;
export const DISPLAY_NAME = packageJson.displayName;

// 使用命名空间组织命令
export const COMMANDS = {
// 从 package.json 的 commands 配置中获取
GENERATE: packageJson.contributes.commands[0].command,
SHOW_MODELS: packageJson.contributes.commands[1].command,
REFRESH_MODELS: `${EXTENSION_NAME}.refreshModels`,
COMMIT: {
GENERATE: packageJson.contributes.commands[0].command,
},
MODEL: {
SHOW: packageJson.contributes.commands[1].command,
REFRESH: `${EXTENSION_NAME}.refreshModels`,
}
} as const;

// 添加类型导出
export type CommandType = typeof COMMANDS;
8 changes: 4 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import * as vscode from "vscode";
import { ConfigurationManager } from "./config/ConfigurationManager";
import { registerCommands } from "./commands";
import { LocalizationManager } from "./utils/LocalizationManager";
import { NotificationHandler } from "./utils/NotificationHandler";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
try {
console.log('Extension "dish-ai-commit-gen" is now active!');

// 初始化本地化管理器
LocalizationManager.initialize(context);

// 初始化本地化管理器
// 初始化本地化管理器(移除重复的初始化)
LocalizationManager.initialize(context);

// 初始化配置管理器并添加到订阅列表
Expand All @@ -33,6 +31,8 @@ export function activate(context: vscode.ExtensionContext) {
registerCommands(context);
} catch (e) {
console.error("Error activating extension:", e);
// 添加用户可见的错误提示
NotificationHandler.error("extension.activation.failed", e instanceof Error ? e.message : String(e));
throw e;
}
}
Expand Down

0 comments on commit cad5b4e

Please sign in to comment.