diff --git a/CHANGELOG.md b/CHANGELOG.md index 20d583e1c..b0a9c733c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Changes to Calva. - [Sort aliases for deps.edn projects](https://github.com/BetterThanTomorrow/calva/issues/2035) - Fix: [Indenter and formatter not in agreement about some forms](https://github.com/BetterThanTomorrow/calva/issues/2032) +- [Sort pre-selected project at the top in REPL connect menu](https://github.com/BetterThanTomorrow/calva/issues/2043) ## [2.0.327] - 2023-01-26 diff --git a/src/project-root.ts b/src/project-root.ts index 1528dafc2..fa370e45d 100644 --- a/src/project-root.ts +++ b/src/project-root.ts @@ -134,7 +134,9 @@ export async function pickProjectRoot( return rootToUri(uris[0]); } - const project_root_options = uris.map((root_or_uri) => { + const sorted = sortPreSelectedFirst(uris, selected); + + const project_root_options = sorted.map((root_or_uri) => { let uri; let reason; if (root_or_uri instanceof vscode.Uri) { @@ -169,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; + }); +}