Skip to content

Commit eeed165

Browse files
authored
Refactors diff editor helper (microsoft#188205)
1 parent e98bfca commit eeed165

File tree

1 file changed

+30
-67
lines changed

1 file changed

+30
-67
lines changed

src/vs/workbench/contrib/codeEditor/browser/diffEditorHelper.ts

Lines changed: 30 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import { registerDiffEditorContribution } from 'vs/editor/browser/editorExtensio
99
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
1010
import { DiffReviewNext, DiffReviewPrev } from 'vs/editor/browser/widget/diffEditor.contribution';
1111
import { DiffEditorWidget2 } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorWidget2';
12-
import { EmbeddedDiffEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
13-
import { IDiffComputationResult } from 'vs/editor/common/diff/smartLinesDiffComputer';
12+
import { EmbeddedDiffEditorWidget, EmbeddedDiffEditorWidget2 } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
1413
import { IDiffEditorContribution } from 'vs/editor/common/editorCommon';
15-
import * as nls from 'vs/nls';
1614
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1715
import { ContextKeyEqualsExpr, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1816
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
@@ -22,20 +20,13 @@ import { FloatingClickWidget } from 'vs/workbench/browser/codeeditor';
2220
import { AccessibilityHelpAction } from 'vs/workbench/contrib/accessibility/browser/accessibilityContribution';
2321
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2422
import { AccessibleViewType, IAccessibleViewService } from 'vs/workbench/contrib/accessibility/browser/accessibleView';
25-
26-
const enum WidgetState {
27-
Hidden,
28-
HintWhitespace
29-
}
23+
import { localize } from 'vs/nls';
24+
import { observableFromEvent } from 'vs/base/common/observable';
25+
import { autorunWithStore2 } from 'vs/base/common/observableImpl/autorun';
3026

3127
class DiffEditorHelperContribution extends Disposable implements IDiffEditorContribution {
32-
3328
public static readonly ID = 'editor.contrib.diffEditorHelper';
3429

35-
private _helperWidget: FloatingClickWidget | null;
36-
private _helperWidgetListener: IDisposable | null;
37-
private _state: WidgetState;
38-
3930
constructor(
4031
private readonly _diffEditor: IDiffEditor,
4132
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@@ -46,21 +37,36 @@ class DiffEditorHelperContribution extends Disposable implements IDiffEditorCont
4637

4738
this._register(createScreenReaderHelp());
4839

49-
this._helperWidget = null;
50-
this._helperWidgetListener = null;
51-
this._state = WidgetState.Hidden;
40+
const isEmbeddedDiffEditor = (this._diffEditor instanceof EmbeddedDiffEditorWidget) || (this._diffEditor instanceof EmbeddedDiffEditorWidget2);
41+
42+
if (!isEmbeddedDiffEditor) {
43+
const computationResult = observableFromEvent(e => this._diffEditor.onDidUpdateDiff(e), () => this._diffEditor.getDiffComputationResult());
44+
const onlyWhiteSpaceChange = computationResult.map(r => r && !r.identical && r.changes2.length === 0);
45+
46+
this._register(autorunWithStore2('update state', (reader, store) => {
47+
if (onlyWhiteSpaceChange.read(reader)) {
48+
const helperWidget = store.add(this._instantiationService.createInstance(
49+
FloatingClickWidget,
50+
this._diffEditor.getModifiedEditor(),
51+
localize('hintWhitespace', "Show Whitespace Differences"),
52+
null
53+
));
54+
store.add(helperWidget.onClick(() => {
55+
this._configurationService.updateValue('diffEditor.ignoreTrimWhitespace', false);
56+
}));
57+
helperWidget.render();
58+
}
59+
}));
5260

53-
if (!(this._diffEditor instanceof EmbeddedDiffEditorWidget)) {
5461
this._register(this._diffEditor.onDidUpdateDiff(() => {
5562
const diffComputationResult = this._diffEditor.getDiffComputationResult();
56-
this._setState(this._deduceState(diffComputationResult));
5763

5864
if (diffComputationResult && diffComputationResult.quitEarly) {
5965
this._notificationService.prompt(
6066
Severity.Warning,
61-
nls.localize('hintTimeout', "The diff algorithm was stopped early (after {0} ms.)", this._diffEditor.maxComputationTime),
67+
localize('hintTimeout', "The diff algorithm was stopped early (after {0} ms.)", this._diffEditor.maxComputationTime),
6268
[{
63-
label: nls.localize('removeTimeout', "Remove Limit"),
69+
label: localize('removeTimeout', "Remove Limit"),
6470
run: () => {
6571
this._configurationService.updateValue('diffEditor.maxComputationTime', 0);
6672
}
@@ -71,49 +77,6 @@ class DiffEditorHelperContribution extends Disposable implements IDiffEditorCont
7177
}));
7278
}
7379
}
74-
75-
private _deduceState(diffComputationResult: IDiffComputationResult | null): WidgetState {
76-
if (!diffComputationResult) {
77-
return WidgetState.Hidden;
78-
}
79-
if (this._diffEditor.ignoreTrimWhitespace && diffComputationResult.changes.length === 0 && !diffComputationResult.identical) {
80-
return WidgetState.HintWhitespace;
81-
}
82-
return WidgetState.Hidden;
83-
}
84-
85-
private _setState(newState: WidgetState) {
86-
if (this._state === newState) {
87-
return;
88-
}
89-
90-
this._state = newState;
91-
92-
if (this._helperWidgetListener) {
93-
this._helperWidgetListener.dispose();
94-
this._helperWidgetListener = null;
95-
}
96-
if (this._helperWidget) {
97-
this._helperWidget.dispose();
98-
this._helperWidget = null;
99-
}
100-
101-
if (this._state === WidgetState.HintWhitespace) {
102-
this._helperWidget = this._instantiationService.createInstance(FloatingClickWidget, this._diffEditor.getModifiedEditor(), nls.localize('hintWhitespace', "Show Whitespace Differences"), null);
103-
this._helperWidgetListener = this._helperWidget.onClick(() => this._onDidClickHelperWidget());
104-
this._helperWidget.render();
105-
}
106-
}
107-
108-
private _onDidClickHelperWidget(): void {
109-
if (this._state === WidgetState.HintWhitespace) {
110-
this._configurationService.updateValue('diffEditor.ignoreTrimWhitespace', false);
111-
}
112-
}
113-
114-
override dispose(): void {
115-
super.dispose();
116-
}
11780
}
11881

11982
function createScreenReaderHelp(): IDisposable {
@@ -140,14 +103,14 @@ function createScreenReaderHelp(): IDisposable {
140103
accessibleViewService.show({
141104
verbositySettingKey: 'diffEditor',
142105
provideContent: () => [
143-
nls.localize('msg1', "You are in a diff editor."),
144-
nls.localize('msg2', "Press {0} or {1} to view the next or previous diff in the diff review mode that is optimized for screen readers.", next, previous),
145-
nls.localize('msg3', "To control which audio cues should be played, the following settings can be configured: {0}.", keys.join(', ')),
106+
localize('msg1', "You are in a diff editor."),
107+
localize('msg2', "Press {0} or {1} to view the next or previous diff in the diff review mode that is optimized for screen readers.", next, previous),
108+
localize('msg3', "To control which audio cues should be played, the following settings can be configured: {0}.", keys.join(', ')),
146109
].join('\n'),
147110
onClose: () => {
148111
codeEditor.focus();
149112
},
150-
options: { type: AccessibleViewType.HelpMenu, ariaLabel: nls.localize('chat-help-label', "Diff editor accessibility help") }
113+
options: { type: AccessibleViewType.HelpMenu, ariaLabel: localize('chat-help-label', "Diff editor accessibility help") }
151114
});
152115
}, ContextKeyExpr.and(
153116
ContextKeyEqualsExpr.create('diffEditorVersion', 2),

0 commit comments

Comments
 (0)