Skip to content

Commit f93a5f9

Browse files
committed
完善代码(避免空调用)
1 parent 9bb5648 commit f93a5f9

2 files changed

Lines changed: 44 additions & 14 deletions

File tree

src/PawSQLSidebarProvider.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { LanguageService } from "./LanguageService";
99
import * as path from "path";
1010
import { getUrls } from "./constants";
11+
import { isBlank } from "./utils/pawsqlUtils";
1112

1213
// Previous classes remain unchanged...
1314
class WorkspaceManagerItem extends vscode.TreeItem {
@@ -350,7 +351,7 @@ export class PawSQLTreeProvider
350351
try {
351352
// Reset all validation states first
352353
this.isConfigValid = false;
353-
if (!backendUrl || !frontendUrl || !apiKey) {
354+
if (isBlank(backendUrl) || isBlank(frontendUrl) || isBlank(apiKey)) {
354355
this.isConfigValid = false;
355356
vscode.commands.executeCommand("setContext", "isConfigured", false);
356357
this.workspaces = [];
@@ -441,14 +442,14 @@ export class PawSQLTreeProvider
441442
// Reset all validation states first
442443
this.isConfigValid = false;
443444

444-
if (!backendUrl || !frontendUrl || !apiKey) {
445+
if (isBlank(backendUrl) || isBlank(frontendUrl) || isBlank(apiKey)) {
445446
this.isConfigValid = false;
446447
vscode.commands.executeCommand("setContext", "isConfigured", false);
447448
this.workspaces = [];
448449
return false;
449450
}
450451

451-
const backendResult = await validateBackend(backendUrl);
452+
const backendResult = await validateBackend(backendUrl ?? "");
452453
const isBackendConnected = backendResult.isAvailable;
453454

454455
if (!isBackendConnected) {
@@ -458,7 +459,7 @@ export class PawSQLTreeProvider
458459
return false;
459460
}
460461

461-
const frontendReuslt = await validateFrontend(frontendUrl);
462+
const frontendReuslt = await validateFrontend(frontendUrl ?? "");
462463
const isFrontendConnected = frontendReuslt.isAvailable;
463464

464465
if (!isFrontendConnected) {
@@ -469,7 +470,7 @@ export class PawSQLTreeProvider
469470
return false;
470471
}
471472

472-
const isApikeyValid = await ApiService.validateUserKey(apiKey);
473+
const isApikeyValid = await ApiService.validateUserKey(apiKey ?? "");
473474

474475
if (!isApikeyValid) {
475476
this.isConfigValid = false;
@@ -523,6 +524,10 @@ export class PawSQLTreeProvider
523524
const apiKey = config.get<string>("apiKey") ?? "";
524525
const frontendUrl = config.get<string>("frontendUrl") ?? "";
525526
const backendUrl = config.get<string>("backendUrl") ?? "";
527+
if (isBlank(backendUrl) || isBlank(frontendUrl) || isBlank(apiKey)) {
528+
return false;
529+
}
530+
526531
let failedCommand = [];
527532
try {
528533
const [backendResult, frontendResult, isApikeyValid] = await Promise.all([
@@ -569,6 +574,16 @@ export class PawSQLTreeProvider
569574
public async validateConfigurationByKey(key: string): Promise<void> {
570575
const config = vscode.workspace.getConfiguration("pawsql");
571576
const value = config.get<string>(key);
577+
if (isBlank(value)) {
578+
this.workspaces = [];
579+
this.isConfigValid = false;
580+
vscode.commands.executeCommand(
581+
"setContext",
582+
"isConfigured",
583+
this.isConfigValid
584+
);
585+
return;
586+
}
572587
try {
573588
if (key === "backendUrl") {
574589
// Validate backend connectivity

src/SqlCodeLensProvider.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ConfigurationService } from "./configurationService";
33
import { LanguageService } from "./LanguageService";
44
import { ApiService, WorkspaceItem } from "./apiService";
55
import parse from "./utils/parse";
6+
import { isBlank } from "./utils/pawsqlUtils";
67

78
interface OptimizingRange {
89
range: vscode.Range;
@@ -73,13 +74,24 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
7374
try {
7475
this.cleanupStaleOptimizingStates();
7576

77+
const config = vscode.workspace.getConfiguration("pawsql");
78+
79+
const apiKey = config.get<string>("apiKey");
80+
let isApikeyValid = false;
81+
if (isBlank(apiKey)) {
82+
isApikeyValid = false;
83+
} else {
84+
isApikeyValid = await ApiService.validateUserKey(apiKey ?? "");
85+
}
86+
7687
const codeLenses: vscode.CodeLens[] = [];
7788
const fileWorkspace = await this.addFileConfigurationLens(
7889
document,
79-
codeLenses
90+
codeLenses,
91+
isApikeyValid
8092
);
8193

82-
if (!token.isCancellationRequested) {
94+
if (isApikeyValid && !token.isCancellationRequested) {
8395
await this.addSqlQueryLenses(
8496
document,
8597
codeLenses,
@@ -97,31 +109,34 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
97109

98110
private async addFileConfigurationLens(
99111
document: vscode.TextDocument,
100-
codeLenses: vscode.CodeLens[]
112+
codeLenses: vscode.CodeLens[],
113+
isApikeyValid: boolean
101114
): Promise<WorkspaceItem | null> {
102115
const fileUri = document.uri.toString();
103116
const fileWorkspace = await ConfigurationService.getFileDefaultWorkspace(
104117
fileUri
105118
);
119+
106120
const config = vscode.workspace.getConfiguration("pawsql");
121+
107122
const defaultWorkspace = config.get<WorkspaceItem>("defaultWorkspace");
108-
const apiKey = config.get<string>("apiKey");
109123

110124
const separatorRange = new vscode.Range(0, 0, 1, 0);
111-
const isApikeyValid = await ApiService.validateUserKey(apiKey ?? "");
112125

113126
if (!isApikeyValid) {
114127
codeLenses.push(this.createInitConfigLens(separatorRange, document));
115128
} else if (fileWorkspace?.workspaceId || defaultWorkspace?.workspaceId) {
116129
codeLenses.push(
117-
this.createWorkspaceLens(
130+
this.createAnalysisWithDefaultWorkspaceLens(
118131
separatorRange,
119132
document,
120133
fileWorkspace ?? defaultWorkspace!
121134
)
122135
);
123136
} else {
124-
codeLenses.push(this.createSelectWorkspaceLens(separatorRange, document));
137+
codeLenses.push(
138+
this.createAnalysisWithSelectWorkspaceLens(separatorRange, document)
139+
);
125140
}
126141

127142
return fileWorkspace;
@@ -184,7 +199,7 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
184199
});
185200
}
186201

187-
private createWorkspaceLens(
202+
private createAnalysisWithDefaultWorkspaceLens(
188203
range: vscode.Range,
189204
document: vscode.TextDocument,
190205
workspace: Workspace
@@ -198,7 +213,7 @@ export class SqlCodeLensProvider implements vscode.CodeLensProvider {
198213
});
199214
}
200215

201-
private createSelectWorkspaceLens(
216+
private createAnalysisWithSelectWorkspaceLens(
202217
range: vscode.Range,
203218
document: vscode.TextDocument
204219
): vscode.CodeLens {

0 commit comments

Comments
 (0)