Skip to content

Commit

Permalink
Extract sort to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Jan 28, 2023
1 parent 01f5c48 commit 78e744b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/project-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,9 @@ export async function pickProjectRoot(
return rootToUri(uris[0]);
}

uris.sort((a, b) => {
if (!selected) {
return 0;
}
return rootToUri(a).fsPath === selected.fsPath
? -1
: rootToUri(b).fsPath === selected.fsPath
? 1
: 0;
});
const sorted = sortPreSelectedFirst(uris, selected);

const project_root_options = uris.map((root_or_uri) => {
const project_root_options = sorted.map((root_or_uri) => {
let uri;
let reason;
if (root_or_uri instanceof vscode.Uri) {
Expand Down Expand Up @@ -180,3 +171,16 @@ export async function pickProjectRoot(

return selected_root?.value;
}

function sortPreSelectedFirst(uris: (ProjectRoot | vscode.Uri)[], selected: vscode.Uri) {
return [...uris].sort((a, b) => {
if (!selected) {
return 0;
}
return rootToUri(a).fsPath === selected.fsPath
? -1
: rootToUri(b).fsPath === selected.fsPath
? 1
: 0;
});
}

0 comments on commit 78e744b

Please sign in to comment.