Preserve selected match even when the file path is collapsed
MODIFY
lib/project/result-view.js
lib/project/result-view.js
@@ -77,6 +77,11 @@ class ResultView {
}
}
+ const isPathSelected = this.isSelected && (
+ !this.isExpanded ||
+ this.selectedMatchIndex === -1
+ );
+
return (
etch.dom.li(
{
@@ -85,7 +90,7 @@ class ResultView {
className: [
'path',
'list-nested-item',
- this.isSelected && this.selectedMatchIndex === -1 ? 'selected' : '',
+ isPathSelected ? 'selected' : '',
this.isExpanded ? '' : 'collapsed'
].join(' ').trim()
},

MODIFY
lib/project/results-view.js
lib/project/results-view.js
@@ -326,6 +326,10 @@ class ResultsView {
return;
}
+ if (this.collapsedResultIndices[this.selectedResultIndex]) {
+ this.selectedMatchIndex = -1;
+ }
+
if (this.selectedMatchIndex >= 0) {
this.selectedMatchIndex--;
} else if (this.selectedResultIndex > 0) {

MODIFY
spec/results-view-spec.js
spec/results-view-spec.js
@@ -126,7 +126,6 @@ describe('ResultsView', () => {
});
});
- describe("when list is scrollable", () => {
describe("core:page-up and core:page-down", () => {
beforeEach(async () => {
workspaceElement.style.height = '300px';
@@ -236,6 +235,55 @@ describe('ResultsView', () => {
expect(resultsView.refs.listView.element.querySelector('li').closest('.path')).toHaveClass('selected');
});
});
+
+ describe("expanding and collapsing results", () => {
+ it('preserves the selected file when collapsing all results', async () => {
+ projectFindView.findEditor.setText('items');
+ atom.commands.dispatch(projectFindView.element, 'core:confirm');
+ await searchPromise;
+
+ resultsView = getResultsView();
+
+ resultsView.moveDown();
+ resultsView.moveDown();
+ resultsView.moveDown();
+
+ const selectedMatch = resultsView.element.querySelector('.selected');
+ expect(selectedMatch).toHaveClass('search-result');
+
+ resultsView.collapseAllResults();
+ const selectedPath = resultsView.element.querySelector('.selected');
+ expect(selectedPath).toHaveClass('path');
+ expect(selectedPath.dataset.path).toContain('sample.coffee');
+
+ // If the result is re-expanded without moving up or down, the original
+ // selected match remains selected.
+ resultsView.expandAllResults();
+ const newSelectedMatch = resultsView.element.querySelector('.selected');
+ expect(newSelectedMatch.innerHTML).toBe(selectedMatch.innerHTML);
+ expect(selectedPath.contains(newSelectedMatch)).toBe(true);
+
+ resultsView.collapseAllResults();
+ resultsView.moveDown();
+ resultsView.expandAllResults();
+
+ // Moving down while the path is collapsed moves to the next path,
+ // as opposed to selecting the next match within the collapsed path.
+ const newSelectedPath = resultsView.element.querySelector('.selected');
+ expect(newSelectedPath.dataset.path).toContain('sample.js');
+
+ resultsView.moveDown();
+ resultsView.moveDown();
+ resultsView.moveDown();
+ expect(resultsView.element.querySelector('.selected')).toHaveClass('search-result');
+
+ // Moving up while the path is collapsed moves to the previous path,
+ // as opposed to moving up to the next match within the collapsed path.
+ resultsView.collapseAllResults();
+ resultsView.moveUp();
+ resultsView.expandAllResults();
+ expect(resultsView.element.querySelector('.selected')).toBe(selectedPath);
+ });
});
describe("opening results", () => {

Preserve selected match even when the file path is collapsed