diffEditorWidget.ts 64.5 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';
A
Alex Dima 已提交
9 10
import {IAction} from 'vs/base/common/actions';
import {RunOnceScheduler} from 'vs/base/common/async';
A
Alex Dima 已提交
11
import {EventEmitter, EmitterEvent} from 'vs/base/common/eventEmitter';
J
Joao Moreno 已提交
12
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
13 14 15 16 17
import * as objects from 'vs/base/common/objects';
import * as dom from 'vs/base/browser/dom';
import {StyleMutator} from 'vs/base/browser/styleMutator';
import {ISashEvent, IVerticalSashLayoutProvider, Sash} from 'vs/base/browser/ui/sash/sash';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
E
Erich Gamma 已提交
18 19
import {DefaultConfig} from 'vs/editor/common/config/defaultConfig';
import {Range} from 'vs/editor/common/core/range';
A
Alex Dima 已提交
20
import * as editorCommon from 'vs/editor/common/editorCommon';
21
import {IEditorWorkerService} from 'vs/editor/common/services/editorWorkerService';
22
import {createLineParts} from 'vs/editor/common/viewLayout/viewLineParts';
A
Alex Dima 已提交
23
import {renderLine, RenderLineInput} from 'vs/editor/common/viewLayout/viewLineRenderer';
A
Alex Dima 已提交
24 25
import * as editorBrowser from 'vs/editor/browser/editorBrowser';
import {CodeEditorWidget} from 'vs/editor/browser/widget/codeEditorWidget';
A
Alex Dima 已提交
26
import {ViewLineToken, ViewLineTokens} from 'vs/editor/common/core/viewLineToken';
27
import {Configuration} from 'vs/editor/browser/config/configuration';
E
Erich Gamma 已提交
28 29 30 31 32 33 34

interface IEditorScrollEvent {
	scrollLeft: number;
	scrollTop: number;
}

interface IEditorDiffDecorations {
A
Alex Dima 已提交
35
	decorations:editorCommon.IModelDeltaDecoration[];
A
Alex Dima 已提交
36
	overviewZones:editorBrowser.OverviewRulerZone[];
E
Erich Gamma 已提交
37 38 39
}

interface IEditorDiffDecorationsWithZones extends IEditorDiffDecorations {
A
Alex Dima 已提交
40
	zones:editorBrowser.IViewZone[];
E
Erich Gamma 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
}

interface IEditorsDiffDecorations {
	original:IEditorDiffDecorations;
	modified:IEditorDiffDecorations;
}

interface IEditorsDiffDecorationsWithZones {
	original:IEditorDiffDecorationsWithZones;
	modified:IEditorDiffDecorationsWithZones;
}

interface IEditorsZones {
A
Alex Dima 已提交
54 55
	original:editorBrowser.IViewZone[];
	modified:editorBrowser.IViewZone[];
E
Erich Gamma 已提交
56 57 58
}

interface IDiffEditorWidgetStyle {
A
Alex Dima 已提交
59
	getEditorsDiffDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalWhitespaces:editorCommon.IEditorWhitespace[], modifiedWhitespaces:editorCommon.IEditorWhitespace[], originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorsDiffDecorationsWithZones;
E
Erich Gamma 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	setEnableSplitViewResizing(enableSplitViewResizing:boolean): void;
	layout(): number;
	dispose(): void;
}

class VisualEditorState {
	private _zones:number[];
	private _zonesMap:{[zoneId:string]:boolean;};
	private _decorations:string[];

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

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

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

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

A
Alex Dima 已提交
101
	public apply(editor:CodeEditorWidget, overviewRuler:editorBrowser.IOverviewRuler, newDecorations:IEditorDiffDecorationsWithZones): void {
E
Erich Gamma 已提交
102 103 104 105
		var i:number,
			length: number;

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

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

		// overview ruler
		overviewRuler.setZones(newDecorations.overviewZones);
	}
}

var DIFF_EDITOR_ID = 0;

A
Alex Dima 已提交
130
export class DiffEditorWidget extends EventEmitter implements editorBrowser.IDiffEditor {
E
Erich Gamma 已提交
131 132 133 134 135 136 137

	private static ONE_OVERVIEW_WIDTH = 15;
	public static ENTIRE_DIFF_OVERVIEW_WIDTH = 30;
	private static UPDATE_DIFF_DECORATIONS_DELAY = 200; // ms

	private id: number;

A
Alex Dima 已提交
138
	private _toDispose:IDisposable[];
E
Erich Gamma 已提交
139 140 141 142 143 144 145 146 147 148 149

	private _theme:string;
	private _domElement:HTMLElement;
	_containerDomElement:HTMLElement;
	private _overviewDomElement:HTMLElement;
	private _overviewViewportDomElement: HTMLElement;

	private _width:number;
	private _height:number;
	private _measureDomElementToken:number;

A
Alex Dima 已提交
150
	private originalEditor:CodeEditorWidget;
E
Erich Gamma 已提交
151 152
	private _originalDomNode:HTMLElement;
	private _originalEditorState:VisualEditorState;
A
Alex Dima 已提交
153
	private _originalOverviewRuler:editorBrowser.IOverviewRuler;
E
Erich Gamma 已提交
154

A
Alex Dima 已提交
155
	private modifiedEditor:CodeEditorWidget;
E
Erich Gamma 已提交
156 157
	private _modifiedDomNode:HTMLElement;
	private _modifiedEditorState:VisualEditorState;
A
Alex Dima 已提交
158
	private _modifiedOverviewRuler:editorBrowser.IOverviewRuler;
E
Erich Gamma 已提交
159 160 161 162

	private _currentlyChangingViewZones:boolean;
	private _beginUpdateDecorationsTimeout:number;
	private _diffComputationToken:number;
A
Alex Dima 已提交
163
	private _lineChanges:editorCommon.ILineChange[];
E
Erich Gamma 已提交
164 165 166 167 168

	private _isVisible:boolean;
	private _isHandlingScrollEvent:boolean;

	private _ignoreTrimWhitespace: boolean;
169
	private _originalIsEditable: boolean;
E
Erich Gamma 已提交
170 171 172 173 174

	private _renderSideBySide:boolean;
	private _enableSplitViewResizing:boolean;
	private _strategy:IDiffEditorWidgetStyle;

A
Alex Dima 已提交
175
	private _updateDecorationsRunner:RunOnceScheduler;
E
Erich Gamma 已提交
176

177 178 179 180
	private _editorWorkerService: IEditorWorkerService;

	constructor(
		domElement:HTMLElement,
A
Alex Dima 已提交
181
		options:editorCommon.IDiffEditorOptions,
182 183 184
		@IEditorWorkerService editorWorkerService: IEditorWorkerService,
		@IInstantiationService instantiationService: IInstantiationService
	) {
E
Erich Gamma 已提交
185
		super();
186
		this._editorWorkerService = editorWorkerService;
E
Erich Gamma 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

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

206 207 208 209 210
		this._originalIsEditable = false;
		if (typeof options.originalEditable !== 'undefined') {
			this._originalIsEditable = Boolean(options.originalEditable);
		}

A
Alex Dima 已提交
211
		this._updateDecorationsRunner = new RunOnceScheduler(() => this._updateDecorations(), 0);
E
Erich Gamma 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

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

		this._overviewViewportDomElement = document.createElement('div');
		this._overviewViewportDomElement.className = 'diffViewport';
		this._overviewViewportDomElement.style.position = 'absolute';

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

		this._overviewDomElement.appendChild(this._overviewViewportDomElement);

A
Alex Dima 已提交
233
		this._toDispose.push(dom.addDisposableListener(this._overviewDomElement, 'mousedown', (e:MouseEvent) => {
E
Erich Gamma 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
			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;

		this._createLeftHandSideEditor(options, instantiationService);
		this._createRightHandSideEditor(options, instantiationService);

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

276 277 278 279 280 281 282 283
	public get ignoreTrimWhitespace(): boolean {
		return this._ignoreTrimWhitespace;
	}

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

E
Erich Gamma 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	private static _getClassName(theme:string, renderSideBySide:boolean): string {
		var result = 'monaco-diff-editor monaco-editor-background ';
		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);
	}

A
Alex Dima 已提交
327
	private _createLeftHandSideEditor(options:editorCommon.IDiffEditorOptions, instantiationService:IInstantiationService): void {
328
		this.originalEditor = instantiationService.createInstance(CodeEditorWidget, this._originalDomNode, this._adjustOptionsForLeftHandSide(options, this._originalIsEditable));
E
Erich Gamma 已提交
329
		this._toDispose.push(this.originalEditor.addBulkListener2((events: any) => this._onOriginalEditorEvents(events)));
A
Alex Dima 已提交
330
		this._toDispose.push(this.addEmitter2(this.originalEditor));
E
Erich Gamma 已提交
331 332
	}

A
Alex Dima 已提交
333
	private _createRightHandSideEditor(options:editorCommon.IDiffEditorOptions, instantiationService:IInstantiationService): void {
334
		this.modifiedEditor = instantiationService.createInstance(CodeEditorWidget, this._modifiedDomNode, this._adjustOptionsForRightHandSide(options));
E
Erich Gamma 已提交
335
		this._toDispose.push(this.modifiedEditor.addBulkListener2((events: any) => this._onModifiedEditorEvents(events)));
A
Alex Dima 已提交
336
		this._toDispose.push(this.addEmitter2(this.modifiedEditor));
E
Erich Gamma 已提交
337 338 339 340 341 342 343
	}

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

	public dispose(): void {
J
Joao Moreno 已提交
344
		this._toDispose = dispose(this._toDispose);
E
Erich Gamma 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

		window.clearInterval(this._measureDomElementToken);

		this._cleanViewZonesAndDecorations();

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

		this.originalEditor.destroy();
		this.modifiedEditor.destroy();

		this._strategy.dispose();

		super.dispose();
	}

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

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

	public getEditorType(): string {
A
Alex Dima 已提交
368
		return editorCommon.EditorType.IDiffEditor;
E
Erich Gamma 已提交
369 370
	}

A
Alex Dima 已提交
371
	public getLineChanges(): editorCommon.ILineChange[] {
E
Erich Gamma 已提交
372 373 374
		return this._lineChanges;
	}

A
Alex Dima 已提交
375
	public getOriginalEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
376 377 378
		return this.originalEditor;
	}

A
Alex Dima 已提交
379
	public getModifiedEditor(): editorBrowser.ICodeEditor {
E
Erich Gamma 已提交
380 381 382
		return this.modifiedEditor;
	}

A
Alex Dima 已提交
383
	public updateOptions(newOptions:editorCommon.IDiffEditorOptions): void {
E
Erich Gamma 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
		// Handle new theme
		this._theme = newOptions && newOptions.theme ? newOptions.theme : this._theme;

		// Handle side by side
		var renderSideBySideChanged = false;
		if (typeof newOptions.renderSideBySide !== 'undefined') {
			if (this._renderSideBySide !== newOptions.renderSideBySide) {
				this._renderSideBySide = newOptions.renderSideBySide;
				renderSideBySideChanged = true;
			}
		}

		if (typeof newOptions.ignoreTrimWhitespace !== 'undefined') {
			if (this._ignoreTrimWhitespace !== newOptions.ignoreTrimWhitespace) {
				this._ignoreTrimWhitespace = newOptions.ignoreTrimWhitespace;
				// Begin comparing
				this._beginUpdateDecorations();
			}
		}

404 405 406 407
		if (typeof newOptions.originalEditable !== 'undefined') {
			this._originalIsEditable = Boolean(newOptions.originalEditable);
		}

E
Erich Gamma 已提交
408 409 410 411
		// Update class name
		this._containerDomElement.className = DiffEditorWidget._getClassName(this._theme, this._renderSideBySide);

		this.modifiedEditor.updateOptions(this._adjustOptionsForRightHandSide(newOptions));
412
		this.originalEditor.updateOptions(this._adjustOptionsForLeftHandSide(newOptions, this._originalIsEditable));
E
Erich Gamma 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

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

	public getValue(options:{ preserveBOM:boolean; lineEnding:string; }=null): string {
		return this.modifiedEditor.getValue(options);
	}

A
Alex Dima 已提交
434
	public getModel(): editorCommon.IDiffEditorModel {
E
Erich Gamma 已提交
435 436 437 438 439 440
		return {
			original: this.originalEditor.getModel(),
			modified: this.modifiedEditor.getModel()
		};
	}

A
Alex Dima 已提交
441
	public setModel(model:editorCommon.IDiffEditorModel): void {
E
Erich Gamma 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		// Guard us against partial null model
		if (model && (!model.original || !model.modified)) {
			throw new Error(!model.original ? 'DiffEditorWidget.setModel: Original model is null' : 'DiffEditorWidget.setModel: Modified model is null');
		}

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

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

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

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

		if (model) {
			this._recreateOverviewRulers();

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

		this._layoutOverviewViewport();
	}

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

A
Alex Dima 已提交
480
	public getVisibleColumnFromPosition(position:editorCommon.IPosition): number {
E
Erich Gamma 已提交
481 482 483
		return this.modifiedEditor.getVisibleColumnFromPosition(position);
	}

A
Alex Dima 已提交
484
	public getPosition(): editorCommon.IEditorPosition {
E
Erich Gamma 已提交
485 486 487
		return this.modifiedEditor.getPosition();
	}

A
Alex Dima 已提交
488
	public setPosition(position: editorCommon.IPosition, reveal?:boolean, revealVerticalInCenter?:boolean, revealHorizontal?:boolean): void {
E
Erich Gamma 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
		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);
	}

A
Alex Dima 已提交
504
	public revealPosition(position: editorCommon.IPosition, revealVerticalInCenter:boolean = false, revealHorizontal:boolean = false): void {
E
Erich Gamma 已提交
505 506 507
		this.modifiedEditor.revealPosition(position, revealVerticalInCenter, revealHorizontal);
	}

A
Alex Dima 已提交
508
	public revealPositionInCenter(position: editorCommon.IPosition): void {
E
Erich Gamma 已提交
509 510 511
		this.modifiedEditor.revealPositionInCenter(position);
	}

A
Alex Dima 已提交
512
	public revealPositionInCenterIfOutsideViewport(position: editorCommon.IPosition): void {
E
Erich Gamma 已提交
513 514 515
		this.modifiedEditor.revealPositionInCenterIfOutsideViewport(position);
	}

A
Alex Dima 已提交
516
	public getSelection(): editorCommon.IEditorSelection {
E
Erich Gamma 已提交
517 518 519
		return this.modifiedEditor.getSelection();
	}

A
Alex Dima 已提交
520
	public getSelections(): editorCommon.IEditorSelection[] {
E
Erich Gamma 已提交
521 522 523
		return this.modifiedEditor.getSelections();
	}

A
Alex Dima 已提交
524 525 526 527
	public setSelection(range:editorCommon.IRange, reveal?:boolean, revealVerticalInCenter?:boolean, revealHorizontal?:boolean): void;
	public setSelection(editorRange:editorCommon.IEditorRange, reveal?:boolean, revealVerticalInCenter?:boolean, revealHorizontal?:boolean): void;
	public setSelection(selection:editorCommon.ISelection, reveal?:boolean, revealVerticalInCenter?:boolean, revealHorizontal?:boolean): void;
	public setSelection(editorSelection:editorCommon.IEditorSelection, reveal?:boolean, revealVerticalInCenter?:boolean, revealHorizontal?:boolean): void;
E
Erich Gamma 已提交
528 529 530 531
	public setSelection(something:any, reveal?:boolean, revealVerticalInCenter?:boolean, revealHorizontal?:boolean): void {
		this.modifiedEditor.setSelection(something, reveal, revealVerticalInCenter, revealHorizontal);
	}

A
Alex Dima 已提交
532
	public setSelections(ranges: editorCommon.ISelection[]): void {
E
Erich Gamma 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
		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);
	}

A
Alex Dima 已提交
548
	public revealRange(range: editorCommon.IRange, revealVerticalInCenter:boolean = false, revealHorizontal:boolean = true): void {
E
Erich Gamma 已提交
549 550 551
		this.modifiedEditor.revealRange(range, revealVerticalInCenter, revealHorizontal);
	}

A
Alex Dima 已提交
552
	public revealRangeInCenter(range: editorCommon.IRange): void {
E
Erich Gamma 已提交
553 554 555
		this.modifiedEditor.revealRangeInCenter(range);
	}

A
Alex Dima 已提交
556
	public revealRangeInCenterIfOutsideViewport(range: editorCommon.IRange): void {
E
Erich Gamma 已提交
557 558 559
		this.modifiedEditor.revealRangeInCenterIfOutsideViewport(range);
	}

A
Alex Dima 已提交
560
	public addAction(descriptor:editorCommon.IActionDescriptor): void {
E
Erich Gamma 已提交
561 562 563
		this.modifiedEditor.addAction(descriptor);
	}

A
Alex Dima 已提交
564
	public getActions(): IAction[] {
E
Erich Gamma 已提交
565 566 567
		return this.modifiedEditor.getActions();
	}

A
Alex Dima 已提交
568
	public getAction(id:string): IAction {
E
Erich Gamma 已提交
569 570 571
		return this.modifiedEditor.getAction(id);
	}

A
Alex Dima 已提交
572
	public saveViewState(): editorCommon.IDiffEditorViewState {
E
Erich Gamma 已提交
573 574 575 576 577 578 579 580
		var originalViewState = this.originalEditor.saveViewState();
		var modifiedViewState = this.modifiedEditor.saveViewState();
		return {
			original: originalViewState,
			modified: modifiedViewState
		};
	}

A
Alex Dima 已提交
581
	public restoreViewState(state:editorCommon.IEditorViewState): void {
E
Erich Gamma 已提交
582 583
		var s = <any>state;
		if (s.original && s.original) {
A
Alex Dima 已提交
584
			var diffEditorState = <editorCommon.IDiffEditorViewState>s;
E
Erich Gamma 已提交
585 586 587 588 589
			this.originalEditor.restoreViewState(diffEditorState.original);
			this.modifiedEditor.restoreViewState(diffEditorState.modified);
		}
	}

A
Alex Dima 已提交
590
	public layout(dimension?:editorCommon.IDimension): void {
E
Erich Gamma 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		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();
	}

	public trigger(source:string, handlerId:string, payload:any): void {
		this.modifiedEditor.trigger(source, handlerId, payload);
	}

A
Alex Dima 已提交
622
	public changeDecorations(callback:(changeAccessor:editorCommon.IModelDecorationsChangeAccessor)=>any): any {
E
Erich Gamma 已提交
623 624 625 626 627 628 629 630 631
		return this.modifiedEditor.changeDecorations(callback);
	}

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



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

A
Alex Dima 已提交
632 633
	private _measureDomElement(forceDoLayoutCall:boolean, dimensions?:editorCommon.IDimension): void {
		dimensions = dimensions || dom.getDomNodePosition(this._containerDomElement);
E
Erich Gamma 已提交
634 635

		if (dimensions.width <= 0) {
636 637
			this._width = 0;
			this._height = 0;
E
Erich Gamma 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
			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 {
		var freeSpace = DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH - 2*DiffEditorWidget.ONE_OVERVIEW_WIDTH;
		var layoutInfo = this.modifiedEditor.getLayoutInfo();
		if (layoutInfo) {
656
			this._originalOverviewRuler.setLayout(new editorCommon.OverviewRulerPosition({
E
Erich Gamma 已提交
657 658 659
				top: 0,
				width: DiffEditorWidget.ONE_OVERVIEW_WIDTH,
				right: freeSpace + DiffEditorWidget.ONE_OVERVIEW_WIDTH,
660
				height: this._height
661 662
			}));
			this._modifiedOverviewRuler.setLayout(new editorCommon.OverviewRulerPosition({
E
Erich Gamma 已提交
663 664 665
				top: 0,
				right: 0,
				width: DiffEditorWidget.ONE_OVERVIEW_WIDTH,
666
				height: this._height
667
			}));
E
Erich Gamma 已提交
668 669 670 671 672
		}
	}

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

A
Alex Dima 已提交
673
	private _recomputeIfNecessary(events:EmitterEvent[]): void {
E
Erich Gamma 已提交
674 675 676
		var changed = false;
		for (var i = 0; !changed && i < events.length; i++) {
			var type = events[i].getType();
A
Alex Dima 已提交
677
			changed = changed || type === 'change' || type === editorCommon.EventType.ModelModeChanged;
E
Erich Gamma 已提交
678 679 680 681 682 683 684 685 686 687 688
		}
		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);
		}
	}

A
Alex Dima 已提交
689
	private _onOriginalEditorEvents(events:EmitterEvent[]): void {
E
Erich Gamma 已提交
690 691 692 693
		for (var i = 0; i < events.length; i++) {
			if (events[i].getType() === 'scroll') {
				this._onOriginalEditorScroll(events[i].getData());
			}
A
Alex Dima 已提交
694
			if (events[i].getType() === editorCommon.EventType.ViewZonesChanged) {
E
Erich Gamma 已提交
695 696 697 698 699 700
				this._onViewZonesChanged();
			}
		}
		this._recomputeIfNecessary(events);
	}

A
Alex Dima 已提交
701
	private _onModifiedEditorEvents(events:EmitterEvent[]): void {
E
Erich Gamma 已提交
702 703 704 705 706 707 708 709 710 711 712 713
		for (var i = 0; i < events.length; i++) {
			if (events[i].getType() === 'scroll') {
				this._onModifiedEditorScroll(events[i].getData());
				this._layoutOverviewViewport();
			}
			if (events[i].getType() === 'scrollSize') {
				this._layoutOverviewViewport();
			}
			if (events[i].getType() === 'viewLayoutChanged') {
				this._layoutOverviewViewport();
			}

A
Alex Dima 已提交
714
			if (events[i].getType() === editorCommon.EventType.ViewZonesChanged) {
E
Erich Gamma 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
				this._onViewZonesChanged();
			}
		}
		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++;
		var currentToken = this._diffComputationToken;
		var currentOriginalModel = this.originalEditor.getModel();
		var currentModifiedModel = this.modifiedEditor.getModel();

742
		this._editorWorkerService.computeDiff(currentOriginalModel.uri, currentModifiedModel.uri, this._ignoreTrimWhitespace).then((result) => {
743 744 745 746 747 748 749
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
				)
			{
				this._lineChanges = result;
				this._updateDecorationsRunner.schedule();
A
Alex Dima 已提交
750
				this.emit(editorCommon.EventType.DiffUpdated, { editor: this, lineChanges: result });
751 752 753 754 755 756 757
			}
		}, (error) => {
			if (currentToken === this._diffComputationToken
				&& currentOriginalModel === this.originalEditor.getModel()
				&& currentModifiedModel === this.modifiedEditor.getModel()
				)
			{
E
Erich Gamma 已提交
758 759 760
				this._lineChanges = null;
				this._updateDecorationsRunner.schedule();
			}
761
		});
E
Erich Gamma 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	}

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

	private _updateDecorations(): void {
		var lineChanges = this._lineChanges || [];

		var foreignOriginal = this._originalEditorState.getForeignViewZones(this.originalEditor.getWhitespaces());
		var foreignModified = this._modifiedEditorState.getForeignViewZones(this.modifiedEditor.getWhitespaces());

		var diffDecorations = this._strategy.getEditorsDiffDecorations(lineChanges, this._ignoreTrimWhitespace, foreignOriginal, foreignModified, this.originalEditor, this.modifiedEditor);

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

786 787
	private _adjustOptionsForSubEditor(options:editorCommon.IDiffEditorOptions): editorCommon.IDiffEditorOptions {
		let clonedOptions:editorCommon.IDiffEditorOptions = objects.clone(options || {});
E
Erich Gamma 已提交
788 789 790 791
		clonedOptions.wrappingColumn = -1;
		clonedOptions.automaticLayout = false;
		clonedOptions.scrollbar = clonedOptions.scrollbar || {};
		clonedOptions.scrollbar.vertical = 'visible';
A
Alex Dima 已提交
792
		clonedOptions.folding = false;
793
		clonedOptions.referenceInfos = false;
E
Erich Gamma 已提交
794 795 796
		return clonedOptions;
	}

797 798 799 800 801 802 803 804
	private _adjustOptionsForLeftHandSide(options:editorCommon.IDiffEditorOptions, isEditable:boolean): editorCommon.IDiffEditorOptions {
		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 已提交
805
	private _adjustOptionsForRightHandSide(options:editorCommon.IDiffEditorOptions): editorCommon.IDiffEditorOptions {
806 807 808 809 810
		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 已提交
811 812 813 814 815 816 817
	}

	private _onOriginalEditorScroll(e:IEditorScrollEvent): void {
		if (this._isHandlingScrollEvent) {
			return;
		}
		this._isHandlingScrollEvent = true;
818 819 820 821
		this.modifiedEditor.setScrollPosition({
			scrollLeft: e.scrollLeft,
			scrollTop: e.scrollTop
		});
E
Erich Gamma 已提交
822 823 824 825 826 827 828 829
		this._isHandlingScrollEvent = false;
	}

	private _onModifiedEditorScroll(e:IEditorScrollEvent): void {
		if(this._isHandlingScrollEvent) {
			return;
		}
		this._isHandlingScrollEvent = true;
830 831 832 833
		this.originalEditor.setScrollPosition({
			scrollLeft: e.scrollLeft,
			scrollTop: e.scrollTop
		});
E
Erich Gamma 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
		this._isHandlingScrollEvent = false;
	}

	private _doLayout(): void {
		var splitPoint = this._strategy.layout();

		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';
		this._overviewViewportDomElement.style.width = DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH + 'px';
		this._overviewViewportDomElement.style.height = '30px';

		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 {
		var layout = this._computeOverviewViewport();
		if (!layout) {
A
Alex Dima 已提交
865 866
			StyleMutator.setTop(this._overviewViewportDomElement, 0);
			StyleMutator.setHeight(this._overviewViewportDomElement, 0);
E
Erich Gamma 已提交
867
		} else {
A
Alex Dima 已提交
868 869
			StyleMutator.setTop(this._overviewViewportDomElement, layout.top);
			StyleMutator.setHeight(this._overviewViewportDomElement, layout.height);
E
Erich Gamma 已提交
870 871 872 873 874 875 876 877 878 879 880 881
		}
	}

	private _computeOverviewViewport(): { height: number; top: number;} {
		var layoutInfo = this.modifiedEditor.getLayoutInfo();
		if (!layoutInfo) {
			return null;
		}

		var scrollTop = this.modifiedEditor.getScrollTop();
		var scrollHeight = this.modifiedEditor.getScrollHeight();

882
		var computedAvailableSize = Math.max(0, layoutInfo.contentHeight);
E
Erich Gamma 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
		var computedRepresentableSize = Math.max(0, computedAvailableSize - 2 * 0);
		var computedRatio = scrollHeight > 0 ? (computedRepresentableSize / scrollHeight) : 0;

		var computedSliderSize = Math.max(1, Math.floor(layoutInfo.contentHeight * computedRatio));
		var computedSliderPosition = Math.floor(scrollTop * computedRatio);

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

	private _createDataSource():IDataSource {
		return {
			getWidth: () => {
				return this._width;
			},

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

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

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

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

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

	private _setStrategy(newStrategy:IDiffEditorWidgetStyle): void {
		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);
	}

A
Alex Dima 已提交
938
	private _getLineChangeAtOrBeforeLineNumber(lineNumber: number, startLineNumberExtractor:(lineChange:editorCommon.ILineChange)=>number): editorCommon.ILineChange {
E
Erich Gamma 已提交
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		if (this._lineChanges.length === 0 || lineNumber < startLineNumberExtractor(this._lineChanges[0])) {
			// There are no changes or `lineNumber` is before the first change
			return null;
		}

		var min = 0, max = this._lineChanges.length - 1;
		while (min < max) {
			var mid = Math.floor((min + max) / 2);
			var midStart = startLineNumberExtractor(this._lineChanges[mid]);
			var midEnd = (mid + 1 <= max ? startLineNumberExtractor(this._lineChanges[mid + 1]) : Number.MAX_VALUE);

			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 {
		var lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.originalStartLineNumber);

		if (!lineChange) {
			return lineNumber;
		}

		var originalEquivalentLineNumber = lineChange.originalStartLineNumber + (lineChange.originalEndLineNumber > 0 ? -1 : 0);
		var modifiedEquivalentLineNumber = lineChange.modifiedStartLineNumber + (lineChange.modifiedEndLineNumber > 0 ? -1 : 0);
		var lineChangeOriginalLength = (lineChange.originalEndLineNumber > 0 ? (lineChange.originalEndLineNumber - lineChange.originalStartLineNumber + 1) : 0);
		var lineChangeModifiedLength = (lineChange.modifiedEndLineNumber > 0 ? (lineChange.modifiedEndLineNumber - lineChange.modifiedStartLineNumber + 1) : 0);


		var delta = lineNumber - originalEquivalentLineNumber;

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

		return modifiedEquivalentLineNumber + lineChangeModifiedLength - lineChangeOriginalLength + delta ;
	}

	private _getEquivalentLineForModifiedLineNumber(lineNumber: number): number {
		var lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, (lineChange) => lineChange.modifiedStartLineNumber);

		if (!lineChange) {
			return lineNumber;
		}

		var originalEquivalentLineNumber = lineChange.originalStartLineNumber + (lineChange.originalEndLineNumber > 0 ? -1 : 0);
		var modifiedEquivalentLineNumber = lineChange.modifiedStartLineNumber + (lineChange.modifiedEndLineNumber > 0 ? -1 : 0);
		var lineChangeOriginalLength = (lineChange.originalEndLineNumber > 0 ? (lineChange.originalEndLineNumber - lineChange.originalStartLineNumber + 1) : 0);
		var lineChangeModifiedLength = (lineChange.modifiedEndLineNumber > 0 ? (lineChange.modifiedEndLineNumber - lineChange.modifiedStartLineNumber + 1) : 0);


		var delta = lineNumber - modifiedEquivalentLineNumber;

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

		return originalEquivalentLineNumber + lineChangeOriginalLength - lineChangeModifiedLength + delta ;
	}

A
Alex Dima 已提交
1007
	public getDiffLineInformationForOriginal(lineNumber:number): editorCommon.IDiffLineInformation {
E
Erich Gamma 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016
		if (!this._lineChanges) {
			// Cannot answer that which I don't know
			return null;
		}
		return {
			equivalentLineNumber: this._getEquivalentLineForOriginalLineNumber(lineNumber)
		};
	}

A
Alex Dima 已提交
1017
	public getDiffLineInformationForModified(lineNumber:number): editorCommon.IDiffLineInformation {
E
Erich Gamma 已提交
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
		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 已提交
1034 1035
	getOriginalEditor(): editorBrowser.ICodeEditor;
	getModifiedEditor(): editorBrowser.ICodeEditor;
E
Erich Gamma 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
}

class DiffEditorWidgetStyle {

	_dataSource:IDataSource;

	constructor(dataSource:IDataSource) {
		this._dataSource = dataSource;
	}

A
Alex Dima 已提交
1046
	public getEditorsDiffDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalWhitespaces:editorCommon.IEditorWhitespace[], modifiedWhitespaces:editorCommon.IEditorWhitespace[], originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorsDiffDecorationsWithZones {
E
Erich Gamma 已提交
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
		// Get view zones
		modifiedWhitespaces = modifiedWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
		originalWhitespaces = originalWhitespaces.sort((a, b) => {
			return a.afterLineNumber - b.afterLineNumber;
		});
		var zones = this._getViewZones(lineChanges, originalWhitespaces, modifiedWhitespaces, originalEditor, modifiedEditor);

		// Get decorations & overview ruler zones
		var originalDecorations = this._getOriginalEditorDecorations(lineChanges, ignoreTrimWhitespace, originalEditor, modifiedEditor);
		var modifiedDecorations = this._getModifiedEditorDecorations(lineChanges, ignoreTrimWhitespace, originalEditor, modifiedEditor);

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

A
Alex Dima 已提交
1074
	_getViewZones(lineChanges:editorCommon.ILineChange[], originalForeignVZ:editorCommon.IEditorWhitespace[], modifiedForeignVZ:editorCommon.IEditorWhitespace[], originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorsZones {
E
Erich Gamma 已提交
1075 1076 1077
		return null;
	}

A
Alex Dima 已提交
1078
	_getOriginalEditorDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1079 1080 1081
		return null;
	}

A
Alex Dima 已提交
1082
	_getModifiedEditorDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1083 1084 1085 1086
		return null;
	}
}

A
Alex Dima 已提交
1087
interface IMyViewZone extends editorBrowser.IViewZone {
E
Erich Gamma 已提交
1088 1089 1090 1091 1092 1093
	shouldNotShrink?: boolean;
}

class ForeignViewZonesIterator {

	private _index: number;
A
Alex Dima 已提交
1094 1095
	private _source: editorCommon.IEditorWhitespace[];
	public current: editorCommon.IEditorWhitespace;
E
Erich Gamma 已提交
1096

A
Alex Dima 已提交
1097
	constructor(source: editorCommon.IEditorWhitespace[]) {
E
Erich Gamma 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
		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;
		}
	}
}

1113
abstract class ViewZonesComputer {
E
Erich Gamma 已提交
1114

A
Alex Dima 已提交
1115 1116 1117
	private lineChanges:editorCommon.ILineChange[];
	private originalForeignVZ:editorCommon.IEditorWhitespace[];
	private modifiedForeignVZ:editorCommon.IEditorWhitespace[];
E
Erich Gamma 已提交
1118

A
Alex Dima 已提交
1119
	constructor(lineChanges:editorCommon.ILineChange[], originalForeignVZ:editorCommon.IEditorWhitespace[], modifiedForeignVZ:editorCommon.IEditorWhitespace[]) {
E
Erich Gamma 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
		this.lineChanges = lineChanges;
		this.originalForeignVZ = originalForeignVZ;
		this.modifiedForeignVZ = modifiedForeignVZ;
	}

	public getViewZones(): IEditorsZones {
		var result: IEditorsZones = {
			original: [],
			modified: []
		};

		var i:number,
			length:number,
A
Alex Dima 已提交
1133
			lineChange:editorCommon.ILineChange;
E
Erich Gamma 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150

		var stepOriginal: IMyViewZone[],
			stepModified: IMyViewZone[],
			stepOriginalIndex: number,
			stepModifiedIndex: number,
			lineChangeModifiedLength:number = 0,
			lineChangeOriginalLength:number = 0,
			originalEquivalentLineNumber: number = 0,
			modifiedEquivalentLineNumber: number = 0,
			originalEndEquivalentLineNumber: number = 0,
			modifiedEndEquivalentLineNumber: number = 0,
			viewZoneLineNumber: number = 0;

		var sortMyViewZones = (a:IMyViewZone, b:IMyViewZone) => {
			return a.afterLineNumber - b.afterLineNumber;
		};

A
Alex Dima 已提交
1151
		var addAndCombineIfPossible = (destination:editorBrowser.IViewZone[], item:IMyViewZone) => {
E
Erich Gamma 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
			if (item.domNode === null && destination.length > 0) {
				var lastItem = destination[destination.length - 1];
				if (lastItem.afterLineNumber === item.afterLineNumber && lastItem.domNode === null) {
					lastItem.heightInLines += item.heightInLines;
					return;
				}
			}
			destination.push(item);
		};

		var modifiedForeignVZ = new ForeignViewZonesIterator(this.modifiedForeignVZ);
		var originalForeignVZ = new ForeignViewZonesIterator(this.originalForeignVZ);

		// 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
		for (i = 0, length = this.lineChanges.length; i <= length; i++) {
			lineChange = (i < length ? this.lineChanges[i] : null);

			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
			stepOriginal = [];
			stepModified = [];

			// ---------------------------- 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) {
				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) {
				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)) {
				var r = this._produceOriginalFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
				if (r) {
					stepOriginal.push(r);
				}
			}

			if (lineChange !== null && isChangeOrDelete(lineChange)) {
				var r = this._produceModifiedFromDiff(lineChange, lineChangeOriginalLength, lineChangeModifiedLength);
				if (r) {
					stepModified.push(r);
				}
			}

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


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

			// [CANCEL & EMIT] Try to cancel view zones out
			stepOriginalIndex = 0;
			stepModifiedIndex = 0;

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

			while (stepOriginalIndex < stepOriginal.length && stepModifiedIndex < stepModified.length) {
				var original = stepOriginal[stepOriginalIndex];
				var modified = stepModified[stepModifiedIndex];

				var originalDelta = original.afterLineNumber - originalEquivalentLineNumber;
				var modifiedDelta = modified.afterLineNumber - modifiedEquivalentLineNumber;

				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
		}

		var ensureDomNode = (z:IMyViewZone) => {
			if (!z.domNode) {
				z.domNode = createFakeLinesDiv();
			}
		};

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

		return result;
	}

1305
	protected abstract _produceOriginalFromDiff(lineChange:editorCommon.ILineChange, lineChangeOriginalLength:number, lineChangeModifiedLength:number): IMyViewZone;
E
Erich Gamma 已提交
1306

1307
	protected abstract _produceModifiedFromDiff(lineChange:editorCommon.ILineChange, lineChangeOriginalLength:number, lineChangeModifiedLength:number): IMyViewZone;
E
Erich Gamma 已提交
1308 1309
}

A
Alex Dima 已提交
1310
class DiffEdtorWidgetSideBySide extends DiffEditorWidgetStyle implements IDiffEditorWidgetStyle, IVerticalSashLayoutProvider {
E
Erich Gamma 已提交
1311 1312 1313 1314

	static MINIMUM_EDITOR_WIDTH = 100;

	private _disableSash: boolean;
A
Alex Dima 已提交
1315
	private _sash:Sash;
E
Erich Gamma 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	private _sashRatio:number;
	private _sashPosition:number;
	private _startSashPosition:number;

	constructor(dataSource:IDataSource, enableSplitViewResizing:boolean) {
		super(dataSource);

		this._disableSash = (enableSplitViewResizing === false);
		this._sashRatio = null;
		this._sashPosition = null;
A
Alex Dima 已提交
1326
		this._sash = new Sash(this._dataSource.getContainerDomNode(), this);
E
Erich Gamma 已提交
1327 1328 1329 1330 1331

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

A
Alex Dima 已提交
1332 1333 1334 1335
		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 已提交
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	}

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

	public setEnableSplitViewResizing(enableSplitViewResizing:boolean): void {
		var newDisableSash = (enableSplitViewResizing === false);
		if (this._disableSash !== newDisableSash) {
			this._disableSash = newDisableSash;

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

	public layout(sashRatio:number = this._sashRatio): number {
		var w = this._dataSource.getWidth();
		var contentWidth = w - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;

		var sashPosition = Math.floor((sashRatio || 0.5) * contentWidth);
		var midPoint = Math.floor(0.5 * contentWidth);

A
Alex Dima 已提交
1362
		sashPosition = this._disableSash ? midPoint : sashPosition || midPoint;
E
Erich Gamma 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

		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 已提交
1384
	private onSashDragStart(): void {
E
Erich Gamma 已提交
1385 1386 1387
		this._startSashPosition = this._sashPosition;
	}

M
Maxime Quandalle 已提交
1388
	private onSashDrag(e:ISashEvent): void {
E
Erich Gamma 已提交
1389 1390 1391 1392 1393 1394 1395 1396 1397
		var w = this._dataSource.getWidth();
		var contentWidth = w - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH;
		var sashPosition = this.layout((this._startSashPosition + (e.currentX - e.startX)) / contentWidth);

		this._sashRatio = sashPosition / contentWidth;

		this._dataSource.relayoutEditors();
	}

M
Maxime Quandalle 已提交
1398 1399 1400 1401 1402 1403 1404
	private onSashDragEnd(): void {
		this._sash.layout();
	}

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

A
Alex Dima 已提交
1408
	public getVerticalSashTop(sash: Sash): number {
E
Erich Gamma 已提交
1409 1410 1411
		return 0;
	}

A
Alex Dima 已提交
1412
	public getVerticalSashLeft(sash: Sash): number {
E
Erich Gamma 已提交
1413 1414 1415
		return this._sashPosition;
	}

A
Alex Dima 已提交
1416
	public getVerticalSashHeight(sash: Sash): number {
E
Erich Gamma 已提交
1417 1418 1419
		return this._dataSource.getHeight();
	}

A
Alex Dima 已提交
1420
	_getViewZones(lineChanges:editorCommon.ILineChange[], originalForeignVZ:editorCommon.IEditorWhitespace[], modifiedForeignVZ:editorCommon.IEditorWhitespace[], originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorsZones {
E
Erich Gamma 已提交
1421 1422 1423 1424
		var c = new SideBySideViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ);
		return c.getViewZones();
	}

A
Alex Dima 已提交
1425
	_getOriginalEditorDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434

		var result:IEditorDiffDecorations = {
				decorations: [],
				overviewZones: []
			},
			i:number,
			length:number,
			j:number,
			lengthJ:number,
A
Alex Dima 已提交
1435 1436
			lineChange:editorCommon.ILineChange,
			charChange:editorCommon.ICharChange,
E
Erich Gamma 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
			lineNumber:number,
			startColumn:number,
			endColumn:number,
			originalModel = originalEditor.getModel();

		for (i = 0, length = lineChanges.length; i < length; i++) {
			lineChange = lineChanges[i];

			if (isChangeOrDelete(lineChange)) {

				result.decorations.push(createDecoration(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE, 'line-delete', true));
				if (!isChangeOrInsert(lineChange) || !lineChange.charChanges) {
					result.decorations.push(createDecoration(lineChange.originalStartLineNumber, 1, lineChange.originalEndLineNumber, Number.MAX_VALUE, 'char-delete', true));
				}

A
Alex Dima 已提交
1452 1453 1454 1455 1456 1457 1458 1459
				result.overviewZones.push(new editorBrowser.OverviewRulerZone(
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(255, 0, 0, 0.4)',
					'rgba(255, 0, 0, 0.4)'
				));
E
Erich Gamma 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

				if (lineChange.charChanges) {
					for (j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						charChange = lineChange.charChanges[j];
						if (isChangeOrDelete(charChange)) {
							if (ignoreTrimWhitespace) {
								for (lineNumber = charChange.originalStartLineNumber; lineNumber <= charChange.originalEndLineNumber; lineNumber++) {
									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;
	}

A
Alex Dima 已提交
1491
	_getModifiedEditorDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500

		var result:IEditorDiffDecorations = {
				decorations: [],
				overviewZones: []
			},
			i:number,
			length:number,
			j:number,
			lengthJ:number,
A
Alex Dima 已提交
1501 1502
			lineChange:editorCommon.ILineChange,
			charChange:editorCommon.ICharChange,
E
Erich Gamma 已提交
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
			lineNumber:number,
			startColumn:number,
			endColumn:number,
			modifiedModel = modifiedEditor.getModel();

		for (i = 0, length = lineChanges.length; i < length; i++) {
			lineChange = lineChanges[i];

			if (isChangeOrInsert(lineChange)) {

				result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, 'line-insert', true));
				if (!isChangeOrDelete(lineChange) || !lineChange.charChanges) {
					result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, 'char-insert', true));
				}
A
Alex Dima 已提交
1517 1518 1519 1520 1521 1522 1523 1524
				result.overviewZones.push(new editorBrowser.OverviewRulerZone(
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(155, 185, 85, 0.4)',
					'rgba(155, 185, 85, 0.4)'
				));
E
Erich Gamma 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558

				if (lineChange.charChanges) {
					for (j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						charChange = lineChange.charChanges[j];
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
								for (lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									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 {

A
Alex Dima 已提交
1559
	constructor(lineChanges:editorCommon.ILineChange[], originalForeignVZ:editorCommon.IEditorWhitespace[], modifiedForeignVZ:editorCommon.IEditorWhitespace[]) {
E
Erich Gamma 已提交
1560 1561 1562
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
	}

A
Alex Dima 已提交
1563
	_produceOriginalFromDiff(lineChange:editorCommon.ILineChange, lineChangeOriginalLength:number, lineChangeModifiedLength:number): IMyViewZone {
E
Erich Gamma 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
		if (lineChangeModifiedLength > lineChangeOriginalLength) {
			return {
				afterLineNumber: Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber),
				heightInLines: (lineChangeModifiedLength - lineChangeOriginalLength),
				domNode: null
			};
		}
		return null;
	}

A
Alex Dima 已提交
1574
	_produceModifiedFromDiff(lineChange:editorCommon.ILineChange, lineChangeOriginalLength:number, lineChangeModifiedLength:number): IMyViewZone {
E
Erich Gamma 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
		if (lineChangeOriginalLength > lineChangeModifiedLength) {
			return {
				afterLineNumber: Math.max(lineChange.modifiedStartLineNumber, lineChange.modifiedEndLineNumber),
				heightInLines: (lineChangeOriginalLength - lineChangeModifiedLength),
				domNode: null
			};
		}
		return null;
	}
}

class DiffEdtorWidgetInline extends DiffEditorWidgetStyle implements IDiffEditorWidgetStyle {

A
Alex Dima 已提交
1588
	private toDispose:IDisposable[];
E
Erich Gamma 已提交
1589 1590 1591 1592 1593
	private decorationsLeft: number;

	constructor(dataSource:IDataSource, enableSplitViewResizing:boolean) {
		super(dataSource);

1594
		this.decorationsLeft = dataSource.getOriginalEditor().getLayoutInfo().decorationsLeft;
E
Erich Gamma 已提交
1595 1596

		this.toDispose = [];
1597
		this.toDispose.push(dataSource.getOriginalEditor().addListener2(editorCommon.EventType.EditorLayout, (layoutInfo:editorCommon.EditorLayoutInfo) => {
E
Erich Gamma 已提交
1598 1599 1600 1601 1602 1603 1604 1605
			if (this.decorationsLeft !== layoutInfo.decorationsLeft) {
				this.decorationsLeft = layoutInfo.decorationsLeft;
				dataSource.relayoutEditors();
			}
		}));
	}

	public dispose(): void {
J
Joao Moreno 已提交
1606
		this.toDispose = dispose(this.toDispose);
E
Erich Gamma 已提交
1607 1608 1609 1610 1611 1612
	}

	public setEnableSplitViewResizing(enableSplitViewResizing:boolean): void {
		// Nothing to do..
	}

A
Alex Dima 已提交
1613
	_getViewZones(lineChanges:editorCommon.ILineChange[], originalForeignVZ:editorCommon.IEditorWhitespace[], modifiedForeignVZ:editorCommon.IEditorWhitespace[], originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorsZones {
E
Erich Gamma 已提交
1614 1615 1616 1617
		var computer = new InlineViewZonesComputer(lineChanges, originalForeignVZ, modifiedForeignVZ, originalEditor, modifiedEditor);
		return computer.getViewZones();
	}

A
Alex Dima 已提交
1618
	_getOriginalEditorDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1619 1620 1621 1622 1623 1624
		var result:IEditorDiffDecorations = {
				decorations: [],
				overviewZones: []
			},
			i:number,
			length:number,
A
Alex Dima 已提交
1625
			lineChange:editorCommon.ILineChange;
E
Erich Gamma 已提交
1626 1627 1628 1629 1630 1631

		for (i = 0, length = lineChanges.length; i < length; i++) {
			lineChange = lineChanges[i];

			// Add overview zones in the overview ruler
			if (isChangeOrDelete(lineChange)) {
A
Alex Dima 已提交
1632 1633 1634 1635 1636 1637 1638 1639
				result.overviewZones.push(new editorBrowser.OverviewRulerZone(
					lineChange.originalStartLineNumber,
					lineChange.originalEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(255, 0, 0, 0.4)',
					'rgba(255, 0, 0, 0.4)'
				));
E
Erich Gamma 已提交
1640 1641 1642 1643 1644 1645
			}
		}

		return result;
	}

A
Alex Dima 已提交
1646
	_getModifiedEditorDecorations(lineChanges:editorCommon.ILineChange[], ignoreTrimWhitespace:boolean, originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor): IEditorDiffDecorations {
E
Erich Gamma 已提交
1647 1648 1649 1650 1651 1652 1653

		var result:IEditorDiffDecorations = {
				decorations: [],
				overviewZones: []
			},
			i:number,
			length:number,
A
Alex Dima 已提交
1654
			lineChange:editorCommon.ILineChange,
E
Erich Gamma 已提交
1655 1656
			j:number,
			lengthJ:number,
A
Alex Dima 已提交
1657
			charChange: editorCommon.ICharChange,
E
Erich Gamma 已提交
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
			lineNumber:number,
			startColumn:number,
			endColumn:number,
			modifiedModel = modifiedEditor.getModel();

		for (i = 0, length = lineChanges.length; i < length; i++) {
			lineChange = lineChanges[i];

			// Add decorations & overview zones
			if (isChangeOrInsert(lineChange)) {
				result.decorations.push(createDecoration(lineChange.modifiedStartLineNumber, 1, lineChange.modifiedEndLineNumber, Number.MAX_VALUE, 'line-insert', true));

A
Alex Dima 已提交
1670 1671 1672 1673 1674 1675 1676 1677
				result.overviewZones.push(new editorBrowser.OverviewRulerZone(
					lineChange.modifiedStartLineNumber,
					lineChange.modifiedEndLineNumber,
					editorCommon.OverviewRulerLane.Full,
					0,
					'rgba(155, 185, 85, 0.4)',
					'rgba(155, 185, 85, 0.4)'
				));
E
Erich Gamma 已提交
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719

				if (lineChange.charChanges) {
					for (j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
						charChange = lineChange.charChanges[j];
						if (isChangeOrInsert(charChange)) {
							if (ignoreTrimWhitespace) {
								for (lineNumber = charChange.modifiedStartLineNumber; lineNumber <= charChange.modifiedEndLineNumber; lineNumber++) {
									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 {

A
Alex Dima 已提交
1720
	private originalModel:editorCommon.IModel;
1721
	private modifiedEditorConfiguration:editorCommon.InternalEditorOptions;
1722
	private modifiedEditorTabSize:number;
E
Erich Gamma 已提交
1723

A
Alex Dima 已提交
1724
	constructor(lineChanges:editorCommon.ILineChange[], originalForeignVZ:editorCommon.IEditorWhitespace[], modifiedForeignVZ:editorCommon.IEditorWhitespace[], originalEditor:editorBrowser.ICodeEditor, modifiedEditor:editorBrowser.ICodeEditor) {
E
Erich Gamma 已提交
1725 1726 1727
		super(lineChanges, originalForeignVZ, modifiedForeignVZ);
		this.originalModel = originalEditor.getModel();
		this.modifiedEditorConfiguration = modifiedEditor.getConfiguration();
1728
		this.modifiedEditorTabSize = modifiedEditor.getModel().getOptions().tabSize;
E
Erich Gamma 已提交
1729 1730
	}

A
Alex Dima 已提交
1731
	_produceOriginalFromDiff(lineChange:editorCommon.ILineChange, lineChangeOriginalLength:number, lineChangeModifiedLength:number): IMyViewZone {
E
Erich Gamma 已提交
1732 1733 1734 1735 1736 1737 1738
		return {
			afterLineNumber:Math.max(lineChange.originalStartLineNumber, lineChange.originalEndLineNumber),
			heightInLines: lineChangeModifiedLength,
			domNode: document.createElement('div')
		};
	}

A
Alex Dima 已提交
1739 1740
	_produceModifiedFromDiff(lineChange:editorCommon.ILineChange, lineChangeOriginalLength:number, lineChangeModifiedLength:number): IMyViewZone {
		var decorations:editorCommon.IModelDecoration[] = [],
E
Erich Gamma 已提交
1741 1742
			j:number,
			lengthJ:number,
A
Alex Dima 已提交
1743 1744
			charChange:editorCommon.ICharChange,
			tempDecoration:editorCommon.IModelDecoration;
E
Erich Gamma 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759

		if (lineChange.charChanges) {
			for (j = 0, lengthJ = lineChange.charChanges.length; j < lengthJ; j++) {
				charChange = lineChange.charChanges[j];
				if (isChangeOrDelete(charChange)) {
					tempDecoration = <any>createDecoration(charChange.originalStartLineNumber, charChange.originalStartColumn, charChange.originalEndLineNumber, charChange.originalEndColumn, 'char-delete', false);
					tempDecoration.options.inlineClassName = tempDecoration.options.className;
					decorations.push(tempDecoration);
				}
			}
		}

		var html: string[] = [],
			lineNumber: number;
		for (lineNumber = lineChange.originalStartLineNumber; lineNumber <= lineChange.originalEndLineNumber; lineNumber++) {
1760
			html = html.concat(this.renderOriginalLine(lineNumber - lineChange.originalStartLineNumber, this.originalModel, this.modifiedEditorConfiguration, this.modifiedEditorTabSize, lineNumber, decorations));
E
Erich Gamma 已提交
1761 1762 1763 1764 1765
		}

		var domNode = document.createElement('div');
		domNode.className = 'view-lines line-delete';
		domNode.innerHTML = html.join('');
1766
		Configuration.applyFontInfoSlow(domNode, this.modifiedEditorConfiguration.fontInfo);
E
Erich Gamma 已提交
1767 1768 1769 1770 1771 1772 1773 1774 1775

		return {
			shouldNotShrink: true,
			afterLineNumber: (lineChange.modifiedEndLineNumber === 0 ? lineChange.modifiedStartLineNumber : lineChange.modifiedStartLineNumber - 1),
			heightInLines: lineChangeOriginalLength,
			domNode: domNode
		};
	}

1776
	private renderOriginalLine(count:number, originalModel:editorCommon.IModel, config:editorCommon.InternalEditorOptions, tabSize:number, lineNumber:number, decorations:editorCommon.IModelDecoration[]): string[] {
1777
		let lineContent = originalModel.getLineContent(lineNumber);
E
Erich Gamma 已提交
1778

1779
		let lineTokens = new ViewLineTokens([new ViewLineToken(0, '')], 0, lineContent.length);
E
Erich Gamma 已提交
1780

1781
		let parts = createLineParts(lineNumber, 1, lineContent, tabSize, lineTokens, decorations, config.viewInfo.renderWhitespace, config.viewInfo.indentGuides);
E
Erich Gamma 已提交
1782

1783
		let r = renderLine(new RenderLineInput(
A
Alex Dima 已提交
1784 1785
			lineContent,
			tabSize,
1786
			config.fontInfo.spaceWidth,
1787 1788
			config.viewInfo.stopRenderingLineAfter,
			config.viewInfo.renderWhitespace,
A
Alex Dima 已提交
1789 1790
			parts.getParts()
		));
E
Erich Gamma 已提交
1791

1792
		let myResult:string[] = [];
E
Erich Gamma 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
		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;">');
		myResult = myResult.concat(r.output);
		myResult.push('</div>');

		return myResult;
	}
}

A
Alex Dima 已提交
1808
function isChangeOrInsert(lineChange:editorCommon.IChange): boolean {
E
Erich Gamma 已提交
1809 1810 1811
	return lineChange.modifiedEndLineNumber > 0;
}

A
Alex Dima 已提交
1812
function isChangeOrDelete(lineChange:editorCommon.IChange): boolean {
E
Erich Gamma 已提交
1813 1814 1815
	return lineChange.originalEndLineNumber > 0;
}

A
Alex Dima 已提交
1816
function createDecoration(startLineNumber:number, startColumn:number, endLineNumber:number, endColumn:number, className:string, isWholeLine:boolean): editorCommon.IModelDeltaDecoration {
E
Erich Gamma 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
	return {
		range: new Range(startLineNumber, startColumn, endLineNumber, endColumn),
		options: {
			className: className,
			isWholeLine: isWholeLine
		}
	};
}

function createFakeLinesDiv(): HTMLElement {
	var r = document.createElement('div');
	r.className = 'diagonal-fill';
	return r;
}