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

A
Alex Dima 已提交
284
		this._diffComputationResult = null;
E
Erich Gamma 已提交
285

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();
		}
A
Alex Dima 已提交
372 373 374 375
		if (this.originalEditor.hasModel()) {
			this._originalOverviewRuler = this.originalEditor.createOverviewRuler('original diffOverviewRuler')!;
			this._overviewDomElement.appendChild(this._originalOverviewRuler.getDomNode());
		}
E
Erich Gamma 已提交
376 377 378 379 380

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

		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);
	}

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

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

			this._layoutOverviewViewport();
A
Alex Dima 已提交
423 424
		}));

A
Alex Dima 已提交
425
		this._register(this.originalEditor.onDidChangeViewZones(() => {
A
Alex Dima 已提交
426 427 428
			this._onViewZonesChanged();
		}));

A
Alex Dima 已提交
429
		this._register(this.originalEditor.onDidChangeModelContent(() => {
A
Alex Dima 已提交
430 431 432 433
			if (this._isVisible) {
				this._beginUpdateDecorationsSoon();
			}
		}));
E
Erich Gamma 已提交
434 435
	}

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

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

			this._layoutOverviewViewport();
		}));

A
Alex Dima 已提交
456
		this._register(this.modifiedEditor.onDidChangeViewZones(() => {
A
Alex Dima 已提交
457 458 459
			this._onViewZonesChanged();
		}));

A
Alex Dima 已提交
460
		this._register(this.modifiedEditor.onDidChangeConfiguration((e) => {
A
Alex Dima 已提交
461 462 463 464 465
			if (e.fontInfo && this.modifiedEditor.getModel()) {
				this._onViewZonesChanged();
			}
		}));

A
Alex Dima 已提交
466
		this._register(this.modifiedEditor.onDidChangeModelContent(() => {
A
Alex Dima 已提交
467 468 469 470
			if (this._isVisible) {
				this._beginUpdateDecorationsSoon();
			}
		}));
E
Erich Gamma 已提交
471 472
	}

A
Alex Dima 已提交
473
	protected _createInnerEditor(instantiationService: IInstantiationService, container: HTMLElement, options: editorOptions.IEditorOptions): CodeEditorWidget {
474
		return instantiationService.createInstance(CodeEditorWidget, container, options, {});
A
Alex Dima 已提交
475 476
	}

E
Erich Gamma 已提交
477
	public dispose(): void {
478 479
		this._codeEditorService.removeDiffEditor(this);

480 481 482 483 484
		if (this._beginUpdateDecorationsTimeout !== -1) {
			window.clearTimeout(this._beginUpdateDecorationsTimeout);
			this._beginUpdateDecorationsTimeout = -1;
		}

E
Erich Gamma 已提交
485 486 487 488
		window.clearInterval(this._measureDomElementToken);

		this._cleanViewZonesAndDecorations();

489 490 491 492 493 494 495 496
		if (this._originalOverviewRuler) {
			this._overviewDomElement.removeChild(this._originalOverviewRuler.getDomNode());
			this._originalOverviewRuler.dispose();
		}
		if (this._modifiedOverviewRuler) {
			this._overviewDomElement.removeChild(this._modifiedOverviewRuler.getDomNode());
			this._modifiedOverviewRuler.dispose();
		}
497 498
		this._overviewDomElement.removeChild(this._overviewViewportDomElement.domNode);
		this._containerDomElement.removeChild(this._overviewDomElement);
E
Erich Gamma 已提交
499

500
		this._containerDomElement.removeChild(this._originalDomNode);
A
Alex Dima 已提交
501
		this.originalEditor.dispose();
502 503

		this._containerDomElement.removeChild(this._modifiedDomNode);
A
Alex Dima 已提交
504
		this.modifiedEditor.dispose();
E
Erich Gamma 已提交
505 506 507

		this._strategy.dispose();

508 509 510
		this._containerDomElement.removeChild(this._reviewPane.domNode.domNode);
		this._containerDomElement.removeChild(this._reviewPane.shadow.domNode);
		this._containerDomElement.removeChild(this._reviewPane.actionBarContainer.domNode);
511 512
		this._reviewPane.dispose();

513 514
		this._domElement.removeChild(this._containerDomElement);

A
Alex Dima 已提交
515
		this._onDidDispose.fire();
A
Alex Dima 已提交
516

E
Erich Gamma 已提交
517 518 519 520 521 522 523 524 525 526
		super.dispose();
	}

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

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

	public getEditorType(): string {
A
Alex Dima 已提交
527
		return editorCommon.EditorType.IDiffEditor;
E
Erich Gamma 已提交
528 529
	}

A
Alex Dima 已提交
530
	public getLineChanges(): editorCommon.ILineChange[] | null {
A
Alex Dima 已提交
531 532 533 534
		if (!this._diffComputationResult) {
			return null;
		}
		return this._diffComputationResult.changes;
E
Erich Gamma 已提交
535 536
	}

A
Alex Dima 已提交
537
	public getOriginalEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
538 539 540
		return this.originalEditor;
	}

A
Alex Dima 已提交
541
	public getModifiedEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
542 543 544
		return this.modifiedEditor;
	}

545
	public updateOptions(newOptions: editorOptions.IDiffEditorOptions): void {
E
Erich Gamma 已提交
546 547

		// Handle side by side
A
Alex Dima 已提交
548
		let renderSideBySideChanged = false;
E
Erich Gamma 已提交
549 550 551 552 553 554 555
		if (typeof newOptions.renderSideBySide !== 'undefined') {
			if (this._renderSideBySide !== newOptions.renderSideBySide) {
				this._renderSideBySide = newOptions.renderSideBySide;
				renderSideBySideChanged = true;
			}
		}

556 557
		let beginUpdateDecorations = false;

E
Erich Gamma 已提交
558 559 560 561
		if (typeof newOptions.ignoreTrimWhitespace !== 'undefined') {
			if (this._ignoreTrimWhitespace !== newOptions.ignoreTrimWhitespace) {
				this._ignoreTrimWhitespace = newOptions.ignoreTrimWhitespace;
				// Begin comparing
562 563 564 565 566 567 568 569
				beginUpdateDecorations = true;
			}
		}

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

573 574 575 576
		if (beginUpdateDecorations) {
			this._beginUpdateDecorations();
		}

577 578 579 580
		if (typeof newOptions.originalEditable !== 'undefined') {
			this._originalIsEditable = Boolean(newOptions.originalEditable);
		}

E
Erich Gamma 已提交
581
		this.modifiedEditor.updateOptions(this._adjustOptionsForRightHandSide(newOptions));
582
		this.originalEditor.updateOptions(this._adjustOptionsForLeftHandSide(newOptions, this._originalIsEditable));
E
Erich Gamma 已提交
583 584 585 586 587 588 589 590 591 592

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

		// renderSideBySide
		if (renderSideBySideChanged) {
			if (this._renderSideBySide) {
M
Matt Bierner 已提交
593
				this._setStrategy(new DiffEdtorWidgetSideBySide(this._createDataSource(), this._enableSplitViewResizing));
E
Erich Gamma 已提交
594 595 596
			} else {
				this._setStrategy(new DiffEdtorWidgetInline(this._createDataSource(), this._enableSplitViewResizing));
			}
597 598
			// Update class name
			this._containerDomElement.className = DiffEditorWidget._getClassName(this._themeService.getTheme(), this._renderSideBySide);
E
Erich Gamma 已提交
599 600 601
		}
	}

A
Alex Dima 已提交
602
	public getModel(): editorCommon.IDiffEditorModel {
E
Erich Gamma 已提交
603
		return {
A
Alex Dima 已提交
604 605
			original: this.originalEditor.getModel()!,
			modified: this.modifiedEditor.getModel()!
E
Erich Gamma 已提交
606 607 608
		};
	}

J
Johannes Rieken 已提交
609
	public setModel(model: editorCommon.IDiffEditorModel): void {
E
Erich Gamma 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		// 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
A
Alex Dima 已提交
629
		this._diffComputationResult = null;
E
Erich Gamma 已提交
630 631 632 633 634 635 636 637
		this._diffComputationToken++;

		if (model) {
			this._recreateOverviewRulers();

			// Begin comparing
			this._beginUpdateDecorations();
		} else {
A
Alex Dima 已提交
638
			this._diffComputationResult = null;
E
Erich Gamma 已提交
639 640 641 642 643 644 645 646 647
		}

		this._layoutOverviewViewport();
	}

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

A
Alex Dima 已提交
648
	public getVisibleColumnFromPosition(position: IPosition): number {
E
Erich Gamma 已提交
649 650 651
		return this.modifiedEditor.getVisibleColumnFromPosition(position);
	}

A
Alex Dima 已提交
652
	public getPosition(): Position | null {
E
Erich Gamma 已提交
653 654 655
		return this.modifiedEditor.getPosition();
	}

656 657
	public setPosition(position: IPosition): void {
		this.modifiedEditor.setPosition(position);
E
Erich Gamma 已提交
658 659
	}

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

664 665
	public revealLineInCenter(lineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLineInCenter(lineNumber, scrollType);
E
Erich Gamma 已提交
666 667
	}

668 669
	public revealLineInCenterIfOutsideViewport(lineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLineInCenterIfOutsideViewport(lineNumber, scrollType);
E
Erich Gamma 已提交
670 671
	}

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

676 677
	public revealPositionInCenter(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealPositionInCenter(position, scrollType);
E
Erich Gamma 已提交
678 679
	}

680 681
	public revealPositionInCenterIfOutsideViewport(position: IPosition, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealPositionInCenterIfOutsideViewport(position, scrollType);
E
Erich Gamma 已提交
682 683
	}

A
Alex Dima 已提交
684
	public getSelection(): Selection | null {
E
Erich Gamma 已提交
685 686 687
		return this.modifiedEditor.getSelection();
	}

A
Alex Dima 已提交
688
	public getSelections(): Selection[] | null {
E
Erich Gamma 已提交
689 690 691
		return this.modifiedEditor.getSelections();
	}

692 693 694 695 696 697
	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 已提交
698 699
	}

A
Alex Dima 已提交
700
	public setSelections(ranges: ISelection[]): void {
E
Erich Gamma 已提交
701 702 703
		this.modifiedEditor.setSelections(ranges);
	}

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

708 709
	public revealLinesInCenter(startLineNumber: number, endLineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLinesInCenter(startLineNumber, endLineNumber, scrollType);
E
Erich Gamma 已提交
710 711
	}

712 713
	public revealLinesInCenterIfOutsideViewport(startLineNumber: number, endLineNumber: number, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealLinesInCenterIfOutsideViewport(startLineNumber, endLineNumber, scrollType);
E
Erich Gamma 已提交
714 715
	}

716 717
	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 已提交
718 719
	}

720 721
	public revealRangeInCenter(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealRangeInCenter(range, scrollType);
E
Erich Gamma 已提交
722 723
	}

724 725
	public revealRangeInCenterIfOutsideViewport(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealRangeInCenterIfOutsideViewport(range, scrollType);
E
Erich Gamma 已提交
726 727
	}

728 729
	public revealRangeAtTop(range: IRange, scrollType: editorCommon.ScrollType = editorCommon.ScrollType.Smooth): void {
		this.modifiedEditor.revealRangeAtTop(range, scrollType);
730 731
	}

A
Alex Dima 已提交
732
	public getSupportedActions(): editorCommon.IEditorAction[] {
733 734 735
		return this.modifiedEditor.getSupportedActions();
	}

A
Alex Dima 已提交
736
	public saveViewState(): editorCommon.IDiffEditorViewState {
A
Alex Dima 已提交
737 738
		let originalViewState = this.originalEditor.saveViewState();
		let modifiedViewState = this.modifiedEditor.saveViewState();
E
Erich Gamma 已提交
739 740 741 742 743 744
		return {
			original: originalViewState,
			modified: modifiedViewState
		};
	}

A
Alex Dima 已提交
745
	public restoreViewState(s: editorCommon.IDiffEditorViewState): void {
E
Erich Gamma 已提交
746
		if (s.original && s.original) {
A
Alex Dima 已提交
747
			let diffEditorState = <editorCommon.IDiffEditorViewState>s;
E
Erich Gamma 已提交
748 749 750 751 752
			this.originalEditor.restoreViewState(diffEditorState.original);
			this.modifiedEditor.restoreViewState(diffEditorState.modified);
		}
	}

J
Johannes Rieken 已提交
753
	public layout(dimension?: editorCommon.IDimension): void {
E
Erich Gamma 已提交
754 755 756 757 758 759 760
		this._measureDomElement(false, dimension);
	}

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

A
Alex Dima 已提交
761 762
	public hasTextFocus(): boolean {
		return this.originalEditor.hasTextFocus() || this.modifiedEditor.hasTextFocus();
E
Erich Gamma 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	}

	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 已提交
781
	public trigger(source: string, handlerId: string, payload: any): void {
E
Erich Gamma 已提交
782 783 784
		this.modifiedEditor.trigger(source, handlerId, payload);
	}

785
	public changeDecorations(callback: (changeAccessor: IModelDecorationsChangeAccessor) => any): any {
E
Erich Gamma 已提交
786 787 788 789 790 791 792 793 794
		return this.modifiedEditor.changeDecorations(callback);
	}

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



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

J
Johannes Rieken 已提交
795
	private _measureDomElement(forceDoLayoutCall: boolean, dimensions?: editorCommon.IDimension): void {
796 797 798 799
		dimensions = dimensions || {
			width: this._containerDomElement.clientWidth,
			height: this._containerDomElement.clientHeight
		};
E
Erich Gamma 已提交
800 801

		if (dimensions.width <= 0) {
802 803
			this._width = 0;
			this._height = 0;
A
Alex Dima 已提交
804
			this._reviewHeight = 0;
E
Erich Gamma 已提交
805 806 807 808 809 810 811 812 813 814
			return;
		}

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

		this._width = dimensions.width;
		this._height = dimensions.height;
815
		this._reviewHeight = this._reviewPane.isVisible() ? this._height : 0;
E
Erich Gamma 已提交
816 817 818 819 820

		this._doLayout();
	}

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

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

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

A
Alex Dima 已提交
848 849 850 851 852 853 854 855 856
	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);
	}

857 858
	private _lastOriginalWarning: URI | null = null;
	private _lastModifiedWarning: URI | null = null;
859

A
Alex Dima 已提交
860
	private static _equals(a: URI | null, b: URI | null): boolean {
861 862 863 864 865 866 867 868 869
		if (!a && !b) {
			return true;
		}
		if (!a || !b) {
			return false;
		}
		return (a.toString() === b.toString());
	}

E
Erich Gamma 已提交
870 871
	private _beginUpdateDecorations(): void {
		this._beginUpdateDecorationsTimeout = -1;
872 873 874
		const currentOriginalModel = this.originalEditor.getModel();
		const currentModifiedModel = this.modifiedEditor.getModel();
		if (!currentOriginalModel || !currentModifiedModel) {
E
Erich Gamma 已提交
875 876 877 878 879 880 881
			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 已提交
882
		let currentToken = this._diffComputationToken;
E
Erich Gamma 已提交
883

884 885 886 887 888 889 890
		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;
891
				this._notificationService.warn(nls.localize("diff.tooLarge", "Cannot compare files because one file is too large."));
892 893 894 895
			}
			return;
		}

896
		this._editorWorkerService.computeDiff(currentOriginalModel.uri, currentModifiedModel.uri, this._ignoreTrimWhitespace).then((result) => {
897 898 899
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
J
Johannes Rieken 已提交
900
			) {
A
Alex Dima 已提交
901
				this._diffComputationResult = result;
902
				this._updateDecorationsRunner.schedule();
A
Alex Dima 已提交
903
				this._onDidUpdateDiff.fire();
904 905 906 907 908
			}
		}, (error) => {
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
J
Johannes Rieken 已提交
909
			) {
A
Alex Dima 已提交
910
				this._diffComputationResult = null;
E
Erich Gamma 已提交
911 912
				this._updateDecorationsRunner.schedule();
			}
913
		});
E
Erich Gamma 已提交
914 915 916 917 918 919 920 921
	}

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

	private _updateDecorations(): void {
922 923 924
		if (!this.originalEditor.getModel() || !this.modifiedEditor.getModel()) {
			return;
		}
A
Alex Dima 已提交
925
		const lineChanges = (this._diffComputationResult ? this._diffComputationResult.changes : []);
E
Erich Gamma 已提交
926

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

930
		let diffDecorations = this._strategy.getEditorsDiffDecorations(lineChanges, this._ignoreTrimWhitespace, this._renderIndicators, foreignOriginal, foreignModified, this.originalEditor, this.modifiedEditor);
E
Erich Gamma 已提交
931 932 933

		try {
			this._currentlyChangingViewZones = true;
A
Alex Dima 已提交
934 935
			this._originalEditorState.apply(this.originalEditor, this._originalOverviewRuler, diffDecorations.original, false);
			this._modifiedEditorState.apply(this.modifiedEditor, this._modifiedOverviewRuler, diffDecorations.modified, true);
E
Erich Gamma 已提交
936 937 938 939 940
		} finally {
			this._currentlyChangingViewZones = false;
		}
	}

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

960
	private _adjustOptionsForLeftHandSide(options: editorOptions.IDiffEditorOptions, isEditable: boolean): editorOptions.IEditorOptions {
961 962 963
		let result = this._adjustOptionsForSubEditor(options);
		result.readOnly = !isEditable;
		result.overviewRulerLanes = 1;
964
		result.extraEditorClassName = 'original-in-monaco-diff-editor';
965 966 967
		return result;
	}

968
	private _adjustOptionsForRightHandSide(options: editorOptions.IDiffEditorOptions): editorOptions.IEditorOptions {
969
		let result = this._adjustOptionsForSubEditor(options);
A
Alex Dima 已提交
970
		result.revealHorizontalRightPadding = editorOptions.EDITOR_DEFAULTS.viewInfo.revealHorizontalRightPadding + DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
A
Alex Dima 已提交
971
		result.scrollbar!.verticalHasArrows = false;
972
		result.extraEditorClassName = 'modified-in-monaco-diff-editor';
973
		return result;
E
Erich Gamma 已提交
974 975
	}

976 977 978 979
	public doLayout(): void {
		this._measureDomElement(true);
	}

E
Erich Gamma 已提交
980
	private _doLayout(): void {
A
Alex Dima 已提交
981
		let splitPoint = this._strategy.layout();
E
Erich Gamma 已提交
982 983 984 985 986 987 988 989

		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 已提交
990
		this._overviewDomElement.style.height = (this._height - this._reviewHeight) + 'px';
E
Erich Gamma 已提交
991 992
		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 已提交
993 994
		this._overviewViewportDomElement.setWidth(DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH);
		this._overviewViewportDomElement.setHeight(30);
E
Erich Gamma 已提交
995

A
Alex Dima 已提交
996 997
		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 已提交
998 999 1000 1001 1002

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

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

E
Erich Gamma 已提交
1005 1006 1007 1008
		this._layoutOverviewViewport();
	}

	private _layoutOverviewViewport(): void {
A
Alex Dima 已提交
1009
		let layout = this._computeOverviewViewport();
E
Erich Gamma 已提交
1010
		if (!layout) {
A
Alex Dima 已提交
1011 1012
			this._overviewViewportDomElement.setTop(0);
			this._overviewViewportDomElement.setHeight(0);
E
Erich Gamma 已提交
1013
		} else {
A
Alex Dima 已提交
1014 1015
			this._overviewViewportDomElement.setTop(layout.top);
			this._overviewViewportDomElement.setHeight(layout.height);
E
Erich Gamma 已提交
1016 1017 1018
		}
	}

A
Alex Dima 已提交
1019
	private _computeOverviewViewport(): { height: number; top: number; } | null {
A
Alex Dima 已提交
1020
		let layoutInfo = this.modifiedEditor.getLayoutInfo();
E
Erich Gamma 已提交
1021 1022 1023 1024
		if (!layoutInfo) {
			return null;
		}

A
Alex Dima 已提交
1025 1026
		let scrollTop = this.modifiedEditor.getScrollTop();
		let scrollHeight = this.modifiedEditor.getScrollHeight();
E
Erich Gamma 已提交
1027

A
Alex Dima 已提交
1028 1029 1030
		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 已提交
1031

1032
		let computedSliderSize = Math.max(0, Math.floor(layoutInfo.contentHeight * computedRatio));
A
Alex Dima 已提交
1033
		let computedSliderPosition = Math.floor(scrollTop * computedRatio);
E
Erich Gamma 已提交
1034 1035 1036 1037 1038 1039 1040

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

J
Johannes Rieken 已提交
1041
	private _createDataSource(): IDataSource {
E
Erich Gamma 已提交
1042 1043 1044 1045 1046 1047
		return {
			getWidth: () => {
				return this._width;
			},

			getHeight: () => {
A
Alex Dima 已提交
1048
				return (this._height - this._reviewHeight);
E
Erich Gamma 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
			},

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

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

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

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

J
Johannes Rieken 已提交
1069
	private _setStrategy(newStrategy: IDiffEditorWidgetStyle): void {
E
Erich Gamma 已提交
1070 1071 1072 1073 1074
		if (this._strategy) {
			this._strategy.dispose();
		}

		this._strategy = newStrategy;
1075
		newStrategy.applyColors(this._themeService.getTheme());
E
Erich Gamma 已提交
1076

A
Alex Dima 已提交
1077
		if (this._diffComputationResult) {
E
Erich Gamma 已提交
1078 1079 1080 1081 1082 1083 1084
			this._updateDecorations();
		}

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

A
Alex Dima 已提交
1085
	private _getLineChangeAtOrBeforeLineNumber(lineNumber: number, startLineNumberExtractor: (lineChange: editorCommon.ILineChange) => number): editorCommon.ILineChange | null {
A
Alex Dima 已提交
1086 1087
		const lineChanges = (this._diffComputationResult ? this._diffComputationResult.changes : []);
		if (lineChanges.length === 0 || lineNumber < startLineNumberExtractor(lineChanges[0])) {
E
Erich Gamma 已提交
1088 1089 1090 1091
			// There are no changes or `lineNumber` is before the first change
			return null;
		}

A
Alex Dima 已提交
1092
		let min = 0, max = lineChanges.length - 1;
E
Erich Gamma 已提交
1093
		while (min < max) {
A
Alex Dima 已提交
1094
			let mid = Math.floor((min + max) / 2);
A
Alex Dima 已提交
1095 1096
			let midStart = startLineNumberExtractor(lineChanges[mid]);
			let midEnd = (mid + 1 <= max ? startLineNumberExtractor(lineChanges[mid + 1]) : Number.MAX_VALUE);
E
Erich Gamma 已提交
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

			if (lineNumber < midStart) {
				max = mid - 1;
			} else if (lineNumber >= midEnd) {
				min = mid + 1;
			} else {
				// HIT!
				min = mid;
				max = mid;
			}
		}
A
Alex Dima 已提交
1108
		return lineChanges[min];
E
Erich Gamma 已提交
1109 1110 1111
	}

	private _getEquivalentLineForOriginalLineNumber(lineNumber: number): number {
A
Alex Dima 已提交
1112
		let lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.originalStartLineNumber);
E
Erich Gamma 已提交
1113 1114 1115 1116 1117

		if (!lineChange) {
			return lineNumber;
		}

A
Alex Dima 已提交
1118 1119 1120 1121
		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 已提交
1122 1123


A
Alex Dima 已提交
1124
		let delta = lineNumber - originalEquivalentLineNumber;
E
Erich Gamma 已提交
1125 1126 1127 1128 1129

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

J
Johannes Rieken 已提交
1130
		return modifiedEquivalentLineNumber + lineChangeModifiedLength - lineChangeOriginalLength + delta;
E
Erich Gamma 已提交
1131 1132 1133
	}

	private _getEquivalentLineForModifiedLineNumber(lineNumber: number): number {
A
Alex Dima 已提交
1134
		let lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.modifiedStartLineNumber);
E
Erich Gamma 已提交
1135 1136 1137 1138 1139

		if (!lineChange) {
			return lineNumber;
		}

A
Alex Dima 已提交
1140 1141 1142 1143
		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 已提交
1144 1145


A
Alex Dima 已提交
1146
		let delta = lineNumber - modifiedEquivalentLineNumber;
E
Erich Gamma 已提交
1147 1148 1149 1150 1151

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

J
Johannes Rieken 已提交
1152
		return originalEquivalentLineNumber + lineChangeOriginalLength - lineChangeModifiedLength + delta;
E
Erich Gamma 已提交
1153 1154
	}

A
Alex Dima 已提交
1155
	public getDiffLineInformationForOriginal(lineNumber: number): editorBrowser.IDiffLineInformation | null {
A
Alex Dima 已提交
1156
		if (!this._diffComputationResult) {
E
Erich Gamma 已提交
1157 1158 1159 1160 1161 1162 1163 1164
			// Cannot answer that which I don't know
			return null;
		}
		return {
			equivalentLineNumber: this._getEquivalentLineForOriginalLineNumber(lineNumber)
		};
	}

A
Alex Dima 已提交
1165
	public getDiffLineInformationForModified(lineNumber: number): editorBrowser.IDiffLineInformation | null {
A
Alex Dima 已提交
1166
		if (!this._diffComputationResult) {
E
Erich Gamma 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
			// 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 已提交
1182 1183
	getOriginalEditor(): editorBrowser.ICodeEditor;
	getModifiedEditor(): editorBrowser.ICodeEditor;
E
Erich Gamma 已提交
1184 1185
}

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

J
Johannes Rieken 已提交
1188
	_dataSource: IDataSource;
1189 1190
	_insertColor: Color;
	_removeColor: Color;
E
Erich Gamma 已提交
1191

J
Johannes Rieken 已提交
1192
	constructor(dataSource: IDataSource) {
A
Alex Dima 已提交
1193
		super();
E
Erich Gamma 已提交
1194 1195 1196
		this._dataSource = dataSource;
	}

1197 1198 1199 1200 1201 1202 1203 1204 1205
	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 已提交
1206
	public getEditorsDiffDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalWhitespaces: IEditorWhitespace[], modifiedWhitespaces: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsDiffDecorationsWithZones {
E
Erich Gamma 已提交
1207 1208 1209 1210 1211 1212 1213
		// Get view zones
		modifiedWhitespaces = modifiedWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
		originalWhitespaces = originalWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
1214
		let zones = this._getViewZones(lineChanges, originalWhitespaces, modifiedWhitespaces, originalEditor, modifiedEditor, renderIndicators);
E
Erich Gamma 已提交
1215 1216

		// Get decorations & overview ruler zones
1217 1218
		let originalDecorations = this._getOriginalEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor);
		let modifiedDecorations = this._getModifiedEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor);
E
Erich Gamma 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

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

1234 1235 1236
	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 已提交
1237 1238 1239

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

A
Alex Dima 已提交
1242
interface IMyViewZone {
E
Erich Gamma 已提交
1243
	shouldNotShrink?: boolean;
A
Alex Dima 已提交
1244 1245 1246 1247 1248
	afterLineNumber: number;
	heightInLines: number;
	minWidthInPx?: number;
	domNode: HTMLElement | null;
	marginDomNode?: HTMLElement | null;
E
Erich Gamma 已提交
1249 1250 1251 1252 1253
}

class ForeignViewZonesIterator {

	private _index: number;
A
Alex Dima 已提交
1254
	private _source: IEditorWhitespace[];
A
Alex Dima 已提交
1255
	public current: IEditorWhitespace | null;
E
Erich Gamma 已提交
1256

A
Alex Dima 已提交
1257
	constructor(source: IEditorWhitespace[]) {
E
Erich Gamma 已提交
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
		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;
		}
	}
}

1273
abstract class ViewZonesComputer {
E
Erich Gamma 已提交
1274

J
Johannes Rieken 已提交
1275
	private lineChanges: editorCommon.ILineChange[];
A
Alex Dima 已提交
1276 1277
	private originalForeignVZ: IEditorWhitespace[];
	private modifiedForeignVZ: IEditorWhitespace[];
E
Erich Gamma 已提交
1278

A
Alex Dima 已提交
1279
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[]) {
E
Erich Gamma 已提交
1280 1281 1282 1283 1284 1285
		this.lineChanges = lineChanges;
		this.originalForeignVZ = originalForeignVZ;
		this.modifiedForeignVZ = modifiedForeignVZ;
	}

	public getViewZones(): IEditorsZones {
A
Alex Dima 已提交
1286
		let result: { original: IMyViewZone[]; modified: IMyViewZone[]; } = {
E
Erich Gamma 已提交
1287 1288 1289 1290
			original: [],
			modified: []
		};

A
Alex Dima 已提交
1291 1292 1293 1294 1295 1296 1297 1298
		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 已提交
1299 1300 1301
			return a.afterLineNumber - b.afterLineNumber;
		};

A
Alex Dima 已提交
1302
		let addAndCombineIfPossible = (destination: IMyViewZone[], item: IMyViewZone) => {
E
Erich Gamma 已提交
1303
			if (item.domNode === null && destination.length > 0) {
A
Alex Dima 已提交
1304
				let lastItem = destination[destination.length - 1];
E
Erich Gamma 已提交
1305 1306 1307 1308 1309 1310 1311 1312
				if (lastItem.afterLineNumber === item.afterLineNumber && lastItem.domNode === null) {
					lastItem.heightInLines += item.heightInLines;
					return;
				}
			}
			destination.push(item);
		};

A
Alex Dima 已提交
1313 1314
		let modifiedForeignVZ = new ForeignViewZonesIterator(this.modifiedForeignVZ);
		let originalForeignVZ = new ForeignViewZonesIterator(this.originalForeignVZ);
E
Erich Gamma 已提交
1315 1316

		// 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 已提交
1317 1318
		for (let i = 0, length = this.lineChanges.length; i <= length; i++) {
			let lineChange = (i < length ? this.lineChanges[i] : null);
E
Erich Gamma 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335

			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 已提交
1336 1337
			let stepOriginal: IMyViewZone[] = [];
			let stepModified: IMyViewZone[] = [];
E
Erich Gamma 已提交
1338 1339 1340 1341 1342

			// ---------------------------- 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 已提交
1343
				let viewZoneLineNumber: number;
E
Erich Gamma 已提交
1344 1345 1346 1347 1348
				if (modifiedForeignVZ.current.afterLineNumber <= modifiedEquivalentLineNumber) {
					viewZoneLineNumber = originalEquivalentLineNumber - modifiedEquivalentLineNumber + modifiedForeignVZ.current.afterLineNumber;
				} else {
					viewZoneLineNumber = originalEndEquivalentLineNumber;
				}
A
Alex Dima 已提交
1349

1350
				let marginDomNode: HTMLDivElement | null = null;
A
Alex Dima 已提交
1351 1352 1353 1354
				if (lineChange && lineChange.modifiedStartLineNumber <= modifiedForeignVZ.current.afterLineNumber && modifiedForeignVZ.current.afterLineNumber <= lineChange.modifiedEndLineNumber) {
					marginDomNode = this._createOriginalMarginDomNodeForModifiedForeignViewZoneInAddedRegion();
				}

E
Erich Gamma 已提交
1355 1356 1357
				stepOriginal.push({
					afterLineNumber: viewZoneLineNumber,
					heightInLines: modifiedForeignVZ.current.heightInLines,
A
Alex Dima 已提交
1358 1359
					domNode: null,
					marginDomNode: marginDomNode
E
Erich Gamma 已提交
1360 1361 1362 1363 1364 1365
				});
				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 已提交
1366
				let viewZoneLineNumber: number;
E
Erich Gamma 已提交
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
				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 已提交
1381
				let r = this._produceOriginalFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
E
Erich Gamma 已提交
1382 1383 1384 1385 1386 1387
				if (r) {
					stepOriginal.push(r);
				}
			}

			if (lineChange !== null && isChangeOrDelete(lineChange)) {
A
Alex Dima 已提交
1388
				let r = this._produceModifiedFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
E
Erich Gamma 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
				if (r) {
					stepModified.push(r);
				}
			}

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


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

			// [CANCEL & EMIT] Try to cancel view zones out
A
Alex Dima 已提交
1400 1401
			let stepOriginalIndex = 0;
			let stepModifiedIndex = 0;
E
Erich Gamma 已提交
1402 1403 1404 1405 1406

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

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

A
Alex Dima 已提交
1410 1411
				let originalDelta = original.afterLineNumber - originalEquivalentLineNumber;
				let modifiedDelta = modified.afterLineNumber - modifiedEquivalentLineNumber;
E
Erich Gamma 已提交
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 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

				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 已提交
1453 1454 1455 1456 1457 1458 1459 1460
		return {
			original: ViewZonesComputer._ensureDomNodes(result.original),
			modified: ViewZonesComputer._ensureDomNodes(result.modified),
		};
	}

	private static _ensureDomNodes(zones: IMyViewZone[]): editorBrowser.IViewZone[] {
		return zones.map((z) => {
E
Erich Gamma 已提交
1461 1462 1463
			if (!z.domNode) {
				z.domNode = createFakeLinesDiv();
			}
A
Alex Dima 已提交
1464 1465
			return <editorBrowser.IViewZone>z;
		});
E
Erich Gamma 已提交
1466 1467
	}

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

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

A
Alex Dima 已提交
1472
	protected abstract _produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null;
E
Erich Gamma 已提交
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 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
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 已提交
1530
class DiffEdtorWidgetSideBySide extends DiffEditorWidgetStyle implements IDiffEditorWidgetStyle, IVerticalSashLayoutProvider {
E
Erich Gamma 已提交
1531 1532 1533 1534

	static MINIMUM_EDITOR_WIDTH = 100;

	private _disableSash: boolean;
J
Johannes Rieken 已提交
1535
	private _sash: Sash;
A
Alex Dima 已提交
1536 1537
	private _sashRatio: number | null;
	private _sashPosition: number | null;
J
Johannes Rieken 已提交
1538
	private _startSashPosition: number;
E
Erich Gamma 已提交
1539

J
Johannes Rieken 已提交
1540
	constructor(dataSource: IDataSource, enableSplitViewResizing: boolean) {
E
Erich Gamma 已提交
1541 1542 1543 1544 1545
		super(dataSource);

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

		if (this._disableSash) {
J
Joao Moreno 已提交
1549
			this._sash.state = SashState.Disabled;
E
Erich Gamma 已提交
1550 1551
		}

I
isidor 已提交
1552 1553 1554 1555
		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 已提交
1556 1557
	}

J
Johannes Rieken 已提交
1558
	public setEnableSplitViewResizing(enableSplitViewResizing: boolean): void {
A
Alex Dima 已提交
1559
		let newDisableSash = (enableSplitViewResizing === false);
E
Erich Gamma 已提交
1560 1561
		if (this._disableSash !== newDisableSash) {
			this._disableSash = newDisableSash;
J
Joao Moreno 已提交
1562
			this._sash.state = this._disableSash ? SashState.Disabled : SashState.Enabled;
E
Erich Gamma 已提交
1563 1564 1565
		}
	}

A
Alex Dima 已提交
1566
	public layout(sashRatio: number | null = this._sashRatio): number {
A
Alex Dima 已提交
1567 1568
		let w = this._dataSource.getWidth();
		let contentWidth = w - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
E
Erich Gamma 已提交
1569

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

A
Alex Dima 已提交
1573
		sashPosition = this._disableSash ? midPoint : sashPosition || midPoint;
E
Erich Gamma 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594

		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 已提交
1595
	private onSashDragStart(): void {
A
Alex Dima 已提交
1596
		this._startSashPosition = this._sashPosition!;
E
Erich Gamma 已提交
1597 1598
	}

J
Johannes Rieken 已提交
1599
	private onSashDrag(e: ISashEvent): void {
A
Alex Dima 已提交
1600 1601 1602
		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 已提交
1603 1604 1605 1606 1607 1608

		this._sashRatio = sashPosition / contentWidth;

		this._dataSource.relayoutEditors();
	}

M
Maxime Quandalle 已提交
1609 1610 1611 1612 1613 1614 1615
	private onSashDragEnd(): void {
		this._sash.layout();
	}

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

A
Alex Dima 已提交
1619
	public getVerticalSashTop(sash: Sash): number {
E
Erich Gamma 已提交
1620 1621 1622
		return 0;
	}

A
Alex Dima 已提交
1623
	public getVerticalSashLeft(sash: Sash): number {
A
Alex Dima 已提交
1624
		return this._sashPosition!;
E
Erich Gamma 已提交
1625 1626
	}

A
Alex Dima 已提交
1627
	public getVerticalSashHeight(sash: Sash): number {
E
Erich Gamma 已提交
1628 1629 1630
		return this._dataSource.getHeight();
	}

1631
	protected _getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsZones {
A
Alex Dima 已提交
1632
		let c = new SideBySideViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ);
E
Erich Gamma 已提交
1633 1634 1635
		return c.getViewZones();
	}

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

A
Alex Dima 已提交
1639
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1640 1641
			decorations: [],
			overviewZones: []
P
Peng Lyu 已提交
1642
		};
A
Alex Dima 已提交
1643

A
Alex Dima 已提交
1644
		let originalModel = originalEditor.getModel()!;
A
Alex Dima 已提交
1645 1646 1647

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

			if (isChangeOrDelete(lineChange)) {
1650 1651
				result.decorations.push({
					range: new Range(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE),
1652
					options: (renderIndicators ? DECORATIONS.lineDeleteWithSign : DECORATIONS.lineDelete)
1653
				});
E
Erich Gamma 已提交
1654
				if (!isChangeOrInsert(lineChange) || !lineChange.charChanges) {
1655
					result.decorations.push(createDecoration(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE, DECORATIONS.charDeleteWholeLine));
E
Erich Gamma 已提交
1656 1657
				}

A
Alex Dima 已提交
1658
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1659 1660
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
A
Alex Dima 已提交
1661
					overviewZoneColor
A
Alex Dima 已提交
1662
				));
E
Erich Gamma 已提交
1663 1664

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1665 1666
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1667 1668
						if (isChangeOrDelete(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1669 1670 1671
								for (let lineNumber = charChange.originalStartLineNumber; lineNumber <= charChange.originalEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
									if (lineNumber === charChange.originalStartLineNumber) {
										startColumn = charChange.originalStartColumn;
									} else {
										startColumn = originalModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.originalEndLineNumber) {
										endColumn = charChange.originalEndColumn;
									} else {
										endColumn = originalModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
1682
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, DECORATIONS.charDelete));
E
Erich Gamma 已提交
1683 1684
								}
							} else {
1685
								result.decorations.push(createDecoration(charChange.originalStartLineNumber, charChange.originalStartColumn, charChange.originalEndLineNumber, charChange.originalEndColumn, DECORATIONS.charDelete));
E
Erich Gamma 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
							}
						}
					}
				}
			}
		}

		return result;
	}

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

A
Alex Dima 已提交
1699
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1700 1701
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1702 1703
		};

A
Alex Dima 已提交
1704
		let modifiedModel = modifiedEditor.getModel()!;
A
Alex Dima 已提交
1705 1706 1707

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

			if (isChangeOrInsert(lineChange)) {

1711 1712
				result.decorations.push({
					range: new Range(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE),
1713
					options: (renderIndicators ? DECORATIONS.lineInsertWithSign : DECORATIONS.lineInsert)
1714
				});
E
Erich Gamma 已提交
1715
				if (!isChangeOrDelete(lineChange) || !lineChange.charChanges) {
1716
					result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, DECORATIONS.charInsertWholeLine));
E
Erich Gamma 已提交
1717
				}
A
Alex Dima 已提交
1718
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1719 1720
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
A
Alex Dima 已提交
1721
					overviewZoneColor
A
Alex Dima 已提交
1722
				));
E
Erich Gamma 已提交
1723 1724

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1725 1726
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1727 1728
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1729 1730 1731
								for (let lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
									if (lineNumber === charChange.modifiedStartLineNumber) {
										startColumn = charChange.modifiedStartColumn;
									} else {
										startColumn = modifiedModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.modifiedEndLineNumber) {
										endColumn = charChange.modifiedEndColumn;
									} else {
										endColumn = modifiedModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
1742
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1743 1744
								}
							} else {
1745
								result.decorations.push(createDecoration(charChange.modifiedStartLineNumber, charChange.modifiedStartColumn, charChange.modifiedEndLineNumber, charChange.modifiedEndColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
							}
						}
					}
				}

			}
		}
		return result;
	}
}

class SideBySideViewZonesComputer extends ViewZonesComputer {

A
Alex Dima 已提交
1759
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[]) {
E
Erich Gamma 已提交
1760 1761 1762
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
	}

A
Alex Dima 已提交
1763
	protected _createOriginalMarginDomNodeForModifiedForeignViewZoneInAddedRegion(): HTMLDivElement | null {
A
Alex Dima 已提交
1764 1765 1766
		return null;
	}

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

A
Alex Dima 已提交
1778
	protected _produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null {
E
Erich Gamma 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
		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 已提交
1794
	constructor(dataSource: IDataSource, enableSplitViewResizing: boolean) {
E
Erich Gamma 已提交
1795 1796
		super(dataSource);

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

1799
		this._register(dataSource.getOriginalEditor().onDidLayoutChange((layoutInfo: editorOptions.EditorLayoutInfo) => {
E
Erich Gamma 已提交
1800 1801 1802 1803 1804 1805 1806
			if (this.decorationsLeft !== layoutInfo.decorationsLeft) {
				this.decorationsLeft = layoutInfo.decorationsLeft;
				dataSource.relayoutEditors();
			}
		}));
	}

J
Johannes Rieken 已提交
1807
	public setEnableSplitViewResizing(enableSplitViewResizing: boolean): void {
E
Erich Gamma 已提交
1808 1809 1810
		// Nothing to do..
	}

1811
	protected _getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean): IEditorsZones {
1812
		let computer = new InlineViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ, originalEditor, modifiedEditor, renderIndicators);
E
Erich Gamma 已提交
1813 1814 1815
		return computer.getViewZones();
	}

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

A
Alex Dima 已提交
1819
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1820 1821
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1822
		};
E
Erich Gamma 已提交
1823

A
Alex Dima 已提交
1824 1825
		for (let i = 0, length = lineChanges.length; i < length; i++) {
			let lineChange = lineChanges[i];
E
Erich Gamma 已提交
1826 1827 1828

			// Add overview zones in the overview ruler
			if (isChangeOrDelete(lineChange)) {
1829 1830
				result.decorations.push({
					range: new Range(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE),
1831
					options: DECORATIONS.lineDeleteMargin
1832 1833
				});

A
Alex Dima 已提交
1834
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1835 1836
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
A
Alex Dima 已提交
1837
					overviewZoneColor
A
Alex Dima 已提交
1838
				));
E
Erich Gamma 已提交
1839 1840 1841 1842 1843 1844
			}
		}

		return result;
	}

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

A
Alex Dima 已提交
1848
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1849 1850
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1851 1852
		};

A
Alex Dima 已提交
1853
		let modifiedModel = modifiedEditor.getModel()!;
A
Alex Dima 已提交
1854 1855 1856

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

			// Add decorations & overview zones
			if (isChangeOrInsert(lineChange)) {
1860 1861
				result.decorations.push({
					range: new Range(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE),
1862
					options: (renderIndicators ? DECORATIONS.lineInsertWithSign : DECORATIONS.lineInsert)
1863
				});
E
Erich Gamma 已提交
1864

A
Alex Dima 已提交
1865
				result.overviewZones.push(new OverviewRulerZone(
A
Alex Dima 已提交
1866 1867
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
A
Alex Dima 已提交
1868
					overviewZoneColor
A
Alex Dima 已提交
1869
				));
E
Erich Gamma 已提交
1870 1871

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1872 1873
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1874 1875
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1876 1877 1878
								for (let lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
									if (lineNumber === charChange.modifiedStartLineNumber) {
										startColumn = charChange.modifiedStartColumn;
									} else {
										startColumn = modifiedModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.modifiedEndLineNumber) {
										endColumn = charChange.modifiedEndColumn;
									} else {
										endColumn = modifiedModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
1889
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1890 1891
								}
							} else {
1892
								result.decorations.push(createDecoration(charChange.modifiedStartLineNumber, charChange.modifiedStartColumn, charChange.modifiedEndLineNumber, charChange.modifiedEndColumn, DECORATIONS.charInsert));
E
Erich Gamma 已提交
1893 1894 1895 1896
							}
						}
					}
				} else {
1897
					result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, DECORATIONS.charInsertWholeLine));
E
Erich Gamma 已提交
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
				}
			}
		}

		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 已提交
1914
	private originalModel: ITextModel;
1915
	private modifiedEditorConfiguration: editorOptions.InternalEditorOptions;
J
Johannes Rieken 已提交
1916
	private modifiedEditorTabSize: number;
R
rebornix 已提交
1917
	private renderIndicators: boolean;
E
Erich Gamma 已提交
1918

A
Alex Dima 已提交
1919
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: IEditorWhitespace[], modifiedForeignVZ: IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean) {
E
Erich Gamma 已提交
1920
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
A
Alex Dima 已提交
1921
		this.originalModel = originalEditor.getModel()!;
E
Erich Gamma 已提交
1922
		this.modifiedEditorConfiguration = modifiedEditor.getConfiguration();
A
Alex Dima 已提交
1923
		this.modifiedEditorTabSize = modifiedEditor.getModel()!.getOptions().tabSize;
R
rebornix 已提交
1924
		this.renderIndicators = renderIndicators;
E
Erich Gamma 已提交
1925 1926
	}

A
Alex Dima 已提交
1927
	protected _createOriginalMarginDomNodeForModifiedForeignViewZoneInAddedRegion(): HTMLDivElement | null {
A
Alex Dima 已提交
1928 1929 1930 1931 1932
		let result = document.createElement('div');
		result.className = 'inline-added-margin-view-zone';
		return result;
	}

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

E
Erich Gamma 已提交
1937
		return {
J
Johannes Rieken 已提交
1938
			afterLineNumber: Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber),
E
Erich Gamma 已提交
1939
			heightInLines: lineChangeModifiedLength,
1940 1941
			domNode: document.createElement('div'),
			marginDomNode: marginDomNode
E
Erich Gamma 已提交
1942 1943 1944
		};
	}

A
Alex Dima 已提交
1945
	protected _produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone | null {
A
Alex Dima 已提交
1946
		let decorations: InlineDecoration[] = [];
E
Erich Gamma 已提交
1947
		if (lineChange.charChanges) {
A
Alex Dima 已提交
1948 1949
			for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
				let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1950
				if (isChangeOrDelete(charChange)) {
1951 1952
					decorations.push(new InlineDecoration(
						new Range(charChange.originalStartLineNumber, charChange.originalStartColumn, charChange.originalEndLineNumber, charChange.originalEndColumn),
1953
						'char-delete',
1954
						InlineDecorationType.Regular
1955
					));
E
Erich Gamma 已提交
1956 1957 1958 1959
				}
			}
		}

1960
		let sb = createStringBuilder(10000);
1961 1962 1963
		let marginHTML: string[] = [];
		let lineDecorationsWidth = this.modifiedEditorConfiguration.layoutInfo.decorationsWidth;
		let lineHeight = this.modifiedEditorConfiguration.lineHeight;
1964 1965
		const typicalHalfwidthCharacterWidth = this.modifiedEditorConfiguration.fontInfo.typicalHalfwidthCharacterWidth;
		let maxCharsPerLine = 0;
A
Alex Dima 已提交
1966
		for (let lineNumber = lineChange.originalStartLineNumber; lineNumber <= lineChange.originalEndLineNumber; lineNumber++) {
1967
			maxCharsPerLine = Math.max(maxCharsPerLine, this._renderOriginalLine(lineNumber - lineChange.originalStartLineNumber, this.originalModel, this.modifiedEditorConfiguration, this.modifiedEditorTabSize, lineNumber, decorations, sb));
1968

R
rebornix 已提交
1969
			if (this.renderIndicators) {
1970 1971 1972 1973 1974
				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 已提交
1975
		}
1976
		maxCharsPerLine += this.modifiedEditorConfiguration.viewInfo.scrollBeyondLastColumn;
E
Erich Gamma 已提交
1977

A
Alex Dima 已提交
1978
		let domNode = document.createElement('div');
E
Erich Gamma 已提交
1979
		domNode.className = 'view-lines line-delete';
1980
		domNode.innerHTML = sb.build();
1981
		Configuration.applyFontInfoSlow(domNode, this.modifiedEditorConfiguration.fontInfo);
E
Erich Gamma 已提交
1982

1983 1984 1985 1986 1987
		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 已提交
1988 1989 1990 1991
		return {
			shouldNotShrink: true,
			afterLineNumber: (lineChange.modifiedEndLineNumber === 0 ? lineChange.modifiedStartLineNumber : lineChange.modifiedStartLineNumber - 1),
			heightInLines: lineChangeOriginalLength,
1992
			minWidthInPx: (maxCharsPerLine * typicalHalfwidthCharacterWidth),
1993 1994
			domNode: domNode,
			marginDomNode: marginDomNode
E
Erich Gamma 已提交
1995 1996 1997
		};
	}

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

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

2004 2005 2006 2007 2008 2009 2010 2011 2012
		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;">');

2013 2014
		const isBasicASCII = ViewLineRenderingData.isBasicASCII(lineContent, originalModel.mightContainNonBasicASCII());
		const containsRTL = ViewLineRenderingData.containsRTL(lineContent, isBasicASCII, originalModel.mightContainRTL());
2015
		const output = renderViewLine(new RenderLineInput(
2016
			(config.fontInfo.isMonospace && !config.viewInfo.disableMonospaceOptimizations),
2017
			config.fontInfo.canUseHalfwidthRightwardsArrow,
A
Alex Dima 已提交
2018
			lineContent,
A
Alex Dima 已提交
2019
			false,
2020 2021
			isBasicASCII,
			containsRTL,
2022
			0,
A
Alex Dima 已提交
2023
			lineTokens,
2024
			actualDecorations,
A
Alex Dima 已提交
2025
			tabSize,
2026
			config.fontInfo.spaceWidth,
2027 2028
			config.viewInfo.stopRenderingLineAfter,
			config.viewInfo.renderWhitespace,
2029 2030
			config.viewInfo.renderControlCharacters,
			config.viewInfo.fontLigatures
2031
		), sb);
E
Erich Gamma 已提交
2032

2033
		sb.appendASCIIString('</div>');
2034 2035 2036

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

J
Johannes Rieken 已提交
2040
function isChangeOrInsert(lineChange: editorCommon.IChange): boolean {
E
Erich Gamma 已提交
2041 2042 2043
	return lineChange.modifiedEndLineNumber > 0;
}

J
Johannes Rieken 已提交
2044
function isChangeOrDelete(lineChange: editorCommon.IChange): boolean {
E
Erich Gamma 已提交
2045 2046 2047 2048
	return lineChange.originalEndLineNumber > 0;
}

function createFakeLinesDiv(): HTMLElement {
A
Alex Dima 已提交
2049
	let r = document.createElement('div');
E
Erich Gamma 已提交
2050 2051 2052
	r.className = 'diagonal-fill';
	return r;
}
2053 2054

registerThemingParticipant((theme, collector) => {
2055
	const added = theme.getColor(diffInserted);
2056 2057
	if (added) {
		collector.addRule(`.monaco-editor .line-insert, .monaco-editor .char-insert { background-color: ${added}; }`);
A
Alex Dima 已提交
2058
		collector.addRule(`.monaco-diff-editor .line-insert, .monaco-diff-editor .char-insert { background-color: ${added}; }`);
2059 2060
		collector.addRule(`.monaco-editor .inline-added-margin-view-zone { background-color: ${added}; }`);
	}
2061 2062

	const removed = theme.getColor(diffRemoved);
2063 2064
	if (removed) {
		collector.addRule(`.monaco-editor .line-delete, .monaco-editor .char-delete { background-color: ${removed}; }`);
A
Alex Dima 已提交
2065
		collector.addRule(`.monaco-diff-editor .line-delete, .monaco-diff-editor .char-delete { background-color: ${removed}; }`);
2066 2067
		collector.addRule(`.monaco-editor .inline-deleted-margin-view-zone { background-color: ${removed}; }`);
	}
2068 2069

	const addedOutline = theme.getColor(diffInsertedOutline);
2070
	if (addedOutline) {
2071
		collector.addRule(`.monaco-editor .line-insert, .monaco-editor .char-insert { border: 1px ${theme.type === 'hc' ? 'dashed' : 'solid'} ${addedOutline}; }`);
2072
	}
2073 2074

	const removedOutline = theme.getColor(diffRemovedOutline);
2075
	if (removedOutline) {
2076
		collector.addRule(`.monaco-editor .line-delete, .monaco-editor .char-delete { border: 1px ${theme.type === 'hc' ? 'dashed' : 'solid'} ${removedOutline}; }`);
2077
	}
2078 2079

	const shadow = theme.getColor(scrollbarShadow);
2080 2081 2082
	if (shadow) {
		collector.addRule(`.monaco-diff-editor.side-by-side .editor.modified { box-shadow: -6px 0 5px -5px ${shadow}; }`);
	}
2083

M
Matt Bierner 已提交
2084
	const border = theme.getColor(diffBorder);
2085 2086 2087
	if (border) {
		collector.addRule(`.monaco-diff-editor.side-by-side .editor.modified { border-left: 1px solid ${border}; }`);
	}
2088
});