Skip to content

Commit 3bd42e5

Browse files
author
lhy
committed
清理不必要的await
1 parent 838550e commit 3bd42e5

9 files changed

Lines changed: 124 additions & 225 deletions

src/CommandManager.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class CommandManager {
1313
constructor(
1414
private readonly extension: PawSQLExtension,
1515
private readonly context: vscode.ExtensionContext,
16-
private readonly sqlCodeLensProvider: SqlCodeLensProvider // 接受 SqlCodeLensProvider 实例
16+
private readonly sqlCodeLensProvider: SqlCodeLensProvider
1717
) {}
1818

1919
public async initializeCommands(): Promise<void> {
@@ -25,17 +25,6 @@ export class CommandManager {
2525
}
2626

2727
private registerApiKeyCommands(): void {
28-
// const commands = [
29-
// {
30-
// command: COMMANDS.PAWSQL_CONFIG,
31-
// callback: () => ConfigurationService.openSettings("pawsqlInit"),
32-
// },
33-
// ];
34-
// commands.forEach(({ command, callback }) => {
35-
// const disposable = vscode.commands.registerCommand(command, callback);
36-
// this.context.subscriptions.push(disposable);
37-
// });
38-
3928
const configFileDefaultDisposable = vscode.commands.registerCommand(
4029
"pawsql.selectFileDefaultWorkspace",
4130
() => this.handleFileDefaultWorkspaceSelection()
@@ -69,19 +58,16 @@ export class CommandManager {
6958
);
7059

7160
if (selected) {
72-
// 更新配置
7361
const currentFile =
7462
vscode.window.activeTextEditor?.document.uri.toString();
7563
if (currentFile) {
7664
const config = vscode.workspace.getConfiguration("pawsql");
77-
7865
const mappings =
7966
config.get<Record<string, WorkspaceItem>>(
8067
"fileWorkspaceMappings"
8168
) || {};
82-
mappings[currentFile] = selected; // 保存映射关系
69+
mappings[currentFile] = selected;
8370

84-
// 更新配置
8571
await config.update(
8672
"fileWorkspaceMappings",
8773
mappings,
@@ -114,7 +100,6 @@ export class CommandManager {
114100
workspaces: ListWorkspacesResponse
115101
): WorkspaceItem[] {
116102
return workspaces.data.records.map((workspace) => {
117-
// 先根据数据库类型设置基础图标
118103
const iconPath = {
119104
light: path.join(
120105
__filename,

src/PawSQLSidebarProvider.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ export class PawSQLTreeProvider
353353
// Validate backend connectivity
354354

355355
const backendResult = await validateBackend(backendUrl ?? "");
356+
356357
const isBackendConnected = backendResult.isAvailable;
357358

358359
if (!isBackendConnected) {
@@ -407,6 +408,8 @@ export class PawSQLTreeProvider
407408
);
408409
await this.loadData(hideMessage);
409410
} catch (error: any) {
411+
console.error(error);
412+
410413
this.isConfigValid = false;
411414
vscode.commands.executeCommand(
412415
"setContext",
@@ -454,12 +457,17 @@ export class PawSQLTreeProvider
454457
}
455458
public async validateConfig(): Promise<boolean> {
456459
const config = vscode.workspace.getConfiguration("pawsql");
457-
const apiKey = config.get<string>("apiKey");
458-
const frontendUrl = config.get<string>("frontendUrl");
459-
const backendUrl = config.get<string>("backendUrl");
460+
const apiKey = config.get<string>("apiKey") ?? "";
461+
const frontendUrl = config.get<string>("frontendUrl") ?? "";
462+
const backendUrl = config.get<string>("backendUrl") ?? "";
460463
let failedCommand = [];
461464
try {
462-
const backendResult = await validateBackend(backendUrl ?? "");
465+
const [backendResult, frontendResult, isApikeyValid] = await Promise.all([
466+
validateBackend(backendUrl),
467+
validateFrontend(frontendUrl),
468+
validateUserKey(apiKey),
469+
]);
470+
463471
const isBackendConnected = backendResult.isAvailable;
464472
if (!isBackendConnected) {
465473
failedCommand.push(
@@ -468,16 +476,14 @@ export class PawSQLTreeProvider
468476
}
469477

470478
// Validate frontend connectivity
471-
const frontendReuslt = await validateFrontend(frontendUrl ?? "");
472-
const isFrontendConnected = frontendReuslt.isAvailable;
479+
const isFrontendConnected = frontendResult.isAvailable;
473480
if (!isFrontendConnected) {
474481
failedCommand.push(
475482
LanguageService.getMessage("sidebar.frontendUrl.label")
476483
);
477484
}
478485

479486
// Validate API key
480-
const isApikeyValid = await validateUserKey(apiKey ?? "");
481487
if (!isFrontendConnected) {
482488
failedCommand.push(LanguageService.getMessage("sidebar.apiKey.label"));
483489
}
@@ -496,6 +502,7 @@ export class PawSQLTreeProvider
496502
}
497503
return isBackendConnected && isFrontendConnected && isApikeyValid;
498504
} catch (error: any) {
505+
console.error(error);
499506
vscode.window.showErrorMessage(
500507
`${LanguageService.getMessage(
501508
"error.config.validate.failed"
@@ -562,6 +569,7 @@ export class PawSQLTreeProvider
562569
}
563570
}
564571
} catch (error: any) {
572+
console.error(error);
565573
this.workspaces = [];
566574
vscode.window.showErrorMessage(
567575
`${LanguageService.getMessage(
@@ -610,7 +618,6 @@ export class PawSQLTreeProvider
610618
this.workspaces.length === 0
611619
);
612620
} catch (error: any) {
613-
console.log(1);
614621
console.log(error);
615622

616623
if (error.code === "ECONNREFUSED") {
@@ -733,7 +740,6 @@ export class PawSQLTreeProvider
733740
this.isAnalysisLoading.set(workspace.workspaceId, false);
734741
return analyses;
735742
} catch (error: any) {
736-
console.log(2);
737743
console.log(error);
738744

739745
vscode.window.showErrorMessage(
@@ -776,7 +782,6 @@ export class PawSQLTreeProvider
776782
this.analysisStatementCache.set(analysis, statements);
777783
return statements;
778784
} catch (error: any) {
779-
console.log(3);
780785
console.log(error);
781786
vscode.window.showErrorMessage(
782787
`${LanguageService.getMessage("error.load.data.failed")}: ${

src/SqlCodeLensProvider.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
6262
): Promise<void> {
6363
const rangeKey = this.getRangeKey(range);
6464

65-
// 立即更新状态
6665
if (isOptimizing) {
6766
this.state.optimizingRanges.set(rangeKey, {
6867
range,
@@ -74,10 +73,8 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
7473

7574
this.state.isOptimizing = this.state.optimizingRanges.size > 0;
7675

77-
// 使用防抖处理刷新
76+
// 这两个调用都需要保留await,因为它们涉及UI更新和状态同步
7877
await this.debouncedRefresh();
79-
80-
// 确保状态已更新
8178
await this.ensureStateUpdate();
8279
}
8380

@@ -94,8 +91,8 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
9491
});
9592
}
9693

97-
private async ensureStateUpdate(): Promise<void> {
98-
await new Promise<void>((resolve) => {
94+
private ensureStateUpdate(): Promise<void> {
95+
return new Promise<void>((resolve) => {
9996
if (typeof window !== "undefined" && window.requestAnimationFrame) {
10097
window.requestAnimationFrame(() => {
10198
setTimeout(resolve, 16);
@@ -132,14 +129,16 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
132129
const codeLenses: vscode.CodeLens[] = [];
133130

134131
try {
135-
// 使用缓存的范围信息来加速状态检查
136132
const optimizingRanges = Array.from(this.state.optimizingRanges.values());
137133

138-
// 1. 处理文件级别的配置
139-
await this.addFileConfigurationLens(document, codeLenses);
134+
// 需要保留await,因为这涉及文件系统操作
135+
const fileWorkspace = await this.addFileConfigurationLens(
136+
document,
137+
codeLenses
138+
);
140139

141-
// 2. 处理 SQL 查询级别的操作
142140
if (!token.isCancellationRequested) {
141+
// 需要保留await,因为这涉及文本解析和工作区配置
143142
await this.addSqlQueryLenses(
144143
document,
145144
codeLenses,
@@ -159,19 +158,17 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
159158
document: vscode.TextDocument,
160159
codeLenses: vscode.CodeLens[]
161160
) {
162-
// 获取文件和用户的工作空间设置
163161
const fileUri = document.uri.toString();
162+
// 需要保留await,因为这涉及文件系统操作
164163
const fileWorkspace = await ConfigurationService.getFileDefaultWorkspace(
165164
fileUri
166165
);
167166

168-
// 在文件开头添加注释分隔符
169167
const separatorRange = new vscode.Range(
170168
new vscode.Position(0, 0),
171169
new vscode.Position(1, 0)
172170
);
173171

174-
// 从配置中读取默认工作空间
175172
const defaultWorkspace = vscode.workspace.getConfiguration("pawsql").get<{
176173
workspaceId: string;
177174
workspaceName: string;
@@ -184,6 +181,7 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
184181
.getConfiguration("pawsql")
185182
.get<string>("apiKey");
186183

184+
// 需要保留await,因为这是API调用
187185
const isApikeyValid = await ApiService.validateUserKey(apiKey ?? "");
188186

189187
if (!isApikeyValid) {
@@ -236,11 +234,13 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
236234
optimizingRanges: Array<{ range: vscode.Range; timestamp: number }>
237235
) {
238236
const text = document.getText();
237+
// 需要保留await,因为这涉及文本解析
239238
const queries = await this.parseWithThrottle(text);
239+
// 需要保留await,因为这涉及文件系统操作
240240
const fileWorkspace = await ConfigurationService.getFileDefaultWorkspace(
241241
document.uri.toString()
242242
);
243-
243+
// 需要保留await,因为这涉及工作区配置
244244
const defaultWorkspace = await ConfigurationService.getDefaultWorkspace();
245245

246246
let lastIndex = 0;
@@ -257,7 +257,6 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
257257

258258
const queryRange = new vscode.Range(startPos, endPos);
259259

260-
// 使用缓存的优化状态
261260
const isOptimizing = optimizingRanges.some((item) =>
262261
this.rangesEqual(item.range, queryRange)
263262
);
@@ -285,7 +284,7 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
285284
codeLenses.push(
286285
new vscode.CodeLens(queryRange, {
287286
title: LanguageService.getMessage("OPTIMIZING_SQL"),
288-
command: "", // 空命令使其不可点击
287+
command: "",
289288
})
290289
);
291290
} else {
@@ -312,7 +311,7 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
312311
}
313312

314313
private parseThrottleTimeout: NodeJS.Timeout | null = null;
315-
private async parseWithThrottle(text: string): Promise<string[]> {
314+
private parseWithThrottle(text: string): Promise<string[]> {
316315
return new Promise((resolve) => {
317316
if (this.parseThrottleTimeout) {
318317
clearTimeout(this.parseThrottleTimeout);

src/apiService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ interface CreateAnalysisParams {
9393
maxSpace?: number;
9494
closeRewrite?: boolean;
9595
rules?: Array<{ ruleCode: string; rewrite: boolean; threshold?: string }>;
96+
singleQueryFlag?: boolean;
9697
}
9798

9899
interface CreateAnalysisResponse {

src/configurationService.ts

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,72 +6,68 @@ export const ConfigurationService = {
66
return vscode.workspace.getConfiguration("pawsql");
77
},
88

9-
async getApiKey(): Promise<string | undefined> {
10-
return this.config.get("apiKey");
9+
getApiKey(): string | undefined {
10+
return this.config.get("apiKey"); // 返回值为 string 或 undefined
1111
},
1212

13-
async getFrontendUrl(): Promise<string | undefined> {
14-
return this.config.get("frontendUrl");
13+
getFrontendUrl(): string | undefined {
14+
return this.config.get("frontendUrl"); // 返回值为 string 或 undefined
1515
},
1616

17-
async getBackendUrl(): Promise<string | undefined> {
18-
return this.config.get("backendUrl");
17+
getBackendUrl(): string | undefined {
18+
return this.config.get("backendUrl"); // 返回值为 string 或 undefined
1919
},
2020

21-
// 获取用户级别的默认工作空间
22-
async getUserDefaultWorkspace(): Promise<WorkspaceItem | undefined> {
23-
return this.config.get<WorkspaceItem>("defaultWorkspace");
21+
getUserDefaultWorkspace(): WorkspaceItem | undefined {
22+
return this.config.get<WorkspaceItem>("defaultWorkspace"); // 返回值为 WorkspaceItem 或 undefined
2423
},
2524

26-
// 设置用户级别的默认工作空间
27-
async setUserDefaultWorkspace(workspaceItem: WorkspaceItem): Promise<void> {
28-
await this.config.update(
25+
setUserDefaultWorkspace(workspaceItem: WorkspaceItem): Thenable<void> {
26+
return this.config.update(
2927
"defaultWorkspace",
3028
workspaceItem,
3129
vscode.ConfigurationTarget.Global
32-
);
30+
); // 返回 Thenable<void>
3331
},
3432

35-
// 设置用户级别的默认工作空间
36-
async clearUserDefaultWorkspace(): Promise<void> {
37-
await this.config.update(
33+
// 清除用户级别的默认工作空间,返回类型为 void
34+
clearUserDefaultWorkspace(): Thenable<void> {
35+
return this.config.update(
3836
"defaultWorkspace",
3937
null,
4038
vscode.ConfigurationTarget.Global
41-
);
39+
); // 返回 Thenable<void>
4240
},
4341

44-
// 设置用户级别的默认工作空间
45-
async clearFileDefaultWorkspace(): Promise<void> {
46-
const config = vscode.workspace.getConfiguration("pawsql");
47-
await config.update(
42+
// 清除文件的默认工作空间,返回类型为 void
43+
clearFileDefaultWorkspace(): Thenable<void> {
44+
return this.config.update(
4845
"fileWorkspaceMappings",
49-
{}, // 设置为空对象,清空所有映射关系
46+
{},
5047
vscode.ConfigurationTarget.Global
51-
);
48+
); // 返回 Thenable<void>
5249
},
5350

54-
async getFileDefaultWorkspace(
55-
fileUri: string
56-
): Promise<WorkspaceItem | undefined> {
57-
const config = vscode.workspace.getConfiguration("pawsql");
51+
// 获取文件级别的默认工作空间,返回类型是 WorkspaceItem | null
52+
getFileDefaultWorkspace(fileUri: string): WorkspaceItem | null {
5853
const mappings =
59-
config.get<Record<string, any>>("fileWorkspaceMappings") || {};
60-
return mappings[fileUri] || null; // 返回整个 workspaceItem
54+
this.config.get<Record<string, WorkspaceItem>>("fileWorkspaceMappings") ||
55+
{};
56+
return mappings[fileUri] || null; // 返回值为 WorkspaceItem 或 null
6157
},
62-
async getDefaultWorkspace(): Promise<any> {
63-
const config = vscode.workspace.getConfiguration("pawsql");
64-
return config.get("defaultWorkspace");
58+
59+
getDefaultWorkspace(): WorkspaceItem | undefined {
60+
return this.config.get("defaultWorkspace"); // 返回值为 WorkspaceItem 或 undefined
6561
},
6662

67-
async openSettings(section: string): Promise<void> {
68-
await vscode.commands.executeCommand(
69-
"workbench.action.openSettings",
70-
section
71-
);
72-
// 打开 pawsql sidebar
73-
await vscode.commands.executeCommand(
74-
"workbench.view.extension.pawsqlContainer"
75-
);
63+
// 打开设置界面并显示 pawsql 侧边栏,返回类型为 void
64+
openSettings(section: string): Thenable<void> {
65+
return vscode.commands
66+
.executeCommand("workbench.action.openSettings", section)
67+
.then(() =>
68+
vscode.commands.executeCommand(
69+
"workbench.view.extension.pawsqlContainer"
70+
)
71+
); // 返回 Thenable<void>
7672
},
7773
};

0 commit comments

Comments
 (0)