Skip to content

Commit

Permalink
feat: support auto scroll to first conflict position
Browse files Browse the repository at this point in the history
  • Loading branch information
tjx666 committed Jul 8, 2023
1 parent cad5244 commit a88d48b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
import('./features/autoKeepTempEditor').then((mod) => mod.autoKeepTempEditor(context));
import('./features/autoScrollToFirstConflict').then((mod) =>
mod.autoScrollToFirstConflict(context),
);

const { commands } = vscode;

Expand Down
38 changes: 38 additions & 0 deletions src/features/autoScrollToFirstConflict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type { ExtensionContext, TextDocument, TextEditor } from 'vscode';
import vscode, { EndOfLine, TextEditorRevealType } from 'vscode';

export function autoScrollToFirstConflict(context: ExtensionContext) {
const revealedDocuments = new Set<TextDocument>();

const goToFirstConflict = (editor: TextEditor | undefined) => {
if (editor) {
const { document } = editor;
if (revealedDocuments.has(document)) return;

const text = document.getText();

const lineEnding = document.eol === EndOfLine.LF ? '\n' : '\r\n';
const currentChangeMark = `<<<<<<< HEAD${lineEnding}`;
const currentChangeIndex = text.indexOf(currentChangeMark);
if (currentChangeIndex === -1) return;

const incomingChangeMark = '>>>>>>> ';
const incomingChangeIndex = text.indexOf(incomingChangeMark, currentChangeIndex);
if (incomingChangeIndex === -1) return;

const start = document.positionAt(currentChangeIndex);
const end = document.positionAt(incomingChangeIndex + incomingChangeMark.length);
const range = new vscode.Range(start, end);
editor.revealRange(range, TextEditorRevealType.InCenter);
}
};

// If the file already open when startup vscode, will not trigger onDidChangeActiveTextEditor
goToFirstConflict(vscode.window.activeTextEditor);

vscode.window.onDidChangeActiveTextEditor(goToFirstConflict, null, context.subscriptions);

vscode.workspace.onDidCloseTextDocument((document) => {
revealedDocuments.delete(document);
});
}

0 comments on commit a88d48b

Please sign in to comment.