From 96dda01dafdf4badf360d8dcc3c7eaf448fae7f9 Mon Sep 17 00:00:00 2001 From: Andrea Mah <31675041+andreamah@users.noreply.github.com> Date: Wed, 28 Sep 2022 17:34:22 -0700 Subject: [PATCH] Search Tree - file order changes when switching from list to tree (#162270) Fixes #162155 --- .../contrib/search/common/searchModel.ts | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/vs/workbench/contrib/search/common/searchModel.ts b/src/vs/workbench/contrib/search/common/searchModel.ts index f071e78c421..4b7a435742c 100644 --- a/src/vs/workbench/contrib/search/common/searchModel.ts +++ b/src/vs/workbench/contrib/search/common/searchModel.ts @@ -928,12 +928,38 @@ export class FolderMatchNoRoot extends FolderMatch { * and their sort order is undefined. */ export function searchMatchComparer(elementA: RenderableMatch, elementB: RenderableMatch, sortOrder: SearchSortOrder = SearchSortOrder.Default): number { + + if (elementA instanceof FileMatch && elementB instanceof FolderMatch) { + return 1; + } + + if (elementB instanceof FileMatch && elementA instanceof FolderMatch) { + return -1; + } + if (elementA instanceof FolderMatch && elementB instanceof FolderMatch) { const elemAIndex = elementA.index(); const elemBIndex = elementB.index(); if (elemAIndex !== null && elemBIndex !== null) { return elemAIndex - elemBIndex; } + + switch (sortOrder) { + case SearchSortOrder.CountDescending: + return elementB.count() - elementA.count(); + case SearchSortOrder.CountAscending: + return elementA.count() - elementB.count(); + case SearchSortOrder.Type: + return compareFileExtensions(elementA.name(), elementB.name()); + case SearchSortOrder.FileNames: + return compareFileNames(elementA.name(), elementB.name()); + // Fall through otherwise + default: + if (!elementA.resource || !elementB.resource) { + return 0; + } + return comparePaths(elementA.resource.fsPath, elementB.resource.fsPath) || compareFileNames(elementA.name(), elementB.name()); + } } if (elementA instanceof FileMatch && elementB instanceof FileMatch) { @@ -947,12 +973,11 @@ export function searchMatchComparer(elementA: RenderableMatch, elementB: Rendera case SearchSortOrder.FileNames: return compareFileNames(elementA.name(), elementB.name()); case SearchSortOrder.Modified: { - if (!(elementA instanceof FolderMatch) || !(elementB instanceof FolderMatch)) { - const fileStatA = elementA.fileStat; - const fileStatB = elementB.fileStat; - if (fileStatA && fileStatB) { - return fileStatB.mtime - fileStatA.mtime; - } + const fileStatA = elementA.fileStat; + const fileStatB = elementB.fileStat; + if (fileStatA && fileStatB) { + return fileStatB.mtime - fileStatA.mtime; + } } // Fall through otherwise