in src/mainClassPicker.ts [66:154]
public async showQuickPickWithRecentlyUsed(options: IMainClassOption[], placeHolder: string, parameter3?: Formatter | boolean,
parameter4?: boolean): Promise<IMainClassOption | undefined> {
// Record the main class picking history in a most recently used cache.
let labelFormatter: Formatter = defaultLabelFormatter;
let autoPick: boolean = true;
if (typeof parameter3 === "function") {
labelFormatter = parameter3;
} else if (typeof parameter3 === "boolean") {
autoPick = parameter3;
}
if (typeof parameter4 === "boolean") {
autoPick = parameter4;
}
if (!options || !options.length) {
return undefined;
} else if (autoPick && options.length === 1) {
return options[0];
}
// Sort the Main Class options with the recently used timestamp.
options.sort((a: IMainClassOption, b: IMainClassOption) => {
return this.getMRUTimestamp(b) - this.getMRUTimestamp(a);
});
const mostRecentlyUsedOption: IMainClassOption | undefined = (options.length && this.contains(options[0])) ? options[0] : undefined;
const isMostRecentlyUsed = (option: IMainClassOption): boolean => {
return !!mostRecentlyUsedOption
&& mostRecentlyUsedOption.mainClass === option.mainClass
&& mostRecentlyUsedOption.projectName === option.projectName;
};
const isFromActiveEditor = (option: IMainClassOption): boolean => {
const activeEditor: TextEditor | undefined = window.activeTextEditor;
const currentActiveFile: string = _.get(activeEditor, "document.uri.fsPath");
if (option.filePath && currentActiveFile) {
return path.relative(option.filePath, currentActiveFile) === "";
}
return false;
};
const isPrivileged = (option: IMainClassOption): boolean => {
return isMostRecentlyUsed(option) || isFromActiveEditor(option);
};
// Show the most recently used Main Class as the first one,
// then the Main Class from Active Editor as second,
// finally other Main Class.
const adjustedOptions: IMainClassOption[] = [];
options.forEach((option: IMainClassOption) => {
if (isPrivileged(option)) {
adjustedOptions.push(option);
}
});
options.forEach((option: IMainClassOption) => {
if (!isPrivileged(option)) {
adjustedOptions.push(option);
}
});
const pickItems = adjustedOptions.map((option) => {
const adjustedDetail = [];
if (isMostRecentlyUsed(option)) {
adjustedDetail.push("$(clock) recently used");
}
if (isFromActiveEditor(option) && option.filePath) {
adjustedDetail.push(`$(file-text) active editor (${path.basename(option.filePath)})`);
}
if (option.projectName) {
adjustedDetail.push(`Project: ${option.projectName}`);
}
const detail: string = adjustedDetail.join(", ");
return {
label: labelFormatter ? labelFormatter(option) : defaultLabelFormatter(option),
description: option.filePath ? path.basename(option.filePath) : "",
detail,
data: option,
};
});
const selected = await window.showQuickPick(pickItems, { placeHolder });
if (selected) {
this.updateMRUTimestamp(selected.data);
return selected.data;
}
return undefined;
}