Skip to content

Commit

Permalink
Ensure a stable search-in-workspace result order
Browse files Browse the repository at this point in the history
Fixes #4113

Currently, the `search-in-workspace` result order is not consistent/stable
and can lead to different ordering between search results. The fix was to
ensure that the tree is properly rendered in order in the `onDone` callback.
The first step is to sort by the `root folders` and then update it's children
by sorting them as well.

Signed-off-by: Vincent Fugnitto <[email protected]>
  • Loading branch information
vince-fugnitto committed Jul 9, 2019
1 parent 7fe439a commit ca29b5c
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
if (token.isCancellationRequested) {
return;
}
// Sort the result map by folder URI.
this.resultTree = new Map([...this.resultTree]
.sort((a: [string, SearchInWorkspaceRootFolderNode], b: [string, SearchInWorkspaceRootFolderNode]) => this.compare(a[1].folderUri, b[1].folderUri)));
// Update the list of children nodes, sorting them by their file URI.
Array.from(this.resultTree.values())
.forEach((folder: SearchInWorkspaceRootFolderNode) => {
folder.children = folder.children.sort((a: SearchInWorkspaceFileNode, b: SearchInWorkspaceFileNode) => this.compare(a.fileUri, b.fileUri));
});
this.refreshModelChildren();
}
}, searchOptions).catch(e => { return; });
Expand Down Expand Up @@ -707,4 +715,17 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
}
return decorations;
}

/**
* Compare two normalized strings.
*
* @param a {string} the first string.
* @param b {string} the second string.
*/
private compare(a: string, b: string): number {
const itemA: string = a.toLowerCase().trim();
const itemB: string = b.toLowerCase().trim();
return itemA.localeCompare(itemB);
}

}

0 comments on commit ca29b5c

Please sign in to comment.