Skip to content

Commit 8eb2078

Browse files
Fix the display order and icon issues for main class prompt wizard (#426)
* Fix the display order and icon issues for main class prompt wizard Signed-off-by: Jinbo Wang <[email protected]> * Fix review comments * Add return type for the function declaration * fix the code format * use _.get to guard the undefined case\
1 parent f4b5507 commit 8eb2078

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/configurationProvider.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -351,51 +351,55 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
351351
return selected && selected.item;
352352
}
353353

354-
private isOpenedInActiveEditor(file: string): boolean {
355-
const activeEditor: vscode.TextEditor = vscode.window.activeTextEditor;
356-
const currentActiveFile: string = activeEditor ? activeEditor.document.uri.fsPath : undefined;
357-
358-
return file && currentActiveFile && path.relative(file, currentActiveFile) === "";
359-
}
360-
361354
private formatRecentlyUsedMainClassOptions(options: IMainClassOption[]): IMainClassQuickPickItem[] {
362355
// Sort the Main Class options with the recently used timestamp.
363356
options.sort((a: IMainClassOption, b: IMainClassOption) => {
364357
return this.debugHistory.getMRUTimestamp(b) - this.debugHistory.getMRUTimestamp(a);
365358
});
366359

367-
// Move the Main Class from Active Editor to the top.
368-
// If it's not the most recently used one, then put it as the second.
369-
let positionForActiveEditor = options.findIndex((value: IMainClassOption) => {
370-
return this.isOpenedInActiveEditor(value.filePath);
360+
const mostRecentlyUsedOption: IMainClassOption = (options.length && this.debugHistory.contains(options[0])) ? options[0] : undefined;
361+
const isMostRecentlyUsed = (option: IMainClassOption): boolean => {
362+
return mostRecentlyUsedOption
363+
&& mostRecentlyUsedOption.mainClass === option.mainClass
364+
&& mostRecentlyUsedOption.projectName === option.projectName;
365+
};
366+
const isFromActiveEditor = (option: IMainClassOption): boolean => {
367+
const activeEditor: vscode.TextEditor = vscode.window.activeTextEditor;
368+
const currentActiveFile: string = _.get(activeEditor, "document.uri.fsPath");
369+
return option.filePath && currentActiveFile && path.relative(option.filePath, currentActiveFile) === "";
370+
};
371+
const isPrivileged = (option: IMainClassOption): boolean => {
372+
return isMostRecentlyUsed(option) || isFromActiveEditor(option);
373+
};
374+
375+
// Show the most recently used Main Class as the first one,
376+
// then the Main Class from Active Editor as second,
377+
// finally other Main Class.
378+
const adjustedOptions: IMainClassOption[] = [];
379+
options.forEach((option: IMainClassOption) => {
380+
if (isPrivileged(option)) {
381+
adjustedOptions.push(option);
382+
}
371383
});
372-
if (positionForActiveEditor >= 1) {
373-
let newPosition = 0;
374-
if (this.debugHistory.contains(options[0])) {
375-
newPosition = 1;
384+
options.forEach((option: IMainClassOption) => {
385+
if (!isPrivileged(option)) {
386+
adjustedOptions.push(option);
376387
}
388+
});
377389

378-
if (newPosition !== positionForActiveEditor) {
379-
const update: IMainClassOption[] = options.splice(positionForActiveEditor, 1);
380-
options.splice(newPosition, 0, ...update);
381-
positionForActiveEditor = newPosition;
390+
const pickItems: IMainClassQuickPickItem[] = this.formatMainClassOptions(adjustedOptions);
391+
pickItems.forEach((pickItem: IMainClassQuickPickItem) => {
392+
const adjustedDetail = [];
393+
if (isMostRecentlyUsed(pickItem.item)) {
394+
adjustedDetail.push("$(clock) recently used");
382395
}
383-
}
384-
385-
const pickItems: IMainClassQuickPickItem[] = this.formatMainClassOptions(options);
386-
387-
if (this.debugHistory.contains(options[0])) {
388-
pickItems[0].detail = "$(clock) recently used";
389-
}
390396

391-
if (positionForActiveEditor >= 0) {
392-
if (pickItems[positionForActiveEditor].detail) {
393-
pickItems[positionForActiveEditor].detail += `, active editor (${path.basename(options[positionForActiveEditor].filePath)})`;
394-
} else {
395-
pickItems[positionForActiveEditor].detail =
396-
`$(file-text) active editor (${path.basename(options[positionForActiveEditor].filePath)})`;
397+
if (isFromActiveEditor(pickItem.item)) {
398+
adjustedDetail.push(`$(file-text) active editor (${path.basename(pickItem.item.filePath)})`);
397399
}
398-
}
400+
401+
pickItem.detail = adjustedDetail.join(", ");
402+
});
399403

400404
return pickItems;
401405
}

0 commit comments

Comments
 (0)