Skip to content

Commit af05818

Browse files
committed
refs #16 Extract texts from multi cursors
1 parent b290623 commit af05818

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

lib/editor-text-extractor.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22
class EditorTextExtractor {
33

44
extract(editor) {
5-
const selection = !editor.selection.isEmpty ? editor.selection : null;
6-
return editor.document.getText(selection);
5+
return this._isTextSelected(editor.selections) ?
6+
this._extractTexts(editor) : this._getEntireText(editor);
7+
}
8+
9+
_isTextSelected(selections) {
10+
return selections.some(selection => !selection.isEmpty);
11+
}
12+
13+
_extractTexts(editor) {
14+
return editor.selections
15+
.filter(selection => !selection.isEmpty)
16+
.map(selection => editor.document.getText(selection))
17+
.join('\n');
18+
}
19+
20+
_getEntireText(editor) {
21+
return editor.document.getText();
722
}
823

924
}

test/lib/editor-text-extractor.test.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,42 @@ suite('EditorTextExtractor', () => {
55

66
test('it extracts a text from editor for comparison', () => {
77
const extractor = new EditorTextExtractor();
8-
const editor = fakeEditor('SELECTED_TEXT');
8+
const editor = fakeEditor(['SELECTED_TEXT']);
99
expect(extractor.extract(editor)).to.eql('SELECTED_TEXT');
1010
});
1111

1212
test('it extracts a whole text if no text is currently selected', () => {
1313
const extractor = new EditorTextExtractor();
14-
const editor = fakeEditor('');
15-
expect(extractor.extract(editor)).to.eql('ENTIRE TEXT ');
14+
const editor = fakeEditor(['']);
15+
expect(extractor.extract(editor)).to.eql('ENTIRE TEXT');
1616
});
1717

18-
function fakeEditor(selectedText) {
18+
test('it extracts texts selected by all cursors and join them with newline character', () => {
19+
const extractor = new EditorTextExtractor();
20+
const editor = fakeEditor(['SELECTED_TEXT_1', 'SELECTED_TEXT_2']);
21+
expect(extractor.extract(editor)).to.eql('SELECTED_TEXT_1\nSELECTED_TEXT_2');
22+
});
23+
24+
test('it ignores cursor that is not selecting text if others are selecting one', () => {
25+
const extractor = new EditorTextExtractor();
26+
const editor = fakeEditor(['', 'SELECTED_TEXT_1', '', 'SELECTED_TEXT_2']);
27+
expect(extractor.extract(editor)).to.eql('SELECTED_TEXT_1\nSELECTED_TEXT_2');
28+
});
29+
30+
test('it extracts the whole text if no cursors are selecting text', () => {
31+
const extractor = new EditorTextExtractor();
32+
const editor = fakeEditor(['', '', '']);
33+
expect(extractor.extract(editor)).to.eql('ENTIRE TEXT');
34+
});
35+
36+
function fakeEditor(selectedTexts) {
37+
const selections = selectedTexts.map(text => ({text, isEmpty: !text}));
1938
return {
20-
selection: {
21-
text: selectedText,
22-
isEmpty: !selectedText
23-
},
39+
selections,
40+
selection: selections[0],
2441
document: {
25-
_entireText: `ENTIRE TEXT ${selectedText}`,
26-
getText: function (selection) {
42+
_entireText: 'ENTIRE TEXT',
43+
getText(selection) {
2744
return selection ? selection.text : this._entireText;
2845
}
2946
}

0 commit comments

Comments
 (0)