Skip to content

Commit 73dfaf4

Browse files
♻️ refactor(utils): 优化工具类的错误处理和性能
- 【DiffSimplifier】添加配置缓存机制,优化配置读取性能 - 【LocalizationManager】增强消息格式化的错误处理和空值处理 - 【NotificationHandler】添加超时机制和统一的消息显示逻辑 - 【ProgressHandler】改进进度展示,支持取消操作 - 添加配置验证和日志记录功能
1 parent 4f553d9 commit 73dfaf4

4 files changed

+90
-22
lines changed

src/utils/DiffSimplifier.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
import * as vscode from "vscode";
22

33
export class DiffSimplifier {
4+
private static configCache: {
5+
enabled: boolean;
6+
contextLines: number;
7+
maxLineLength: number;
8+
} | null = null;
9+
410
private static getConfig() {
11+
if (this.configCache) {
12+
return this.configCache;
13+
}
14+
515
const config = vscode.workspace.getConfiguration("dish-ai-commit");
6-
return {
7-
enabled: config.get<boolean>("enableDiffSimplification") || false,
8-
contextLines: config.get<number>("diffSimplification.contextLines") || 3,
16+
this.configCache = {
17+
enabled: config.get<boolean>("enableDiffSimplification") ?? false,
18+
contextLines: config.get<number>("diffSimplification.contextLines") ?? 3,
919
maxLineLength:
10-
config.get<number>("diffSimplification.maxLineLength") || 120,
20+
config.get<number>("diffSimplification.maxLineLength") ?? 120,
1121
};
22+
23+
return this.configCache;
24+
}
25+
26+
// 添加配置更新方法
27+
static clearConfigCache() {
28+
this.configCache = null;
1229
}
1330

1431
private static readonly CONTEXT_LINES = 3;
@@ -60,10 +77,12 @@ export class DiffSimplifier {
6077
return simplified.join("\n");
6178
}
6279

80+
// 优化 truncateLine 方法
6381
private static truncateLine(line: string, maxLength: number): string {
64-
if (line.length <= maxLength) {
82+
if (!line || line.length <= maxLength) {
6583
return line;
6684
}
67-
return line.substring(0, maxLength - 3) + "...";
85+
const ellipsis = "...";
86+
return `${line.substring(0, maxLength - ellipsis.length)}${ellipsis}`;
6887
}
6988
}

src/utils/LocalizationManager.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,27 @@ export class LocalizationManager {
5555
}
5656

5757
public format(key: string, ...args: any[]): string {
58-
let message = this.getMessage(key);
59-
args.forEach((arg, index) => {
60-
message = message.replace(`{${index}}`, arg.toString());
61-
});
62-
return message;
58+
try {
59+
let message = this.getMessage(key);
60+
if (!message) {
61+
console.warn(`Missing localization key: ${key}`);
62+
return key;
63+
}
64+
65+
args.forEach((arg, index) => {
66+
const value = arg?.toString() ?? "";
67+
message = message.replace(`{${index}}`, value);
68+
});
69+
return message;
70+
} catch (error) {
71+
console.error(`Error formatting message for key ${key}:`, error);
72+
return key;
73+
}
74+
}
75+
76+
// 添加新方法用于验证所有翻译键是否存在
77+
public validateMessages(keys: string[]): string[] {
78+
const missingKeys = keys.filter((key) => !this.messages[key]);
79+
return missingKeys;
6380
}
6481
}

src/utils/NotificationHandler.ts

+33-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,45 @@ import * as vscode from "vscode";
22
import { LocalizationManager } from "./LocalizationManager";
33

44
export class NotificationHandler {
5+
private static readonly TIMEOUT = 10000; // 10 seconds
6+
7+
private static async showMessage(
8+
type: "info" | "warn" | "error",
9+
key: string,
10+
...args: any[]
11+
): Promise<void> {
12+
try {
13+
const message = LocalizationManager.getInstance().format(key, ...args);
14+
const promise =
15+
type === "info"
16+
? vscode.window.showInformationMessage(message)
17+
: type === "warn"
18+
? vscode.window.showWarningMessage(message)
19+
: vscode.window.showErrorMessage(message);
20+
21+
await Promise.race([
22+
promise,
23+
new Promise((_, reject) =>
24+
setTimeout(
25+
() => reject(new Error("Notification timeout")),
26+
this.TIMEOUT
27+
)
28+
),
29+
]);
30+
} catch (error) {
31+
console.error(`Failed to show ${type} message:`, error);
32+
}
33+
}
34+
535
public static async info(key: string, ...args: any[]): Promise<void> {
6-
const message = LocalizationManager.getInstance().format(key, ...args);
7-
await vscode.window.showInformationMessage(message);
36+
return this.showMessage("info", key, ...args);
837
}
938

1039
public static async warn(key: string, ...args: any[]): Promise<void> {
11-
const message = LocalizationManager.getInstance().format(key, ...args);
12-
await vscode.window.showWarningMessage(message);
40+
return this.showMessage("warn", key, ...args);
1341
}
1442

1543
public static async error(key: string, ...args: any[]): Promise<void> {
16-
const message = LocalizationManager.getInstance().format(key, ...args);
17-
await vscode.window.showErrorMessage(message);
44+
return this.showMessage("error", key, ...args);
1845
}
1946
}

src/utils/ProgressHandler.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ export class ProgressHandler {
88
static async withProgress<T>(
99
title: string,
1010
task: (
11-
progress: vscode.Progress<{ message?: string; increment?: number }>
11+
progress: vscode.Progress<{ message?: string; increment?: number }>,
12+
token: vscode.CancellationToken
1213
) => Promise<T>
1314
): Promise<T> {
1415
return vscode.window.withProgress(
1516
{
1617
location: vscode.ProgressLocation.Notification,
1718
title: `[${DISPLAY_NAME}]: ${title}`,
18-
cancellable: false,
19+
cancellable: true,
1920
},
20-
async (progress) => {
21-
// progress.report({ increment: 0 });
22-
return await task(progress);
21+
async (progress, token) => {
22+
try {
23+
return await task(progress, token);
24+
} catch (error) {
25+
console.error(`Progress task failed:`, error);
26+
throw error;
27+
}
2328
}
2429
);
2530
}

0 commit comments

Comments
 (0)