Skip to content

Commit 176e777

Browse files
committed
refs #16 Show line ranges selected by all cursours
1 parent af05818 commit 176e777

11 files changed

+74
-38
lines changed

lib/editor-line-range-extractor.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
class EditorLineRangeExtractor {
33

44
extract(editor) {
5-
const selection = editor.selection;
6-
7-
if (selection.isEmpty) return null;
8-
return {
9-
start: selection.start.line,
10-
end: selection.end.line
11-
};
5+
return editor.selections
6+
.filter(selection => !selection.isEmpty)
7+
.map(selection => ({
8+
start: selection.start.line,
9+
end: selection.end.line
10+
}));
1211
}
1312

1413
}

lib/selection-info-builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SelectionInfoBuilder {
1212
return {
1313
text: this._editorTextExtractor.extract(editor),
1414
fileName: path.basename(editor.document.fileName),
15-
lineRange: this._editorLineRangeExtractor.extract(editor)
15+
lineRanges: this._editorLineRangeExtractor.extract(editor)
1616
};
1717
}
1818

lib/selection-info-registry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SelectionInfoRegistry {
99
this._data[key] = {
1010
text: textInfo.text,
1111
fileName: textInfo.fileName,
12-
lineRange: textInfo.lineRange
12+
lineRanges: textInfo.lineRanges
1313
};
1414
}
1515

lib/text-title-builder.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ class TextTitleBuilder {
44
build(textInfo) {
55
if (!textInfo) return 'N/A';
66

7-
const suffix = this._lineRangeSuffix(textInfo.lineRange);
7+
const suffix = this._lineRangesSuffix(textInfo.lineRanges);
88
return `${textInfo.fileName}${suffix}`;
99
}
1010

11-
_lineRangeSuffix(lineRange) {
12-
if (!lineRange) return '';
11+
_lineRangesSuffix(lineRanges) {
12+
return lineRanges.length !== 0 ?
13+
` (${lineRanges.map(this._lineRangeLabel)})` : '';
14+
}
15+
16+
_lineRangeLabel(lineRange) {
1317
const isOneLine = lineRange.start === lineRange.end;
1418
return isOneLine ?
15-
` (l.${lineRange.start + 1})` :
16-
` (ll.${lineRange.start + 1}-${lineRange.end + 1})`;
19+
`l.${lineRange.start + 1}` :
20+
`ll.${lineRange.start + 1}-${lineRange.end + 1}`;
1721
}
1822

1923
}

test/lib/commands/compare-selection-with-clipboard.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ suite('CompareSelectionWithClipboardCommand', () => {
99
extract: stubWithArgs(['EDITOR'], {
1010
text: 'SELECTED_TEXT',
1111
fileName: 'FILENAME',
12-
lineRange: 'SELECTED_RANGE'
12+
lineRanges: 'SELECTED_RANGE'
1313
})
1414
};
1515
const selectionInfoRegistry = {set: sinon.spy()};
@@ -35,7 +35,7 @@ suite('CompareSelectionWithClipboardCommand', () => {
3535
{
3636
text: 'SELECTED_TEXT',
3737
fileName: 'FILENAME',
38-
lineRange: 'SELECTED_RANGE'
38+
lineRanges: 'SELECTED_RANGE'
3939
}
4040
);
4141
expect(diffPresenter.takeDiff).to.have.been.calledWith('clipboard', 'reg2');

test/lib/commands/compare-selection-with-text1.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ suite('CompareSelectionWithText1', () => {
88
extract: stubWithArgs(['EDITOR'], {
99
text: 'SELECTED_TEXT',
1010
fileName: 'FILENAME',
11-
lineRange: 'SELECTED_RANGE'
11+
lineRanges: 'SELECTED_RANGE'
1212
})
1313
};
1414
const selectionInfoRegistry = {set: sinon.spy()};
@@ -22,7 +22,7 @@ suite('CompareSelectionWithText1', () => {
2222
{
2323
text: 'SELECTED_TEXT',
2424
fileName: 'FILENAME',
25-
lineRange: 'SELECTED_RANGE'
25+
lineRanges: 'SELECTED_RANGE'
2626
}
2727
);
2828
expect(diffPresenter.takeDiff).to.have.been.calledWith('reg1', 'reg2');

test/lib/commands/save-text-1.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ suite('SelectText1Command', () => {
88
extract: stubWithArgs(['EDITOR'], {
99
text: 'SELECTED_TEXT',
1010
fileName: 'FILENAME',
11-
lineRange: 'SELECTED_RANGE'
11+
lineRanges: 'SELECTED_RANGE'
1212
})
1313
};
1414
const selectionInfoRegistry = {set: sinon.spy()};
@@ -19,7 +19,7 @@ suite('SelectText1Command', () => {
1919
{
2020
text: 'SELECTED_TEXT',
2121
fileName: 'FILENAME',
22-
lineRange: 'SELECTED_RANGE'
22+
lineRanges: 'SELECTED_RANGE'
2323
}
2424
);
2525
});
@@ -37,7 +37,7 @@ suite('SelectText1Command', () => {
3737
extract: stubWithArgs(['EDITOR'], {
3838
text: 'SELECTED_TEXT',
3939
fileName: 'FILENAME',
40-
lineRange: 'SELECTED_RANGE'
40+
lineRanges: 'SELECTED_RANGE'
4141
})
4242
};
4343
const selectionInfoRegistry = {set: sinon.stub().throws(new Error('UNEXPECTED_ERROR'))};

test/lib/editor-line-range-extractor.test.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,43 @@ suite('EditorLineRangeExtractor', () => {
55

66
test('it extracts the line range of text selection', () => {
77
const extractor = new EditorLineRangeExtractor();
8-
const editor = fakeEditor('SELECTED_TEXT');
9-
expect(extractor.extract(editor)).to.eql({start: 'START_LINE', end: 'END_LINE'});
8+
const editor = fakeEditor(['SELECTED_TEXT']);
9+
expect(extractor.extract(editor)).to.eql([{start: 'START_LINE_1', end: 'END_LINE_1'}]);
1010
});
1111

1212
test('it returns null if no text is selected', () => {
1313
const extractor = new EditorLineRangeExtractor();
14-
const editor = fakeEditor('');
15-
expect(extractor.extract(editor)).to.eql(null);
14+
const editor = fakeEditor(['']);
15+
expect(extractor.extract(editor)).to.eql([]);
1616
});
1717

18-
function fakeEditor(selectedText) {
18+
test('it extracts all the line ranges of text selections', () => {
19+
const extractor = new EditorLineRangeExtractor();
20+
const editor = fakeEditor(['SELECTED_TEXT_1', 'SELECTED_TEXT_2']);
21+
expect(extractor.extract(editor)).to.eql([
22+
{start: 'START_LINE_1', end: 'END_LINE_1'},
23+
{start: 'START_LINE_2', end: 'END_LINE_2'}
24+
]);
25+
});
26+
27+
test('it skips all cursors that are not selecting any text', () => {
28+
const extractor = new EditorLineRangeExtractor();
29+
const editor = fakeEditor(['SELECTED_TEXT_1', '', 'SELECTED_TEXT_3']);
30+
expect(extractor.extract(editor)).to.eql([
31+
{start: 'START_LINE_1', end: 'END_LINE_1'},
32+
{start: 'START_LINE_3', end: 'END_LINE_3'}
33+
]);
34+
});
35+
36+
function fakeEditor(selectedTexts) {
37+
const selections = selectedTexts.map((text, i) => ({
38+
start: {line: `START_LINE_${i + 1}`},
39+
end: {line: `END_LINE_${i + 1}`},
40+
isEmpty: !text
41+
}));
1942
return {
20-
selection: {
21-
start: {line: 'START_LINE'},
22-
end: {line: 'END_LINE'},
23-
isEmpty: !selectedText
24-
}
43+
selections,
44+
selection: selections[0]
2545
};
2646
}
2747
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ suite('SelectionInfoBuilder', () => {
55

66
test('it extracts a text from editor for comparison', () => {
77
const editorTextExtractor = {extract: sinon.stub().returns('SELECTED_TEXT')};
8-
const editorLineRangeExtractor = {extract: sinon.stub().returns('LINE_RANGE')};
8+
const editorLineRangeExtractor = {extract: sinon.stub().returns('LINE_RANGES')};
99
const selectionInfoBuilder = new SelectionInfoBuilder({editorTextExtractor, editorLineRangeExtractor});
1010
const editor = fakeEditor('SELECTED_TEXT');
1111
const textInfo = selectionInfoBuilder.extract(editor);
1212

1313
expect(textInfo).to.eql({
1414
text: 'SELECTED_TEXT',
1515
fileName: 'FILENAME',
16-
lineRange: 'LINE_RANGE'
16+
lineRanges: 'LINE_RANGES'
1717
});
1818
expect(editorTextExtractor.extract).to.have.been.calledWith(editor);
1919
expect(editorLineRangeExtractor.extract).to.have.been.calledWith(editor);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ suite('SelectionInfoRegistry', () => {
88
selectionInfoRegistry.set('KEY', {
99
text: 'TEXT',
1010
fileName: 'FILE_NAME',
11-
lineRange: 'LINE_RANGE',
11+
lineRanges: 'LINE_RANGES',
1212
'..': '..'
1313
});
1414
expect(selectionInfoRegistry.get('KEY')).to.eql({
1515
text: 'TEXT',
1616
fileName: 'FILE_NAME',
17-
lineRange: 'LINE_RANGE'
17+
lineRanges: 'LINE_RANGES'
1818
});
1919
});
2020

0 commit comments

Comments
 (0)