Skip to content

Commit aade704

Browse files
committed
ref #11 Keep the order of tabs on showing diff
1 parent 95bfed2 commit aade704

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

lib/commands/compare-visible-editors.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ class CompareVisibleEditorsCommand {
1717
this._messageBar.showInfo('Please first open 2 documents to compare.')
1818
return
1919
}
20-
const textInfo1 = this._selectionInfoBuilder.extract(editors[0])
21-
this._selectionInfoRegistry.set(TextKey.VISIBLE_EDITOR1, textInfo1)
2220

23-
const textInfo2 = this._selectionInfoBuilder.extract(editors[1])
24-
this._selectionInfoRegistry.set(TextKey.VISIBLE_EDITOR2, textInfo2)
21+
const textInfos = editors.map(editor =>
22+
this._selectionInfoBuilder.extract(editor)
23+
)
24+
this._registerTextInfo(
25+
textInfos,
26+
editors[0].viewColumn > editors[1].viewColumn
27+
)
2528

2629
await 'HACK' // HACK: Avoid "TextEditor has been disposed" error
2730
await this._diffPresenter.takeDiff(
@@ -33,6 +36,13 @@ class CompareVisibleEditorsCommand {
3336
}
3437
}
3538

39+
_registerTextInfo (textInfos, isReverseOrder) {
40+
const textInfo1 = textInfos[isReverseOrder ? 1 : 0]
41+
const textInfo2 = textInfos[isReverseOrder ? 0 : 1]
42+
this._selectionInfoRegistry.set(TextKey.VISIBLE_EDITOR1, textInfo1)
43+
this._selectionInfoRegistry.set(TextKey.VISIBLE_EDITOR2, textInfo2)
44+
}
45+
3646
_handleError (e) {
3747
this._logger.error(e.stack)
3848
}

test/lib/commands/compare-visible-editors.test.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,39 @@ const CompareVisibleEditorsCommand = require('../../../lib/commands/compare-visi
22
const td = require('testdouble')
33

44
suite('CompareVisibleEditorsCommand', () => {
5+
const editor1 = { viewColumn: 1 }
6+
const editor2 = { viewColumn: 2 }
7+
58
test('it compares 2 visible editors', async () => {
6-
const {command, deps} = createCommand(['EDITOR_1', 'EDITOR_2'])
9+
const { command, deps } = createCommand([editor1, editor2])
710
await command.execute()
811

912
td.verify(deps.selectionInfoRegistry.set('visible1', 'TEXT_INFO1'))
1013
td.verify(deps.selectionInfoRegistry.set('visible2', 'TEXT_INFO2'))
1114
td.verify(deps.diffPresenter.takeDiff('visible1', 'visible2'))
1215
})
1316

17+
test('it keeps the visual order of the editors when presents a diff', async () => {
18+
const { command, deps } = createCommand([editor2, editor1])
19+
await command.execute()
20+
21+
td.verify(deps.selectionInfoRegistry.set('visible1', 'TEXT_INFO1'))
22+
td.verify(deps.selectionInfoRegistry.set('visible2', 'TEXT_INFO2'))
23+
})
24+
1425
test('it tells you that it needs 2 visible editors', async () => {
15-
const {command, deps} = createCommand(['EDITOR_1'])
26+
const { command, deps } = createCommand([editor1])
1627
await command.execute()
1728

18-
td.verify(deps.messageBar.showInfo('Please first open 2 documents to compare.'))
29+
td.verify(
30+
deps.messageBar.showInfo('Please first open 2 documents to compare.')
31+
)
1932
})
2033

2134
function createCommand (visibleTextEditors) {
2235
const selectionInfoBuilder = td.object(['extract'])
23-
td.when(selectionInfoBuilder.extract('EDITOR_1')).thenReturn('TEXT_INFO1')
24-
td.when(selectionInfoBuilder.extract('EDITOR_2')).thenReturn('TEXT_INFO2')
36+
td.when(selectionInfoBuilder.extract(editor1)).thenReturn('TEXT_INFO1')
37+
td.when(selectionInfoBuilder.extract(editor2)).thenReturn('TEXT_INFO2')
2538

2639
const dependencies = {
2740
editorWindow: { visibleTextEditors },
@@ -31,6 +44,6 @@ suite('CompareVisibleEditorsCommand', () => {
3144
selectionInfoRegistry: td.object(['set'])
3245
}
3346
const command = new CompareVisibleEditorsCommand(dependencies)
34-
return {command, deps: dependencies}
47+
return { command, deps: dependencies }
3548
}
3649
})

0 commit comments

Comments
 (0)