Skip to content

Commit c0f03ae

Browse files
committed
style: unify code
1 parent 7aca404 commit c0f03ae

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

src/copilot/copilotHelper.ts

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ import { commands, Uri, CancellationToken } from "vscode";
55
import { sendError } from "vscode-extension-telemetry-wrapper";
66
import { GetImportClassContentError, GetProjectDependenciesError } from "./utils";
77
import { Commands } from '../commands';
8+
9+
/**
10+
* Enum for error messages used in Promise rejection
11+
*/
12+
export enum ErrorMessage {
13+
OperationCancelled = "Operation cancelled",
14+
OperationTimedOut = "Operation timed out"
15+
}
16+
17+
/**
18+
* Enum for empty reason codes when operations return empty results
19+
*/
20+
export enum EmptyReason {
21+
CopilotCancelled = "CopilotCancelled",
22+
CommandNullResult = "CommandNullResult",
23+
Timeout = "Timeout",
24+
NoWorkspace = "NoWorkspace",
25+
NoDependenciesResults = "NoDependenciesResults",
26+
NoActiveEditor = "NoActiveEditor",
27+
NotJavaFile = "NotJavaFile",
28+
NoImportsResults = "NoImportsResults"
29+
}
30+
831
export interface INodeImportClass {
932
uri: string;
1033
value: string; // Changed from 'class' to 'className' to match Java code
@@ -50,7 +73,7 @@ export namespace CopilotHelper {
5073
if (cancellationToken?.isCancellationRequested) {
5174
return {
5275
classInfoList: [],
53-
emptyReason: "CopilotCancelled",
76+
emptyReason: EmptyReason.CopilotCancelled,
5477
isEmpty: true
5578
};
5679
}
@@ -64,20 +87,20 @@ export namespace CopilotHelper {
6487
commandPromise,
6588
new Promise<IImportClassContentResult>((_, reject) => {
6689
cancellationToken.onCancellationRequested(() => {
67-
reject(new Error('Operation cancelled'));
90+
reject(new Error(ErrorMessage.OperationCancelled));
6891
});
6992
}),
7093
new Promise<IImportClassContentResult>((_, reject) => {
7194
setTimeout(() => {
72-
reject(new Error('Operation timed out'));
95+
reject(new Error(ErrorMessage.OperationTimedOut));
7396
}, 80); // 80ms timeout
7497
})
7598
]);
7699

77100
if (!result) {
78101
return {
79102
classInfoList: [],
80-
emptyReason: "CommandNullResult",
103+
emptyReason: EmptyReason.CommandNullResult,
81104
isEmpty: true
82105
};
83106
}
@@ -88,34 +111,34 @@ export namespace CopilotHelper {
88111
commandPromise,
89112
new Promise<IImportClassContentResult>((_, reject) => {
90113
setTimeout(() => {
91-
reject(new Error('Operation timed out'));
114+
reject(new Error(ErrorMessage.OperationTimedOut));
92115
}, 80); // 80ms timeout
93116
})
94117
]);
95118

96119
if (!result) {
97120
return {
98121
classInfoList: [],
99-
emptyReason: "CommandNullResult",
122+
emptyReason: EmptyReason.CommandNullResult,
100123
isEmpty: true
101124
};
102125
}
103126

104127
return result;
105128
}
106129
} catch (error: any) {
107-
if (error.message === 'Operation cancelled') {
130+
if (error.message === ErrorMessage.OperationCancelled) {
108131
return {
109132
classInfoList: [],
110-
emptyReason: "CopilotCancelled",
133+
emptyReason: EmptyReason.CopilotCancelled,
111134
isEmpty: true
112135
};
113136
}
114137

115-
if (error.message === 'Operation timed out') {
138+
if (error.message === ErrorMessage.OperationTimedOut) {
116139
return {
117140
classInfoList: [],
118-
emptyReason: "Timeout",
141+
emptyReason: EmptyReason.Timeout,
119142
isEmpty: true
120143
};
121144
}
@@ -158,7 +181,7 @@ export namespace CopilotHelper {
158181
if (cancellationToken?.isCancellationRequested) {
159182
return {
160183
dependencyInfoList: [],
161-
emptyReason: "CopilotCancelled",
184+
emptyReason: EmptyReason.CopilotCancelled,
162185
isEmpty: true
163186
};
164187
}
@@ -172,20 +195,20 @@ export namespace CopilotHelper {
172195
commandPromise,
173196
new Promise<IProjectDependenciesResult>((_, reject) => {
174197
cancellationToken.onCancellationRequested(() => {
175-
reject(new Error('Operation cancelled'));
198+
reject(new Error(ErrorMessage.OperationCancelled));
176199
});
177200
}),
178201
new Promise<IProjectDependenciesResult>((_, reject) => {
179202
setTimeout(() => {
180-
reject(new Error('Operation timed out'));
203+
reject(new Error(ErrorMessage.OperationTimedOut));
181204
}, 40); // 40ms timeout
182205
})
183206
]);
184207

185208
if (!result) {
186209
return {
187210
dependencyInfoList: [],
188-
emptyReason: "CommandNullResult",
211+
emptyReason: EmptyReason.CommandNullResult,
189212
isEmpty: true
190213
};
191214
}
@@ -196,34 +219,34 @@ export namespace CopilotHelper {
196219
commandPromise,
197220
new Promise<IProjectDependenciesResult>((_, reject) => {
198221
setTimeout(() => {
199-
reject(new Error('Operation timed out'));
222+
reject(new Error(ErrorMessage.OperationTimedOut));
200223
}, 40); // 40ms timeout
201224
})
202225
]);
203226

204227
if (!result) {
205228
return {
206229
dependencyInfoList: [],
207-
emptyReason: "CommandNullResult",
230+
emptyReason: EmptyReason.CommandNullResult,
208231
isEmpty: true
209232
};
210233
}
211234

212235
return result;
213236
}
214237
} catch (error: any) {
215-
if (error.message === 'Operation cancelled') {
238+
if (error.message === ErrorMessage.OperationCancelled) {
216239
return {
217240
dependencyInfoList: [],
218-
emptyReason: 'CopilotCancelled',
241+
emptyReason: EmptyReason.CopilotCancelled,
219242
isEmpty: true
220243
};
221244
}
222245

223-
if (error.message === 'Operation timed out') {
246+
if (error.message === ErrorMessage.OperationTimedOut) {
224247
return {
225248
dependencyInfoList: [],
226-
emptyReason: "Timeout",
249+
emptyReason: EmptyReason.Timeout,
227250
isEmpty: true
228251
};
229252
}
@@ -256,7 +279,7 @@ export namespace CopilotHelper {
256279

257280
// Check if workspace folders exist
258281
if (!workspaceFolders || workspaceFolders.length === 0) {
259-
sendTelemetry("resolveProjectDependencies", "ContextEmpty", "NoWorkspace");
282+
sendTelemetry("resolveProjectDependencies", "ContextEmpty", EmptyReason.NoWorkspace);
260283
return items;
261284
}
262285

@@ -273,7 +296,7 @@ export namespace CopilotHelper {
273296
sendTelemetry("resolveProjectDependencies", "ContextEmpty", projectDependenciesResult.emptyReason);
274297
} else if (projectDependenciesResult.dependencyInfoList.length === 0) {
275298
// No error but still empty - likely no dependencies in project
276-
sendTelemetry("resolveProjectDependencies", "ContextEmpty", "NoDependenciesResults");
299+
sendTelemetry("resolveProjectDependencies", "ContextEmpty", EmptyReason.NoDependenciesResults);
277300
}
278301

279302
// Check for cancellation after telemetry
@@ -314,12 +337,12 @@ export namespace CopilotHelper {
314337

315338
// Check if there's an active editor with a Java document
316339
if (!activeEditor) {
317-
sendTelemetry("resolveLocalImports", "ContextEmpty", "NoActiveEditor");
340+
sendTelemetry("resolveLocalImports", "ContextEmpty", EmptyReason.NoActiveEditor);
318341
return items;
319342
}
320343

321344
if (activeEditor.document.languageId !== 'java') {
322-
sendTelemetry("resolveLocalImports", "ContextEmpty", "NotJavaFile");
345+
sendTelemetry("resolveLocalImports", "ContextEmpty", EmptyReason.NotJavaFile);
323346
return items;
324347
}
325348

@@ -339,7 +362,7 @@ export namespace CopilotHelper {
339362
sendTelemetry("resolveLocalImports", "ContextEmpty", importClassResult.emptyReason);
340363
} else if (importClassResult.classInfoList.length === 0) {
341364
// No error but still empty - likely no imports in file
342-
sendTelemetry("resolveLocalImports", "ContextEmpty", "NoImportsResults");
365+
sendTelemetry("resolveLocalImports", "ContextEmpty", EmptyReason.NoImportsResults);
343366
}
344367

345368
// Check for cancellation before processing results

0 commit comments

Comments
 (0)