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 8, 2019
1 parent 7fe439a commit 14a2063
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ export class SearchInWorkspaceResultTreeWidget extends TreeWidget {
if (token.isCancellationRequested) {
return;
}
// Sort the list of root folder nodes by URI.
Array.from(this.resultTree.values())
.sort(((a: SearchInWorkspaceRootFolderNode, b: SearchInWorkspaceRootFolderNode) => this.compare(a.folderUri, b.folderUri)))
// Update the list of children nodes by sorting them by file URI.
.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 +714,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 14a2063

Please sign in to comment.