Skip to content

Commit 48c4917

Browse files
committed
refs #16 Sort selections by column number if in the same line
1 parent a2bc69d commit 48c4917

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lib/selection-info-builder.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ class SelectionInfoBuilder {
1717
_collectNonEmptySelections(selections) {
1818
return selections
1919
.filter(s => !s.isEmpty)
20-
.sort((s1, s2) => s1.start.line - s2.start.line);
20+
.sort((s1, s2) => {
21+
const lineComparison = s1.start.line - s2.start.line;
22+
return lineComparison !== 0 ?
23+
lineComparison :
24+
s1.start.character - s2.start.character;
25+
});
2126
}
2227

2328
_extractText(selections, extractText) {

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,21 @@ suite('SelectionInfoBuilder', () => {
5656

5757
test('it sorts the selections by ascending order of line number', () => {
5858
const selections = [
59-
{start: {line: 5}, end: {line: 6}},
60-
{start: {line: 1}, end: {line: 2}}
59+
{start: {line: 5}, end: {line: 6}, text: 'A'},
60+
{start: {line: 1}, end: {line: 2}, text: 'B'}
6161
];
6262
const textInfo = extractTextInfoFromSelections(selections);
63-
expect(textInfo.lineRanges).to.eql([
64-
{start: 1, end: 2},
65-
{start: 5, end: 6}
66-
]);
63+
expect(textInfo.text).to.eql('B\nA');
64+
});
65+
66+
test('it sorts the selections by ascending order of column number if in the same line', () => {
67+
const selections = [
68+
{start: {line: 5, character: 7}, end: {line: 5, character: 8}, text: 'A'},
69+
{start: {line: 5, character: 4}, end: {line: 5, character: 5}, text: 'B'},
70+
{start: {line: 1, character: 1}, end: {line: 1, character: 2}, text: 'C'}
71+
];
72+
const textInfo = extractTextInfoFromSelections(selections);
73+
expect(textInfo.text).to.eql('C\nB\nA');
6774
});
6875

6976
test('it extracts a file name from editor', () => {
@@ -75,15 +82,16 @@ suite('SelectionInfoBuilder', () => {
7582
const selections = selectedTexts.map((text, i) => ({
7683
text,
7784
start: {line: `START_LINE_${i + 1}`},
78-
end: {line: `END_LINE_${i + 1}`},
79-
isEmpty: !text
85+
end: {line: `END_LINE_${i + 1}`}
8086
}));
8187
return extractTextInfoFromSelections(selections);
8288
}
8389

8490
function extractTextInfoFromSelections(selections) {
8591
const selectionInfoBuilder = new SelectionInfoBuilder();
86-
return selectionInfoBuilder.extract(fakeEditor(selections));
92+
const selectionWithIsEmptyFlag = selections
93+
.map(s => Object.assign({}, s, {isEmpty: !s.text}));
94+
return selectionInfoBuilder.extract(fakeEditor(selectionWithIsEmptyFlag));
8795
}
8896

8997
function fakeEditor(selections) {

0 commit comments

Comments
 (0)