Skip to content

Commit a2bc69d

Browse files
committed
refs #16 Sort selections by line number
1 parent 8792e56 commit a2bc69d

File tree

5 files changed

+46
-67
lines changed

5 files changed

+46
-67
lines changed

lib/command-factory.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const CompareSelectionWithClipboardCommand = require('./commands/compare-selecti
55
const TextTitleBuilder = require('./text-title-builder');
66
const Clipboard = require('./clipboard');
77
const DiffPresenter = require('./diff-presenter');
8-
const EditorLineRangeExtractor = require('./editor-line-range-extractor');
9-
const EditorTextExtractor = require('./editor-text-extractor');
108
const SelectionInfoBuilder = require('./selection-info-builder');
119
const clipboardy = require('clipboardy');
1210

@@ -57,7 +55,7 @@ class CommandFactory {
5755
}
5856

5957
_getSelectionInfoBuilder() {
60-
this._selectionInfoBuilder = this._selectionInfoBuilder || this._createSelectionInfoBuilder();
58+
this._selectionInfoBuilder = this._selectionInfoBuilder || new SelectionInfoBuilder();
6159
return this._selectionInfoBuilder;
6260
}
6361

@@ -77,13 +75,6 @@ class CommandFactory {
7775
});
7876
}
7977

80-
_createSelectionInfoBuilder() {
81-
return new SelectionInfoBuilder({
82-
editorLineRangeExtractor: new EditorLineRangeExtractor(),
83-
editorTextExtractor: new EditorTextExtractor()
84-
});
85-
}
86-
8778
}
8879

8980
module.exports = CommandFactory;

lib/editor-line-range-extractor.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/editor-text-extractor.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

lib/selection-info-builder.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,36 @@ const path = require('path');
33

44
class SelectionInfoBuilder {
55

6-
constructor(params) {
7-
this._editorTextExtractor = params.editorTextExtractor;
8-
this._editorLineRangeExtractor = params.editorLineRangeExtractor;
9-
}
10-
116
extract(editor) {
7+
const validSelections = this._collectNonEmptySelections(editor.selections);
8+
const extractText = selection => editor.document.getText(selection);
9+
1210
return {
13-
text: this._editorTextExtractor.extract(editor),
11+
text: this._extractText(validSelections, extractText),
1412
fileName: path.basename(editor.document.fileName),
15-
lineRanges: this._editorLineRangeExtractor.extract(editor)
13+
lineRanges: this._extractLineRanges(validSelections)
1614
};
1715
}
1816

17+
_collectNonEmptySelections(selections) {
18+
return selections
19+
.filter(s => !s.isEmpty)
20+
.sort((s1, s2) => s1.start.line - s2.start.line);
21+
}
22+
23+
_extractText(selections, extractText) {
24+
return selections.length === 0 ?
25+
extractText() :
26+
selections.map(extractText).join('\n');
27+
}
28+
29+
_extractLineRanges(selections) {
30+
return selections.map(selection => ({
31+
start: selection.start.line,
32+
end: selection.end.line
33+
}));
34+
}
35+
1936
}
2037

2138
module.exports = SelectionInfoBuilder;

test/lib/selection-info-builder.test.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11

22
const SelectionInfoBuilder = require('../../lib/selection-info-builder');
3-
const EditorTextExtractor = require('../../lib/editor-text-extractor');
4-
const EditorLineRangeExtractor = require('../../lib/editor-line-range-extractor');
53

64
suite('SelectionInfoBuilder', () => {
75

@@ -56,25 +54,39 @@ suite('SelectionInfoBuilder', () => {
5654
]);
5755
});
5856

57+
test('it sorts the selections by ascending order of line number', () => {
58+
const selections = [
59+
{start: {line: 5}, end: {line: 6}},
60+
{start: {line: 1}, end: {line: 2}}
61+
];
62+
const textInfo = extractTextInfoFromSelections(selections);
63+
expect(textInfo.lineRanges).to.eql([
64+
{start: 1, end: 2},
65+
{start: 5, end: 6}
66+
]);
67+
});
68+
5969
test('it extracts a file name from editor', () => {
6070
const textInfo = extractTextInfo(['SELECTED_TEXT']);
6171
expect(textInfo).to.include({fileName: 'FILENAME'});
6272
});
6373

6474
function extractTextInfo(selectedTexts) {
65-
const editorTextExtractor = new EditorTextExtractor();
66-
const editorLineRangeExtractor = new EditorLineRangeExtractor();
67-
const selectionInfoBuilder = new SelectionInfoBuilder({editorTextExtractor, editorLineRangeExtractor});
68-
return selectionInfoBuilder.extract(fakeEditor(selectedTexts));
69-
}
70-
71-
function fakeEditor(selectedTexts) {
7275
const selections = selectedTexts.map((text, i) => ({
7376
text,
7477
start: {line: `START_LINE_${i + 1}`},
7578
end: {line: `END_LINE_${i + 1}`},
7679
isEmpty: !text
7780
}));
81+
return extractTextInfoFromSelections(selections);
82+
}
83+
84+
function extractTextInfoFromSelections(selections) {
85+
const selectionInfoBuilder = new SelectionInfoBuilder();
86+
return selectionInfoBuilder.extract(fakeEditor(selections));
87+
}
88+
89+
function fakeEditor(selections) {
7890
return {
7991
selections,
8092
selection: selections[0],

0 commit comments

Comments
 (0)