Skip to content

Commit f596237

Browse files
authored
Implements diff review for diff editor v2 (microsoft#185746)
1 parent e7df224 commit f596237

File tree

7 files changed

+923
-68
lines changed

7 files changed

+923
-68
lines changed

src/vs/editor/browser/editorBrowser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,10 @@ export interface IDiffEditor extends editorCommon.IEditor {
12601260
* @internal
12611261
*/
12621262
revealFirstDiff(): unknown;
1263+
1264+
diffReviewNext(): void;
1265+
1266+
diffReviewPrev(): void;
12631267
}
12641268

12651269
/**

src/vs/editor/browser/widget/codeEditorContributions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
1010
import { EditorContributionInstantiation, IEditorContributionDescription } from 'vs/editor/browser/editorExtensions';
1111
import { IEditorContribution } from 'vs/editor/common/editorCommon';
1212
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
13+
import './diffEditor.contribution';
1314

1415
export class CodeEditorContributions extends Disposable {
1516

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
7+
import { ICodeEditor, IDiffEditor } from 'vs/editor/browser/editorBrowser';
8+
import { EditorAction, ServicesAccessor, registerEditorAction } from 'vs/editor/browser/editorExtensions';
9+
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
10+
import { localize } from 'vs/nls';
11+
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
12+
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
13+
14+
class DiffReviewNext extends EditorAction {
15+
constructor() {
16+
super({
17+
id: 'editor.action.diffReview.next',
18+
label: localize('editor.action.diffReview.next', "Go to Next Difference"),
19+
alias: 'Go to Next Difference',
20+
precondition: ContextKeyExpr.has('isInDiffEditor'),
21+
kbOpts: {
22+
kbExpr: null,
23+
primary: KeyCode.F7,
24+
weight: KeybindingWeight.EditorContrib
25+
}
26+
});
27+
}
28+
29+
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
30+
const diffEditor = findFocusedDiffEditor(accessor);
31+
diffEditor?.diffReviewNext();
32+
}
33+
}
34+
35+
class DiffReviewPrev extends EditorAction {
36+
constructor() {
37+
super({
38+
id: 'editor.action.diffReview.prev',
39+
label: localize('editor.action.diffReview.prev', "Go to Previous Difference"),
40+
alias: 'Go to Previous Difference',
41+
precondition: ContextKeyExpr.has('isInDiffEditor'),
42+
kbOpts: {
43+
kbExpr: null,
44+
primary: KeyMod.Shift | KeyCode.F7,
45+
weight: KeybindingWeight.EditorContrib
46+
}
47+
});
48+
}
49+
50+
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
51+
const diffEditor = findFocusedDiffEditor(accessor);
52+
diffEditor?.diffReviewPrev();
53+
}
54+
}
55+
56+
function findFocusedDiffEditor(accessor: ServicesAccessor): IDiffEditor | null {
57+
const codeEditorService = accessor.get(ICodeEditorService);
58+
const diffEditors = codeEditorService.listDiffEditors();
59+
const activeCodeEditor = codeEditorService.getFocusedCodeEditor() ?? codeEditorService.getActiveCodeEditor();
60+
if (!activeCodeEditor) {
61+
return null;
62+
}
63+
64+
for (let i = 0, len = diffEditors.length; i < len; i++) {
65+
const diffEditor = <IDiffEditor>diffEditors[i];
66+
if (diffEditor.getModifiedEditor().getId() === activeCodeEditor.getId() || diffEditor.getOriginalEditor().getId() === activeCodeEditor.getId()) {
67+
return diffEditor;
68+
}
69+
}
70+
return null;
71+
}
72+
73+
registerEditorAction(DiffReviewNext);
74+
registerEditorAction(DiffReviewPrev);

src/vs/editor/browser/widget/diffEditorWidget2/diffEditorWidget2.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { Range } from 'vs/editor/common/core/range';
4444
import { LineRangeMapping } from 'vs/editor/common/diff/linesDiffComputer';
4545
import { deepClone } from 'vs/base/common/objects';
4646
import { autorunWithStore2 } from 'vs/base/common/observableImpl/autorun';
47+
import { DiffReview2 } from 'vs/editor/browser/widget/diffEditorWidget2/diffReview';
4748

4849
const diffEditorDefaultOptions: ValidDiffEditorBaseOptions = {
4950
enableSplitViewResizing: true,
@@ -93,6 +94,8 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
9394

9495
private unchangedRangesFeature!: UnchangedRangesFeature;
9596

97+
private readonly _reviewPane: DiffReview2;
98+
9699
constructor(
97100
private readonly _domElement: HTMLElement,
98101
options: Readonly<IDiffEditorConstructionOptions>,
@@ -177,6 +180,11 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
177180
this._renderOverviewRuler,
178181
));
179182

183+
this._reviewPane = this._register(this._instantiationService.createInstance(DiffReview2, this));
184+
this.elements.root.appendChild(this._reviewPane.domNode.domNode);
185+
this.elements.root.appendChild(this._reviewPane.shadow.domNode);
186+
this.elements.root.appendChild(this._reviewPane.actionBarContainer.domNode);
187+
180188
this._createDiffEditorContributions();
181189

182190
codeEditorService.addDiffEditor(this);
@@ -217,6 +225,7 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
217225
(this._renderOverviewRuler.read(reader) ? OverviewRulerPart.ENTIRE_DIFF_OVERVIEW_WIDTH : 0),
218226
height
219227
});
228+
this._reviewPane.layout(0, width, height);
220229

221230
return {
222231
modifiedEditor: this._modifiedEditor.getLayoutInfo(),
@@ -676,6 +685,13 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
676685
});
677686
}
678687

688+
public diffReviewNext(): void {
689+
this._reviewPane.next();
690+
}
691+
692+
public diffReviewPrev(): void {
693+
this._reviewPane.prev();
694+
}
679695

680696
public async waitForDiff(): Promise<void> {
681697
const diffModel = this._diffModel.get();

0 commit comments

Comments
 (0)