diffEditorWidget.ts 76.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import 'vs/css!./media/diffEditor';
7
import * as nls from 'vs/nls';
J
Johannes Rieken 已提交
8
import { RunOnceScheduler } from 'vs/base/common/async';
A
Alex Dima 已提交
9
import { Disposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
10 11
import * as objects from 'vs/base/common/objects';
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
12
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
J
Joao Moreno 已提交
13
import { ISashEvent, IVerticalSashLayoutProvider, Sash, SashState } from 'vs/base/browser/ui/sash/sash';
J
Johannes Rieken 已提交
14 15
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
16
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
A
Alex Dima 已提交
17
import { Range, IRange } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
18
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
19
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
A
Alex Dima 已提交
20
import { LineDecoration } from 'vs/editor/common/viewLayout/lineDecorations';
A
Alex Dima 已提交
21
import { renderViewLine, RenderLineInput } from 'vs/editor/common/viewLayout/viewLineRenderer';
A
Alex Dima 已提交
22
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
A
Alex Dima 已提交
23
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
J
Johannes Rieken 已提交
24
import { Configuration } from 'vs/editor/browser/config/configuration';
A
Alex Dima 已提交
25
import { Position, IPosition } from 'vs/editor/common/core/position';
A
Alex Dima 已提交
26
import { Selection, ISelection } from 'vs/editor/common/core/selection';
27
import { InlineDecoration, InlineDecorationType, ViewLineRenderingData } from 'vs/editor/common/viewModel/viewModel';
J
Joao Moreno 已提交
28
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
M
Matt Bierner 已提交
29
import { Event, Emitter } from 'vs/base/common/event';
30
import * as editorOptions from 'vs/editor/common/config/editorOptions';
31
import { registerThemingParticipant, IThemeService, ITheme, getThemeTypeSelector } from 'vs/platform/theme/common/themeService';
32
import { scrollbarShadow, diffInserted, diffRemoved, defaultInsertColor, defaultRemoveColor, diffInsertedOutline, diffRemovedOutline, diffBorder } from 'vs/platform/theme/common/colorRegistry';
B
Benjamin Pasero 已提交
33
import { Color } from 'vs/base/common/color';
34 35
import { OverviewRulerZone } from 'vs/editor/common/view/overviewZoneManager';
import { IEditorWhitespace } from 'vs/editor/common/viewLayout/whitespaceComputer';
A
Alex Dima 已提交
36
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
B
Benjamin Pasero 已提交
37
import { DiffReview } from 'vs/editor/browser/widget/diffReview';
38
import { URI } from 'vs/base/common/uri';
39
import { IStringBuilder, createStringBuilder } from 'vs/editor/common/core/stringBuilder';
A
Alex Dima 已提交
40
import { IModelDeltaDecoration, IModelDecorationsChangeAccessor, ITextModel } from 'vs/editor/common/model';
41
import { INotificationService } from 'vs/platform/notification/common/notification';
A
Alex Dima 已提交
42
import { StableEditorScrollState } from 'vs/editor/browser/core/editorState';
E
Erich Gamma 已提交
43 44

interface IEditorDiffDecorations {
45
	decorations: IModelDeltaDecoration[];
A
Alex Dima 已提交
46
	overviewZones: OverviewRulerZone[];
E
Erich Gamma 已提交
47 48 49
}

interface IEditorDiffDecorationsWithZones extends IEditorDiffDecorations {
J
Johannes Rieken 已提交
50
	zones: editorBrowser.IViewZone[];
E
Erich Gamma 已提交
51 52 53
}

interface IEditorsDiffDecorationsWithZones {
J
Johannes Rieken 已提交
54 55
	original: IEditorDiffDecorationsWithZones;
	modified: IEditorDiffDecorationsWithZones;
E
Erich Gamma 已提交
56 57 58
}

interface IEditorsZones {
J
Johannes Rieken 已提交
59 60
	original: editorBrowser.IViewZone[];
	modified: editorBrowser.IViewZone[];
E
Erich Gamma 已提交
61 62 63
}

interface IDiffEditorWidgetStyle {
A
Alex Dima 已提交
64
	getEditorsDiffDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalWhitespaces: IEditorWhitespace[], modifiedWhitespaces: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsDiffDecorationsWithZones;
J
Johannes Rieken 已提交
65
	setEnableSplitViewResizing(enableSplitViewResizing: boolean): void;
66
	applyColors(theme: ITheme): boolean;
E
Erich Gamma 已提交
67 68 69 70 71
	layout(): number;
	dispose(): void;
}

class VisualEditorState {
J
Johannes Rieken 已提交
72 73 74
	private _zones: number[];
	private _zonesMap: { [zoneId: string]: boolean; };
	private _decorations: string[];
E
Erich Gamma 已提交
75 76 77 78 79 80 81

	constructor() {
		this._zones = [];
		this._zonesMap = {};
		this._decorations = [];
	}

A
Alex Dima 已提交
82
	public getForeignViewZones(allViewZones: IEditorWhitespace[]): IEditorWhitespace[] {
E
Erich Gamma 已提交
83 84 85
		return allViewZones.filter((z) => !this._zonesMap[String(z.id)]);
	}

A
Alex Dima 已提交
86
	public clean(editor: CodeEditorWidget): void {
E
Erich Gamma 已提交
87 88
		// (1) View zones
		if (this._zones.length > 0) {
J
Johannes Rieken 已提交
89
			editor.changeViewZones((viewChangeAccessor: editorBrowser.IViewZoneChangeAccessor) => {
A
Alex Dima 已提交
90
				for (let i = 0, length = this._zones.length; i < length; i++) {
E
Erich Gamma 已提交
91 92 93 94 95 96 97 98
					viewChangeAccessor.removeZone(this._zones[i]);
				}
			});
		}
		this._zones = [];
		this._zonesMap = {};

		// (2) Model decorations
A
Alex Dima 已提交
99
		this._decorations = editor.deltaDecorations(this._decorations, []);
E
Erich Gamma 已提交
100 101
	}

A
Alex Dima 已提交
102
	public apply(editor: CodeEditorWidget, overviewRuler: editorBrowser.IOverviewRuler, newDecorations: IEditorDiffDecorationsWithZones, restoreScrollState: boolean): void {
A
Alex Dima 已提交
103 104 105

		const scrollState = restoreScrollState ? StableEditorScrollState.capture(editor) : null;

E
Erich Gamma 已提交
106
		// view zones
J
Johannes Rieken 已提交
107
		editor.changeViewZones((viewChangeAccessor: editorBrowser.IViewZoneChangeAccessor) => {
A
Alex Dima 已提交
108
			for (let i = 0, length = this._zones.length; i < length; i++) {
E
Erich Gamma 已提交
109 110 111 112
				viewChangeAccessor.removeZone(this._zones[i]);
			}
			this._zones = [];
			this._zonesMap = {};
A
Alex Dima 已提交
113
			for (let i = 0, length = newDecorations.zones.length; i < length; i++) {
E
Erich Gamma 已提交
114
				newDecorations.zones[i].suppressMouseDown = true;
A
Alex Dima 已提交
115
				let zoneId = viewChangeAccessor.addZone(newDecorations.zones[i]);
E
Erich Gamma 已提交
116 117 118 119 120
				this._zones.push(zoneId);
				this._zonesMap[String(zoneId)] = true;
			}
		});

A
Alex Dima 已提交
121 122 123 124
		if (scrollState) {
			scrollState.restore(editor);
		}

E
Erich Gamma 已提交
125 126 127 128
		// decorations
		this._decorations = editor.deltaDecorations(this._decorations, newDecorations.decorations);

		// overview ruler
129 130 131
		if (overviewRuler) {
			overviewRuler.setZones(newDecorations.overviewZones);
		}
E
Erich Gamma 已提交
132 133 134
	}
}

A
Alex Dima 已提交
135
let DIFF_EDITOR_ID = 0;
E
Erich Gamma 已提交
136

A
Alex Dima 已提交
137
export class DiffEditorWidget extends Disposable implements editorBrowser.IDiffEditor {
A
Alex Dima 已提交
138

139
	private static readonly ONE_OVERVIEW_WIDTH = 15;
M
Matt Bierner 已提交
140
	public static readonly ENTIRE_DIFF_OVERVIEW_WIDTH = 30;
141
	private static readonly UPDATE_DIFF_DECORATIONS_DELAY = 200; // ms
E
Erich Gamma 已提交
142

A
Alex Dima 已提交
143 144
	private readonly _onDidDispose: Emitter<void> = this._register(new Emitter<void>());
	public readonly onDidDispose: Event<void> = this._onDidDispose.event;
E
Erich Gamma 已提交
145

A
Alex Dima 已提交
146 147 148 149
	private readonly _onDidUpdateDiff: Emitter<void> = this._register(new Emitter<void>());
	public readonly onDidUpdateDiff: Event<void> = this._onDidUpdateDiff.event;

	private readonly id: number;
E
Erich Gamma 已提交
150

J
Johannes Rieken 已提交
151
	private _domElement: HTMLElement;
A
Alex Dima 已提交
152 153 154
	protected readonly _containerDomElement: HTMLElement;
	private readonly _overviewDomElement: HTMLElement;
	private readonly _overviewViewportDomElement: FastDomNode<HTMLElement>;
E
Erich Gamma 已提交
155

J
Johannes Rieken 已提交
156 157
	private _width: number;
	private _height: number;
A
Alex Dima 已提交
158
	private _reviewHeight: number;
A
Alex Dima 已提交
159
	private readonly _measureDomElementToken: number;
E
Erich Gamma 已提交
160

A
Alex Dima 已提交
161
	private originalEditor: CodeEditorWidget;
J
Johannes Rieken 已提交
162 163 164
	private _originalDomNode: HTMLElement;
	private _originalEditorState: VisualEditorState;
	private _originalOverviewRuler: editorBrowser.IOverviewRuler;
E
Erich Gamma 已提交
165

A
Alex Dima 已提交
166
	private modifiedEditor: CodeEditorWidget;
J
Johannes Rieken 已提交
167 168 169
	private _modifiedDomNode: HTMLElement;
	private _modifiedEditorState: VisualEditorState;
	private _modifiedOverviewRuler: editorBrowser.IOverviewRuler;
E
Erich Gamma 已提交
170

J
Johannes Rieken 已提交
171 172 173
	private _currentlyChangingViewZones: boolean;
	private _beginUpdateDecorationsTimeout: number;
	private _diffComputationToken: number;
A
Alex Dima 已提交
174
	private _lineChanges: editorCommon.ILineChange[] | null;
E
Erich Gamma 已提交
175

J
Johannes Rieken 已提交
176 177
	private _isVisible: boolean;
	private _isHandlingScrollEvent: boolean;
E
Erich Gamma 已提交
178 179

	private _ignoreTrimWhitespace: boolean;
180
	private _originalIsEditable: boolean;
E
Erich Gamma 已提交
181

J
Johannes Rieken 已提交
182
	private _renderSideBySide: boolean;
183
	private _renderIndicators: boolean;
J
Johannes Rieken 已提交
184 185
	private _enableSplitViewResizing: boolean;
	private _strategy: IDiffEditorWidgetStyle;
E
Erich Gamma 已提交
186

J
Johannes Rieken 已提交
187
	private _updateDecorationsRunner: RunOnceScheduler;
E
Erich Gamma 已提交
188

189
	private _editorWorkerService: IEditorWorkerService;
190
	protected _contextKeyService: IContextKeyService;
191
	private _codeEditorService: ICodeEditorService;
192
	private _themeService: IThemeService;
193
	private _notificationService: INotificationService;
194

A
Alex Dima 已提交
195 196
	private _reviewPane: DiffReview;

197
	constructor(
J
Johannes Rieken 已提交
198
		domElement: HTMLElement,
199
		options: editorOptions.IDiffEditorOptions,
200
		@IEditorWorkerService editorWorkerService: IEditorWorkerService,
201
		@IContextKeyService contextKeyService: IContextKeyService,
202
		@IInstantiationService instantiationService: IInstantiationService,
203
		@ICodeEditorService codeEditorService: ICodeEditorService,
204
		@IThemeService themeService: IThemeService,
205
		@INotificationService notificationService: INotificationService
206
	) {
E
Erich Gamma 已提交
207
		super();
A
Alex Dima 已提交
208

209
		this._editorWorkerService = editorWorkerService;
210
		this._codeEditorService = codeEditorService;
211
		this._contextKeyService = this._register(contextKeyService.createScoped(domElement));
J
Joao Moreno 已提交
212
		this._contextKeyService.createKey('isInDiffEditor', true);
213
		this._themeService = themeService;
214
		this._notificationService = notificationService;
E
Erich Gamma 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

		this.id = (++DIFF_EDITOR_ID);

		this._domElement = domElement;
		options = options || {};

		// renderSideBySide
		this._renderSideBySide = true;
		if (typeof options.renderSideBySide !== 'undefined') {
			this._renderSideBySide = options.renderSideBySide;
		}

		// ignoreTrimWhitespace
		this._ignoreTrimWhitespace = true;
		if (typeof options.ignoreTrimWhitespace !== 'undefined') {
			this._ignoreTrimWhitespace = options.ignoreTrimWhitespace;
		}

233 234 235 236 237 238
		// renderIndicators
		this._renderIndicators = true;
		if (typeof options.renderIndicators !== 'undefined') {
			this._renderIndicators = options.renderIndicators;
		}

239 240 241 242 243
		this._originalIsEditable = false;
		if (typeof options.originalEditable !== 'undefined') {
			this._originalIsEditable = Boolean(options.originalEditable);
		}

A
Alex Dima 已提交
244
		this._updateDecorationsRunner = this._register(new RunOnceScheduler(() => this._updateDecorations(), 0));
E
Erich Gamma 已提交
245 246

		this._containerDomElement = document.createElement('div');
247
		this._containerDomElement.className = DiffEditorWidget._getClassName(this._themeService.getTheme(), this._renderSideBySide);
E
Erich Gamma 已提交
248 249 250 251
		this._containerDomElement.style.position = 'relative';
		this._containerDomElement.style.height = '100%';
		this._domElement.appendChild(this._containerDomElement);

A
Alex Dima 已提交
252 253 254
		this._overviewViewportDomElement = createFastDomNode(document.createElement('div'));
		this._overviewViewportDomElement.setClassName('diffViewport');
		this._overviewViewportDomElement.setPosition('absolute');
E
Erich Gamma 已提交
255 256 257 258 259

		this._overviewDomElement = document.createElement('div');
		this._overviewDomElement.className = 'diffOverview';
		this._overviewDomElement.style.position = 'absolute';

A
Alex Dima 已提交
260
		this._overviewDomElement.appendChild(this._overviewViewportDomElement.domNode);
E
Erich Gamma 已提交
261

262
		this._register(dom.addStandardDisposableListener(this._overviewDomElement, 'mousedown', (e) => {
E
Erich Gamma 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			this.modifiedEditor.delegateVerticalScrollbarMouseDown(e);
		}));
		this._containerDomElement.appendChild(this._overviewDomElement);

		this._createLeftHandSide();
		this._createRightHandSide();

		this._beginUpdateDecorationsTimeout = -1;
		this._currentlyChangingViewZones = false;
		this._diffComputationToken = 0;

		this._originalEditorState = new VisualEditorState();
		this._modifiedEditorState = new VisualEditorState();

		this._isVisible = true;
		this._isHandlingScrollEvent = false;

		this._width = 0;
		this._height = 0;
A
Alex Dima 已提交
282
		this._reviewHeight = 0;
E
Erich Gamma 已提交
283 284 285

		this._lineChanges = null;

286 287
		const leftContextKeyService = this._contextKeyService.createScoped();
		leftContextKeyService.createKey('isInDiffLeftEditor', true);
J
Joao Moreno 已提交
288

289 290 291
		const leftServices = new ServiceCollection();
		leftServices.set(IContextKeyService, leftContextKeyService);
		const leftScopedInstantiationService = instantiationService.createChild(leftServices);
J
Joao Moreno 已提交
292

293 294 295 296 297 298 299 300 301
		const rightContextKeyService = this._contextKeyService.createScoped();
		rightContextKeyService.createKey('isInDiffRightEditor', true);

		const rightServices = new ServiceCollection();
		rightServices.set(IContextKeyService, rightContextKeyService);
		const rightScopedInstantiationService = instantiationService.createChild(rightServices);

		this._createLeftHandSideEditor(options, leftScopedInstantiationService);
		this._createRightHandSideEditor(options, rightScopedInstantiationService);
E
Erich Gamma 已提交
302

303 304 305
		this._reviewPane = new DiffReview(this);
		this._containerDomElement.appendChild(this._reviewPane.domNode.domNode);
		this._containerDomElement.appendChild(this._reviewPane.shadow.domNode);
306
		this._containerDomElement.appendChild(this._reviewPane.actionBarContainer.domNode);
307

E
Erich Gamma 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		if (options.automaticLayout) {
			this._measureDomElementToken = window.setInterval(() => this._measureDomElement(false), 100);
		}

		// enableSplitViewResizing
		this._enableSplitViewResizing = true;
		if (typeof options.enableSplitViewResizing !== 'undefined') {
			this._enableSplitViewResizing = options.enableSplitViewResizing;
		}

		if (this._renderSideBySide) {
			this._setStrategy(new DiffEdtorWidgetSideBySide(this._createDataSource(), this._enableSplitViewResizing));
		} else {
			this._setStrategy(new DiffEdtorWidgetInline(this._createDataSource(), this._enableSplitViewResizing));
		}
323

324
		this._register(themeService.onThemeChange(t => {
325 326 327
			if (this._strategy && this._strategy.applyColors(t)) {
				this._updateDecorationsRunner.schedule();
			}
328 329
			this._containerDomElement.className = DiffEditorWidget._getClassName(this._themeService.getTheme(), this._renderSideBySide);
		}));
330 331

		this._codeEditorService.addDiffEditor(this);
E
Erich Gamma 已提交
332 333
	}

334 335 336 337 338 339 340 341
	public get ignoreTrimWhitespace(): boolean {
		return this._ignoreTrimWhitespace;
	}

	public get renderSideBySide(): boolean {
		return this._renderSideBySide;
	}

342 343 344 345
	public get renderIndicators(): boolean {
		return this._renderIndicators;
	}

A
Alex Dima 已提交
346 347 348 349 350 351 352 353 354 355 356 357
	public hasWidgetFocus(): boolean {
		return dom.isAncestor(document.activeElement, this._domElement);
	}

	public diffReviewNext(): void {
		this._reviewPane.next();
	}

	public diffReviewPrev(): void {
		this._reviewPane.prev();
	}

358
	private static _getClassName(theme: ITheme, renderSideBySide: boolean): string {
A
Alex Dima 已提交
359
		let result = 'monaco-diff-editor monaco-editor-background ';
E
Erich Gamma 已提交
360 361 362
		if (renderSideBySide) {
			result += 'side-by-side ';
		}
363
		result += getThemeTypeSelector(theme.type);
E
Erich Gamma 已提交
364 365 366 367 368 369 370 371
		return result;
	}

	private _recreateOverviewRulers(): void {
		if (this._originalOverviewRuler) {
			this._overviewDomElement.removeChild(this._originalOverviewRuler.getDomNode());
			this._originalOverviewRuler.dispose();
		}
372
		this._originalOverviewRuler = this.originalEditor.createOverviewRuler('original diffOverviewRuler');
E
Erich Gamma 已提交
373 374 375 376 377 378
		this._overviewDomElement.appendChild(this._originalOverviewRuler.getDomNode());

		if (this._modifiedOverviewRuler) {
			this._overviewDomElement.removeChild(this._modifiedOverviewRuler.getDomNode());
			this._modifiedOverviewRuler.dispose();
		}
379
		this._modifiedOverviewRuler = this.modifiedEditor.createOverviewRuler('modified diffOverviewRuler');
E
Erich Gamma 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
		this._overviewDomElement.appendChild(this._modifiedOverviewRuler.getDomNode());

		this._layoutOverviewRulers();
	}

	private _createLeftHandSide(): void {
		this._originalDomNode = document.createElement('div');
		this._originalDomNode.className = 'editor original';
		this._originalDomNode.style.position = 'absolute';
		this._originalDomNode.style.height = '100%';
		this._containerDomElement.appendChild(this._originalDomNode);
	}

	private _createRightHandSide(): void {
		this._modifiedDomNode = document.createElement('div');
		this._modifiedDomNode.className = 'editor modified';
		this._modifiedDomNode.style.position = 'absolute';
		this._modifiedDomNode.style.height = '100%';
		this._containerDomElement.appendChild(this._modifiedDomNode);
	}

401
	private _createLeftHandSideEditor(options: editorOptions.IDiffEditorOptions, instantiationService: IInstantiationService): void {
A
Alex Dima 已提交
402
		this.originalEditor = this._createInnerEditor(instantiationService, this._originalDomNode, this._adjustOptionsForLeftHandSide(options, this._originalIsEditable));
A
Alex Dima 已提交
403

A
Alex Dima 已提交
404
		this._register(this.originalEditor.onDidScrollChange((e) => {
A
Alex Dima 已提交
405 406 407
			if (this._isHandlingScrollEvent) {
				return;
			}
408
			if (!e.scrollTopChanged && !e.scrollLeftChanged && !e.scrollHeightChanged) {
A
Alex Dima 已提交
409 410 411 412 413 414 415 416
				return;
			}
			this._isHandlingScrollEvent = true;
			this.modifiedEditor.setScrollPosition({
				scrollLeft: e.scrollLeft,
				scrollTop: e.scrollTop
			});
			this._isHandlingScrollEvent = false;
417 418

			this._layoutOverviewViewport();
A
Alex Dima 已提交
419 420
		}));

A
Alex Dima 已提交
421
		this._register(this.originalEditor.onDidChangeViewZones(() => {
A
Alex Dima 已提交
422 423 424
			this._onViewZonesChanged();
		}));

A
Alex Dima 已提交
425
		this._register(this.originalEditor.onDidChangeModelContent(() => {
A
Alex Dima 已提交
426 427 428 429
			if (this._isVisible) {
				this._beginUpdateDecorationsSoon();
			}
		}));
E
Erich Gamma 已提交
430 431
	}

432
	private _createRightHandSideEditor(options: editorOptions.IDiffEditorOptions, instantiationService: IInstantiationService): void {
A
Alex Dima 已提交
433
		this.modifiedEditor = this._createInnerEditor(instantiationService, this._modifiedDomNode, this._adjustOptionsForRightHandSide(options));
A
Alex Dima 已提交
434

A
Alex Dima 已提交
435
		this._register(this.modifiedEditor.onDidScrollChange((e) => {
A
Alex Dima 已提交
436 437 438
			if (this._isHandlingScrollEvent) {
				return;
			}
439
			if (!e.scrollTopChanged && !e.scrollLeftChanged && !e.scrollHeightChanged) {
A
Alex Dima 已提交
440 441 442 443 444 445 446 447 448 449 450 451
				return;
			}
			this._isHandlingScrollEvent = true;
			this.originalEditor.setScrollPosition({
				scrollLeft: e.scrollLeft,
				scrollTop: e.scrollTop
			});
			this._isHandlingScrollEvent = false;

			this._layoutOverviewViewport();
		}));

A
Alex Dima 已提交
452
		this._register(this.modifiedEditor.onDidChangeViewZones(() => {
A
Alex Dima 已提交
453 454 455
			this._onViewZonesChanged();
		}));

A
Alex Dima 已提交
456
		this._register(this.modifiedEditor.onDidChangeConfiguration((e) => {
A
Alex Dima 已提交
457 458 459 460 461
			if (e.fontInfo && this.modifiedEditor.getModel()) {
				this._onViewZonesChanged();
			}
		}));

A
Alex Dima 已提交
462
		this._register(this.modifiedEditor.onDidChangeModelContent(() => {
A
Alex Dima 已提交
463 464 465 466
			if (this._isVisible) {
				this._beginUpdateDecorationsSoon();
			}
		}));
E
Erich Gamma 已提交
467 468
	}

A
Alex Dima 已提交
469
	protected _createInnerEditor(instantiationService: IInstantiationService, container: HTMLElement, options: editorOptions.IEditorOptions): CodeEditorWidget {
470
		return instantiationService.createInstance(CodeEditorWidget, container, options, {});
A
Alex Dima 已提交
471 472
	}

E
Erich Gamma 已提交
473
	public dispose(): void {
474 475
		this._codeEditorService.removeDiffEditor(this);

476 477 478 479 480
		if (this._beginUpdateDecorationsTimeout !== -1) {
			window.clearTimeout(this._beginUpdateDecorationsTimeout);
			this._beginUpdateDecorationsTimeout = -1;
		}

E
Erich Gamma 已提交
481 482 483 484
		window.clearInterval(this._measureDomElementToken);

		this._cleanViewZonesAndDecorations();

485 486 487 488 489 490 491 492
		if (this._originalOverviewRuler) {
			this._overviewDomElement.removeChild(this._originalOverviewRuler.getDomNode());
			this._originalOverviewRuler.dispose();
		}
		if (this._modifiedOverviewRuler) {
			this._overviewDomElement.removeChild(this._modifiedOverviewRuler.getDomNode());
			this._modifiedOverviewRuler.dispose();
		}
493 494
		this._overviewDomElement.removeChild(this._overviewViewportDomElement.domNode);
		this._containerDomElement.removeChild(this._overviewDomElement);
E
Erich Gamma 已提交
495

496
		this._containerDomElement.removeChild(this._originalDomNode);
A
Alex Dima 已提交
497
		this.originalEditor.dispose();
498 499

		this._containerDomElement.removeChild(this._modifiedDomNode);
A
Alex Dima 已提交
500
		this.modifiedEditor.dispose();
E
Erich Gamma 已提交
501 502 503

		this._strategy.dispose();

504 505 506
		this._containerDomElement.removeChild(this._reviewPane.domNode.domNode);
		this._containerDomElement.removeChild(this._reviewPane.shadow.domNode);
		this._containerDomElement.removeChild(this._reviewPane.actionBarContainer.domNode);
507 508
		this._reviewPane.dispose();

509 510
		this._domElement.removeChild(this._containerDomElement);

A
Alex Dima 已提交
511
		this._onDidDispose.fire();
A
Alex Dima 已提交
512

E
Erich Gamma 已提交
513 514 515 516 517 518 519 520 521 522
		super.dispose();
	}

	//------------ begin IDiffEditor methods

	public getId(): string {
		return this.getEditorType() + ':' + this.id;
	}

	public getEditorType(): string {
A
Alex Dima 已提交
523
		return editorCommon.EditorType.IDiffEditor;
E
Erich Gamma 已提交
524 525
	}

A
Alex Dima 已提交
526
	public getLineChanges(): editorCommon.ILineChange[] | null {
E
Erich Gamma 已提交
527 528 529
		return this._lineChanges;
	}

A
Alex Dima 已提交
530
	public getOriginalEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
531 532 533
		return this.originalEditor;
	}

A
Alex Dima 已提交
534
	public getModifiedEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
535 536 537
		return this.modifiedEditor;
	}

538
	public updateOptions(newOptions: editorOptions.IDiffEditorOptions): void {
E
Erich Gamma 已提交
539 540

		// Handle side by side
A
Alex Dima 已提交
541
		let renderSideBySideChanged = false;
E
Erich Gamma 已提交
542 543 544 545 546 547 548
		if (typeof newOptions.renderSideBySide !== 'undefined') {
			if (this._renderSideBySide !== newOptions.renderSideBySide) {
				this._renderSideBySide = newOptions.renderSideBySide;
				renderSideBySideChanged = true;
			}
		}

549 550
		let beginUpdateDecorations = false;

E
Erich Gamma 已提交
551 552 553 554
		if (typeof newOptions.ignoreTrimWhitespace !== 'undefined') {
			if (this._ignoreTrimWhitespace !== newOptions.ignoreTrimWhitespace) {
				this._ignoreTrimWhitespace = newOptions.ignoreTrimWhitespace;
				// Begin comparing
555 556 557 558 559 560 561 562
				beginUpdateDecorations = true;
			}
		}

		if (typeof newOptions.renderIndicators !== 'undefined') {
			if (this._renderIndicators !== newOptions.renderIndicators) {
				this._renderIndicators = newOptions.renderIndicators;
				beginUpdateDecorations = true;
E
Erich Gamma 已提交
563 564 565
			}
		}

566 567 568 569
		if (beginUpdateDecorations) {
			this._beginUpdateDecorations();
		}

570 571 572 573
		if (typeof newOptions.originalEditable !== 'undefined') {
			this._originalIsEditable = Boolean(newOptions.originalEditable);
		}

E
Erich Gamma 已提交
574
		this.modifiedEditor.updateOptions(this._adjustOptionsForRightHandSide(newOptions));
575
		this.originalEditor.updateOptions(this._adjustOptionsForLeftHandSide(newOptions, this._originalIsEditable));
E
Erich Gamma 已提交
576 577 578 579 580 581 582 583 584 585

		// enableSplitViewResizing
		if (typeof newOptions.enableSplitViewResizing !== 'undefined') {
			this._enableSplitViewResizing = newOptions.enableSplitViewResizing;
		}
		this._strategy.setEnableSplitViewResizing(this._enableSplitViewResizing);

		// renderSideBySide
		if (renderSideBySideChanged) {
			if (this._renderSideBySide) {
M
Matt Bierner 已提交
586
				this._setStrategy(new DiffEdtorWidgetSideBySide(this._createDataSource(), this._enableSplitViewResizing));
E
Erich Gamma 已提交
587 588 589
			} else {
				this._setStrategy(new DiffEdtorWidgetInline(this._createDataSource(), this._enableSplitViewResizing));
			}
590 591
			// Update class name
			this._containerDomElement.className = DiffEditorWidget._getClassName(this._themeService.getTheme(), this._renderSideBySide);
E
Erich Gamma 已提交
592 593 594
		}
	}

A
Alex Dima 已提交
595
	public getModel(): editorCommon.IDiffEditorModel {
E
Erich Gamma 已提交
596 597 598 599 600 601
		return {
			original: this.originalEditor.getModel(),
			modified: this.modifiedEditor.getModel()
		};
	}

J
Johannes Rieken 已提交
602
	public setModel(model: editorCommon.IDiffEditorModel): void {
E
Erich Gamma 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
		// Guard us against partial null model
		if (model && (!model.original || !model.modified)) {
			throw new Error(!model.original ? 'DiffEditorWidget.setModel: Original model is null' : 'DiffEditorWidget.setModel: Modified model is null');
		}

		// Remove all view zones & decorations
		this._cleanViewZonesAndDecorations();

		// Update code editor models
		this.originalEditor.setModel(model ? model.original : null);
		this.modifiedEditor.setModel(model ? model.modified : null);
		this._updateDecorationsRunner.cancel();

		if (model) {
			this.originalEditor.setScrollTop(0);
			this.modifiedEditor.setScrollTop(0);
		}

		// Disable any diff computations that will come in
		this._lineChanges = null;
		this._diffComputationToken++;

		if (model) {
			this._recreateOverviewRulers();

			// Begin comparing
			this._beginUpdateDecorations();
		} else {
			this._lineChanges = null;
		}

		this._layoutOverviewViewport();
	}

	public getDomNode(): HTMLElement {
		return this._domElement;
	}

A
Alex Dima 已提交
641
	public getVisibleColumnFromPosition(position: IPosition): number {
E
Erich Gamma 已提交
642 643 644
		return this.modifiedEditor.getVisibleColumnFromPosition(position);
	}

A
Alex Dima 已提交
645
	public getPosition(): Position | null {
E
Erich Gamma 已提交
646 647 648
		return this.modifiedEditor.getPosition();
	}

649 650
	public setPosition(position: IPosition): void {
		this.modifiedEditor.setPosition(position);
E
Erich Gamma 已提交
651 652
	}

653 654
	public revealLine(lineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLine(lineNumber, scrollType);
E
Erich Gamma 已提交
655 656
	}

657 658
	public revealLineInCenter(lineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLineInCenter(lineNumber, scrollType);
E
Erich Gamma 已提交
659 660
	}

661 662
	public revealLineInCenterIfOutsideViewport(lineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLineInCenterIfOutsideViewport(lineNumber, scrollType);
E
Erich Gamma 已提交
663 664
	}

665 666
	public revealPosition(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealPosition(position, scrollType);
E
Erich Gamma 已提交
667 668
	}

669 670
	public revealPositionInCenter(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealPositionInCenter(position, scrollType);
E
Erich Gamma 已提交
671 672
	}

673 674
	public revealPositionInCenterIfOutsideViewport(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealPositionInCenterIfOutsideViewport(position, scrollType);
E
Erich Gamma 已提交
675 676
	}

A
Alex Dima 已提交
677
	public getSelection(): Selection | null {
E
Erich Gamma 已提交
678 679 680
		return this.modifiedEditor.getSelection();
	}

A
Alex Dima 已提交
681
	public getSelections(): Selection[] | null {
E
Erich Gamma 已提交
682 683 684
		return this.modifiedEditor.getSelections();
	}

685 686 687 688 689 690
	public setSelection(range: IRange): void;
	public setSelection(editorRange: Range): void;
	public setSelection(selection: ISelection): void;
	public setSelection(editorSelection: Selection): void;
	public setSelection(something: any): void {
		this.modifiedEditor.setSelection(something);
E
Erich Gamma 已提交
691 692
	}

A
Alex Dima 已提交
693
	public setSelections(ranges: ISelection[]): void {
E
Erich Gamma 已提交
694 695 696
		this.modifiedEditor.setSelections(ranges);
	}

697 698
	public revealLines(startLineNumber: number, endLineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLines(startLineNumber, endLineNumber, scrollType);
E
Erich Gamma 已提交
699 700
	}

701 702
	public revealLinesInCenter(startLineNumber: number, endLineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLinesInCenter(startLineNumber, endLineNumber, scrollType);
E
Erich Gamma 已提交
703 704
	}

705 706
	public revealLinesInCenterIfOutsideViewport(startLineNumber: number, endLineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLinesInCenterIfOutsideViewport(startLineNumber, endLineNumber, scrollType);
E
Erich Gamma 已提交
707 708
	}

709 710
	public revealRange(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth, revealVerticalInCenter: boolean = false, revealHorizontal: boolean = true): void {
		this.modifiedEditor.revealRange(range, scrollType, revealVerticalInCenter, revealHorizontal);
E
Erich Gamma 已提交
711 712
	}

713 714
	public revealRangeInCenter(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealRangeInCenter(range, scrollType);
E
Erich Gamma 已提交
715 716
	}

717 718
	public revealRangeInCenterIfOutsideViewport(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealRangeInCenterIfOutsideViewport(range, scrollType);
E
Erich Gamma 已提交
719 720
	}

721 722
	public revealRangeAtTop(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealRangeAtTop(range, scrollType);
723 724
	}

A
Alex Dima 已提交
725
	public getSupportedActions(): editorCommon.IEditorAction[] {
726 727 728
		return this.modifiedEditor.getSupportedActions();
	}

A
Alex Dima 已提交
729
	public saveViewState(): editorCommon.IDiffEditorViewState {
A
Alex Dima 已提交
730 731
		let originalViewState = this.originalEditor.saveViewState();
		let modifiedViewState = this.modifiedEditor.saveViewState();
E
Erich Gamma 已提交
732 733 734 735 736 737
		return {
			original: originalViewState,
			modified: modifiedViewState
		};
	}

A
Alex Dima 已提交
738
	public restoreViewState(s: editorCommon.IDiffEditorViewState): void {
E
Erich Gamma 已提交
739
		if (s.original && s.original) {
A
Alex Dima 已提交
740
			let diffEditorState = <editorCommon.IDiffEditorViewState>s;
E
Erich Gamma 已提交
741 742 743 744 745
			this.originalEditor.restoreViewState(diffEditorState.original);
			this.modifiedEditor.restoreViewState(diffEditorState.modified);
		}
	}

J
Johannes Rieken 已提交
746
	public layout(dimension?: editorCommon.IDimension): void {
E
Erich Gamma 已提交
747 748 749 750 751 752 753
		this._measureDomElement(false, dimension);
	}

	public focus(): void {
		this.modifiedEditor.focus();
	}

A
Alex Dima 已提交
754 755
	public hasTextFocus(): boolean {
		return this.originalEditor.hasTextFocus() || this.modifiedEditor.hasTextFocus();
E
Erich Gamma 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	}

	public onVisible(): void {
		this._isVisible = true;
		this.originalEditor.onVisible();
		this.modifiedEditor.onVisible();
		// Begin comparing
		this._beginUpdateDecorations();
	}

	public onHide(): void {
		this._isVisible = false;
		this.originalEditor.onHide();
		this.modifiedEditor.onHide();
		// Remove all view zones & decorations
		this._cleanViewZonesAndDecorations();
	}

J
Johannes Rieken 已提交
774
	public trigger(source: string, handlerId: string, payload: any): void {
E
Erich Gamma 已提交
775 776 777
		this.modifiedEditor.trigger(source, handlerId, payload);
	}

778
	public changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
E
Erich Gamma 已提交
779 780 781 782 783 784 785 786 787
		return this.modifiedEditor.changeDecorations(callback);
	}

	//------------ end IDiffEditor methods



	//------------ begin layouting methods

J
Johannes Rieken 已提交
788
	private _measureDomElement(forceDoLayoutCall: boolean, dimensions?: editorCommon.IDimension): void {
789 790 791 792
		dimensions = dimensions || {
			width: this._containerDomElement.clientWidth,
			height: this._containerDomElement.clientHeight
		};
E
Erich Gamma 已提交
793 794

		if (dimensions.width <= 0) {
795 796
			this._width = 0;
			this._height = 0;
A
Alex Dima 已提交
797
			this._reviewHeight = 0;
E
Erich Gamma 已提交
798 799 800 801 802 803 804 805 806 807
			return;
		}

		if (!forceDoLayoutCall && dimensions.width === this._width && dimensions.height === this._height) {
			// Nothing has changed
			return;
		}

		this._width = dimensions.width;
		this._height = dimensions.height;
808
		this._reviewHeight = this._reviewPane.isVisible() ? this._height : 0;
E
Erich Gamma 已提交
809 810 811 812 813

		this._doLayout();
	}

	private _layoutOverviewRulers(): void {
A
Alex Dima 已提交
814 815
		let freeSpace = DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH - 2 * DiffEditorWidget.ONE_OVERVIEW_WIDTH;
		let layoutInfo = this.modifiedEditor.getLayoutInfo();
E
Erich Gamma 已提交
816
		if (layoutInfo) {
A
Alex Dima 已提交
817
			this._originalOverviewRuler.setLayout({
E
Erich Gamma 已提交
818 819 820
				top: 0,
				width: DiffEditorWidget.ONE_OVERVIEW_WIDTH,
				right: freeSpace + DiffEditorWidget.ONE_OVERVIEW_WIDTH,
A
Alex Dima 已提交
821
				height: (this._height - this._reviewHeight)
A
Alex Dima 已提交
822 823
			});
			this._modifiedOverviewRuler.setLayout({
E
Erich Gamma 已提交
824 825 826
				top: 0,
				right: 0,
				width: DiffEditorWidget.ONE_OVERVIEW_WIDTH,
A
Alex Dima 已提交
827
				height: (this._height - this._reviewHeight)
A
Alex Dima 已提交
828
			});
E
Erich Gamma 已提交
829 830 831 832 833 834 835 836 837 838 839 840
		}
	}

	//------------ end layouting methods

	private _onViewZonesChanged(): void {
		if (this._currentlyChangingViewZones) {
			return;
		}
		this._updateDecorationsRunner.schedule();
	}

A
Alex Dima 已提交
841 842 843 844 845 846 847 848 849
	private _beginUpdateDecorationsSoon(): void {
		// Clear previous timeout if necessary
		if (this._beginUpdateDecorationsTimeout !== -1) {
			window.clearTimeout(this._beginUpdateDecorationsTimeout);
			this._beginUpdateDecorationsTimeout = -1;
		}
		this._beginUpdateDecorationsTimeout = window.setTimeout(() => this._beginUpdateDecorations(), DiffEditorWidget.UPDATE_DIFF_DECORATIONS_DELAY);
	}

850 851
	private _lastOriginalWarning: URI | null = null;
	private _lastModifiedWarning: URI | null = null;
852

A
Alex Dima 已提交
853
	private static _equals(a: URI | null, b: URI | null): boolean {
854 855 856 857 858 859 860 861 862
		if (!a && !b) {
			return true;
		}
		if (!a || !b) {
			return false;
		}
		return (a.toString() === b.toString());
	}

E
Erich Gamma 已提交
863 864
	private _beginUpdateDecorations(): void {
		this._beginUpdateDecorationsTimeout = -1;
865 866 867
		const currentOriginalModel = this.originalEditor.getModel();
		const currentModifiedModel = this.modifiedEditor.getModel();
		if (!currentOriginalModel || !currentModifiedModel) {
E
Erich Gamma 已提交
868 869 870 871 872 873 874
			return;
		}

		// Prevent old diff requests to come if a new request has been initiated
		// The best method would be to call cancel on the Promise, but this is not
		// yet supported, so using tokens for now.
		this._diffComputationToken++;
A
Alex Dima 已提交
875
		let currentToken = this._diffComputationToken;
E
Erich Gamma 已提交
876

877 878 879 880 881 882 883
		if (!this._editorWorkerService.canComputeDiff(currentOriginalModel.uri, currentModifiedModel.uri)) {
			if (
				!DiffEditorWidget._equals(currentOriginalModel.uri, this._lastOriginalWarning)
				|| !DiffEditorWidget._equals(currentModifiedModel.uri, this._lastModifiedWarning)
			) {
				this._lastOriginalWarning = currentOriginalModel.uri;
				this._lastModifiedWarning = currentModifiedModel.uri;
884
				this._notificationService.warn(nls.localize("diff.tooLarge", "Cannot compare files because one file is too large."));
885 886 887 888
			}
			return;
		}

889
		this._editorWorkerService.computeDiff(currentOriginalModel.uri, currentModifiedModel.uri, this._ignoreTrimWhitespace).then((result) => {
890 891 892
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
J
Johannes Rieken 已提交
893
			) {
894 895
				this._lineChanges = result;
				this._updateDecorationsRunner.schedule();
A
Alex Dima 已提交
896
				this._onDidUpdateDiff.fire();
897 898 899 900 901
			}
		}, (error) => {
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
J
Johannes Rieken 已提交
902
			) {
E
Erich Gamma 已提交
903 904 905
				this._lineChanges = null;
				this._updateDecorationsRunner.schedule();
			}
906
		});
E
Erich Gamma 已提交
907 908 909 910 911 912 913 914
	}

	private _cleanViewZonesAndDecorations(): void {
		this._originalEditorState.clean(this.originalEditor);
		this._modifiedEditorState.clean(this.modifiedEditor);
	}

	private _updateDecorations(): void {
915 916 917
		if (!this.originalEditor.getModel() || !this.modifiedEditor.getModel()) {
			return;
		}
A
Alex Dima 已提交
918
		let lineChanges = this._lineChanges || [];
E
Erich Gamma 已提交
919

A
Alex Dima 已提交
920 921
		let foreignOriginal = this._originalEditorState.getForeignViewZones(this.originalEditor.getWhitespaces());
		let foreignModified = this._modifiedEditorState.getForeignViewZones(this.modifiedEditor.getWhitespaces());
E
Erich Gamma 已提交
922

923
		let diffDecorations = this._strategy.getEditorsDiffDecorations(lineChanges, this._ignoreTrimWhitespace, this._renderIndicators, foreignOriginal, foreignModified, this.originalEditor, this.modifiedEditor);
E
Erich Gamma 已提交
924 925 926

		try {
			this._currentlyChangingViewZones = true;
A
Alex Dima 已提交
927 928
			this._originalEditorState.apply(this.originalEditor, this._originalOverviewRuler, diffDecorations.original, false);
			this._modifiedEditorState.apply(this.modifiedEditor, this._modifiedOverviewRuler, diffDecorations.modified, true);
E
Erich Gamma 已提交
929 930 931 932 933
		} finally {
			this._currentlyChangingViewZones = false;
		}
	}

934
	private _adjustOptionsForSubEditor(options: editorOptions.IDiffEditorOptions): editorOptions.IDiffEditorOptions {
J
Johannes Rieken 已提交
935
		let clonedOptions: editorOptions.IDiffEditorOptions = objects.deepClone(options || {});
A
Alex Dima 已提交
936
		clonedOptions.inDiffEditor = true;
937
		clonedOptions.wordWrap = 'off';
938
		clonedOptions.wordWrapMinified = false;
E
Erich Gamma 已提交
939 940 941
		clonedOptions.automaticLayout = false;
		clonedOptions.scrollbar = clonedOptions.scrollbar || {};
		clonedOptions.scrollbar.vertical = 'visible';
A
Alex Dima 已提交
942
		clonedOptions.folding = false;
943
		clonedOptions.codeLens = false;
J
Joao Moreno 已提交
944
		clonedOptions.fixedOverflowWidgets = true;
945
		// clonedOptions.lineDecorationsWidth = '2ch';
946 947 948
		if (!clonedOptions.minimap) {
			clonedOptions.minimap = {};
		}
949
		clonedOptions.minimap.enabled = false;
E
Erich Gamma 已提交
950 951 952
		return clonedOptions;
	}

953
	private _adjustOptionsForLeftHandSide(options: editorOptions.IDiffEditorOptions, isEditable: boolean): editorOptions.IEditorOptions {
954 955 956
		let result = this._adjustOptionsForSubEditor(options);
		result.readOnly = !isEditable;
		result.overviewRulerLanes = 1;
957
		result.extraEditorClassName = 'original-in-monaco-diff-editor';
958 959 960
		return result;
	}

961
	private _adjustOptionsForRightHandSide(options: editorOptions.IDiffEditorOptions): editorOptions.IEditorOptions {
962
		let result = this._adjustOptionsForSubEditor(options);
A
Alex Dima 已提交
963
		result.revealHorizontalRightPadding = editorOptions.EDITOR_DEFAULTS.viewInfo.revealHorizontalRightPadding + DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
964
		result.scrollbar.verticalHasArrows = false;
965
		result.extraEditorClassName = 'modified-in-monaco-diff-editor';
966
		return result;
E
Erich Gamma 已提交
967 968
	}

969 970 971 972
	public doLayout(): void {
		this._measureDomElement(true);
	}

E
Erich Gamma 已提交
973
	private _doLayout(): void {
A
Alex Dima 已提交
974
		let splitPoint = this._strategy.layout();
E
Erich Gamma 已提交
975 976 977 978 979 980 981 982

		this._originalDomNode.style.width = splitPoint + 'px';
		this._originalDomNode.style.left = '0px';

		this._modifiedDomNode.style.width = (this._width - splitPoint) + 'px';
		this._modifiedDomNode.style.left = splitPoint + 'px';

		this._overviewDomElement.style.top = '0px';
A
Alex Dima 已提交
983
		this._overviewDomElement.style.height = (this._height - this._reviewHeight) + 'px';
E
Erich Gamma 已提交
984 985
		this._overviewDomElement.style.width = DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH + 'px';
		this._overviewDomElement.style.left = (this._width - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH) + 'px';
A
Alex Dima 已提交
986 987
		this._overviewViewportDomElement.setWidth(DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH);
		this._overviewViewportDomElement.setHeight(30);
E
Erich Gamma 已提交
988

A
Alex Dima 已提交
989 990
		this.originalEditor.layout({ width: splitPoint, height: (this._height - this._reviewHeight) });
		this.modifiedEditor.layout({ width: this._width - splitPoint - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH, height: (this._height - this._reviewHeight) });
E
Erich Gamma 已提交
991 992 993 994 995

		if (this._originalOverviewRuler || this._modifiedOverviewRuler) {
			this._layoutOverviewRulers();
		}

A
Alex Dima 已提交
996 997
		this._reviewPane.layout(this._height - this._reviewHeight, this._width, this._reviewHeight);

E
Erich Gamma 已提交
998 999 1000 1001
		this._layoutOverviewViewport();
	}

	private _layoutOverviewViewport(): void {
A
Alex Dima 已提交
1002
		let layout = this._computeOverviewViewport();
E
Erich Gamma 已提交
1003
		if (!layout) {
A
Alex Dima 已提交
1004 1005
			this._overviewViewportDomElement.setTop(0);
			this._overviewViewportDomElement.setHeight(0);
E
Erich Gamma 已提交
1006
		} else {
A
Alex Dima 已提交
1007 1008
			this._overviewViewportDomElement.setTop(layout.top);
			this._overviewViewportDomElement.setHeight(layout.height);
E
Erich Gamma 已提交
1009 1010 1011
		}
	}

A
Alex Dima 已提交
1012
	private _computeOverviewViewport(): { height: number; top: number; } | null {
A
Alex Dima 已提交
1013
		let layoutInfo = this.modifiedEditor.getLayoutInfo();
E
Erich Gamma 已提交
1014 1015 1016 1017
		if (!layoutInfo) {
			return null;
		}

A
Alex Dima 已提交
1018 1019
		let scrollTop = this.modifiedEditor.getScrollTop();
		let scrollHeight = this.modifiedEditor.getScrollHeight();
E
Erich Gamma 已提交
1020

A
Alex Dima 已提交
1021 1022 1023
		let computedAvailableSize = Math.max(0, layoutInfo.contentHeight);
		let computedRepresentableSize = Math.max(0, computedAvailableSize - 2 * 0);
		let computedRatio = scrollHeight > 0 ? (computedRepresentableSize / scrollHeight) : 0;
E
Erich Gamma 已提交
1024

1025
		let computedSliderSize = Math.max(0, Math.floor(layoutInfo.contentHeight * computedRatio));
A
Alex Dima 已提交
1026
		let computedSliderPosition = Math.floor(scrollTop * computedRatio);
E
Erich Gamma 已提交
1027 1028 1029 1030 1031 1032 1033

		return {
			height: computedSliderSize,
			top: computedSliderPosition
		};
	}

J
Johannes Rieken 已提交
1034
	private _createDataSource(): IDataSource {
E
Erich Gamma 已提交
1035 1036 1037 1038 1039 1040
		return {
			getWidth: () => {
				return this._width;
			},

			getHeight: () => {
A
Alex Dima 已提交
1041
				return (this._height - this._reviewHeight);
E
Erich Gamma 已提交
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
			},

			getContainerDomNode: () => {
				return this._containerDomElement;
			},

			relayoutEditors: () => {
				this._doLayout();
			},

			getOriginalEditor: () => {
				return this.originalEditor;
			},

			getModifiedEditor: () => {
				return this.modifiedEditor;
			}
		};
	}

J
Johannes Rieken 已提交
1062
	private _setStrategy(newStrategy: IDiffEditorWidgetStyle): void {
E
Erich Gamma 已提交
1063 1064 1065 1066 1067
		if (this._strategy) {
			this._strategy.dispose();
		}

		this._strategy = newStrategy;
1068
		newStrategy.applyColors(this._themeService.getTheme());
E
Erich Gamma 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077

		if (this._lineChanges) {
			this._updateDecorations();
		}

		// Just do a layout, the strategy might need it
		this._measureDomElement(true);
	}

A
Alex Dima 已提交
1078
	private _getLineChangeAtOrBeforeLineNumber(lineNumber: number, startLineNumberExtractor: (lineChange: editorCommon.ILineChange) => number): editorCommon.ILineChange | null {
E
Erich Gamma 已提交
1079 1080 1081 1082 1083
		if (this._lineChanges.length === 0 || lineNumber < startLineNumberExtractor(this._lineChanges[0])) {
			// There are no changes or `lineNumber` is before the first change
			return null;
		}

A
Alex Dima 已提交
1084
		let min = 0, max = this._lineChanges.length - 1;
E
Erich Gamma 已提交
1085
		while (min < max) {
A
Alex Dima 已提交
1086 1087 1088
			let mid = Math.floor((min + max) / 2);
			let midStart = startLineNumberExtractor(this._lineChanges[mid]);
			let midEnd = (mid + 1 <= max ? startLineNumberExtractor(this._lineChanges[mid + 1]) : Number.MAX_VALUE);
E
Erich Gamma 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103

			if (lineNumber < midStart) {
				max = mid - 1;
			} else if (lineNumber >= midEnd) {
				min = mid + 1;
			} else {
				// HIT!
				min = mid;
				max = mid;
			}
		}
		return this._lineChanges[min];
	}

	private _getEquivalentLineForOriginalLineNumber(lineNumber: number): number {
A
Alex Dima 已提交
1104
		let lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.originalStartLineNumber);
E
Erich Gamma 已提交
1105 1106 1107 1108 1109

		if (!lineChange) {
			return lineNumber;
		}

A
Alex Dima 已提交
1110 1111 1112 1113
		let originalEquivalentLineNumber = lineChange.originalStartLineNumber + (lineChange.originalEndLineNumber > 0 ? -1 : 0);
		let modifiedEquivalentLineNumber = lineChange.modifiedStartLineNumber + (lineChange.modifiedEndLineNumber > 0 ? -1 : 0);
		let lineChangeOriginalLength = (lineChange.originalEndLineNumber > 0 ? (lineChange.originalEndLineNumber - lineChange.originalStartLineNumber + 1) : 0);
		let lineChangeModifiedLength = (lineChange.modifiedEndLineNumber > 0 ? (lineChange.modifiedEndLineNumber - lineChange.modifiedStartLineNumber + 1) : 0);
E
Erich Gamma 已提交
1114 1115


A
Alex Dima 已提交
1116
		let delta = lineNumber - originalEquivalentLineNumber;
E
Erich Gamma 已提交
1117 1118 1119 1120 1121

		if (delta <= lineChangeOriginalLength) {
			return modifiedEquivalentLineNumber + Math.min(delta, lineChangeModifiedLength);
		}

J
Johannes Rieken 已提交
1122
		return modifiedEquivalentLineNumber + lineChangeModifiedLength - lineChangeOriginalLength + delta;
E
Erich Gamma 已提交
1123 1124 1125
	}

	private _getEquivalentLineForModifiedLineNumber(lineNumber: number): number {
A
Alex Dima 已提交
1126
		let lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.modifiedStartLineNumber);
E
Erich Gamma 已提交
1127 1128 1129 1130 1131

		if (!lineChange) {
			return lineNumber;
		}

A
Alex Dima 已提交
1132 1133 1134 1135
		let originalEquivalentLineNumber = lineChange.originalStartLineNumber + (lineChange.originalEndLineNumber > 0 ? -1 : 0);
		let modifiedEquivalentLineNumber = lineChange.modifiedStartLineNumber + (lineChange.modifiedEndLineNumber > 0 ? -1 : 0);
		let lineChangeOriginalLength = (lineChange.originalEndLineNumber > 0 ? (lineChange.originalEndLineNumber - lineChange.originalStartLineNumber + 1) : 0);
		let lineChangeModifiedLength = (lineChange.modifiedEndLineNumber > 0 ? (lineChange.modifiedEndLineNumber - lineChange.modifiedStartLineNumber + 1) : 0);
E
Erich Gamma 已提交
1136 1137


A
Alex Dima 已提交
1138
		let delta = lineNumber - modifiedEquivalentLineNumber;
E
Erich Gamma 已提交
1139 1140 1141 1142 1143

		if (delta <= lineChangeModifiedLength) {
			return originalEquivalentLineNumber + Math.min(delta, lineChangeOriginalLength);
		}

J
Johannes Rieken 已提交
1144
		return originalEquivalentLineNumber + lineChangeOriginalLength - lineChangeModifiedLength + delta;
E
Erich Gamma 已提交
1145 1146
	}

A
Alex Dima 已提交
1147
	public getDiffLineInformationForOriginal(lineNumber: number): editorBrowser.IDiffLineInformation | null {
E
Erich Gamma 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156
		if (!this._lineChanges) {
			// Cannot answer that which I don't know
			return null;
		}
		return {
			equivalentLineNumber: this._getEquivalentLineForOriginalLineNumber(lineNumber)
		};
	}

A
Alex Dima 已提交
1157
	public getDiffLineInformationForModified(lineNumber: number): editorBrowser.IDiffLineInformation | null {
E
Erich Gamma 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
		if (!this._lineChanges) {
			// Cannot answer that which I don't know
			return null;
		}
		return {
			equivalentLineNumber: this._getEquivalentLineForModifiedLineNumber(lineNumber)
		};
	}
}

interface IDataSource {
	getWidth(): number;
	getHeight(): number;
	getContainerDomNode(): HTMLElement;
	relayoutEditors(): void;

A
Alex Dima 已提交
1174 1175
	getOriginalEditor(): editorBrowser.ICodeEditor;
	getModifiedEditor(): editorBrowser.ICodeEditor;
E
Erich Gamma 已提交
1176 1177
}

A
Alex Dima 已提交
1178
abstract class DiffEditorWidgetStyle extends Disposable implements IDiffEditorWidgetStyle {
E
Erich Gamma 已提交
1179

J
Johannes Rieken 已提交
1180
	_dataSource: IDataSource;
1181 1182
	_insertColor: Color;
	_removeColor: Color;
E
Erich Gamma 已提交
1183

J
Johannes Rieken 已提交
1184
	constructor(dataSource: IDataSource) {
A
Alex Dima 已提交
1185
		super();
E
Erich Gamma 已提交
1186 1187 1188
		this._dataSource = dataSource;
	}

1189 1190 1191 1192 1193 1194 1195 1196 1197
	public applyColors(theme: ITheme): boolean {
		let newInsertColor = (theme.getColor(diffInserted) || defaultInsertColor).transparent(2);
		let newRemoveColor = (theme.getColor(diffRemoved) || defaultRemoveColor).transparent(2);
		let hasChanges = !newInsertColor.equals(this._insertColor) || !newRemoveColor.equals(this._removeColor);
		this._insertColor = newInsertColor;
		this._removeColor = newRemoveColor;
		return hasChanges;
	}

A
Alex Dima 已提交
1198
	public getEditorsDiffDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalWhitespaces: IEditorWhitespace[], modifiedWhitespaces: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsDiffDecorationsWithZones {
E
Erich Gamma 已提交
1199 1200 1201 1202 1203 1204 1205
		// Get view zones
		modifiedWhitespaces = modifiedWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
		originalWhitespaces = originalWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
1206
		let zones = this._getViewZones(lineChanges, originalWhitespaces, modifiedWhitespaces, originalEditor, modifiedEditor, renderIndicators);
E
Erich Gamma 已提交
1207 1208

		// Get decorations & overview ruler zones
1209 1210
		let originalDecorations = this._getOriginalEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor);
		let modifiedDecorations = this._getModifiedEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor);
E
Erich Gamma 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225

		return {
			original: {
				decorations: originalDecorations.decorations,
				overviewZones: originalDecorations.overviewZones,
				zones: zones.original
			},
			modified: {
				decorations: modifiedDecorations.decorations,
				overviewZones: modifiedDecorations.overviewZones,
				zones: zones.modified
			}
		};
	}

1226 1227 1228
	protected abstract _getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean): IEditorsZones;
	protected abstract _getOriginalEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations;
	protected abstract _getModifiedEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations;
A
Alex Dima 已提交
1229 1230 1231

	public abstract setEnableSplitViewResizing(enableSplitViewResizing: boolean): void;
	public abstract layout(): number;
E
Erich Gamma 已提交
1232 1233
}

A
Alex Dima 已提交
1234
interface IMyViewZone extends editorBrowser.IViewZone {
E
Erich Gamma 已提交
1235 1236 1237 1238 1239 1240
	shouldNotShrink?: boolean;
}

class ForeignViewZonesIterator {

	private _index: number;
A
Alex Dima 已提交
1241
	private _source: IEditorWhitespace[];
A
Alex Dima 已提交
1242
	public current: IEditorWhitespace | null;
E
Erich Gamma 已提交
1243

A
Alex Dima 已提交
1244
	constructor(source: IEditorWhitespace[]) {
E
Erich Gamma 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
		this._source = source;
		this._index = -1;
		this.advance();
	}

	public advance(): void {
		this._index++;
		if (this._index < this._source.length) {
			this.current = this._source[this._index];
		} else {
			this.current = null;
		}
	}
}

1260
abstract class ViewZonesComputer {
E
Erich Gamma 已提交
1261

J
Johannes Rieken 已提交
1262
	private lineChanges: editorCommon.ILineChange[];
A
Alex Dima 已提交
1263 1264
	private originalForeignVZ: IEditorWhitespace[];
	private modifiedForeignVZ: IEditorWhitespace[];
E
Erich Gamma 已提交
1265

A
Alex Dima 已提交
1266
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[]) {
E
Erich Gamma 已提交
1267 1268 1269 1270 1271 1272
		this.lineChanges = lineChanges;
		this.originalForeignVZ = originalForeignVZ;
		this.modifiedForeignVZ = modifiedForeignVZ;
	}

	public getViewZones(): IEditorsZones {
A
Alex Dima 已提交
1273
		let result: IEditorsZones = {
E
Erich Gamma 已提交
1274 1275 1276 1277
			original: [],
			modified: []
		};

A
Alex Dima 已提交
1278 1279 1280 1281 1282 1283 1284 1285
		let lineChangeModifiedLength: number = 0;
		let lineChangeOriginalLength: number = 0;
		let originalEquivalentLineNumber: number = 0;
		let modifiedEquivalentLineNumber: number = 0;
		let originalEndEquivalentLineNumber: number = 0;
		let modifiedEndEquivalentLineNumber: number = 0;

		let sortMyViewZones = (a: IMyViewZone, b: IMyViewZone) => {
E
Erich Gamma 已提交
1286 1287 1288
			return a.afterLineNumber - b.afterLineNumber;
		};

A
Alex Dima 已提交
1289
		let addAndCombineIfPossible = (destination: editorBrowser.IViewZone[], item: IMyViewZone) => {
E
Erich Gamma 已提交
1290
			if (item.domNode === null && destination.length > 0) {
A
Alex Dima 已提交
1291
				let lastItem = destination[destination.length - 1];
E
Erich Gamma 已提交
1292 1293 1294 1295 1296 1297 1298 1299
				if (lastItem.afterLineNumber === item.afterLineNumber && lastItem.domNode === null) {
					lastItem.heightInLines += item.heightInLines;
					return;
				}
			}
			destination.push(item);
		};

A
Alex Dima 已提交
1300 1301
		let modifiedForeignVZ = new ForeignViewZonesIterator(this.modifiedForeignVZ);
		let originalForeignVZ = new ForeignViewZonesIterator(this.originalForeignVZ);
E
Erich Gamma 已提交
1302 1303

		// In order to include foreign view zones after the last line change, the for loop will iterate once more after the end of the `lineChanges` array
A
Alex Dima 已提交
1304 1305
		for (let i = 0, length = this.lineChanges.length; i <= length; i++) {
			let lineChange = (i < length ? this.lineChanges[i] : null);
E
Erich Gamma 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

			if (lineChange !== null) {
				originalEquivalentLineNumber = lineChange.originalStartLineNumber + (lineChange.originalEndLineNumber > 0 ? -1 : 0);
				modifiedEquivalentLineNumber = lineChange.modifiedStartLineNumber + (lineChange.modifiedEndLineNumber > 0 ? -1 : 0);
				lineChangeOriginalLength = (lineChange.originalEndLineNumber > 0 ? (lineChange.originalEndLineNumber - lineChange.originalStartLineNumber + 1) : 0);
				lineChangeModifiedLength = (lineChange.modifiedEndLineNumber > 0 ? (lineChange.modifiedEndLineNumber - lineChange.modifiedStartLineNumber + 1) : 0);
				originalEndEquivalentLineNumber = Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber);
				modifiedEndEquivalentLineNumber = Math.max(lineChange.modifiedStartLineNumber, lineChange.modifiedEndLineNumber);
			} else {
				// Increase to very large value to get the producing tests of foreign view zones running
				originalEquivalentLineNumber += 10000000 + lineChangeOriginalLength;
				modifiedEquivalentLineNumber += 10000000 + lineChangeModifiedLength;
				originalEndEquivalentLineNumber = originalEquivalentLineNumber;
				modifiedEndEquivalentLineNumber = modifiedEquivalentLineNumber;
			}

			// Each step produces view zones, and after producing them, we try to cancel them out, to avoid empty-empty view zone cases
A
Alex Dima 已提交
1323 1324
			let stepOriginal: IMyViewZone[] = [];
			let stepModified: IMyViewZone[] = [];
E
Erich Gamma 已提交
1325 1326 1327 1328 1329

			// ---------------------------- PRODUCE VIEW ZONES

			// [PRODUCE] View zone(s) in original-side due to foreign view zone(s) in modified-side
			while (modifiedForeignVZ.current && modifiedForeignVZ.current.afterLineNumber <= modifiedEndEquivalentLineNumber) {
A
Alex Dima 已提交
1330
				let viewZoneLineNumber: number;
E
Erich Gamma 已提交
1331 1332 1333 1334 1335
				if (modifiedForeignVZ.current.afterLineNumber <= modifiedEquivalentLineNumber) {
					viewZoneLineNumber = originalEquivalentLineNumber - modifiedEquivalentLineNumber + modifiedForeignVZ.current.afterLineNumber;
				} else {
					viewZoneLineNumber = originalEndEquivalentLineNumber;
				}
A
Alex Dima 已提交
1336

1337
				let marginDomNode: HTMLDivElement | null = null;
A
Alex Dima 已提交
1338 1339 1340 1341
				if (lineChange && lineChange.modifiedStartLineNumber <= modifiedForeignVZ.current.afterLineNumber && modifiedForeignVZ.current.afterLineNumber <= lineChange.modifiedEndLineNumber) {
					marginDomNode = this._createOriginalMarginDomNodeForModifiedForeignViewZoneInAddedRegion();
				}

E
Erich Gamma 已提交
1342 1343 1344
				stepOriginal.push({
					afterLineNumber: viewZoneLineNumber,
					heightInLines: modifiedForeignVZ.current.heightInLines,
A
Alex Dima 已提交
1345 1346
					domNode: null,
					marginDomNode: marginDomNode
E
Erich Gamma 已提交
1347 1348 1349 1350 1351 1352
				});
				modifiedForeignVZ.advance();
			}

			// [PRODUCE] View zone(s) in modified-side due to foreign view zone(s) in original-side
			while (originalForeignVZ.current && originalForeignVZ.current.afterLineNumber <= originalEndEquivalentLineNumber) {
A
Alex Dima 已提交
1353
				let viewZoneLineNumber: number;
E
Erich Gamma 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
				if (originalForeignVZ.current.afterLineNumber <= originalEquivalentLineNumber) {
					viewZoneLineNumber = modifiedEquivalentLineNumber - originalEquivalentLineNumber + originalForeignVZ.current.afterLineNumber;
				} else {
					viewZoneLineNumber = modifiedEndEquivalentLineNumber;
				}
				stepModified.push({
					afterLineNumber: viewZoneLineNumber,
					heightInLines: originalForeignVZ.current.heightInLines,
					domNode: null
				});
				originalForeignVZ.advance();
			}

			if (lineChange !== null && isChangeOrInsert(lineChange)) {
A
Alex Dima 已提交
1368
				let r = this._produceOriginalFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
E
Erich Gamma 已提交
1369 1370 1371 1372 1373 1374
				if (r) {
					stepOriginal.push(r);
				}
			}

			if (lineChange !== null && isChangeOrDelete(lineChange)) {
A
Alex Dima 已提交
1375
				let r = this._produceModifiedFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
E
Erich Gamma 已提交
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
				if (r) {
					stepModified.push(r);
				}
			}

			// ---------------------------- END PRODUCE VIEW ZONES


			// ---------------------------- EMIT MINIMAL VIEW ZONES

			// [CANCEL & EMIT] Try to cancel view zones out
A
Alex Dima 已提交
1387 1388
			let stepOriginalIndex = 0;
			let stepModifiedIndex = 0;
E
Erich Gamma 已提交
1389 1390 1391 1392 1393

			stepOriginal = stepOriginal.sort(sortMyViewZones);
			stepModified = stepModified.sort(sortMyViewZones);

			while (stepOriginalIndex < stepOriginal.length && stepModifiedIndex < stepModified.length) {
A
Alex Dima 已提交
1394 1395
				let original = stepOriginal[stepOriginalIndex];
				let modified = stepModified[stepModifiedIndex];
E
Erich Gamma 已提交
1396

A
Alex Dima 已提交
1397 1398
				let originalDelta = original.afterLineNumber - originalEquivalentLineNumber;
				let modifiedDelta = modified.afterLineNumber - modifiedEquivalentLineNumber;
E
Erich Gamma 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439

				if (originalDelta < modifiedDelta) {
					addAndCombineIfPossible(result.original, original);
					stepOriginalIndex++;
				} else if (modifiedDelta < originalDelta) {
					addAndCombineIfPossible(result.modified, modified);
					stepModifiedIndex++;
				} else if (original.shouldNotShrink) {
					addAndCombineIfPossible(result.original, original);
					stepOriginalIndex++;
				} else if (modified.shouldNotShrink) {
					addAndCombineIfPossible(result.modified, modified);
					stepModifiedIndex++;
				} else {
					if (original.heightInLines >= modified.heightInLines) {
						// modified view zone gets removed
						original.heightInLines -= modified.heightInLines;
						stepModifiedIndex++;
					} else {
						// original view zone gets removed
						modified.heightInLines -= original.heightInLines;
						stepOriginalIndex++;
					}
				}
			}

			// [EMIT] Remaining original view zones
			while (stepOriginalIndex < stepOriginal.length) {
				addAndCombineIfPossible(result.original, stepOriginal[stepOriginalIndex]);
				stepOriginalIndex++;
			}

			// [EMIT] Remaining modified view zones
			while (stepModifiedIndex < stepModified.length) {
				addAndCombineIfPossible(result.modified, stepModified[stepModifiedIndex]);
				stepModifiedIndex++;
			}

			// ---------------------------- END EMIT MINIMAL VIEW ZONES
		}

A
Alex Dima 已提交
1440
		let ensureDomNode = (z: IMyViewZone) => {
E
Erich Gamma 已提交
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
			if (!z.domNode) {
				z.domNode = createFakeLinesDiv();
			}
		};

		result.original.forEach(ensureDomNode);
		result.modified.forEach(ensureDomNode);

		return result;
	}

A
Alex Dima 已提交
1452
	protected abstract _createOriginalMarginDomNodeForModifiedForeignViewZoneInAddedRegion(): HTMLDivElement | null;
A
Alex Dima 已提交
1453

A
Alex Dima 已提交
1454
	protected abstract _produceOriginalFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null;
E
Erich Gamma 已提交
1455

A
Alex Dima 已提交
1456
	protected abstract _produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null;
E
Erich Gamma 已提交
1457 1458
}

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
function createDecoration(startLineNumber: number, startColumn: number, endLineNumber: number, endColumn: number, options: ModelDecorationOptions) {
	return {
		range: new Range(startLineNumber, startColumn, endLineNumber, endColumn),
		options: options
	};
}

const DECORATIONS = {

	charDelete: ModelDecorationOptions.register({
		className: 'char-delete'
	}),
	charDeleteWholeLine: ModelDecorationOptions.register({
		className: 'char-delete',
		isWholeLine: true
	}),

	charInsert: ModelDecorationOptions.register({
		className: 'char-insert'
	}),
	charInsertWholeLine: ModelDecorationOptions.register({
		className: 'char-insert',
		isWholeLine: true
	}),

	lineInsert: ModelDecorationOptions.register({
		className: 'line-insert',
		marginClassName: 'line-insert',
		isWholeLine: true
	}),
	lineInsertWithSign: ModelDecorationOptions.register({
		className: 'line-insert',
		linesDecorationsClassName: 'insert-sign',
		marginClassName: 'line-insert',
		isWholeLine: true
	}),

	lineDelete: ModelDecorationOptions.register({
		className: 'line-delete',
		marginClassName: 'line-delete',
		isWholeLine: true
	}),
	lineDeleteWithSign: ModelDecorationOptions.register({
		className: 'line-delete',
		linesDecorationsClassName: 'delete-sign',
		marginClassName: 'line-delete',
		isWholeLine: true

	}),
	lineDeleteMargin: ModelDecorationOptions.register({
		marginClassName: 'line-delete',
	})

};

A
Alex Dima 已提交
1514
class DiffEdtorWidgetSideBySide extends DiffEditorWidgetStyle implements IDiffEditorWidgetStyle, IVerticalSashLayoutProvider {
E
Erich Gamma 已提交
1515 1516 1517 1518

	static MINIMUM_EDITOR_WIDTH = 100;

	private _disableSash: boolean;
J
Johannes Rieken 已提交
1519 1520 1521 1522
	private _sash: Sash;
	private _sashRatio: number;
	private _sashPosition: number;
	private _startSashPosition: number;
E
Erich Gamma 已提交
1523

J
Johannes Rieken 已提交
1524
	constructor(dataSource: IDataSource, enableSplitViewResizing: boolean) {
E
Erich Gamma 已提交
1525 1526 1527 1528 1529
		super(dataSource);

		this._disableSash = (enableSplitViewResizing === false);
		this._sashRatio = null;
		this._sashPosition = null;
A
Alex Dima 已提交
1530
		this._sash = this._register(new Sash(this._dataSource.getContainerDomNode(), this));
E
Erich Gamma 已提交
1531 1532

		if (this._disableSash) {
J
Joao Moreno 已提交
1533
			this._sash.state = SashState.Disabled;
E
Erich Gamma 已提交
1534 1535
		}

I
isidor 已提交
1536 1537 1538 1539
		this._sash.onDidStart(() => this.onSashDragStart());
		this._sash.onDidChange((e: ISashEvent) => this.onSashDrag(e));
		this._sash.onDidEnd(() => this.onSashDragEnd());
		this._sash.onDidReset(() => this.onSashReset());
E
Erich Gamma 已提交
1540 1541
	}

J
Johannes Rieken 已提交
1542
	public setEnableSplitViewResizing(enableSplitViewResizing: boolean): void {
A
Alex Dima 已提交
1543
		let newDisableSash = (enableSplitViewResizing === false);
E
Erich Gamma 已提交
1544 1545
		if (this._disableSash !== newDisableSash) {
			this._disableSash = newDisableSash;
J
Joao Moreno 已提交
1546
			this._sash.state = this._disableSash ? SashState.Disabled : SashState.Enabled;
E
Erich Gamma 已提交
1547 1548 1549
		}
	}

J
Johannes Rieken 已提交
1550
	public layout(sashRatio: number = this._sashRatio): number {
A
Alex Dima 已提交
1551 1552
		let w = this._dataSource.getWidth();
		let contentWidth = w - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
E
Erich Gamma 已提交
1553

A
Alex Dima 已提交
1554 1555
		let sashPosition = Math.floor((sashRatio || 0.5) * contentWidth);
		let midPoint = Math.floor(0.5 * contentWidth);
E
Erich Gamma 已提交
1556

A
Alex Dima 已提交
1557
		sashPosition = this._disableSash ? midPoint : sashPosition || midPoint;
E
Erich Gamma 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578

		if (contentWidth > DiffEdtorWidgetSideBySide.MINIMUM_EDITOR_WIDTH * 2) {
			if (sashPosition < DiffEdtorWidgetSideBySide.MINIMUM_EDITOR_WIDTH) {
				sashPosition = DiffEdtorWidgetSideBySide.MINIMUM_EDITOR_WIDTH;
			}

			if (sashPosition > contentWidth - DiffEdtorWidgetSideBySide.MINIMUM_EDITOR_WIDTH) {
				sashPosition = contentWidth - DiffEdtorWidgetSideBySide.MINIMUM_EDITOR_WIDTH;
			}
		} else {
			sashPosition = midPoint;
		}

		if (this._sashPosition !== sashPosition) {
			this._sashPosition = sashPosition;
			this._sash.layout();
		}

		return this._sashPosition;
	}

M
Maxime Quandalle 已提交
1579
	private onSashDragStart(): void {
E
Erich Gamma 已提交
1580 1581 1582
		this._startSashPosition = this._sashPosition;
	}

J
Johannes Rieken 已提交
1583
	private onSashDrag(e: ISashEvent): void {
A
Alex Dima 已提交
1584 1585 1586
		let w = this._dataSource.getWidth();
		let contentWidth = w - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
		let sashPosition = this.layout((this._startSashPosition + (e.currentX - e.startX)) / contentWidth);
E
Erich Gamma 已提交
1587 1588 1589 1590 1591 1592

		this._sashRatio = sashPosition / contentWidth;

		this._dataSource.relayoutEditors();
	}

M
Maxime Quandalle 已提交
1593 1594 1595 1596 1597 1598 1599
	private onSashDragEnd(): void {
		this._sash.layout();
	}

	private onSashReset(): void {
		this._sashRatio = 0.5;
		this._dataSource.relayoutEditors();
E
Erich Gamma 已提交
1600 1601 1602
		this._sash.layout();
	}

A
Alex Dima 已提交
1603
	public getVerticalSashTop(sash: Sash): number {
E
Erich Gamma 已提交
1604 1605 1606
		return 0;
	}

A
Alex Dima 已提交
1607
	public getVerticalSashLeft(sash: Sash): number {
E
Erich Gamma 已提交
1608 1609 1610
		return this._sashPosition;
	}

A
Alex Dima 已提交
1611
	public getVerticalSashHeight(sash: Sash): number {
E
Erich Gamma 已提交
1612 1613 1614
		return this._dataSource.getHeight();
	}

1615
	protected _getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsZones {
A
Alex Dima 已提交
1616
		let c = new SideBySideViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ);
E
Erich Gamma 已提交
1617 1618 1619
		return c.getViewZones();
	}

1620
	protected _getOriginalEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
A
Alex Dima 已提交
1621
		const overviewZoneColor = this._removeColor.toString();
E
Erich Gamma 已提交
1622

A
Alex Dima 已提交
1623
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1624 1625
			decorations: [],
			overviewZones: []
P
Peng Lyu 已提交
1626
		};
A
Alex Dima 已提交
1627 1628 1629 1630 1631

		let originalModel = originalEditor.getModel();

		for (let i = 0, length = lineChanges.length; i < length; i++) {
			let lineChange = lineChanges[i];
E
Erich Gamma 已提交
1632 1633

			if (isChangeOrDelete(lineChange)) {
1634 1635
				result.decorations.push({
					range: new Range(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE),
1636
					options: (renderIndicators ? DECORATIONS.lineDeleteWithSign : DECORATIONS.lineDelete)
1637
				});
E
Erich Gamma 已提交
1638
				if (!isChangeOrInsert(lineChange) || !lineChange.charChanges) {
1639
					result.decorations.push(createDecoration(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE, DECORATIONS.charDeleteWholeLine));
E
Erich Gamma 已提交
1640 1641
				}

A
Alex Dima 已提交
1642
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1643 1644
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
A
Alex Dima 已提交
1645
					overviewZoneColor
A
Alex Dima 已提交
1646
				));
E
Erich Gamma 已提交
1647 1648

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1649 1650
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1651 1652
						if (isChangeOrDelete(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1653 1654 1655
								for (let lineNumber = charChange.originalStartLineNumber; lineNumber <= charChange.originalEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
									if (lineNumber === charChange.originalStartLineNumber) {
										startColumn = charChange.originalStartColumn;
									} else {
										startColumn = originalModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.originalEndLineNumber) {
										endColumn = charChange.originalEndColumn;
									} else {
										endColumn = originalModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
1666
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, DECORATIONS.charDelete));
E
Erich Gamma 已提交
1667 1668
								}
							} else {
1669
								result.decorations.push(createDecoration(charChange.originalStartLineNumber, charChange.originalStartColumn, charChange.originalEndLineNumber, charChange.originalEndColumn, DECORATIONS.charDelete));
E
Erich Gamma 已提交
1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
							}
						}
					}
				}
			}
		}

		return result;
	}

1680
	protected _getModifiedEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
A
Alex Dima 已提交
1681
		const overviewZoneColor = this._insertColor.toString();
E
Erich Gamma 已提交
1682

A
Alex Dima 已提交
1683
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1684 1685
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1686 1687 1688 1689 1690 1691
		};

		let modifiedModel = modifiedEditor.getModel();

		for (let i = 0, length = lineChanges.length; i < length; i++) {
			let lineChange = lineChanges[i];
E
Erich Gamma 已提交
1692 1693 1694

			if (isChangeOrInsert(lineChange)) {

1695 1696
				result.decorations.push({
					range: new Range(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE),
1697
					options: (renderIndicators ? DECORATIONS.lineInsertWithSign : DECORATIONS.lineInsert)
1698
				});
E
Erich Gamma 已提交
1699
				if (!isChangeOrDelete(lineChange) || !lineChange.charChanges) {
1700
					result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, DECORATIONS.charInsertWholeLine));
E
Erich Gamma 已提交
1701
				}
A
Alex Dima 已提交
1702
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1703 1704
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
A
Alex Dima 已提交
1705
					overviewZoneColor
A
Alex Dima 已提交
1706
				));
E
Erich Gamma 已提交
1707 1708

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1709 1710
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1711 1712
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1713 1714 1715
								for (let lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
									if (lineNumber === charChange.modifiedStartLineNumber) {
										startColumn = charChange.modifiedStartColumn;
									} else {
										startColumn = modifiedModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.modifiedEndLineNumber) {
										endColumn = charChange.modifiedEndColumn;
									} else {
										endColumn = modifiedModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
1726
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1727 1728
								}
							} else {
1729
								result.decorations.push(createDecoration(charChange.modifiedStartLineNumber, charChange.modifiedStartColumn, charChange.modifiedEndLineNumber, charChange.modifiedEndColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
							}
						}
					}
				}

			}
		}
		return result;
	}
}

class SideBySideViewZonesComputer extends ViewZonesComputer {

A
Alex Dima 已提交
1743
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[]) {
E
Erich Gamma 已提交
1744 1745 1746
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
	}

A
Alex Dima 已提交
1747
	protected _createOriginalMarginDomNodeForModifiedForeignViewZoneInAddedRegion(): HTMLDivElement | null {
A
Alex Dima 已提交
1748 1749 1750
		return null;
	}

A
Alex Dima 已提交
1751
	protected _produceOriginalFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null {
E
Erich Gamma 已提交
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
		if (lineChangeModifiedLength > lineChangeOriginalLength) {
			return {
				afterLineNumber: Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber),
				heightInLines: (lineChangeModifiedLength - lineChangeOriginalLength),
				domNode: null
			};
		}
		return null;
	}

A
Alex Dima 已提交
1762
	protected _produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null {
E
Erich Gamma 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
		if (lineChangeOriginalLength > lineChangeModifiedLength) {
			return {
				afterLineNumber: Math.max(lineChange.modifiedStartLineNumber, lineChange.modifiedEndLineNumber),
				heightInLines: (lineChangeOriginalLength - lineChangeModifiedLength),
				domNode: null
			};
		}
		return null;
	}
}

class DiffEdtorWidgetInline extends DiffEditorWidgetStyle implements IDiffEditorWidgetStyle {

	private decorationsLeft: number;

J
Johannes Rieken 已提交
1778
	constructor(dataSource: IDataSource, enableSplitViewResizing: boolean) {
E
Erich Gamma 已提交
1779 1780
		super(dataSource);

1781
		this.decorationsLeft = dataSource.getOriginalEditor().getLayoutInfo().decorationsLeft;
E
Erich Gamma 已提交
1782

1783
		this._register(dataSource.getOriginalEditor().onDidLayoutChange((layoutInfo: editorOptions.EditorLayoutInfo) => {
E
Erich Gamma 已提交
1784 1785 1786 1787 1788 1789 1790
			if (this.decorationsLeft !== layoutInfo.decorationsLeft) {
				this.decorationsLeft = layoutInfo.decorationsLeft;
				dataSource.relayoutEditors();
			}
		}));
	}

J
Johannes Rieken 已提交
1791
	public setEnableSplitViewResizing(enableSplitViewResizing: boolean): void {
E
Erich Gamma 已提交
1792 1793 1794
		// Nothing to do..
	}

1795
	protected _getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean): IEditorsZones {
1796
		let computer = new InlineViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ, originalEditor, modifiedEditor, renderIndicators);
E
Erich Gamma 已提交
1797 1798 1799
		return computer.getViewZones();
	}

1800
	protected _getOriginalEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
A
Alex Dima 已提交
1801 1802
		const overviewZoneColor = this._removeColor.toString();

A
Alex Dima 已提交
1803
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1804 1805
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1806
		};
E
Erich Gamma 已提交
1807

A
Alex Dima 已提交
1808 1809
		for (let i = 0, length = lineChanges.length; i < length; i++) {
			let lineChange = lineChanges[i];
E
Erich Gamma 已提交
1810 1811 1812

			// Add overview zones in the overview ruler
			if (isChangeOrDelete(lineChange)) {
1813 1814
				result.decorations.push({
					range: new Range(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE),
1815
					options: DECORATIONS.lineDeleteMargin
1816 1817
				});

A
Alex Dima 已提交
1818
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1819 1820
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
A
Alex Dima 已提交
1821
					overviewZoneColor
A
Alex Dima 已提交
1822
				));
E
Erich Gamma 已提交
1823 1824 1825 1826 1827 1828
			}
		}

		return result;
	}

1829
	protected _getModifiedEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
A
Alex Dima 已提交
1830
		const overviewZoneColor = this._insertColor.toString();
E
Erich Gamma 已提交
1831

A
Alex Dima 已提交
1832
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1833 1834
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1835 1836 1837 1838 1839 1840
		};

		let modifiedModel = modifiedEditor.getModel();

		for (let i = 0, length = lineChanges.length; i < length; i++) {
			let lineChange = lineChanges[i];
E
Erich Gamma 已提交
1841 1842 1843

			// Add decorations & overview zones
			if (isChangeOrInsert(lineChange)) {
1844 1845
				result.decorations.push({
					range: new Range(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE),
1846
					options: (renderIndicators ? DECORATIONS.lineInsertWithSign : DECORATIONS.lineInsert)
1847
				});
E
Erich Gamma 已提交
1848

A
Alex Dima 已提交
1849
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1850 1851
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
A
Alex Dima 已提交
1852
					overviewZoneColor
A
Alex Dima 已提交
1853
				));
E
Erich Gamma 已提交
1854 1855

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1856 1857
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1858 1859
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1860 1861 1862
								for (let lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
									if (lineNumber === charChange.modifiedStartLineNumber) {
										startColumn = charChange.modifiedStartColumn;
									} else {
										startColumn = modifiedModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.modifiedEndLineNumber) {
										endColumn = charChange.modifiedEndColumn;
									} else {
										endColumn = modifiedModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
1873
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1874 1875
								}
							} else {
1876
								result.decorations.push(createDecoration(charChange.modifiedStartLineNumber, charChange.modifiedStartColumn, charChange.modifiedEndLineNumber, charChange.modifiedEndColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1877 1878 1879 1880
							}
						}
					}
				} else {
1881
					result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, DECORATIONS.charInsertWholeLine));
E
Erich Gamma 已提交
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
				}
			}
		}

		return result;
	}

	public layout(): number {
		// An editor should not be smaller than 5px
		return Math.max(5, this.decorationsLeft);
	}

}

class InlineViewZonesComputer extends ViewZonesComputer {

A
Alex Dima 已提交
1898
	private originalModel: ITextModel;
1899
	private modifiedEditorConfiguration: editorOptions.InternalEditorOptions;
J
Johannes Rieken 已提交
1900
	private modifiedEditorTabSize: number;
R
rebornix 已提交
1901
	private renderIndicators: boolean;
E
Erich Gamma 已提交
1902

A
Alex Dima 已提交
1903
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean) {
E
Erich Gamma 已提交
1904 1905 1906
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
		this.originalModel = originalEditor.getModel();
		this.modifiedEditorConfiguration = modifiedEditor.getConfiguration();
1907
		this.modifiedEditorTabSize = modifiedEditor.getModel().getOptions().tabSize;
R
rebornix 已提交
1908
		this.renderIndicators = renderIndicators;
E
Erich Gamma 已提交
1909 1910
	}

A
Alex Dima 已提交
1911
	protected _createOriginalMarginDomNodeForModifiedForeignViewZoneInAddedRegion(): HTMLDivElement | null {
A
Alex Dima 已提交
1912 1913 1914 1915 1916
		let result = document.createElement('div');
		result.className = 'inline-added-margin-view-zone';
		return result;
	}

A
Alex Dima 已提交
1917
	protected _produceOriginalFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null {
1918 1919 1920
		let marginDomNode = document.createElement('div');
		marginDomNode.className = 'inline-added-margin-view-zone';

E
Erich Gamma 已提交
1921
		return {
J
Johannes Rieken 已提交
1922
			afterLineNumber: Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber),
E
Erich Gamma 已提交
1923
			heightInLines: lineChangeModifiedLength,
1924 1925
			domNode: document.createElement('div'),
			marginDomNode: marginDomNode
E
Erich Gamma 已提交
1926 1927 1928
		};
	}

A
Alex Dima 已提交
1929
	protected _produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null {
A
Alex Dima 已提交
1930
		let decorations: InlineDecoration[] = [];
E
Erich Gamma 已提交
1931
		if (lineChange.charChanges) {
A
Alex Dima 已提交
1932 1933
			for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
				let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1934
				if (isChangeOrDelete(charChange)) {
1935 1936
					decorations.push(new InlineDecoration(
						new Range(charChange.originalStartLineNumber, charChange.originalStartColumn, charChange.originalEndLineNumber, charChange.originalEndColumn),
1937
						'char-delete',
1938
						InlineDecorationType.Regular
1939
					));
E
Erich Gamma 已提交
1940 1941 1942 1943
				}
			}
		}

1944
		let sb = createStringBuilder(10000);
1945 1946 1947
		let marginHTML: string[] = [];
		let lineDecorationsWidth = this.modifiedEditorConfiguration.layoutInfo.decorationsWidth;
		let lineHeight = this.modifiedEditorConfiguration.lineHeight;
1948 1949
		const typicalHalfwidthCharacterWidth = this.modifiedEditorConfiguration.fontInfo.typicalHalfwidthCharacterWidth;
		let maxCharsPerLine = 0;
A
Alex Dima 已提交
1950
		for (let lineNumber = lineChange.originalStartLineNumber; lineNumber <= lineChange.originalEndLineNumber; lineNumber++) {
1951
			maxCharsPerLine = Math.max(maxCharsPerLine, this._renderOriginalLine(lineNumber - lineChange.originalStartLineNumber, this.originalModel, this.modifiedEditorConfiguration, this.modifiedEditorTabSize, lineNumber, decorations, sb));
1952

R
rebornix 已提交
1953
			if (this.renderIndicators) {
1954 1955 1956 1957 1958
				let index = lineNumber - lineChange.originalStartLineNumber;
				marginHTML = marginHTML.concat([
					`<div class="delete-sign" style="position:absolute;top:${index * lineHeight}px;width:${lineDecorationsWidth}px;height:${lineHeight}px;right:0;"></div>`
				]);
			}
E
Erich Gamma 已提交
1959
		}
1960
		maxCharsPerLine += this.modifiedEditorConfiguration.viewInfo.scrollBeyondLastColumn;
E
Erich Gamma 已提交
1961

A
Alex Dima 已提交
1962
		let domNode = document.createElement('div');
E
Erich Gamma 已提交
1963
		domNode.className = 'view-lines line-delete';
1964
		domNode.innerHTML = sb.build();
1965
		Configuration.applyFontInfoSlow(domNode, this.modifiedEditorConfiguration.fontInfo);
E
Erich Gamma 已提交
1966

1967 1968 1969 1970 1971
		let marginDomNode = document.createElement('div');
		marginDomNode.className = 'inline-deleted-margin-view-zone';
		marginDomNode.innerHTML = marginHTML.join('');
		Configuration.applyFontInfoSlow(marginDomNode, this.modifiedEditorConfiguration.fontInfo);

E
Erich Gamma 已提交
1972 1973 1974 1975
		return {
			shouldNotShrink: true,
			afterLineNumber: (lineChange.modifiedEndLineNumber === 0 ? lineChange.modifiedStartLineNumber : lineChange.modifiedStartLineNumber - 1),
			heightInLines: lineChangeOriginalLength,
1976
			minWidthInPx: (maxCharsPerLine * typicalHalfwidthCharacterWidth),
1977 1978
			domNode: domNode,
			marginDomNode: marginDomNode
E
Erich Gamma 已提交
1979 1980 1981
		};
	}

1982
	private _renderOriginalLine(count: number, originalModel: ITextModel, config: editorOptions.InternalEditorOptions, tabSize: number, lineNumber: number, decorations: InlineDecoration[], sb: IStringBuilder): number {
1983 1984
		const lineTokens = originalModel.getLineTokens(lineNumber);
		const lineContent = lineTokens.getLineContent();
A
Alex Dima 已提交
1985

1986
		const actualDecorations = LineDecoration.filter(decorations, lineNumber, 1, lineContent.length + 1);
A
Alex Dima 已提交
1987

1988 1989 1990 1991 1992 1993 1994 1995 1996
		sb.appendASCIIString('<div class="view-line');
		if (decorations.length === 0) {
			// No char changes
			sb.appendASCIIString(' char-delete');
		}
		sb.appendASCIIString('" style="top:');
		sb.appendASCIIString(String(count * config.lineHeight));
		sb.appendASCIIString('px;width:1000000px;">');

1997 1998
		const isBasicASCII = ViewLineRenderingData.isBasicASCII(lineContent, originalModel.mightContainNonBasicASCII());
		const containsRTL = ViewLineRenderingData.containsRTL(lineContent, isBasicASCII, originalModel.mightContainRTL());
1999
		const output = renderViewLine(new RenderLineInput(
2000
			(config.fontInfo.isMonospace && !config.viewInfo.disableMonospaceOptimizations),
2001
			config.fontInfo.canUseHalfwidthRightwardsArrow,
A
Alex Dima 已提交
2002
			lineContent,
A
Alex Dima 已提交
2003
			false,
2004 2005
			isBasicASCII,
			containsRTL,
2006
			0,
A
Alex Dima 已提交
2007
			lineTokens,
2008
			actualDecorations,
A
Alex Dima 已提交
2009
			tabSize,
2010
			config.fontInfo.spaceWidth,
2011 2012
			config.viewInfo.stopRenderingLineAfter,
			config.viewInfo.renderWhitespace,
2013 2014
			config.viewInfo.renderControlCharacters,
			config.viewInfo.fontLigatures
2015
		), sb);
E
Erich Gamma 已提交
2016

2017
		sb.appendASCIIString('</div>');
2018 2019 2020

		const absoluteOffsets = output.characterMapping.getAbsoluteOffsets();
		return absoluteOffsets.length > 0 ? absoluteOffsets[absoluteOffsets.length - 1] : 0;
E
Erich Gamma 已提交
2021 2022 2023
	}
}

J
Johannes Rieken 已提交
2024
function isChangeOrInsert(lineChange: editorCommon.IChange): boolean {
E
Erich Gamma 已提交
2025 2026 2027
	return lineChange.modifiedEndLineNumber > 0;
}

J
Johannes Rieken 已提交
2028
function isChangeOrDelete(lineChange: editorCommon.IChange): boolean {
E
Erich Gamma 已提交
2029 2030 2031 2032
	return lineChange.originalEndLineNumber > 0;
}

function createFakeLinesDiv(): HTMLElement {
A
Alex Dima 已提交
2033
	let r = document.createElement('div');
E
Erich Gamma 已提交
2034 2035 2036
	r.className = 'diagonal-fill';
	return r;
}
2037 2038

registerThemingParticipant((theme, collector) => {
2039
	const added = theme.getColor(diffInserted);
2040 2041
	if (added) {
		collector.addRule(`.monaco-editor .line-insert, .monaco-editor .char-insert { background-color: ${added}; }`);
A
Alex Dima 已提交
2042
		collector.addRule(`.monaco-diff-editor .line-insert, .monaco-diff-editor .char-insert { background-color: ${added}; }`);
2043 2044
		collector.addRule(`.monaco-editor .inline-added-margin-view-zone { background-color: ${added}; }`);
	}
2045 2046

	const removed = theme.getColor(diffRemoved);
2047 2048
	if (removed) {
		collector.addRule(`.monaco-editor .line-delete, .monaco-editor .char-delete { background-color: ${removed}; }`);
A
Alex Dima 已提交
2049
		collector.addRule(`.monaco-diff-editor .line-delete, .monaco-diff-editor .char-delete { background-color: ${removed}; }`);
2050 2051
		collector.addRule(`.monaco-editor .inline-deleted-margin-view-zone { background-color: ${removed}; }`);
	}
2052 2053

	const addedOutline = theme.getColor(diffInsertedOutline);
2054
	if (addedOutline) {
2055
		collector.addRule(`.monaco-editor .line-insert, .monaco-editor .char-insert { border: 1px ${theme.type === 'hc' ? 'dashed' : 'solid'} ${addedOutline}; }`);
2056
	}
2057 2058

	const removedOutline = theme.getColor(diffRemovedOutline);
2059
	if (removedOutline) {
2060
		collector.addRule(`.monaco-editor .line-delete, .monaco-editor .char-delete { border: 1px ${theme.type === 'hc' ? 'dashed' : 'solid'} ${removedOutline}; }`);
2061
	}
2062 2063

	const shadow = theme.getColor(scrollbarShadow);
2064 2065 2066
	if (shadow) {
		collector.addRule(`.monaco-diff-editor.side-by-side .editor.modified { box-shadow: -6px 0 5px -5px ${shadow}; }`);
	}
2067

M
Matt Bierner 已提交
2068
	const border = theme.getColor(diffBorder);
2069 2070 2071
	if (border) {
		collector.addRule(`.monaco-diff-editor.side-by-side .editor.modified { border-left: 1px solid ${border}; }`);
	}
2072
});