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

'use strict';

import 'vs/css!./media/diffEditor';
J
Johannes Rieken 已提交
9 10 11
import { RunOnceScheduler } from 'vs/base/common/async';
import { EventEmitter, EmitterEvent } from 'vs/base/common/eventEmitter';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
12 13
import * as objects from 'vs/base/common/objects';
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
14
import { FastDomNode, createFastDomNode } from 'vs/base/browser/fastDomNode';
J
Johannes Rieken 已提交
15 16 17
import { ISashEvent, IVerticalSashLayoutProvider, Sash } from 'vs/base/browser/ui/sash/sash';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
18
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
J
Johannes Rieken 已提交
19 20
import { DefaultConfig } from 'vs/editor/common/config/defaultConfig';
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
21
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
22
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
A
Alex Dima 已提交
23
import { LineDecoration } from 'vs/editor/common/viewLayout/lineDecorations';
A
Alex Dima 已提交
24
import { renderViewLine, RenderLineInput } from 'vs/editor/common/viewLayout/viewLineRenderer';
A
Alex Dima 已提交
25
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
26
import { CodeEditor } from 'vs/editor/browser/codeEditor';
A
Alex Dima 已提交
27
import { ViewLineToken } from 'vs/editor/common/core/viewLineToken';
J
Johannes Rieken 已提交
28 29 30 31
import { Configuration } from 'vs/editor/browser/config/configuration';
import { Position } from 'vs/editor/common/core/position';
import { Selection } from 'vs/editor/common/core/selection';
import { InlineDecoration } from 'vs/editor/common/viewModel/viewModel';
J
Joao Moreno 已提交
32
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
A
Alex Dima 已提交
33
import { ColorId, MetadataConsts, FontStyle } from 'vs/editor/common/modes';
E
Erich Gamma 已提交
34 35

interface IEditorDiffDecorations {
J
Johannes Rieken 已提交
36 37
	decorations: editorCommon.IModelDeltaDecoration[];
	overviewZones: editorCommon.OverviewRulerZone[];
E
Erich Gamma 已提交
38 39 40
}

interface IEditorDiffDecorationsWithZones extends IEditorDiffDecorations {
J
Johannes Rieken 已提交
41
	zones: editorBrowser.IViewZone[];
E
Erich Gamma 已提交
42 43 44
}

interface IEditorsDiffDecorations {
J
Johannes Rieken 已提交
45 46
	original: IEditorDiffDecorations;
	modified: IEditorDiffDecorations;
E
Erich Gamma 已提交
47 48 49
}

interface IEditorsDiffDecorationsWithZones {
J
Johannes Rieken 已提交
50 51
	original: IEditorDiffDecorationsWithZones;
	modified: IEditorDiffDecorationsWithZones;
E
Erich Gamma 已提交
52 53 54
}

interface IEditorsZones {
J
Johannes Rieken 已提交
55 56
	original: editorBrowser.IViewZone[];
	modified: editorBrowser.IViewZone[];
E
Erich Gamma 已提交
57 58 59
}

interface IDiffEditorWidgetStyle {
60
	getEditorsDiffDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalWhitespaces: editorCommon.IEditorWhitespace[], modifiedWhitespaces: editorCommon.IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsDiffDecorationsWithZones;
J
Johannes Rieken 已提交
61
	setEnableSplitViewResizing(enableSplitViewResizing: boolean): void;
E
Erich Gamma 已提交
62 63 64 65 66
	layout(): number;
	dispose(): void;
}

class VisualEditorState {
J
Johannes Rieken 已提交
67 68 69
	private _zones: number[];
	private _zonesMap: { [zoneId: string]: boolean; };
	private _decorations: string[];
E
Erich Gamma 已提交
70 71 72 73 74 75 76

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

J
Johannes Rieken 已提交
77
	public getForeignViewZones(allViewZones: editorCommon.IEditorWhitespace[]): editorCommon.IEditorWhitespace[] {
E
Erich Gamma 已提交
78 79 80
		return allViewZones.filter((z) => !this._zonesMap[String(z.id)]);
	}

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

		// (2) Model decorations
		if (this._decorations.length > 0) {
J
Johannes Rieken 已提交
95
			editor.changeDecorations((changeAccessor: editorCommon.IModelDecorationsChangeAccessor) => {
E
Erich Gamma 已提交
96 97 98 99 100 101
				changeAccessor.deltaDecorations(this._decorations, []);
			});
		}
		this._decorations = [];
	}

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

		// decorations
		this._decorations = editor.deltaDecorations(this._decorations, newDecorations.decorations);

		// overview ruler
122 123 124
		if (overviewRuler) {
			overviewRuler.setZones(newDecorations.overviewZones);
		}
E
Erich Gamma 已提交
125 126 127
	}
}

A
Alex Dima 已提交
128
let DIFF_EDITOR_ID = 0;
E
Erich Gamma 已提交
129

A
Alex Dima 已提交
130
export class DiffEditorWidget extends EventEmitter implements editorBrowser.IDiffEditor {
E
Erich Gamma 已提交
131

J
Johannes Rieken 已提交
132
	public onDidChangeModelRawContent(listener: (e: editorCommon.IModelContentChangedEvent) => void): IDisposable {
A
Alex Dima 已提交
133 134
		return this.addListener2(editorCommon.EventType.ModelRawContentChanged, listener);
	}
J
Johannes Rieken 已提交
135
	public onDidChangeModelContent(listener: (e: editorCommon.IModelContentChangedEvent2) => void): IDisposable {
A
Alex Dima 已提交
136
		return this.addListener2(editorCommon.EventType.ModelContentChanged2, listener);
A
Alex Dima 已提交
137
	}
A
Alex Dima 已提交
138 139
	public onDidChangeModelLanguage(listener: (e: editorCommon.IModelLanguageChangedEvent) => void): IDisposable {
		return this.addListener2(editorCommon.EventType.ModelLanguageChanged, listener);
A
Alex Dima 已提交
140
	}
J
Johannes Rieken 已提交
141
	public onDidChangeModelOptions(listener: (e: editorCommon.IModelOptionsChangedEvent) => void): IDisposable {
A
Alex Dima 已提交
142 143
		return this.addListener2(editorCommon.EventType.ModelOptionsChanged, listener);
	}
J
Johannes Rieken 已提交
144
	public onDidChangeConfiguration(listener: (e: editorCommon.IConfigurationChangedEvent) => void): IDisposable {
A
Alex Dima 已提交
145 146
		return this.addListener2(editorCommon.EventType.ConfigurationChanged, listener);
	}
J
Johannes Rieken 已提交
147
	public onDidChangeCursorPosition(listener: (e: editorCommon.ICursorPositionChangedEvent) => void): IDisposable {
A
Alex Dima 已提交
148 149
		return this.addListener2(editorCommon.EventType.CursorPositionChanged, listener);
	}
J
Johannes Rieken 已提交
150
	public onDidChangeCursorSelection(listener: (e: editorCommon.ICursorSelectionChangedEvent) => void): IDisposable {
A
Alex Dima 已提交
151 152
		return this.addListener2(editorCommon.EventType.CursorSelectionChanged, listener);
	}
J
Johannes Rieken 已提交
153
	public onDidDispose(listener: () => void): IDisposable {
A
Alex Dima 已提交
154 155
		return this.addListener2(editorCommon.EventType.Disposed, listener);
	}
J
Johannes Rieken 已提交
156
	public onDidUpdateDiff(listener: () => void): IDisposable {
A
Alex Dima 已提交
157 158 159
		return this.addListener2(editorCommon.EventType.DiffUpdated, listener);
	}

E
Erich Gamma 已提交
160 161 162 163 164 165
	private static ONE_OVERVIEW_WIDTH = 15;
	public static ENTIRE_DIFF_OVERVIEW_WIDTH = 30;
	private static UPDATE_DIFF_DECORATIONS_DELAY = 200; // ms

	private id: number;

J
Johannes Rieken 已提交
166
	private _toDispose: IDisposable[];
E
Erich Gamma 已提交
167

J
Johannes Rieken 已提交
168 169 170 171
	private _theme: string;
	private _domElement: HTMLElement;
	_containerDomElement: HTMLElement;
	private _overviewDomElement: HTMLElement;
A
Alex Dima 已提交
172
	private _overviewViewportDomElement: FastDomNode<HTMLElement>;
E
Erich Gamma 已提交
173

J
Johannes Rieken 已提交
174 175 176
	private _width: number;
	private _height: number;
	private _measureDomElementToken: number;
E
Erich Gamma 已提交
177

J
Johannes Rieken 已提交
178 179 180 181
	private originalEditor: CodeEditor;
	private _originalDomNode: HTMLElement;
	private _originalEditorState: VisualEditorState;
	private _originalOverviewRuler: editorBrowser.IOverviewRuler;
E
Erich Gamma 已提交
182

J
Johannes Rieken 已提交
183 184 185 186
	private modifiedEditor: CodeEditor;
	private _modifiedDomNode: HTMLElement;
	private _modifiedEditorState: VisualEditorState;
	private _modifiedOverviewRuler: editorBrowser.IOverviewRuler;
E
Erich Gamma 已提交
187

J
Johannes Rieken 已提交
188 189 190 191
	private _currentlyChangingViewZones: boolean;
	private _beginUpdateDecorationsTimeout: number;
	private _diffComputationToken: number;
	private _lineChanges: editorCommon.ILineChange[];
E
Erich Gamma 已提交
192

J
Johannes Rieken 已提交
193 194
	private _isVisible: boolean;
	private _isHandlingScrollEvent: boolean;
E
Erich Gamma 已提交
195 196

	private _ignoreTrimWhitespace: boolean;
197
	private _originalIsEditable: boolean;
E
Erich Gamma 已提交
198

J
Johannes Rieken 已提交
199
	private _renderSideBySide: boolean;
200
	private _renderIndicators: boolean;
J
Johannes Rieken 已提交
201 202
	private _enableSplitViewResizing: boolean;
	private _strategy: IDiffEditorWidgetStyle;
E
Erich Gamma 已提交
203

J
Johannes Rieken 已提交
204
	private _updateDecorationsRunner: RunOnceScheduler;
E
Erich Gamma 已提交
205

206
	private _editorWorkerService: IEditorWorkerService;
207
	protected _contextKeyService: IContextKeyService;
208
	private _codeEditorService: ICodeEditorService;
209 210

	constructor(
J
Johannes Rieken 已提交
211 212
		domElement: HTMLElement,
		options: editorCommon.IDiffEditorOptions,
213
		@IEditorWorkerService editorWorkerService: IEditorWorkerService,
214
		@IContextKeyService contextKeyService: IContextKeyService,
215 216
		@IInstantiationService instantiationService: IInstantiationService,
		@ICodeEditorService codeEditorService: ICodeEditorService
217
	) {
E
Erich Gamma 已提交
218
		super();
219
		this._editorWorkerService = editorWorkerService;
220
		this._codeEditorService = codeEditorService;
J
Joao Moreno 已提交
221 222
		this._contextKeyService = contextKeyService.createScoped(domElement);
		this._contextKeyService.createKey('isInDiffEditor', true);
E
Erich Gamma 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

		this.id = (++DIFF_EDITOR_ID);

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

		this._theme = options.theme || DefaultConfig.editor.theme;
		// 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;
		}

242 243 244 245 246 247
		// renderIndicators
		this._renderIndicators = true;
		if (typeof options.renderIndicators !== 'undefined') {
			this._renderIndicators = options.renderIndicators;
		}

248 249 250 251 252
		this._originalIsEditable = false;
		if (typeof options.originalEditable !== 'undefined') {
			this._originalIsEditable = Boolean(options.originalEditable);
		}

A
Alex Dima 已提交
253
		this._updateDecorationsRunner = new RunOnceScheduler(() => this._updateDecorations(), 0);
E
Erich Gamma 已提交
254 255 256 257 258 259 260 261 262 263

		this._toDispose = [];
		this._toDispose.push(this._updateDecorationsRunner);

		this._containerDomElement = document.createElement('div');
		this._containerDomElement.className = DiffEditorWidget._getClassName(this._theme, this._renderSideBySide);
		this._containerDomElement.style.position = 'relative';
		this._containerDomElement.style.height = '100%';
		this._domElement.appendChild(this._containerDomElement);

A
Alex Dima 已提交
264 265 266
		this._overviewViewportDomElement = createFastDomNode(document.createElement('div'));
		this._overviewViewportDomElement.setClassName('diffViewport');
		this._overviewViewportDomElement.setPosition('absolute');
E
Erich Gamma 已提交
267 268 269 270 271 272

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

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

J
Johannes Rieken 已提交
275
		this._toDispose.push(dom.addDisposableListener(this._overviewDomElement, 'mousedown', (e: MouseEvent) => {
E
Erich Gamma 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
			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;

		this._lineChanges = null;

J
Joao Moreno 已提交
298 299 300 301 302 303 304
		const services = new ServiceCollection();
		services.set(IContextKeyService, this._contextKeyService);

		const scopedInstantiationService = instantiationService.createChild(services);

		this._createLeftHandSideEditor(options, scopedInstantiationService);
		this._createRightHandSideEditor(options, scopedInstantiationService);
E
Erich Gamma 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

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

		this._codeEditorService.addDiffEditor(this);
E
Erich Gamma 已提交
323 324
	}

325 326 327 328 329 330 331 332
	public get ignoreTrimWhitespace(): boolean {
		return this._ignoreTrimWhitespace;
	}

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

333 334 335 336
	public get renderIndicators(): boolean {
		return this._renderIndicators;
	}

J
Johannes Rieken 已提交
337
	private static _getClassName(theme: string, renderSideBySide: boolean): string {
A
Alex Dima 已提交
338
		let result = 'monaco-diff-editor monaco-editor-background ';
E
Erich Gamma 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
		if (renderSideBySide) {
			result += 'side-by-side ';
		}
		result += theme;
		return result;
	}

	private _recreateOverviewRulers(): void {
		if (this._originalOverviewRuler) {
			this._overviewDomElement.removeChild(this._originalOverviewRuler.getDomNode());
			this._originalOverviewRuler.dispose();
		}
		this._originalOverviewRuler = this.originalEditor.getView().createOverviewRuler('original diffOverviewRuler', 4, Number.MAX_VALUE);
		this._overviewDomElement.appendChild(this._originalOverviewRuler.getDomNode());

		if (this._modifiedOverviewRuler) {
			this._overviewDomElement.removeChild(this._modifiedOverviewRuler.getDomNode());
			this._modifiedOverviewRuler.dispose();
		}
		this._modifiedOverviewRuler = this.modifiedEditor.getView().createOverviewRuler('modified diffOverviewRuler', 4, Number.MAX_VALUE);
		this._overviewDomElement.appendChild(this._modifiedOverviewRuler.getDomNode());

		this._layoutOverviewRulers();
	}

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

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

380
	private _createLeftHandSideEditor(options: editorCommon.IDiffEditorOptions, instantiationService: IInstantiationService): void {
A
Alex Dima 已提交
381
		this.originalEditor = this._createInnerEditor(instantiationService, this._originalDomNode, this._adjustOptionsForLeftHandSide(options, this._originalIsEditable));
382
		this._toDispose.push(this.originalEditor.addBulkListener2((events) => this._onOriginalEditorEvents(events)));
A
Alex Dima 已提交
383
		this._toDispose.push(this.addEmitter2(this.originalEditor));
E
Erich Gamma 已提交
384 385
	}

J
Johannes Rieken 已提交
386
	private _createRightHandSideEditor(options: editorCommon.IDiffEditorOptions, instantiationService: IInstantiationService): void {
A
Alex Dima 已提交
387
		this.modifiedEditor = this._createInnerEditor(instantiationService, this._modifiedDomNode, this._adjustOptionsForRightHandSide(options));
388
		this._toDispose.push(this.modifiedEditor.addBulkListener2((events) => this._onModifiedEditorEvents(events)));
A
Alex Dima 已提交
389
		this._toDispose.push(this.addEmitter2(this.modifiedEditor));
E
Erich Gamma 已提交
390 391
	}

A
Alex Dima 已提交
392 393 394 395
	protected _createInnerEditor(instantiationService: IInstantiationService, container: HTMLElement, options: editorCommon.IEditorOptions): CodeEditor {
		return instantiationService.createInstance(CodeEditor, container, options);
	}

E
Erich Gamma 已提交
396 397 398 399 400
	public destroy(): void {
		this.dispose();
	}

	public dispose(): void {
401 402
		this._codeEditorService.removeDiffEditor(this);

J
Joao Moreno 已提交
403
		this._toDispose = dispose(this._toDispose);
E
Erich Gamma 已提交
404 405 406 407 408 409 410 411

		window.clearInterval(this._measureDomElementToken);

		this._cleanViewZonesAndDecorations();

		this._originalOverviewRuler.dispose();
		this._modifiedOverviewRuler.dispose();

A
Alex Dima 已提交
412 413
		this.originalEditor.dispose();
		this.modifiedEditor.dispose();
E
Erich Gamma 已提交
414 415 416

		this._strategy.dispose();

A
Alex Dima 已提交
417 418
		this.emit(editorCommon.EventType.Disposed);

E
Erich Gamma 已提交
419 420 421 422 423 424 425 426 427 428
		super.dispose();
	}

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

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

	public getEditorType(): string {
A
Alex Dima 已提交
429
		return editorCommon.EditorType.IDiffEditor;
E
Erich Gamma 已提交
430 431
	}

A
Alex Dima 已提交
432
	public getLineChanges(): editorCommon.ILineChange[] {
E
Erich Gamma 已提交
433 434 435
		return this._lineChanges;
	}

A
Alex Dima 已提交
436
	public getOriginalEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
437 438 439
		return this.originalEditor;
	}

A
Alex Dima 已提交
440
	public getModifiedEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
441 442 443
		return this.modifiedEditor;
	}

J
Johannes Rieken 已提交
444
	public updateOptions(newOptions: editorCommon.IDiffEditorOptions): void {
E
Erich Gamma 已提交
445 446 447 448
		// Handle new theme
		this._theme = newOptions && newOptions.theme ? newOptions.theme : this._theme;

		// Handle side by side
A
Alex Dima 已提交
449
		let renderSideBySideChanged = false;
E
Erich Gamma 已提交
450 451 452 453 454 455 456
		if (typeof newOptions.renderSideBySide !== 'undefined') {
			if (this._renderSideBySide !== newOptions.renderSideBySide) {
				this._renderSideBySide = newOptions.renderSideBySide;
				renderSideBySideChanged = true;
			}
		}

457 458
		let beginUpdateDecorations = false;

E
Erich Gamma 已提交
459 460 461 462
		if (typeof newOptions.ignoreTrimWhitespace !== 'undefined') {
			if (this._ignoreTrimWhitespace !== newOptions.ignoreTrimWhitespace) {
				this._ignoreTrimWhitespace = newOptions.ignoreTrimWhitespace;
				// Begin comparing
463 464 465 466 467 468 469 470
				beginUpdateDecorations = true;
			}
		}

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

474 475 476 477
		if (beginUpdateDecorations) {
			this._beginUpdateDecorations();
		}

478 479 480 481
		if (typeof newOptions.originalEditable !== 'undefined') {
			this._originalIsEditable = Boolean(newOptions.originalEditable);
		}

E
Erich Gamma 已提交
482 483 484 485
		// Update class name
		this._containerDomElement.className = DiffEditorWidget._getClassName(this._theme, this._renderSideBySide);

		this.modifiedEditor.updateOptions(this._adjustOptionsForRightHandSide(newOptions));
486
		this.originalEditor.updateOptions(this._adjustOptionsForLeftHandSide(newOptions, this._originalIsEditable));
E
Erich Gamma 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

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

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

J
Johannes Rieken 已提交
504
	public getValue(options: { preserveBOM: boolean; lineEnding: string; } = null): string {
E
Erich Gamma 已提交
505 506 507
		return this.modifiedEditor.getValue(options);
	}

A
Alex Dima 已提交
508
	public getModel(): editorCommon.IDiffEditorModel {
E
Erich Gamma 已提交
509 510 511 512 513 514
		return {
			original: this.originalEditor.getModel(),
			modified: this.modifiedEditor.getModel()
		};
	}

J
Johannes Rieken 已提交
515
	public setModel(model: editorCommon.IDiffEditorModel): void {
E
Erich Gamma 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
		// Guard us against partial null model
		if (model && (!model.original || !model.modified)) {
			throw new Error(!model.original ? 'DiffEditorWidget.setModel: Original model is null' : 'DiffEditorWidget.setModel: Modified model is null');
		}

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

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

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

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

		if (model) {
			this._recreateOverviewRulers();

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

		this._layoutOverviewViewport();
	}

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

J
Johannes Rieken 已提交
554
	public getVisibleColumnFromPosition(position: editorCommon.IPosition): number {
E
Erich Gamma 已提交
555 556 557
		return this.modifiedEditor.getVisibleColumnFromPosition(position);
	}

A
Alex Dima 已提交
558
	public getPosition(): Position {
E
Erich Gamma 已提交
559 560 561
		return this.modifiedEditor.getPosition();
	}

J
Johannes Rieken 已提交
562
	public setPosition(position: editorCommon.IPosition, reveal?: boolean, revealVerticalInCenter?: boolean, revealHorizontal?: boolean): void {
E
Erich Gamma 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
		this.modifiedEditor.setPosition(position, reveal, revealVerticalInCenter, revealHorizontal);
	}

	public revealLine(lineNumber: number): void {
		this.modifiedEditor.revealLine(lineNumber);
	}

	public revealLineInCenter(lineNumber: number): void {
		this.modifiedEditor.revealLineInCenter(lineNumber);
	}

	public revealLineInCenterIfOutsideViewport(lineNumber: number): void {
		this.modifiedEditor.revealLineInCenterIfOutsideViewport(lineNumber);
	}

J
Johannes Rieken 已提交
578
	public revealPosition(position: editorCommon.IPosition, revealVerticalInCenter: boolean = false, revealHorizontal: boolean = false): void {
E
Erich Gamma 已提交
579 580 581
		this.modifiedEditor.revealPosition(position, revealVerticalInCenter, revealHorizontal);
	}

A
Alex Dima 已提交
582
	public revealPositionInCenter(position: editorCommon.IPosition): void {
E
Erich Gamma 已提交
583 584 585
		this.modifiedEditor.revealPositionInCenter(position);
	}

A
Alex Dima 已提交
586
	public revealPositionInCenterIfOutsideViewport(position: editorCommon.IPosition): void {
E
Erich Gamma 已提交
587 588 589
		this.modifiedEditor.revealPositionInCenterIfOutsideViewport(position);
	}

590
	public getSelection(): Selection {
E
Erich Gamma 已提交
591 592 593
		return this.modifiedEditor.getSelection();
	}

594
	public getSelections(): Selection[] {
E
Erich Gamma 已提交
595 596 597
		return this.modifiedEditor.getSelections();
	}

J
Johannes Rieken 已提交
598 599 600 601 602
	public setSelection(range: editorCommon.IRange, reveal?: boolean, revealVerticalInCenter?: boolean, revealHorizontal?: boolean): void;
	public setSelection(editorRange: Range, reveal?: boolean, revealVerticalInCenter?: boolean, revealHorizontal?: boolean): void;
	public setSelection(selection: editorCommon.ISelection, reveal?: boolean, revealVerticalInCenter?: boolean, revealHorizontal?: boolean): void;
	public setSelection(editorSelection: Selection, reveal?: boolean, revealVerticalInCenter?: boolean, revealHorizontal?: boolean): void;
	public setSelection(something: any, reveal?: boolean, revealVerticalInCenter?: boolean, revealHorizontal?: boolean): void {
E
Erich Gamma 已提交
603 604 605
		this.modifiedEditor.setSelection(something, reveal, revealVerticalInCenter, revealHorizontal);
	}

A
Alex Dima 已提交
606
	public setSelections(ranges: editorCommon.ISelection[]): void {
E
Erich Gamma 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		this.modifiedEditor.setSelections(ranges);
	}

	public revealLines(startLineNumber: number, endLineNumber: number): void {
		this.modifiedEditor.revealLines(startLineNumber, endLineNumber);
	}

	public revealLinesInCenter(startLineNumber: number, endLineNumber: number): void {
		this.modifiedEditor.revealLinesInCenter(startLineNumber, endLineNumber);
	}

	public revealLinesInCenterIfOutsideViewport(startLineNumber: number, endLineNumber: number): void {
		this.modifiedEditor.revealLinesInCenterIfOutsideViewport(startLineNumber, endLineNumber);
	}

J
Johannes Rieken 已提交
622
	public revealRange(range: editorCommon.IRange, revealVerticalInCenter: boolean = false, revealHorizontal: boolean = true): void {
E
Erich Gamma 已提交
623 624 625
		this.modifiedEditor.revealRange(range, revealVerticalInCenter, revealHorizontal);
	}

A
Alex Dima 已提交
626
	public revealRangeInCenter(range: editorCommon.IRange): void {
E
Erich Gamma 已提交
627 628 629
		this.modifiedEditor.revealRangeInCenter(range);
	}

A
Alex Dima 已提交
630
	public revealRangeInCenterIfOutsideViewport(range: editorCommon.IRange): void {
E
Erich Gamma 已提交
631 632 633
		this.modifiedEditor.revealRangeInCenterIfOutsideViewport(range);
	}

634 635 636 637
	public revealRangeAtTop(range: editorCommon.IRange): void {
		this.modifiedEditor.revealRangeAtTop(range);
	}

A
Alex Dima 已提交
638
	public getActions(): editorCommon.IEditorAction[] {
E
Erich Gamma 已提交
639 640 641
		return this.modifiedEditor.getActions();
	}

A
Alex Dima 已提交
642
	public getSupportedActions(): editorCommon.IEditorAction[] {
643 644 645
		return this.modifiedEditor.getSupportedActions();
	}

J
Johannes Rieken 已提交
646
	public getAction(id: string): editorCommon.IEditorAction {
E
Erich Gamma 已提交
647 648 649
		return this.modifiedEditor.getAction(id);
	}

A
Alex Dima 已提交
650
	public saveViewState(): editorCommon.IDiffEditorViewState {
A
Alex Dima 已提交
651 652
		let originalViewState = this.originalEditor.saveViewState();
		let modifiedViewState = this.modifiedEditor.saveViewState();
E
Erich Gamma 已提交
653 654 655 656 657 658
		return {
			original: originalViewState,
			modified: modifiedViewState
		};
	}

A
Alex Dima 已提交
659
	public restoreViewState(s: editorCommon.IDiffEditorViewState): void {
E
Erich Gamma 已提交
660
		if (s.original && s.original) {
A
Alex Dima 已提交
661
			let diffEditorState = <editorCommon.IDiffEditorViewState>s;
E
Erich Gamma 已提交
662 663 664 665 666
			this.originalEditor.restoreViewState(diffEditorState.original);
			this.modifiedEditor.restoreViewState(diffEditorState.modified);
		}
	}

J
Johannes Rieken 已提交
667
	public layout(dimension?: editorCommon.IDimension): void {
E
Erich Gamma 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		this._measureDomElement(false, dimension);
	}

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

	public isFocused(): boolean {
		return this.originalEditor.isFocused() || this.modifiedEditor.isFocused();
	}

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

J
Johannes Rieken 已提交
699
	public changeDecorations(callback: (changeAccessor: editorCommon.IModelDecorationsChangeAccessor) => any): any {
E
Erich Gamma 已提交
700 701 702 703 704 705 706 707 708
		return this.modifiedEditor.changeDecorations(callback);
	}

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



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

J
Johannes Rieken 已提交
709
	private _measureDomElement(forceDoLayoutCall: boolean, dimensions?: editorCommon.IDimension): void {
710 711 712 713
		dimensions = dimensions || {
			width: this._containerDomElement.clientWidth,
			height: this._containerDomElement.clientHeight
		};
E
Erich Gamma 已提交
714 715

		if (dimensions.width <= 0) {
716 717
			this._width = 0;
			this._height = 0;
E
Erich Gamma 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
			return;
		}

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

		this._width = dimensions.width;
		this._height = dimensions.height;

		this._doLayout();
	}

	private _layoutOverviewRulers(): void {
A
Alex Dima 已提交
733 734
		let freeSpace = DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH - 2 * DiffEditorWidget.ONE_OVERVIEW_WIDTH;
		let layoutInfo = this.modifiedEditor.getLayoutInfo();
E
Erich Gamma 已提交
735
		if (layoutInfo) {
736
			this._originalOverviewRuler.setLayout(new editorCommon.OverviewRulerPosition({
E
Erich Gamma 已提交
737 738 739
				top: 0,
				width: DiffEditorWidget.ONE_OVERVIEW_WIDTH,
				right: freeSpace + DiffEditorWidget.ONE_OVERVIEW_WIDTH,
740
				height: this._height
741 742
			}));
			this._modifiedOverviewRuler.setLayout(new editorCommon.OverviewRulerPosition({
E
Erich Gamma 已提交
743 744 745
				top: 0,
				right: 0,
				width: DiffEditorWidget.ONE_OVERVIEW_WIDTH,
746
				height: this._height
747
			}));
E
Erich Gamma 已提交
748 749 750 751 752
		}
	}

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

J
Johannes Rieken 已提交
753
	private _recomputeIfNecessary(events: EmitterEvent[]): void {
A
Alex Dima 已提交
754 755 756
		let changed = false;
		for (let i = 0; !changed && i < events.length; i++) {
			let type = events[i].getType();
A
Alex Dima 已提交
757
			changed = changed || type === editorCommon.EventType.ModelRawContentChanged;
E
Erich Gamma 已提交
758 759 760 761 762 763 764 765 766 767 768
		}
		if (changed && this._isVisible) {
			// 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);
		}
	}

J
Johannes Rieken 已提交
769
	private _onOriginalEditorEvents(events: EmitterEvent[]): void {
A
Alex Dima 已提交
770
		for (let i = 0; i < events.length; i++) {
771 772 773 774 775
			let type = events[i].getType();
			let data = events[i].getData();

			if (type === 'scroll') {
				this._onOriginalEditorScroll(data);
E
Erich Gamma 已提交
776
			}
777
			if (type === editorCommon.EventType.ViewZonesChanged) {
E
Erich Gamma 已提交
778 779
				this._onViewZonesChanged();
			}
780 781 782 783
			if (type === editorCommon.EventType.ConfigurationChanged) {
				let isViewportWrapping = this.originalEditor.getConfiguration().wrappingInfo.isViewportWrapping;
				if (isViewportWrapping) {
					// oh no, you didn't!
784
					this.originalEditor.updateOptions({ wordWrap: 'off' });
785 786
				}
			}
E
Erich Gamma 已提交
787 788 789 790
		}
		this._recomputeIfNecessary(events);
	}

J
Johannes Rieken 已提交
791
	private _onModifiedEditorEvents(events: EmitterEvent[]): void {
A
Alex Dima 已提交
792
		for (let i = 0; i < events.length; i++) {
793 794 795 796 797
			let type = events[i].getType();
			let data = events[i].getData();

			if (type === 'scroll') {
				this._onModifiedEditorScroll(data);
E
Erich Gamma 已提交
798 799
				this._layoutOverviewViewport();
			}
800
			if (type === 'viewLayoutChanged') {
E
Erich Gamma 已提交
801 802
				this._layoutOverviewViewport();
			}
803
			if (type === editorCommon.EventType.ViewZonesChanged) {
E
Erich Gamma 已提交
804 805
				this._onViewZonesChanged();
			}
806
			if (type === editorCommon.EventType.ConfigurationChanged) {
807
				let e = <editorCommon.IConfigurationChangedEvent>data;
808 809 810
				let isViewportWrapping = this.modifiedEditor.getConfiguration().wrappingInfo.isViewportWrapping;
				if (isViewportWrapping) {
					// oh no, you didn't!
811
					this.modifiedEditor.updateOptions({ wordWrap: 'off' });
812
				}
813
				if (e.fontInfo && this.modifiedEditor.getModel()) {
814 815
					this._onViewZonesChanged();
				}
816
			}
E
Erich Gamma 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
		}
		this._recomputeIfNecessary(events);
	}

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

	private _beginUpdateDecorations(): void {
		this._beginUpdateDecorationsTimeout = -1;
		if (!this.modifiedEditor.getModel()) {
			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 已提交
838 839 840
		let currentToken = this._diffComputationToken;
		let currentOriginalModel = this.originalEditor.getModel();
		let currentModifiedModel = this.modifiedEditor.getModel();
E
Erich Gamma 已提交
841

842
		this._editorWorkerService.computeDiff(currentOriginalModel.uri, currentModifiedModel.uri, this._ignoreTrimWhitespace).then((result) => {
843 844 845
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
J
Johannes Rieken 已提交
846
			) {
847 848
				this._lineChanges = result;
				this._updateDecorationsRunner.schedule();
J
Johannes Rieken 已提交
849
				this.emit(editorCommon.EventType.DiffUpdated, {});
850 851 852 853 854
			}
		}, (error) => {
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
J
Johannes Rieken 已提交
855
			) {
E
Erich Gamma 已提交
856 857 858
				this._lineChanges = null;
				this._updateDecorationsRunner.schedule();
			}
859
		});
E
Erich Gamma 已提交
860 861 862 863 864 865 866 867
	}

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

	private _updateDecorations(): void {
868 869 870
		if (!this.originalEditor.getModel() || !this.modifiedEditor.getModel()) {
			return;
		}
A
Alex Dima 已提交
871
		let lineChanges = this._lineChanges || [];
E
Erich Gamma 已提交
872

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

876
		let diffDecorations = this._strategy.getEditorsDiffDecorations(lineChanges, this._ignoreTrimWhitespace, this._renderIndicators, foreignOriginal, foreignModified, this.originalEditor, this.modifiedEditor);
E
Erich Gamma 已提交
877 878 879 880 881 882 883 884 885 886

		try {
			this._currentlyChangingViewZones = true;
			this._originalEditorState.apply(this.originalEditor, this._originalOverviewRuler, diffDecorations.original);
			this._modifiedEditorState.apply(this.modifiedEditor, this._modifiedOverviewRuler, diffDecorations.modified);
		} finally {
			this._currentlyChangingViewZones = false;
		}
	}

J
Johannes Rieken 已提交
887 888
	private _adjustOptionsForSubEditor(options: editorCommon.IDiffEditorOptions): editorCommon.IDiffEditorOptions {
		let clonedOptions: editorCommon.IDiffEditorOptions = objects.clone(options || {});
889
		clonedOptions.wordWrap = 'off';
890
		clonedOptions.wordWrapMinified = false;
E
Erich Gamma 已提交
891 892 893
		clonedOptions.automaticLayout = false;
		clonedOptions.scrollbar = clonedOptions.scrollbar || {};
		clonedOptions.scrollbar.vertical = 'visible';
A
Alex Dima 已提交
894
		clonedOptions.folding = false;
895
		clonedOptions.codeLens = false;
J
Joao Moreno 已提交
896
		clonedOptions.fixedOverflowWidgets = true;
897
		clonedOptions.lineDecorationsWidth = '2ch';
898 899 900
		if (!clonedOptions.minimap) {
			clonedOptions.minimap = {};
		}
901
		clonedOptions.minimap.enabled = false;
E
Erich Gamma 已提交
902 903 904
		return clonedOptions;
	}

A
Alex Dima 已提交
905
	private _adjustOptionsForLeftHandSide(options: editorCommon.IDiffEditorOptions, isEditable: boolean): editorCommon.IEditorOptions {
906 907 908 909 910 911 912
		let result = this._adjustOptionsForSubEditor(options);
		result.readOnly = !isEditable;
		result.overviewRulerLanes = 1;
		result.theme = this._theme + ' original-in-monaco-diff-editor';
		return result;
	}

A
Alex Dima 已提交
913
	private _adjustOptionsForRightHandSide(options: editorCommon.IDiffEditorOptions): editorCommon.IEditorOptions {
914 915 916 917 918
		let result = this._adjustOptionsForSubEditor(options);
		result.revealHorizontalRightPadding = DefaultConfig.editor.revealHorizontalRightPadding + DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
		result.scrollbar.verticalHasArrows = false;
		result.theme = this._theme + ' modified-in-monaco-diff-editor';
		return result;
E
Erich Gamma 已提交
919 920
	}

J
Johannes Rieken 已提交
921
	private _onOriginalEditorScroll(e: editorCommon.IScrollEvent): void {
A
Alex Dima 已提交
922 923 924
		if (!e.scrollTopChanged && !e.scrollLeftChanged) {
			return;
		}
E
Erich Gamma 已提交
925 926 927 928
		if (this._isHandlingScrollEvent) {
			return;
		}
		this._isHandlingScrollEvent = true;
929 930 931 932
		this.modifiedEditor.setScrollPosition({
			scrollLeft: e.scrollLeft,
			scrollTop: e.scrollTop
		});
E
Erich Gamma 已提交
933 934 935
		this._isHandlingScrollEvent = false;
	}

J
Johannes Rieken 已提交
936
	private _onModifiedEditorScroll(e: editorCommon.IScrollEvent): void {
A
Alex Dima 已提交
937 938 939
		if (!e.scrollTopChanged && !e.scrollLeftChanged) {
			return;
		}
J
Johannes Rieken 已提交
940
		if (this._isHandlingScrollEvent) {
E
Erich Gamma 已提交
941 942 943
			return;
		}
		this._isHandlingScrollEvent = true;
944 945 946 947
		this.originalEditor.setScrollPosition({
			scrollLeft: e.scrollLeft,
			scrollTop: e.scrollTop
		});
E
Erich Gamma 已提交
948 949 950 951
		this._isHandlingScrollEvent = false;
	}

	private _doLayout(): void {
A
Alex Dima 已提交
952
		let splitPoint = this._strategy.layout();
E
Erich Gamma 已提交
953 954 955 956 957 958 959 960 961 962

		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';
		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 已提交
963 964
		this._overviewViewportDomElement.setWidth(DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH);
		this._overviewViewportDomElement.setHeight(30);
E
Erich Gamma 已提交
965 966 967 968 969 970 971 972 973 974 975 976

		this.originalEditor.layout({ width: splitPoint, height: this._height });
		this.modifiedEditor.layout({ width: this._width - splitPoint - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH, height: this._height });

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

		this._layoutOverviewViewport();
	}

	private _layoutOverviewViewport(): void {
A
Alex Dima 已提交
977
		let layout = this._computeOverviewViewport();
E
Erich Gamma 已提交
978
		if (!layout) {
A
Alex Dima 已提交
979 980
			this._overviewViewportDomElement.setTop(0);
			this._overviewViewportDomElement.setHeight(0);
E
Erich Gamma 已提交
981
		} else {
A
Alex Dima 已提交
982 983
			this._overviewViewportDomElement.setTop(layout.top);
			this._overviewViewportDomElement.setHeight(layout.height);
E
Erich Gamma 已提交
984 985 986
		}
	}

J
Johannes Rieken 已提交
987
	private _computeOverviewViewport(): { height: number; top: number; } {
A
Alex Dima 已提交
988
		let layoutInfo = this.modifiedEditor.getLayoutInfo();
E
Erich Gamma 已提交
989 990 991 992
		if (!layoutInfo) {
			return null;
		}

A
Alex Dima 已提交
993 994
		let scrollTop = this.modifiedEditor.getScrollTop();
		let scrollHeight = this.modifiedEditor.getScrollHeight();
E
Erich Gamma 已提交
995

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

A
Alex Dima 已提交
1000 1001
		let computedSliderSize = Math.max(1, Math.floor(layoutInfo.contentHeight * computedRatio));
		let computedSliderPosition = Math.floor(scrollTop * computedRatio);
E
Erich Gamma 已提交
1002 1003 1004 1005 1006 1007 1008

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

J
Johannes Rieken 已提交
1009
	private _createDataSource(): IDataSource {
E
Erich Gamma 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		return {
			getWidth: () => {
				return this._width;
			},

			getHeight: () => {
				return this._height;
			},

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

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

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

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

J
Johannes Rieken 已提交
1037
	private _setStrategy(newStrategy: IDiffEditorWidgetStyle): void {
E
Erich Gamma 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
		if (this._strategy) {
			this._strategy.dispose();
		}

		this._strategy = newStrategy;

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

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

J
Johannes Rieken 已提交
1052
	private _getLineChangeAtOrBeforeLineNumber(lineNumber: number, startLineNumberExtractor: (lineChange: editorCommon.ILineChange) => number): editorCommon.ILineChange {
E
Erich Gamma 已提交
1053 1054 1055 1056 1057
		if (this._lineChanges.length === 0 || lineNumber < startLineNumberExtractor(this._lineChanges[0])) {
			// There are no changes or `lineNumber` is before the first change
			return null;
		}

A
Alex Dima 已提交
1058
		let min = 0, max = this._lineChanges.length - 1;
E
Erich Gamma 已提交
1059
		while (min < max) {
A
Alex Dima 已提交
1060 1061 1062
			let mid = Math.floor((min + max) / 2);
			let midStart = startLineNumberExtractor(this._lineChanges[mid]);
			let midEnd = (mid + 1 <= max ? startLineNumberExtractor(this._lineChanges[mid + 1]) : Number.MAX_VALUE);
E
Erich Gamma 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

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

	private _getEquivalentLineForOriginalLineNumber(lineNumber: number): number {
A
Alex Dima 已提交
1078
		let lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.originalStartLineNumber);
E
Erich Gamma 已提交
1079 1080 1081 1082 1083

		if (!lineChange) {
			return lineNumber;
		}

A
Alex Dima 已提交
1084 1085 1086 1087
		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 已提交
1088 1089


A
Alex Dima 已提交
1090
		let delta = lineNumber - originalEquivalentLineNumber;
E
Erich Gamma 已提交
1091 1092 1093 1094 1095

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

J
Johannes Rieken 已提交
1096
		return modifiedEquivalentLineNumber + lineChangeModifiedLength - lineChangeOriginalLength + delta;
E
Erich Gamma 已提交
1097 1098 1099
	}

	private _getEquivalentLineForModifiedLineNumber(lineNumber: number): number {
A
Alex Dima 已提交
1100
		let lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.modifiedStartLineNumber);
E
Erich Gamma 已提交
1101 1102 1103 1104 1105

		if (!lineChange) {
			return lineNumber;
		}

A
Alex Dima 已提交
1106 1107 1108 1109
		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 已提交
1110 1111


A
Alex Dima 已提交
1112
		let delta = lineNumber - modifiedEquivalentLineNumber;
E
Erich Gamma 已提交
1113 1114 1115 1116 1117

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

J
Johannes Rieken 已提交
1118
		return originalEquivalentLineNumber + lineChangeOriginalLength - lineChangeModifiedLength + delta;
E
Erich Gamma 已提交
1119 1120
	}

J
Johannes Rieken 已提交
1121
	public getDiffLineInformationForOriginal(lineNumber: number): editorCommon.IDiffLineInformation {
E
Erich Gamma 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130
		if (!this._lineChanges) {
			// Cannot answer that which I don't know
			return null;
		}
		return {
			equivalentLineNumber: this._getEquivalentLineForOriginalLineNumber(lineNumber)
		};
	}

J
Johannes Rieken 已提交
1131
	public getDiffLineInformationForModified(lineNumber: number): editorCommon.IDiffLineInformation {
E
Erich Gamma 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
		if (!this._lineChanges) {
			// Cannot answer that which I don't know
			return null;
		}
		return {
			equivalentLineNumber: this._getEquivalentLineForModifiedLineNumber(lineNumber)
		};
	}
}

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

A
Alex Dima 已提交
1148 1149
	getOriginalEditor(): editorBrowser.ICodeEditor;
	getModifiedEditor(): editorBrowser.ICodeEditor;
E
Erich Gamma 已提交
1150 1151 1152 1153
}

class DiffEditorWidgetStyle {

J
Johannes Rieken 已提交
1154
	_dataSource: IDataSource;
E
Erich Gamma 已提交
1155

J
Johannes Rieken 已提交
1156
	constructor(dataSource: IDataSource) {
E
Erich Gamma 已提交
1157 1158 1159
		this._dataSource = dataSource;
	}

1160
	public getEditorsDiffDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalWhitespaces: editorCommon.IEditorWhitespace[], modifiedWhitespaces: editorCommon.IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsDiffDecorationsWithZones {
E
Erich Gamma 已提交
1161 1162 1163 1164 1165 1166 1167
		// Get view zones
		modifiedWhitespaces = modifiedWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
		originalWhitespaces = originalWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
1168
		let zones = this._getViewZones(lineChanges, originalWhitespaces, modifiedWhitespaces, originalEditor, modifiedEditor, renderIndicators);
E
Erich Gamma 已提交
1169 1170

		// Get decorations & overview ruler zones
1171 1172
		let originalDecorations = this._getOriginalEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor);
		let modifiedDecorations = this._getModifiedEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor);
E
Erich Gamma 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187

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

R
rebornix 已提交
1188
	_getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: editorCommon.IEditorWhitespace[], modifiedForeignVZ: editorCommon.IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean): IEditorsZones {
E
Erich Gamma 已提交
1189 1190 1191
		return null;
	}

1192
	_getOriginalEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1193 1194 1195
		return null;
	}

1196
	_getModifiedEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1197 1198 1199 1200
		return null;
	}
}

A
Alex Dima 已提交
1201
interface IMyViewZone extends editorBrowser.IViewZone {
E
Erich Gamma 已提交
1202 1203 1204 1205 1206 1207
	shouldNotShrink?: boolean;
}

class ForeignViewZonesIterator {

	private _index: number;
A
Alex Dima 已提交
1208 1209
	private _source: editorCommon.IEditorWhitespace[];
	public current: editorCommon.IEditorWhitespace;
E
Erich Gamma 已提交
1210

A
Alex Dima 已提交
1211
	constructor(source: editorCommon.IEditorWhitespace[]) {
E
Erich Gamma 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		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;
		}
	}
}

1227
abstract class ViewZonesComputer {
E
Erich Gamma 已提交
1228

J
Johannes Rieken 已提交
1229 1230 1231
	private lineChanges: editorCommon.ILineChange[];
	private originalForeignVZ: editorCommon.IEditorWhitespace[];
	private modifiedForeignVZ: editorCommon.IEditorWhitespace[];
E
Erich Gamma 已提交
1232

J
Johannes Rieken 已提交
1233
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: editorCommon.IEditorWhitespace[], modifiedForeignVZ: editorCommon.IEditorWhitespace[]) {
E
Erich Gamma 已提交
1234 1235 1236 1237 1238 1239
		this.lineChanges = lineChanges;
		this.originalForeignVZ = originalForeignVZ;
		this.modifiedForeignVZ = modifiedForeignVZ;
	}

	public getViewZones(): IEditorsZones {
A
Alex Dima 已提交
1240
		let result: IEditorsZones = {
E
Erich Gamma 已提交
1241 1242 1243 1244
			original: [],
			modified: []
		};

A
Alex Dima 已提交
1245 1246 1247 1248 1249 1250 1251 1252
		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 已提交
1253 1254 1255
			return a.afterLineNumber - b.afterLineNumber;
		};

A
Alex Dima 已提交
1256
		let addAndCombineIfPossible = (destination: editorBrowser.IViewZone[], item: IMyViewZone) => {
E
Erich Gamma 已提交
1257
			if (item.domNode === null && destination.length > 0) {
A
Alex Dima 已提交
1258
				let lastItem = destination[destination.length - 1];
E
Erich Gamma 已提交
1259 1260 1261 1262 1263 1264 1265 1266
				if (lastItem.afterLineNumber === item.afterLineNumber && lastItem.domNode === null) {
					lastItem.heightInLines += item.heightInLines;
					return;
				}
			}
			destination.push(item);
		};

A
Alex Dima 已提交
1267 1268
		let modifiedForeignVZ = new ForeignViewZonesIterator(this.modifiedForeignVZ);
		let originalForeignVZ = new ForeignViewZonesIterator(this.originalForeignVZ);
E
Erich Gamma 已提交
1269 1270

		// 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 已提交
1271 1272
		for (let i = 0, length = this.lineChanges.length; i <= length; i++) {
			let lineChange = (i < length ? this.lineChanges[i] : null);
E
Erich Gamma 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289

			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 已提交
1290 1291
			let stepOriginal: IMyViewZone[] = [];
			let stepModified: IMyViewZone[] = [];
E
Erich Gamma 已提交
1292 1293 1294 1295 1296

			// ---------------------------- 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 已提交
1297
				let viewZoneLineNumber: number;
E
Erich Gamma 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
				if (modifiedForeignVZ.current.afterLineNumber <= modifiedEquivalentLineNumber) {
					viewZoneLineNumber = originalEquivalentLineNumber - modifiedEquivalentLineNumber + modifiedForeignVZ.current.afterLineNumber;
				} else {
					viewZoneLineNumber = originalEndEquivalentLineNumber;
				}
				stepOriginal.push({
					afterLineNumber: viewZoneLineNumber,
					heightInLines: modifiedForeignVZ.current.heightInLines,
					domNode: null
				});
				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 已提交
1313
				let viewZoneLineNumber: number;
E
Erich Gamma 已提交
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
				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 已提交
1328
				let r = this._produceOriginalFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
E
Erich Gamma 已提交
1329 1330 1331 1332 1333 1334
				if (r) {
					stepOriginal.push(r);
				}
			}

			if (lineChange !== null && isChangeOrDelete(lineChange)) {
A
Alex Dima 已提交
1335
				let r = this._produceModifiedFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
E
Erich Gamma 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
				if (r) {
					stepModified.push(r);
				}
			}

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


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

			// [CANCEL & EMIT] Try to cancel view zones out
A
Alex Dima 已提交
1347 1348
			let stepOriginalIndex = 0;
			let stepModifiedIndex = 0;
E
Erich Gamma 已提交
1349 1350 1351 1352 1353

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

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

A
Alex Dima 已提交
1357 1358
				let originalDelta = original.afterLineNumber - originalEquivalentLineNumber;
				let modifiedDelta = modified.afterLineNumber - modifiedEquivalentLineNumber;
E
Erich Gamma 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399

				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 已提交
1400
		let ensureDomNode = (z: IMyViewZone) => {
E
Erich Gamma 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
			if (!z.domNode) {
				z.domNode = createFakeLinesDiv();
			}
		};

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

		return result;
	}

J
Johannes Rieken 已提交
1412
	protected abstract _produceOriginalFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone;
E
Erich Gamma 已提交
1413

J
Johannes Rieken 已提交
1414
	protected abstract _produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone;
E
Erich Gamma 已提交
1415 1416
}

A
Alex Dima 已提交
1417
class DiffEdtorWidgetSideBySide extends DiffEditorWidgetStyle implements IDiffEditorWidgetStyle, IVerticalSashLayoutProvider {
E
Erich Gamma 已提交
1418 1419 1420 1421

	static MINIMUM_EDITOR_WIDTH = 100;

	private _disableSash: boolean;
J
Johannes Rieken 已提交
1422 1423 1424 1425
	private _sash: Sash;
	private _sashRatio: number;
	private _sashPosition: number;
	private _startSashPosition: number;
E
Erich Gamma 已提交
1426

J
Johannes Rieken 已提交
1427
	constructor(dataSource: IDataSource, enableSplitViewResizing: boolean) {
E
Erich Gamma 已提交
1428 1429 1430 1431 1432
		super(dataSource);

		this._disableSash = (enableSplitViewResizing === false);
		this._sashRatio = null;
		this._sashPosition = null;
A
Alex Dima 已提交
1433
		this._sash = new Sash(this._dataSource.getContainerDomNode(), this);
E
Erich Gamma 已提交
1434 1435 1436 1437 1438

		if (this._disableSash) {
			this._sash.disable();
		}

A
Alex Dima 已提交
1439 1440 1441 1442
		this._sash.addListener2('start', () => this.onSashDragStart());
		this._sash.addListener2('change', (e: ISashEvent) => this.onSashDrag(e));
		this._sash.addListener2('end', () => this.onSashDragEnd());
		this._sash.addListener2('reset', () => this.onSashReset());
E
Erich Gamma 已提交
1443 1444 1445 1446 1447 1448
	}

	public dispose(): void {
		this._sash.dispose();
	}

J
Johannes Rieken 已提交
1449
	public setEnableSplitViewResizing(enableSplitViewResizing: boolean): void {
A
Alex Dima 已提交
1450
		let newDisableSash = (enableSplitViewResizing === false);
E
Erich Gamma 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
		if (this._disableSash !== newDisableSash) {
			this._disableSash = newDisableSash;

			if (this._disableSash) {
				this._sash.disable();
			} else {
				this._sash.enable();
			}
		}
	}

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

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

A
Alex Dima 已提交
1469
		sashPosition = this._disableSash ? midPoint : sashPosition || midPoint;
E
Erich Gamma 已提交
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

		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 已提交
1491
	private onSashDragStart(): void {
E
Erich Gamma 已提交
1492 1493 1494
		this._startSashPosition = this._sashPosition;
	}

J
Johannes Rieken 已提交
1495
	private onSashDrag(e: ISashEvent): void {
A
Alex Dima 已提交
1496 1497 1498
		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 已提交
1499 1500 1501 1502 1503 1504

		this._sashRatio = sashPosition / contentWidth;

		this._dataSource.relayoutEditors();
	}

M
Maxime Quandalle 已提交
1505 1506 1507 1508 1509 1510 1511
	private onSashDragEnd(): void {
		this._sash.layout();
	}

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

A
Alex Dima 已提交
1515
	public getVerticalSashTop(sash: Sash): number {
E
Erich Gamma 已提交
1516 1517 1518
		return 0;
	}

A
Alex Dima 已提交
1519
	public getVerticalSashLeft(sash: Sash): number {
E
Erich Gamma 已提交
1520 1521 1522
		return this._sashPosition;
	}

A
Alex Dima 已提交
1523
	public getVerticalSashHeight(sash: Sash): number {
E
Erich Gamma 已提交
1524 1525 1526
		return this._dataSource.getHeight();
	}

J
Johannes Rieken 已提交
1527
	_getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: editorCommon.IEditorWhitespace[], modifiedForeignVZ: editorCommon.IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorsZones {
A
Alex Dima 已提交
1528
		let c = new SideBySideViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ);
E
Erich Gamma 已提交
1529 1530 1531
		return c.getViewZones();
	}

1532
	_getOriginalEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1533

A
Alex Dima 已提交
1534
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1535 1536
			decorations: [],
			overviewZones: []
P
Peng Lyu 已提交
1537
		};
A
Alex Dima 已提交
1538 1539 1540 1541 1542

		let originalModel = originalEditor.getModel();

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

			if (isChangeOrDelete(lineChange)) {
1545 1546 1547 1548
				result.decorations.push({
					range: new Range(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE),
					options: {
						className: 'line-delete',
1549
						linesDecorationsClassName: renderIndicators ? 'delete-sign' : undefined,
1550 1551 1552 1553
						marginClassName: 'line-delete',
						isWholeLine: true
					}
				});
E
Erich Gamma 已提交
1554 1555 1556 1557
				if (!isChangeOrInsert(lineChange) || !lineChange.charChanges) {
					result.decorations.push(createDecoration(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE, 'char-delete', true));
				}

1558
				result.overviewZones.push(new editorCommon.OverviewRulerZone(
A
Alex Dima 已提交
1559 1560 1561 1562 1563 1564 1565
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(255, 0, 0, 0.4)',
					'rgba(255, 0, 0, 0.4)'
				));
E
Erich Gamma 已提交
1566 1567

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1568 1569
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1570 1571
						if (isChangeOrDelete(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1572 1573 1574
								for (let lineNumber = charChange.originalStartLineNumber; lineNumber <= charChange.originalEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
									if (lineNumber === charChange.originalStartLineNumber) {
										startColumn = charChange.originalStartColumn;
									} else {
										startColumn = originalModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.originalEndLineNumber) {
										endColumn = charChange.originalEndColumn;
									} else {
										endColumn = originalModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, 'char-delete', false));
								}
							} else {
								result.decorations.push(createDecoration(charChange.originalStartLineNumber, charChange.originalStartColumn, charChange.originalEndLineNumber, charChange.originalEndColumn, 'char-delete', false));
							}
						}
					}
				}
			}
		}

		return result;
	}

1599
	_getModifiedEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1600

A
Alex Dima 已提交
1601
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1602 1603
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1604 1605 1606 1607 1608 1609
		};

		let modifiedModel = modifiedEditor.getModel();

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

			if (isChangeOrInsert(lineChange)) {

1613 1614 1615 1616
				result.decorations.push({
					range: new Range(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE),
					options: {
						className: 'line-insert',
1617
						linesDecorationsClassName: renderIndicators ? 'insert-sign' : undefined,
1618 1619 1620 1621
						marginClassName: 'line-insert',
						isWholeLine: true
					}
				});
E
Erich Gamma 已提交
1622 1623 1624
				if (!isChangeOrDelete(lineChange) || !lineChange.charChanges) {
					result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, 'char-insert', true));
				}
1625
				result.overviewZones.push(new editorCommon.OverviewRulerZone(
A
Alex Dima 已提交
1626 1627 1628 1629 1630 1631 1632
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(155, 185, 85, 0.4)',
					'rgba(155, 185, 85, 0.4)'
				));
E
Erich Gamma 已提交
1633 1634

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1635 1636
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1637 1638
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1639 1640 1641
								for (let lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
									if (lineNumber === charChange.modifiedStartLineNumber) {
										startColumn = charChange.modifiedStartColumn;
									} else {
										startColumn = modifiedModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.modifiedEndLineNumber) {
										endColumn = charChange.modifiedEndColumn;
									} else {
										endColumn = modifiedModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, 'char-insert', false));
								}
							} else {
								result.decorations.push(createDecoration(charChange.modifiedStartLineNumber, charChange.modifiedStartColumn, charChange.modifiedEndLineNumber, charChange.modifiedEndColumn, 'char-insert', false));
							}
						}
					}
				}

			}
		}
		return result;
	}
}

class SideBySideViewZonesComputer extends ViewZonesComputer {

J
Johannes Rieken 已提交
1669
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: editorCommon.IEditorWhitespace[], modifiedForeignVZ: editorCommon.IEditorWhitespace[]) {
E
Erich Gamma 已提交
1670 1671 1672
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
	}

J
Johannes Rieken 已提交
1673
	_produceOriginalFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone {
E
Erich Gamma 已提交
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
		if (lineChangeModifiedLength > lineChangeOriginalLength) {
			return {
				afterLineNumber: Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber),
				heightInLines: (lineChangeModifiedLength - lineChangeOriginalLength),
				domNode: null
			};
		}
		return null;
	}

J
Johannes Rieken 已提交
1684
	_produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone {
E
Erich Gamma 已提交
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
		if (lineChangeOriginalLength > lineChangeModifiedLength) {
			return {
				afterLineNumber: Math.max(lineChange.modifiedStartLineNumber, lineChange.modifiedEndLineNumber),
				heightInLines: (lineChangeOriginalLength - lineChangeModifiedLength),
				domNode: null
			};
		}
		return null;
	}
}

class DiffEdtorWidgetInline extends DiffEditorWidgetStyle implements IDiffEditorWidgetStyle {

J
Johannes Rieken 已提交
1698
	private toDispose: IDisposable[];
E
Erich Gamma 已提交
1699 1700
	private decorationsLeft: number;

J
Johannes Rieken 已提交
1701
	constructor(dataSource: IDataSource, enableSplitViewResizing: boolean) {
E
Erich Gamma 已提交
1702 1703
		super(dataSource);

1704
		this.decorationsLeft = dataSource.getOriginalEditor().getLayoutInfo().decorationsLeft;
E
Erich Gamma 已提交
1705 1706

		this.toDispose = [];
J
Johannes Rieken 已提交
1707
		this.toDispose.push(dataSource.getOriginalEditor().onDidLayoutChange((layoutInfo: editorCommon.EditorLayoutInfo) => {
E
Erich Gamma 已提交
1708 1709 1710 1711 1712 1713 1714 1715
			if (this.decorationsLeft !== layoutInfo.decorationsLeft) {
				this.decorationsLeft = layoutInfo.decorationsLeft;
				dataSource.relayoutEditors();
			}
		}));
	}

	public dispose(): void {
J
Joao Moreno 已提交
1716
		this.toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
1717 1718
	}

J
Johannes Rieken 已提交
1719
	public setEnableSplitViewResizing(enableSplitViewResizing: boolean): void {
E
Erich Gamma 已提交
1720 1721 1722
		// Nothing to do..
	}

R
rebornix 已提交
1723
	_getViewZones(lineChanges: editorCommon.ILineChange[], originalForeignVZ: editorCommon.IEditorWhitespace[], modifiedForeignVZ: editorCommon.IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean): IEditorsZones {
1724
		let computer = new InlineViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ, originalEditor, modifiedEditor, renderIndicators);
E
Erich Gamma 已提交
1725 1726 1727
		return computer.getViewZones();
	}

1728
	_getOriginalEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
A
Alex Dima 已提交
1729
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1730 1731
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1732
		};
E
Erich Gamma 已提交
1733

A
Alex Dima 已提交
1734 1735
		for (let i = 0, length = lineChanges.length; i < length; i++) {
			let lineChange = lineChanges[i];
E
Erich Gamma 已提交
1736 1737 1738

			// Add overview zones in the overview ruler
			if (isChangeOrDelete(lineChange)) {
1739 1740 1741 1742 1743 1744 1745
				result.decorations.push({
					range: new Range(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE),
					options: {
						marginClassName: 'line-delete',
					}
				});

1746
				result.overviewZones.push(new editorCommon.OverviewRulerZone(
A
Alex Dima 已提交
1747 1748 1749 1750 1751 1752 1753
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(255, 0, 0, 0.4)',
					'rgba(255, 0, 0, 0.4)'
				));
E
Erich Gamma 已提交
1754 1755 1756 1757 1758 1759
			}
		}

		return result;
	}

1760
	_getModifiedEditorDecorations(lineChanges: editorCommon.ILineChange[], ignoreTrimWhitespace: boolean, renderIndicators: boolean, originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1761

A
Alex Dima 已提交
1762
		let result: IEditorDiffDecorations = {
J
Johannes Rieken 已提交
1763 1764
			decorations: [],
			overviewZones: []
A
Alex Dima 已提交
1765 1766 1767 1768 1769 1770
		};

		let modifiedModel = modifiedEditor.getModel();

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

			// Add decorations & overview zones
			if (isChangeOrInsert(lineChange)) {
1774 1775 1776 1777
				result.decorations.push({
					range: new Range(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE),
					options: {
						className: 'line-insert',
1778
						linesDecorationsClassName: renderIndicators ? 'insert-sign' : undefined,
1779 1780 1781 1782
						marginClassName: 'line-insert',
						isWholeLine: true
					}
				});
E
Erich Gamma 已提交
1783

1784
				result.overviewZones.push(new editorCommon.OverviewRulerZone(
A
Alex Dima 已提交
1785 1786 1787 1788 1789 1790 1791
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(155, 185, 85, 0.4)',
					'rgba(155, 185, 85, 0.4)'
				));
E
Erich Gamma 已提交
1792 1793

				if (lineChange.charChanges) {
A
Alex Dima 已提交
1794 1795
					for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1796 1797
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
A
Alex Dima 已提交
1798 1799 1800
								for (let lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									let startColumn: number;
									let endColumn: number;
E
Erich Gamma 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
									if (lineNumber === charChange.modifiedStartLineNumber) {
										startColumn = charChange.modifiedStartColumn;
									} else {
										startColumn = modifiedModel.getLineFirstNonWhitespaceColumn(lineNumber);
									}
									if (lineNumber === charChange.modifiedEndLineNumber) {
										endColumn = charChange.modifiedEndColumn;
									} else {
										endColumn = modifiedModel.getLineLastNonWhitespaceColumn(lineNumber);
									}
									result.decorations.push(createDecoration(lineNumber, startColumn, lineNumber, endColumn, 'char-insert', false));
								}
							} else {
								result.decorations.push(createDecoration(charChange.modifiedStartLineNumber, charChange.modifiedStartColumn, charChange.modifiedEndLineNumber, charChange.modifiedEndColumn, 'char-insert', false));
							}
						}
					}
				} else {
					result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, 'char-insert', true));
				}
			}
		}

		return result;
	}

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

}

class InlineViewZonesComputer extends ViewZonesComputer {

J
Johannes Rieken 已提交
1836 1837 1838
	private originalModel: editorCommon.IModel;
	private modifiedEditorConfiguration: editorCommon.InternalEditorOptions;
	private modifiedEditorTabSize: number;
R
rebornix 已提交
1839
	private renderIndicators: boolean;
E
Erich Gamma 已提交
1840

R
rebornix 已提交
1841
	constructor(lineChanges: editorCommon.ILineChange[], originalForeignVZ: editorCommon.IEditorWhitespace[], modifiedForeignVZ: editorCommon.IEditorWhitespace[], originalEditor: editorBrowser.ICodeEditor, modifiedEditor: editorBrowser.ICodeEditor, renderIndicators: boolean) {
E
Erich Gamma 已提交
1842 1843 1844
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
		this.originalModel = originalEditor.getModel();
		this.modifiedEditorConfiguration = modifiedEditor.getConfiguration();
1845
		this.modifiedEditorTabSize = modifiedEditor.getModel().getOptions().tabSize;
R
rebornix 已提交
1846
		this.renderIndicators = renderIndicators;
E
Erich Gamma 已提交
1847 1848
	}

J
Johannes Rieken 已提交
1849
	_produceOriginalFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone {
1850 1851 1852 1853
		let marginDomNode = document.createElement('div');
		marginDomNode.className = 'inline-added-margin-view-zone';
		Configuration.applyFontInfoSlow(marginDomNode, this.modifiedEditorConfiguration.fontInfo);

E
Erich Gamma 已提交
1854
		return {
J
Johannes Rieken 已提交
1855
			afterLineNumber: Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber),
E
Erich Gamma 已提交
1856
			heightInLines: lineChangeModifiedLength,
1857 1858
			domNode: document.createElement('div'),
			marginDomNode: marginDomNode
E
Erich Gamma 已提交
1859 1860 1861
		};
	}

J
Johannes Rieken 已提交
1862
	_produceModifiedFromDiff(lineChange: editorCommon.ILineChange, lineChangeOriginalLength: number, lineChangeModifiedLength: number): IMyViewZone {
A
Alex Dima 已提交
1863
		let decorations: InlineDecoration[] = [];
E
Erich Gamma 已提交
1864
		if (lineChange.charChanges) {
A
Alex Dima 已提交
1865 1866
			for (let j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
				let charChange = lineChange.charChanges[j];
E
Erich Gamma 已提交
1867
				if (isChangeOrDelete(charChange)) {
1868 1869
					decorations.push(new InlineDecoration(
						new Range(charChange.originalStartLineNumber, charChange.originalStartColumn, charChange.originalEndLineNumber, charChange.originalEndColumn),
1870 1871
						'char-delete',
						false
1872
					));
E
Erich Gamma 已提交
1873 1874 1875 1876
				}
			}
		}

A
Alex Dima 已提交
1877
		let html: string[] = [];
1878 1879 1880
		let marginHTML: string[] = [];
		let lineDecorationsWidth = this.modifiedEditorConfiguration.layoutInfo.decorationsWidth;
		let lineHeight = this.modifiedEditorConfiguration.lineHeight;
A
Alex Dima 已提交
1881
		for (let lineNumber = lineChange.originalStartLineNumber; lineNumber <= lineChange.originalEndLineNumber; lineNumber++) {
1882
			html = html.concat(this.renderOriginalLine(lineNumber - lineChange.originalStartLineNumber, this.originalModel, this.modifiedEditorConfiguration, this.modifiedEditorTabSize, lineNumber, decorations));
1883

R
rebornix 已提交
1884
			if (this.renderIndicators) {
1885 1886 1887 1888 1889
				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 已提交
1890 1891
		}

A
Alex Dima 已提交
1892
		let domNode = document.createElement('div');
E
Erich Gamma 已提交
1893 1894
		domNode.className = 'view-lines line-delete';
		domNode.innerHTML = html.join('');
1895
		Configuration.applyFontInfoSlow(domNode, this.modifiedEditorConfiguration.fontInfo);
E
Erich Gamma 已提交
1896

1897 1898 1899 1900 1901
		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 已提交
1902 1903 1904 1905
		return {
			shouldNotShrink: true,
			afterLineNumber: (lineChange.modifiedEndLineNumber === 0 ? lineChange.modifiedStartLineNumber : lineChange.modifiedStartLineNumber - 1),
			heightInLines: lineChangeOriginalLength,
1906 1907
			domNode: domNode,
			marginDomNode: marginDomNode
E
Erich Gamma 已提交
1908 1909 1910
		};
	}

J
Johannes Rieken 已提交
1911
	private renderOriginalLine(count: number, originalModel: editorCommon.IModel, config: editorCommon.InternalEditorOptions, tabSize: number, lineNumber: number, decorations: InlineDecoration[]): string[] {
1912
		let lineContent = originalModel.getLineContent(lineNumber);
E
Erich Gamma 已提交
1913

A
Alex Dima 已提交
1914
		let actualDecorations = LineDecoration.filter(decorations, lineNumber, 1, lineContent.length + 1);
E
Erich Gamma 已提交
1915

A
Alex Dima 已提交
1916 1917 1918 1919 1920 1921
		const defaultMetadata = (
			(FontStyle.None << MetadataConsts.FONT_STYLE_OFFSET)
			| (ColorId.DefaultForeground << MetadataConsts.FOREGROUND_OFFSET)
			| (ColorId.DefaultBackground << MetadataConsts.BACKGROUND_OFFSET)
		) >>> 0;

A
Alex Dima 已提交
1922
		let r = renderViewLine(new RenderLineInput(
1923
			(config.fontInfo.isMonospace && !config.viewInfo.disableMonospaceOptimizations),
A
Alex Dima 已提交
1924
			lineContent,
1925
			originalModel.mightContainRTL(),
1926
			0,
A
Alex Dima 已提交
1927
			[new ViewLineToken(lineContent.length, defaultMetadata)],
1928
			actualDecorations,
A
Alex Dima 已提交
1929
			tabSize,
1930
			config.fontInfo.spaceWidth,
1931 1932
			config.viewInfo.stopRenderingLineAfter,
			config.viewInfo.renderWhitespace,
1933
			config.viewInfo.renderControlCharacters
A
Alex Dima 已提交
1934
		));
E
Erich Gamma 已提交
1935

J
Johannes Rieken 已提交
1936
		let myResult: string[] = [];
E
Erich Gamma 已提交
1937 1938 1939 1940 1941 1942 1943 1944
		myResult.push('<div class="view-line');
		if (decorations.length === 0) {
			// No char changes
			myResult.push(' char-delete');
		}
		myResult.push('" style="top:');
		myResult.push(String(count * config.lineHeight));
		myResult.push('px;width:1000000px;">');
1945
		myResult = myResult.concat(r.html);
E
Erich Gamma 已提交
1946 1947 1948 1949 1950 1951
		myResult.push('</div>');

		return myResult;
	}
}

J
Johannes Rieken 已提交
1952
function isChangeOrInsert(lineChange: editorCommon.IChange): boolean {
E
Erich Gamma 已提交
1953 1954 1955
	return lineChange.modifiedEndLineNumber > 0;
}

J
Johannes Rieken 已提交
1956
function isChangeOrDelete(lineChange: editorCommon.IChange): boolean {
E
Erich Gamma 已提交
1957 1958 1959
	return lineChange.originalEndLineNumber > 0;
}

J
Johannes Rieken 已提交
1960
function createDecoration(startLineNumber: number, startColumn: number, endLineNumber: number, endColumn: number, className: string, isWholeLine: boolean) {
E
Erich Gamma 已提交
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
	return {
		range: new Range(startLineNumber, startColumn, endLineNumber, endColumn),
		options: {
			className: className,
			isWholeLine: isWholeLine
		}
	};
}

function createFakeLinesDiv(): HTMLElement {
A
Alex Dima 已提交
1971
	let r = document.createElement('div');
E
Erich Gamma 已提交
1972 1973 1974
	r.className = 'diagonal-fill';
	return r;
}