Merge Conflict Editor
Relevant source files
The following files were used as context for generating this wiki page:
The Merge Conflict extension for Visual Studio Code provides highlighting and a rich set of commands to streamline the process of resolving inline merge conflicts. It enhances the standard text editor by decorating conflict blocks, providing CodeLenses with actionable commands, and integrating with the SCM view.
This extension is designed to work seamlessly within both standard and virtual workspaces and is enabled by default in untrusted workspaces. It activates automatically on editor startup, ensuring that its features are available whenever a file with merge conflicts is opened.
Sources: extensions/merge-conflict/package.json, extensions/merge-conflict/package.nls.json
Core Features
The extension's functionality is built around a set of commands, UI integrations, and configurable settings that provide a comprehensive toolkit for handling merge conflicts.
Activation
The extension is activated when the onStartupFinished event is fired, meaning it is loaded and ready soon after VS Code starts, without waiting for a specific file or command to be invoked.
"activationEvents": [
"onStartupFinished"
]
Sources: extensions/merge-conflict/package.json:19-21
Commands
A suite of commands is contributed to handle various aspects of merge conflict resolution. These commands can be invoked from the Command Palette or through integrated UI elements like CodeLenses and context menus. All commands are enabled only when the context !isMergeEditor is true, indicating they are for the inline text-based conflict resolution experience.
| Command ID | Title | Description |
|---|---|---|
merge-conflict.accept.all-current | Accept All Current | Accepts all "current" changes in the file. |
merge-conflict.accept.all-incoming | Accept All Incoming | Accepts all "incoming" changes in the file. |
merge-conflict.accept.all-both | Accept All Both | Accepts both "current" and "incoming" changes for all conflicts in the file. |
merge-conflict.accept.current | Accept Current | Accepts the "current" change for the conflict block at the cursor's position. |
merge-conflict.accept.incoming | Accept Incoming | Accepts the "incoming" change for the conflict block at the cursor's position. |
merge-conflict.accept.selection | Accept Selection | Accepts the currently selected lines within a conflict block as the resolution. |
merge-conflict.accept.both | Accept Both | Accepts both "current" and "incoming" changes for the conflict block at the cursor's position. |
merge-conflict.next | Next Conflict | Navigates the cursor to the next merge conflict in the file. |
merge-conflict.previous | Previous Conflict | Navigates the cursor to the previous merge conflict in the file. |
merge-conflict.compare | Compare Current Conflict | Opens a diff view to compare the "current" and "incoming" changes for the conflict at the cursor. |
Sources: extensions/merge-conflict/package.json:28-106, extensions/merge-conflict/package.nls.json:4-14
User Interface Integration
The extension's commands are exposed in the UI through editor CodeLenses, editor title bar icons, and context menus in the Source Control view.
Menu Contributions
This diagram illustrates where the merge conflict commands are placed within the VS Code UI.
- SCM Context Menu: When viewing files in the "Merge" group of the Git SCM provider, users can right-click to access
Accept All CurrentandAccept All Incoming. This is controlled by thewhenclausescmProvider == git && scmResourceGroup == merge. - Editor Title Bar: For easy navigation,
Next ConflictandPrevious Conflictbuttons appear in the editor's title bar when a file contains conflicts (mergeConflictsCount && mergeConflictsCount != 0).
Sources: extensions/merge-conflict/package.json:108-135
Visual Elements
The extension uses two primary visual aids within the editor, which can be toggled via configuration:
- CodeLens: Actionable links like "Accept Current" are displayed directly above each conflict block.
- Decorators: The "current" and "incoming" blocks are highlighted with distinct background colors and borders for easy identification.
The visual appearance of these decorators is derived from syntax highlighting rules, similar to how diff files are rendered. The underlying text markers (<<<<<<<, =======, >>>>>>>) and the content within are assigned specific scopes that themes can target.
Sources: extensions/merge-conflict/package.json:141-149, extensions/merge-conflict/package.nls.json:17-18, extensions/diff/syntaxes/diff.tmLanguage.json
Configuration
Users can customize the behavior of the merge conflict extension through several settings.
| Setting | Type | Default | Description |
|---|---|---|---|
merge-conflict.codeLens.enabled | boolean | true | Enables or disables the CodeLens for merge conflict blocks. |
merge-conflict.decorators.enabled | boolean | true | Enables or disables decorators for merge conflict blocks. |
merge-conflict.autoNavigateNextConflict.enabled | boolean | false | If true, automatically navigates to the next conflict after one is resolved. |
merge-conflict.diffViewPosition | string | Current | Controls where the diff view opens: Current, Beside, or Below. |
Sources: extensions/merge-conflict/package.json:137-169, extensions/merge-conflict/package.nls.json:15-22
Syntax Highlighting
While the merge conflict extension manages the logic, the visual highlighting of conflict blocks relies on the editor's text-mate grammar engine. The markers and content within a conflict are parsed and assigned scopes, which are then colored by the active theme. This process is analogous to how diff files are highlighted.
The source.diff grammar provides scopes for inserted and deleted lines, which are fundamental to representing merge conflicts.
markup.inserted.diff: Used for added lines, typically prefixed with+. In a merge conflict, this corresponds to "incoming" changes.markup.deleted.diff: Used for removed lines, typically prefixed with-. In a merge conflict, this corresponds to "current" changes.meta.diff.header: Used for header lines like--- a/file.jsand+++ b/file.js.
This mechanism is also seen in other contexts, like git-commit messages, which can embed diffs.
Sources: extensions/diff/package.json, extensions/diff/syntaxes/diff.tmLanguage.json:79-105, extensions/git-base/syntaxes/git-commit.tmLanguage.json:49-68