Skip to content

Commit c2a35f0

Browse files
committed
Removed SelectionInfoBuilder as it is no longer adding much value
1 parent 7f630d0 commit c2a35f0

File tree

9 files changed

+113
-90
lines changed

9 files changed

+113
-90
lines changed

src/lib/adaptors/text-editor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Selection, TextEditor as VsTextEditor} from 'vscode';
1+
import {Selection, TextEditor as VsTextEditor, ViewColumn} from 'vscode';
22
import {basename} from 'path';
33
import {LineRange} from '../entities/selection-info';
44

@@ -13,6 +13,10 @@ export default class TextEditor {
1313
return basename(this.vsEditor.document.fileName);
1414
}
1515

16+
get viewColumn(): ViewColumn {
17+
return this.vsEditor.viewColumn;
18+
}
19+
1620
get selectedText() {
1721
const validSelections = this.collectNonEmptySelections(this.vsEditor.selections);
1822
return this.extractText(validSelections);

src/lib/adaptors/window.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as vscode from 'vscode';
2+
import TextEditor from './text-editor';
3+
4+
export default class WindowComponent {
5+
private window: typeof vscode.window;
6+
7+
constructor(window: typeof vscode.window) {
8+
this.window = window;
9+
}
10+
11+
get visibleTextEditors(): TextEditor[] {
12+
return this.window.visibleTextEditors.map(editor => new TextEditor(editor));
13+
}
14+
}

src/lib/bootstrapper-factory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ import NormalisationRuleStore from './normalisation-rule-store';
66
import SelectionInfoRegistry from './selection-info-registry';
77
import * as vscode from 'vscode';
88
import CommandAdaptor from './adaptors/command';
9+
import WindowComponent from './adaptors/window';
910

1011
export default class BootstrapperFactory {
1112
create() {
1213
const logger = console;
1314
const selectionInfoRegistry = new SelectionInfoRegistry();
1415
const normalisationRuleStore = this.createNormalisationRuleStore();
1516
const commandAdaptor = new CommandAdaptor(vscode.commands, vscode.Uri.parse);
17+
const windowComponent = new WindowComponent(vscode.window);
1618
const commandFactory = new CommandFactory(
1719
selectionInfoRegistry,
1820
normalisationRuleStore,
1921
commandAdaptor,
22+
windowComponent,
2023
vscode,
2124
() => new Date()
2225
);

src/lib/command-factory.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@ import Clipboard from './clipboard';
77
import DiffPresenter from './diff-presenter';
88
import MessageBar from './message-bar';
99
import NormalisationRulePicker from './normalisation-rule-picker';
10-
import SelectionInfoBuilder from './selection-info-builder';
1110
import ToggleNormalisationRulesCommand from './commands/toggle-normalisation-rules';
1211
import NormalisationRuleStore from './normalisation-rule-store';
1312
import SelectionInfoRegistry from './selection-info-registry';
1413
import * as clipboardy from 'clipboardy';
1514
import CommandAdaptor from './adaptors/command';
15+
import WindowComponent from './adaptors/window';
1616

1717
export default class CommandFactory {
1818
private readonly normalisationRuleStore: NormalisationRuleStore;
1919
private readonly selectionInfoRegistry: SelectionInfoRegistry;
2020
private readonly commandAdaptor: CommandAdaptor;
21+
private readonly windowComponent: WindowComponent;
2122
private readonly vscode: any;
2223
private readonly getCurrentDate: () => Date;
2324
private clipboard?: Clipboard;
2425
private diffPresenter?: DiffPresenter;
2526
private messageBar?: MessageBar;
26-
private selectionInfoBuilder?: SelectionInfoBuilder;
2727

2828
constructor(selectionInfoRegistry: SelectionInfoRegistry,
2929
normalisationRuleStore: NormalisationRuleStore,
3030
commandAdaptor: CommandAdaptor,
31+
windowComponent: WindowComponent,
3132
vscode: any,
3233
getCurrentDate: () => Date) {
3334
this.normalisationRuleStore = normalisationRuleStore;
3435
this.selectionInfoRegistry = selectionInfoRegistry;
3536
this.commandAdaptor = commandAdaptor;
37+
this.windowComponent = windowComponent;
3638
this.getCurrentDate = getCurrentDate;
3739
this.vscode = vscode;
3840
}
@@ -59,10 +61,9 @@ export default class CommandFactory {
5961
createCompareVisibleEditorsCommand() {
6062
return new CompareVisibleEditorsCommand(
6163
this.getDiffPresenter(),
62-
this.getSelectionInfoBuilder(),
6364
this.selectionInfoRegistry,
6465
this.getMessageBar(),
65-
this.vscode.window
66+
this.windowComponent
6667
);
6768
}
6869

@@ -89,12 +90,6 @@ export default class CommandFactory {
8990
return this.messageBar;
9091
}
9192

92-
private getSelectionInfoBuilder() {
93-
this.selectionInfoBuilder =
94-
this.selectionInfoBuilder || new SelectionInfoBuilder();
95-
return this.selectionInfoBuilder;
96-
}
97-
9893
private createClipboard() {
9994
return new Clipboard(clipboardy, process.platform);
10095
}

src/lib/commands/compare-visible-editors.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
import DiffPresenter from '../diff-presenter';
22
import MessageBar from '../message-bar';
3-
import SelectionInfoBuilder from '../selection-info-builder';
43
import SelectionInfoRegistry from '../selection-info-registry';
54
import {TextKey} from '../const';
6-
import * as vscode from 'vscode';
75
import {SelectionInfo} from '../entities/selection-info';
86
import {Command} from './command';
7+
import WindowComponent from '../adaptors/window';
98

109
export default class CompareVisibleEditorsCommand implements Command {
11-
private readonly editorWindow: typeof vscode.window;
10+
private readonly windowComponent: WindowComponent;
1211
private readonly diffPresenter: DiffPresenter;
1312
private readonly messageBar: MessageBar;
14-
private readonly selectionInfoBuilder: SelectionInfoBuilder;
1513
private readonly selectionInfoRegistry: SelectionInfoRegistry;
1614

1715
constructor(diffPresenter: DiffPresenter,
18-
selectionInfoBuilder: SelectionInfoBuilder,
1916
selectionInfoRegistry: SelectionInfoRegistry,
2017
messageBar: MessageBar,
21-
editorWindow: typeof vscode.window) {
22-
this.editorWindow = editorWindow;
18+
windowComponent: WindowComponent) {
19+
this.windowComponent = windowComponent;
2320
this.diffPresenter = diffPresenter;
2421
this.messageBar = messageBar;
25-
this.selectionInfoBuilder = selectionInfoBuilder;
2622
this.selectionInfoRegistry = selectionInfoRegistry;
2723
}
2824

2925
async execute() {
30-
const editors = this.editorWindow.visibleTextEditors;
26+
const editors = this.windowComponent.visibleTextEditors;
3127
if (editors.length !== 2) {
3228
this.messageBar.showInfo('Please first open 2 documents to compare.');
3329
return;
3430
}
3531

36-
const textInfos = editors.map(editor =>
37-
this.selectionInfoBuilder.extract(editor)
38-
);
32+
const textInfos = editors.map(editor => ({
33+
text: editor.selectedText,
34+
fileName: editor.fileName,
35+
lineRanges: editor.selectedLineRanges
36+
}));
3937
this.registerTextInfo(
4038
textInfos,
4139
editors[0].viewColumn > editors[1].viewColumn

src/lib/selection-info-builder.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
1-
import SelectionInfoBuilder from '../../lib/selection-info-builder';
1+
import TextEditor from '../../../lib/adaptors/text-editor';
22
import * as assert from 'assert';
3+
import {mockType} from '../../helpers';
4+
import * as vscode from 'vscode';
5+
6+
suite('TextEditor', () => {
37

4-
suite('SelectionInfoBuilder', () => {
58
test('it extracts text from editor', () => {
6-
const textInfo = extractTextInfo(['SELECTED_TEXT']);
7-
assert.equal(textInfo.text, 'SELECTED_TEXT');
9+
const editor = createEditorWithTexts(['SELECTED_TEXT']);
10+
assert.equal(editor.selectedText, 'SELECTED_TEXT');
811
});
912

1013
test('it extracts a whole text if no text is currently selected', () => {
11-
const textInfo = extractTextInfo(['']);
12-
assert.equal(textInfo.text, 'ENTIRE TEXT');
14+
const editor = createEditorWithTexts(['']);
15+
assert.equal(editor.selectedText, 'ENTIRE TEXT');
1316
});
1417

1518
test('it extracts texts selected by all cursors and join them with newline character', () => {
16-
const textInfo = extractTextInfo(['SELECTED_TEXT_1', 'SELECTED_TEXT_2']);
17-
assert.equal(textInfo.text, 'SELECTED_TEXT_1\nSELECTED_TEXT_2');
19+
const editor = createEditorWithTexts(['SELECTED_TEXT_1', 'SELECTED_TEXT_2']);
20+
assert.equal(editor.selectedText, 'SELECTED_TEXT_1\nSELECTED_TEXT_2');
1821
});
1922

2023
test('it ignores cursor that is not selecting text if others are selecting one', () => {
21-
const textInfo = extractTextInfo([
24+
const editor = createEditorWithTexts([
2225
'',
2326
'SELECTED_TEXT_1',
2427
'',
2528
'SELECTED_TEXT_2'
2629
]);
27-
assert.equal(textInfo.text, 'SELECTED_TEXT_1\nSELECTED_TEXT_2');
30+
assert.equal(editor.selectedText, 'SELECTED_TEXT_1\nSELECTED_TEXT_2');
2831
});
2932

3033
test('it extracts the whole text if no cursors are selecting text', () => {
31-
const textInfo = extractTextInfo(['', '', '']);
32-
assert.equal(textInfo.text, 'ENTIRE TEXT');
34+
const editor = createEditorWithTexts(['', '', '']);
35+
assert.equal(editor.selectedText, 'ENTIRE TEXT');
3336
});
3437

3538
test('it extracts selected line ranges from editor', () => {
36-
const textInfo = extractTextInfo(['SELECTED_TEXT']);
37-
assert.deepEqual(textInfo.lineRanges, [
39+
const editor = createEditorWithTexts(['SELECTED_TEXT']);
40+
assert.deepEqual(editor.selectedLineRanges, [
3841
{start: 'START_LINE_1', end: 'END_LINE_1'}
3942
]);
4043
});
4144

4245
test('it returns an empty list if no text is selected', () => {
43-
const textInfo = extractTextInfo(['']);
44-
assert.deepEqual(textInfo.lineRanges, []);
46+
const editor = createEditorWithTexts(['']);
47+
assert.deepEqual(editor.selectedLineRanges, []);
4548
});
4649

4750
test('it extracts all the line ranges of text selections', () => {
48-
const textInfo = extractTextInfo(['SELECTED_TEXT_1', 'SELECTED_TEXT_2']);
49-
assert.deepEqual(textInfo.lineRanges, [
51+
const editor = createEditorWithTexts(['SELECTED_TEXT_1', 'SELECTED_TEXT_2']);
52+
assert.deepEqual(editor.selectedLineRanges, [
5053
{start: 'START_LINE_1', end: 'END_LINE_1'},
5154
{start: 'START_LINE_2', end: 'END_LINE_2'}
5255
]);
5356
});
5457

5558
test('it skips all cursors that are not selecting any text', () => {
56-
const textInfo = extractTextInfo(['SELECTED_TEXT_1', '', 'SELECTED_TEXT_3']);
57-
assert.deepEqual(textInfo.lineRanges, [
59+
const editor = createEditorWithTexts(['SELECTED_TEXT_1', '', 'SELECTED_TEXT_3']);
60+
assert.deepEqual(editor.selectedLineRanges, [
5861
{start: 'START_LINE_1', end: 'END_LINE_1'},
5962
{start: 'START_LINE_3', end: 'END_LINE_3'}
6063
]);
@@ -65,8 +68,8 @@ suite('SelectionInfoBuilder', () => {
6568
{start: {line: 5}, end: {line: 6}, text: 'A'},
6669
{start: {line: 1}, end: {line: 2}, text: 'B'}
6770
];
68-
const textInfo = extractTextInfoFromSelections(selections);
69-
assert.deepEqual(textInfo.text, 'B\nA');
71+
const editor = createEditorWithSelections(selections);
72+
assert.deepEqual(editor.selectedText, 'B\nA');
7073
});
7174

7275
test('it sorts the selections by ascending order of column number if in the same line', () => {
@@ -87,34 +90,33 @@ suite('SelectionInfoBuilder', () => {
8790
text: 'C'
8891
}
8992
];
90-
const textInfo = extractTextInfoFromSelections(selections);
91-
assert.deepEqual(textInfo.text, 'C\nB\nA');
93+
const editor = createEditorWithSelections(selections);
94+
assert.deepEqual(editor.selectedText, 'C\nB\nA');
9295
});
9396

9497
test('it extracts a file name from editor', () => {
95-
const textInfo = extractTextInfo(['SELECTED_TEXT']);
96-
assert.equal(textInfo.fileName, 'FILENAME');
98+
const editor = createEditorWithTexts(['SELECTED_TEXT']);
99+
assert.equal(editor.fileName, 'FILENAME');
97100
});
98101

99-
function extractTextInfo(selectedTexts: string[]) {
102+
function createEditorWithTexts(selectedTexts: string[]) {
100103
const selections = selectedTexts.map((text, i) => ({
101104
text,
102105
start: {line: `START_LINE_${i + 1}`},
103106
end: {line: `END_LINE_${i + 1}`}
104107
}));
105-
return extractTextInfoFromSelections(selections);
108+
return createEditorWithSelections(selections);
106109
}
107110

108-
function extractTextInfoFromSelections(selections: any[]) {
109-
const selectionInfoBuilder = new SelectionInfoBuilder();
111+
function createEditorWithSelections(selections: any[]) {
110112
const selectionWithIsEmptyFlag = selections.map(s =>
111113
Object.assign({}, s, {isEmpty: !s.text})
112114
);
113-
return selectionInfoBuilder.extract(fakeEditor(selectionWithIsEmptyFlag) as any);
115+
return new TextEditor(fakeEditor(selectionWithIsEmptyFlag) as any);
114116
}
115117

116118
function fakeEditor(selections: any[]) {
117-
return {
119+
return mockType<vscode.TextEditor>({
118120
selections,
119121
selection: selections[0],
120122
document: {
@@ -124,6 +126,6 @@ suite('SelectionInfoBuilder', () => {
124126
return selection ? selection.text : this._entireText;
125127
}
126128
}
127-
};
129+
});
128130
}
129131
});

src/test/lib/commands/compare-selection-with-text1.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import CommandFactory from '../../../lib/command-factory';
55
import NormalisationRuleStore from '../../../lib/normalisation-rule-store';
66
import CommandAdaptor from '../../../lib/adaptors/command';
77
import TextEditor from '../../../lib/adaptors/text-editor';
8+
import WindowComponent from '../../../lib/adaptors/window';
89

910
suite('CompareSelectionWithText1', () => {
1011

@@ -22,12 +23,13 @@ suite('CompareSelectionWithText1', () => {
2223
});
2324

2425
const fakeVscode = {window: mockType<typeof vscode.window>()};
26+
const windowComponent = mock(WindowComponent);
2527
const normalisationRuleStore = mock(NormalisationRuleStore);
2628

2729
test('it saves selected text and takes a diff of 2 texts', async () => {
2830

2931
const commandAdaptor = mock(CommandAdaptor);
30-
const commandFactory = new CommandFactory(selectionInfoRegistry, normalisationRuleStore, commandAdaptor, fakeVscode, () => new Date('2016-06-15T11:43:00Z'));
32+
const commandFactory = new CommandFactory(selectionInfoRegistry, normalisationRuleStore, commandAdaptor, windowComponent, fakeVscode, () => new Date('2016-06-15T11:43:00Z'));
3133
const command = commandFactory.createCompareSelectionWithText1Command();
3234

3335
await command.execute(editor);

0 commit comments

Comments
 (0)