Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 3ea44d7

Browse files
author
Max Brunsfeld
authored
Merge pull request #884 from dirk-thomas/context_spec
Add spec for context lines
2 parents ceaa300 + 0c614b4 commit 3ea44d7

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

lib/project/match-view.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class MatchView {
6868
leadingContextLines ? leadingContextLines.map((line, index) =>
6969
$.li({className: 'list-item'},
7070
$.span({className: 'line-number text-subtle'}, range.start.row + 1 - leadingContextLines.length + index),
71-
$.span({className: 'preview'}, $.span({}, line))
71+
$.span({className: 'preview'}, $.span({}, line.trimLeft()))
7272
)
7373
) : null,
7474

@@ -92,7 +92,7 @@ class MatchView {
9292
trailingContextLines ? trailingContextLines.map((line, index) =>
9393
$.li({className: 'list-item'},
9494
$.span({className: 'line-number text-subtle'}, range.end.row + 1 + index + 1),
95-
$.span({className: 'preview'}, $.span({}, line))
95+
$.span({className: 'preview'}, $.span({}, line.trimLeft()))
9696
)
9797
) : null
9898
)

lib/project/results-model.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ class ResultsModel
247247
return unless editor.getPath()
248248

249249
matches = []
250+
# the following condition is pretty hacky
251+
# it doesn't work correctly for e.g. version 1.2
250252
if parseFloat(atom.getVersion()) >= 1.17
251253
leadingContextLineCount = atom.config.get('find-and-replace.searchContextLineCountBefore')
252254
trailingContextLineCount = atom.config.get('find-and-replace.searchContextLineCountAfter')

spec/results-model-spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe("ResultsModel", () => {
1010

1111
beforeEach(async () => {
1212
atom.config.set("core.excludeVcsIgnoredPaths", false);
13+
atom.config.set("find-and-replace.searchContextLineCountBefore", 2);
14+
atom.config.set("find-and-replace.searchContextLineCountAfter", 3);
1315
atom.project.setPaths([path.join(__dirname, "fixtures")]);
1416

1517
editor = await atom.workspace.open("sample.js");
@@ -33,6 +35,18 @@ describe("ResultsModel", () => {
3335
expect(resultsModel.getPathCount()).toBe(1);
3436
expect(resultsModel.getMatchCount()).toBe(6);
3537
expect(resultsModel.getPaths()).toEqual([editor.getPath()]);
38+
// the following condition is pretty hacky
39+
// it doesn't work correctly for e.g. version 1.2
40+
if (parseFloat(atom.getVersion()) >= 1.17) {
41+
expect(result.matches[0].leadingContextLines.length).toBe(1);
42+
expect(result.matches[0].leadingContextLines[0]).toBe("var quicksort = function () {");
43+
expect(result.matches[0].trailingContextLines.length).toBe(3);
44+
expect(result.matches[0].trailingContextLines[0]).toBe(" if (items.length <= 1) return items;");
45+
expect(result.matches[0].trailingContextLines[1]).toBe(" var pivot = items.shift(), current, left = [], right = [];");
46+
expect(result.matches[0].trailingContextLines[2]).toBe(" while(items.length > 0) {");
47+
expect(result.matches[5].leadingContextLines.length).toBe(2);
48+
expect(result.matches[5].trailingContextLines.length).toBe(3);
49+
}
3650

3751
editor.setText("there are some items in here");
3852
advanceClock(editor.buffer.stoppedChangingDelay);
@@ -44,6 +58,12 @@ describe("ResultsModel", () => {
4458
expect(resultsModel.getMatchCount()).toBe(1);
4559
expect(resultsModel.getPaths()).toEqual([editor.getPath()]);
4660
expect(result.matches[0].lineText).toBe("there are some items in here");
61+
// the following condition is pretty hacky
62+
// it doesn't work correctly for e.g. version 1.2
63+
if (parseFloat(atom.getVersion()) >= 1.17) {
64+
expect(result.matches[0].leadingContextLines.length).toBe(0);
65+
expect(result.matches[0].trailingContextLines.length).toBe(0);
66+
}
4767

4868
editor.setText("no matches in here");
4969
advanceClock(editor.buffer.stoppedChangingDelay);

spec/results-view-spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,43 @@ describe('ResultsView', () => {
692692
atom.commands.dispatch(projectFindView.element, 'core:confirm');
693693
expect(resultsPane.refs.previewCount.textContent).toContain('Project search results');
694694
})
695+
});
696+
697+
describe('search result context lines', () => {
698+
beforeEach(async () => {
699+
atom.config.set('find-and-replace.searchContextLineCountBefore', 2);
700+
atom.config.set('find-and-replace.searchContextLineCountAfter', 3);
701+
702+
projectFindView.findEditor.setText('items');
703+
atom.commands.dispatch(projectFindView.element, 'core:confirm');
704+
await searchPromise;
705+
706+
resultsView = getResultsView();
707+
});
708+
709+
it('shows the context lines', async () => {
710+
// the following condition is pretty hacky
711+
// it doesn't work correctly for e.g. version 1.2
712+
if (parseFloat(atom.getVersion()) >= 1.17) {
713+
const pathNodes = resultsView.refs.listView.element.querySelectorAll('.path');
714+
expect(pathNodes.length).toBe(1)
715+
const pathNameNode = pathNodes[0].querySelector('.path-name');
716+
expect(pathNameNode.textContent).toBe('sample.coffee');
717+
const resultNode = pathNodes[0].querySelector('.search-result');
718+
const lineNodes = resultNode.querySelectorAll('.list-item');
719+
expect(lineNodes.length).toBe(5)
720+
expect(lineNodes[0]).not.toHaveClass('match-line');
721+
expect(lineNodes[0].querySelector('.preview').textContent).toBe('class quicksort');
722+
expect(lineNodes[1]).toHaveClass('match-line');
723+
expect(lineNodes[1].querySelector('.preview').textContent).toBe('sort: (items) ->');
724+
expect(lineNodes[2]).not.toHaveClass('match-line');
725+
expect(lineNodes[2].querySelector('.preview').textContent).toBe('return items if items.length <= 1');
726+
expect(lineNodes[3]).not.toHaveClass('match-line');
727+
expect(lineNodes[3].querySelector('.preview').textContent).toBe('');
728+
expect(lineNodes[4]).not.toHaveClass('match-line');
729+
expect(lineNodes[4].querySelector('.preview').textContent).toBe('pivot = items.shift()');
730+
}
731+
});
695732
})
696733
});
697734

0 commit comments

Comments
 (0)