Skip to content

Commit

Permalink
Use DiffEditor DiffNavigator to navigate diffs (#14889)
Browse files Browse the repository at this point in the history
Enables previous/next change for all Monaco diff editors
  • Loading branch information
colin-grant-work authored Feb 18, 2025
1 parent 15f9c39 commit 55ff28b
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions packages/scm/src/browser/scm-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ import { ScmCommand } from './scm-provider';
import { ScmDecorationsService } from '../browser/decorations/scm-decorations-service';
import { nls } from '@theia/core/lib/common/nls';
import { isHighContrast } from '@theia/core/lib/common/theme';
import { EditorMainMenu } from '@theia/editor/lib/browser';
import { EditorMainMenu, EditorWidget } from '@theia/editor/lib/browser';
import { DirtyDiffNavigator } from './dirty-diff/dirty-diff-navigator';
import { MonacoDiffEditor } from '@theia/monaco/lib/browser/monaco-diff-editor';

export const SCM_WIDGET_FACTORY_ID = ScmWidget.ID;
export const SCM_VIEW_CONTAINER_ID = 'scm-view-container';
Expand Down Expand Up @@ -95,12 +96,14 @@ export namespace SCM_COMMANDS {
export const GOTO_NEXT_CHANGE = Command.toDefaultLocalizedCommand({
id: 'workbench.action.editor.nextChange',
category: 'Source Control',
label: 'Go to Next Change'
label: 'Go to Next Change',
iconClass: codicon('arrow-down')
});
export const GOTO_PREVIOUS_CHANGE = Command.toDefaultLocalizedCommand({
id: 'workbench.action.editor.previousChange',
category: 'Source Control',
label: 'Go to Previous Change'
label: 'Go to Previous Change',
iconClass: codicon('arrow-up')
});
export const SHOW_NEXT_CHANGE = Command.toDefaultLocalizedCommand({
id: 'editor.action.dirtydiff.next',
Expand Down Expand Up @@ -198,10 +201,34 @@ export class ScmContribution extends AbstractViewContribution<ScmWidget> impleme
// This is consistent with behavior in VS Code, and also with other similar commands (such as `Next Problem/Previous Problem`) in Theia.
// See https://github.com/eclipse-theia/theia/pull/13104#discussion_r1497316614 for a detailed discussion.
commandRegistry.registerCommand(SCM_COMMANDS.GOTO_NEXT_CHANGE, {
execute: () => this.dirtyDiffNavigator.gotoNextChange()
execute: widget => {
if (widget instanceof EditorWidget && widget.editor instanceof MonacoDiffEditor) {
widget.editor.diffNavigator.next();
} else {
this.dirtyDiffNavigator.gotoNextChange();
}
},
isEnabled: widget => {
if (widget instanceof EditorWidget && widget.editor instanceof MonacoDiffEditor) {
return widget.editor.diffNavigator.hasNext();
}
return true;
}
});
commandRegistry.registerCommand(SCM_COMMANDS.GOTO_PREVIOUS_CHANGE, {
execute: () => this.dirtyDiffNavigator.gotoPreviousChange()
execute: widget => {
if (widget instanceof EditorWidget && widget.editor instanceof MonacoDiffEditor) {
widget.editor.diffNavigator.previous();
} else {
this.dirtyDiffNavigator.gotoPreviousChange();
}
},
isEnabled: widget => {
if (widget instanceof EditorWidget && widget.editor instanceof MonacoDiffEditor) {
return widget.editor.diffNavigator.hasPrevious();
}
return true;
}
});
commandRegistry.registerCommand(SCM_COMMANDS.SHOW_NEXT_CHANGE, {
execute: () => this.dirtyDiffNavigator.showNextChange()
Expand Down Expand Up @@ -272,6 +299,18 @@ export class ScmContribution extends AbstractViewContribution<ScmWidget> impleme
}
});

registry.registerItem({
id: SCM_COMMANDS.GOTO_PREVIOUS_CHANGE.id,
command: SCM_COMMANDS.GOTO_PREVIOUS_CHANGE.id,
isVisible: widget => widget instanceof EditorWidget && widget.editor instanceof MonacoDiffEditor,
});

registry.registerItem({
id: SCM_COMMANDS.GOTO_NEXT_CHANGE.id,
command: SCM_COMMANDS.GOTO_NEXT_CHANGE.id,
isVisible: widget => widget instanceof EditorWidget && widget.editor instanceof MonacoDiffEditor,
});

registry.registerItem({
...SCM_COMMANDS.COLLAPSE_ALL,
command: SCM_COMMANDS.COLLAPSE_ALL.id
Expand Down

0 comments on commit 55ff28b

Please sign in to comment.