diff --git a/src/vs/workbench/contrib/search/browser/searchView.ts b/src/vs/workbench/contrib/search/browser/searchView.ts index 4399e11c6fadc541f6bc8533a19330181ca905ac..435ad281ae37d8c2a6fb2ec7d9cd10788abc5467 100644 --- a/src/vs/workbench/contrib/search/browser/searchView.ts +++ b/src/vs/workbench/contrib/search/browser/searchView.ts @@ -711,7 +711,7 @@ export class SearchView extends ViewletPanel { const [selected] = this.tree.getSelection(); // Expand the initial selected node, if needed - if (selected instanceof FileMatch) { + if (selected && !(selected instanceof Match)) { if (this.tree.isCollapsed(selected)) { this.tree.expand(selected); } @@ -721,18 +721,16 @@ export class SearchView extends ViewletPanel { let next = navigator.next(); if (!next) { - // Reached the end - get a new navigator from the root. - navigator = this.tree.navigate(); next = navigator.first(); } - // Expand and go past FileMatch nodes + // Expand until first child is a Match while (!(next instanceof Match)) { if (this.tree.isCollapsed(next)) { this.tree.expand(next); } - // Select the FileMatch's first child + // Select the first child next = navigator.next(); } @@ -749,30 +747,17 @@ export class SearchView extends ViewletPanel { let prev = navigator.previous(); - // Expand and go past FileMatch nodes - if (!(prev instanceof Match)) { - prev = navigator.previous(); - if (!prev) { - // Wrap around - prev = navigator.last(); - - // This is complicated because .last will set the navigator to the last FileMatch, - // so expand it and FF to its last child - this.tree.expand(prev); - let tmp: RenderableMatch | null; - while (tmp = navigator.next()) { - prev = tmp; - } - } + // Select previous until find a Match or a collapsed item + while (!prev || (!(prev instanceof Match) && !this.tree.isCollapsed(prev))) { + prev = prev ? navigator.previous() : navigator.last(); + } - if (!(prev instanceof Match)) { - // There is a second non-Match result, which must be a collapsed FileMatch. - // Expand it then select its last child. - const nextItem = navigator.next(); - this.tree.expand(prev); - navigator = this.tree.navigate(nextItem); // recreate navigator because modifying the tree can invalidate it - prev = navigator.previous(); - } + // Expand until last child is a Match + while (!(prev instanceof Match)) { + const nextItem = navigator.next(); + this.tree.expand(prev); + navigator = this.tree.navigate(nextItem); // recreate navigator because modifying the tree can invalidate it + prev = nextItem ? navigator.previous() : navigator.last(); // select last child } // Reveal the newly selected element