Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 07b2a8e

Browse files
authored
Merge pull request #2532 from atom/jump-to-first-conflict
Scroll to first conflict when opening editor
2 parents c56f672 + 3c7e178 commit 07b2a8e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/controllers/editor-conflict-controller.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export default class EditorConflictController extends React.Component {
4848
this.props.editor.onDidDestroy(() => this.props.refreshResolutionProgress(this.props.editor.getPath())),
4949
buffer.onDidReload(() => this.reparseConflicts()),
5050
);
51+
52+
this.scrollToFirstConflict();
5153
}
5254

5355
render() {
@@ -219,6 +221,19 @@ export default class EditorConflictController extends React.Component {
219221
this.updateMarkerCount();
220222
}
221223

224+
scrollToFirstConflict() {
225+
let firstConflict = null;
226+
for (const conflict of this.state.conflicts) {
227+
if (firstConflict == null || firstConflict.getRange().compare(conflict.getRange()) > 0) {
228+
firstConflict = conflict;
229+
}
230+
}
231+
232+
if (firstConflict) {
233+
this.props.editor.scrollToBufferPosition(firstConflict.getRange().start, {center: true});
234+
}
235+
}
236+
222237
reparseConflicts() {
223238
const newConflicts = new Set(Conflict.allFromEditor(this.props.editor, this.layer, this.props.isRebase));
224239
this.setState({conflicts: newConflicts});

test/controllers/editor-conflict-controller.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import temp from 'temp';
33
import path from 'path';
44
import React from 'react';
55
import {mount} from 'enzyme';
6+
import {Point} from 'atom';
67

78
import ResolutionProgress from '../../lib/models/conflicts/resolution-progress';
89
import {OURS, BASE, THEIRS} from '../../lib/models/conflicts/source';
@@ -48,14 +49,17 @@ describe('EditorConflictController', function() {
4849
atomEnv.destroy();
4950
});
5051

51-
const useFixture = async function(fixtureName, {isRebase} = {isRebase: false}) {
52+
const useFixture = async function(fixtureName, {isRebase, withEditor} = {isRebase: false}) {
5253
const fixturePath = path.join(
5354
path.dirname(__filename), '..', 'fixtures', 'conflict-marker-examples', fixtureName);
5455
const tempDir = temp.mkdirSync('conflict-fixture-');
5556
fixtureFile = path.join(tempDir, fixtureName);
5657
fs.copySync(fixturePath, fixtureFile);
5758

5859
editor = await workspace.open(fixtureFile);
60+
if (withEditor) {
61+
withEditor(editor);
62+
}
5963
editorView = atomEnv.views.getView(editor);
6064

6165
app = (
@@ -75,6 +79,14 @@ describe('EditorConflictController', function() {
7579
return editor.getTextInBufferRange(side.marker.getBufferRange());
7680
};
7781

82+
it('scrolls the first conflict into view', async function() {
83+
await useFixture('triple-2way-diff.txt', {
84+
withEditor(e) { sinon.stub(e, 'scrollToBufferPosition'); },
85+
});
86+
87+
assert.isTrue(editor.scrollToBufferPosition.calledWith(new Point(4, 0), {center: true}));
88+
});
89+
7890
describe('on a file with 2-way diff markers', function() {
7991
let conflicts;
8092

0 commit comments

Comments
 (0)