generated from tjx666/awesome-vscode-extension-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support auto scroll to first conflict position
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |