Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shortcuts to compare incoming/current with common ancestor #236491

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions extensions/merge-conflict/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@
"original": "Compare Current Conflict",
"command": "merge-conflict.compare",
"enablement": "!isMergeEditor"
},
{
"category": "%command.category%",
"title": "%command.compare-ancestor-current%",
"original": "Compare Current Conflict (common ancestor ↔ current)",
"command": "merge-conflict.compare-ancestor-current",
"enablement": "!isMergeEditor"
},
{
"category": "%command.category%",
"title": "%command.compare-ancestor-incoming%",
"original": "Compare Current Conflict (common ancestor ↔ incoming)",
"command": "merge-conflict.compare-ancestor-incoming",
"enablement": "!isMergeEditor"
}
],
"menus": {
Expand Down
2 changes: 2 additions & 0 deletions extensions/merge-conflict/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"command.next": "Next Conflict",
"command.previous": "Previous Conflict",
"command.compare": "Compare Current Conflict",
"command.compare-ancestor-current": "Compare Current Conflict (common ancestor ↔ current)",
"command.compare-ancestor-incoming": "Compare Current Conflict (common ancestor ↔ incoming)",
"config.title": "Merge Conflict",
"config.autoNavigateNextConflictEnabled": "Whether to automatically navigate to the next merge conflict after resolving a merge conflict.",
"config.codeLensEnabled": "Create a CodeLens for merge conflict blocks within editor.",
Expand Down
33 changes: 29 additions & 4 deletions extensions/merge-conflict/src/codelensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,31 @@ export default class MergeConflictCodeLensProvider implements vscode.CodeLensPro
conflicts.forEach(conflict => {
const acceptCurrentCommand: vscode.Command = {
command: 'merge-conflict.accept.current',
title: vscode.l10n.t("Accept Current Change"),
title: vscode.l10n.t("Accept Current"),
arguments: ['known-conflict', conflict]
};

const acceptIncomingCommand: vscode.Command = {
command: 'merge-conflict.accept.incoming',
title: vscode.l10n.t("Accept Incoming Change"),
title: vscode.l10n.t("Accept Incoming"),
arguments: ['known-conflict', conflict]
};

const acceptBothCommand: vscode.Command = {
command: 'merge-conflict.accept.both',
title: vscode.l10n.t("Accept Both Changes"),
title: vscode.l10n.t("Accept Both"),
arguments: ['known-conflict', conflict]
};

// Only allow comparing with the common ancestor if there is a single
// one. This is the common case with diff3/zdiff3.
const hasSingleCommonAncestor = conflict.commonAncestors.length === 1;

const diffCommand: vscode.Command = {
command: 'merge-conflict.compare',
title: vscode.l10n.t("Compare Changes"),
title: hasSingleCommonAncestor
? vscode.l10n.t("Compare Current & Incoming")
: vscode.l10n.t("Compare Changes"),
arguments: [conflict]
};

Expand All @@ -92,6 +98,25 @@ export default class MergeConflictCodeLensProvider implements vscode.CodeLensPro
new vscode.CodeLens(range, acceptBothCommand),
new vscode.CodeLens(range, diffCommand)
);

if (hasSingleCommonAncestor) {
const diffBaseCurrentCommand: vscode.Command = {
command: 'merge-conflict.compare-ancestor-current',
title: vscode.l10n.t("Compare Ancestor & Current"),
arguments: [conflict]
};

const diffBaseIncomingCommand: vscode.Command = {
command: 'merge-conflict.compare-ancestor-incoming',
title: vscode.l10n.t("Compare Ancestor & Incoming"),
arguments: [conflict]
};

items.push(
new vscode.CodeLens(range, diffBaseCurrentCommand),
new vscode.CodeLens(range, diffBaseIncomingCommand),
);
}
});

return items;
Expand Down
70 changes: 60 additions & 10 deletions extensions/merge-conflict/src/commandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export default class CommandHandler implements vscode.Disposable {
this.registerTextEditorCommand('merge-conflict.accept.all-both', this.acceptAllBoth),
this.registerTextEditorCommand('merge-conflict.next', this.navigateNext),
this.registerTextEditorCommand('merge-conflict.previous', this.navigatePrevious),
this.registerTextEditorCommand('merge-conflict.compare', this.compare)
this.registerTextEditorCommand('merge-conflict.compare', this.compareCurrentIncoming),
this.registerTextEditorCommand('merge-conflict.compare-ancestor-current', this.compareAncestorCurrent),
this.registerTextEditorCommand('merge-conflict.compare-ancestor-incoming', this.compareAncestorIncoming),
);
}

Expand Down Expand Up @@ -82,7 +84,14 @@ export default class CommandHandler implements vscode.Disposable {
return this.acceptAll(interfaces.CommitType.Both, editor);
}

async compare(editor: vscode.TextEditor, conflict: interfaces.IDocumentMergeConflict | null) {
private async compare(
editor: vscode.TextEditor,
conflict: interfaces.IDocumentMergeConflict | null,
leftTitle: string,
leftRegion: (conflict: interfaces.IDocumentMergeConflict) => interfaces.IMergeRegion,
rightTitle: string,
rightRegion: (conflict: interfaces.IDocumentMergeConflict) => interfaces.IMergeRegion,
) {

// No conflict, command executed from command palette
if (!conflict) {
Expand All @@ -104,18 +113,18 @@ export default class CommandHandler implements vscode.Disposable {
}

const scheme = editor.document.uri.scheme;
let range = conflict.current.content;
const leftRanges = conflicts.map(conflict => [conflict.current.content, conflict.range]);
const rightRanges = conflicts.map(conflict => [conflict.incoming.content, conflict.range]);
const leftRanges = conflicts.map(conflict => [leftRegion(conflict)?.content, conflict.range]);
const rightRanges = conflicts.map(conflict => [rightRegion(conflict)?.content, conflict.range]);

const leftUri = editor.document.uri.with({
scheme: ContentProvider.scheme,
query: JSON.stringify({ scheme, range: range, ranges: leftRanges })
query: JSON.stringify({ scheme, ranges: leftRanges })
});


range = conflict.incoming.content;
const rightUri = leftUri.with({ query: JSON.stringify({ scheme, ranges: rightRanges }) });
const rightUri = editor.document.uri.with({
scheme: ContentProvider.scheme,
query: JSON.stringify({ scheme, ranges: rightRanges })
});

let mergeConflictLineOffsets = 0;
for (const nextconflict of conflicts) {
Expand All @@ -132,7 +141,7 @@ export default class CommandHandler implements vscode.Disposable {

const docPath = editor.document.uri.path;
const fileName = docPath.substring(docPath.lastIndexOf('/') + 1); // avoid NodeJS path to keep browser webpack small
const title = vscode.l10n.t("{0}: Current Changes ↔ Incoming Changes", fileName);
const title = `${fileName}: ${leftTitle} ↔ ${rightTitle}`;
const mergeConflictConfig = vscode.workspace.getConfiguration('merge-conflict');
const openToTheSide = mergeConflictConfig.get<string>('diffViewPosition');
const opts: vscode.TextDocumentShowOptions = {
Expand All @@ -147,6 +156,47 @@ export default class CommandHandler implements vscode.Disposable {
await vscode.commands.executeCommand('vscode.diff', leftUri, rightUri, title, opts);
}

compareCurrentIncoming(editor: vscode.TextEditor, conflict: interfaces.IDocumentMergeConflict | null) {
return this.compare(
editor,
conflict,
vscode.l10n.t("Current Changes"),
(conflict: interfaces.IDocumentMergeConflict) => conflict.current,
vscode.l10n.t("Incoming Changes"),
(conflict: interfaces.IDocumentMergeConflict) => conflict.incoming,
);
}

compareAncestorCurrent(editor: vscode.TextEditor, conflict: interfaces.IDocumentMergeConflict | null) {
return this.compare(
editor,
conflict,
vscode.l10n.t("Common Ancestor"),
// Fall back to not showing any changes if there's no common ancestor.
// This can happen if the conflict file is broken, or they use the
// command palette to run this command on a file without common
// ancestors markers.
(conflict: interfaces.IDocumentMergeConflict) => conflict.commonAncestors[0] || conflict.current,
vscode.l10n.t("Current Changes"),
(conflict: interfaces.IDocumentMergeConflict) => conflict.current,
);
}

compareAncestorIncoming(editor: vscode.TextEditor, conflict: interfaces.IDocumentMergeConflict | null) {
return this.compare(
editor,
conflict,
vscode.l10n.t("Common Ancestor"),
// Fall back to not showing any changes if there's no common ancestor.
// This can happen if the conflict file is broken, or they use the
// command palette to run this command on a file without common
// ancestors markers.
(conflict: interfaces.IDocumentMergeConflict) => conflict.commonAncestors[0] || conflict.incoming,
vscode.l10n.t("Incoming Changes"),
(conflict: interfaces.IDocumentMergeConflict) => conflict.incoming,
);
}

navigateNext(editor: vscode.TextEditor): Promise<void> {
return this.navigate(editor, NavigationDirection.Forwards);
}
Expand Down