UNPKG

monaco-editor

Version:
958 lines • 87.8 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import './media/diffEditor.css'; import * as nls from '../../../nls.js'; import * as dom from '../../../base/browser/dom.js'; import { createFastDomNode } from '../../../base/browser/fastDomNode.js'; import { Sash } from '../../../base/browser/ui/sash/sash.js'; import { RunOnceScheduler } from '../../../base/common/async.js'; import { Emitter } from '../../../base/common/event.js'; import { Disposable } from '../../../base/common/lifecycle.js'; import * as objects from '../../../base/common/objects.js'; import { Configuration } from '../config/configuration.js'; import { StableEditorScrollState } from '../core/editorState.js'; import { ICodeEditorService } from '../services/codeEditorService.js'; import { CodeEditorWidget } from './codeEditorWidget.js'; import { DiffReview } from './diffReview.js'; import * as editorOptions from '../../common/config/editorOptions.js'; import { Range } from '../../common/core/range.js'; import { createStringBuilder } from '../../common/core/stringBuilder.js'; import * as editorCommon from '../../common/editorCommon.js'; import { ModelDecorationOptions } from '../../common/model/textModel.js'; import { IEditorWorkerService } from '../../common/services/editorWorkerService.js'; import { OverviewRulerZone } from '../../common/view/overviewZoneManager.js'; import { LineDecoration } from '../../common/viewLayout/lineDecorations.js'; import { RenderLineInput, renderViewLine } from '../../common/viewLayout/viewLineRenderer.js'; import { InlineDecoration, ViewLineRenderingData } from '../../common/viewModel/viewModel.js'; import { IContextKeyService } from '../../../platform/contextkey/common/contextkey.js'; import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js'; import { ServiceCollection } from '../../../platform/instantiation/common/serviceCollection.js'; import { INotificationService } from '../../../platform/notification/common/notification.js'; import { defaultInsertColor, defaultRemoveColor, diffBorder, diffInserted, diffInsertedOutline, diffRemoved, diffRemovedOutline, scrollbarShadow } from '../../../platform/theme/common/colorRegistry.js'; import { IThemeService, getThemeTypeSelector, registerThemingParticipant } from '../../../platform/theme/common/themeService.js'; var VisualEditorState = /** @class */ (function () { function VisualEditorState() { this._zones = []; this._zonesMap = {}; this._decorations = []; } VisualEditorState.prototype.getForeignViewZones = function (allViewZones) { var _this = this; return allViewZones.filter(function (z) { return !_this._zonesMap[String(z.id)]; }); }; VisualEditorState.prototype.clean = function (editor) { var _this = this; // (1) View zones if (this._zones.length > 0) { editor.changeViewZones(function (viewChangeAccessor) { for (var i = 0, length_1 = _this._zones.length; i < length_1; i++) { viewChangeAccessor.removeZone(_this._zones[i]); } }); } this._zones = []; this._zonesMap = {}; // (2) Model decorations this._decorations = editor.deltaDecorations(this._decorations, []); }; VisualEditorState.prototype.apply = function (editor, overviewRuler, newDecorations, restoreScrollState) { var _this = this; var scrollState = restoreScrollState ? StableEditorScrollState.capture(editor) : null; // view zones editor.changeViewZones(function (viewChangeAccessor) { for (var i = 0, length_2 = _this._zones.length; i < length_2; i++) { viewChangeAccessor.removeZone(_this._zones[i]); } _this._zones = []; _this._zonesMap = {}; for (var i = 0, length_3 = newDecorations.zones.length; i < length_3; i++) { newDecorations.zones[i].suppressMouseDown = true; var zoneId = viewChangeAccessor.addZone(newDecorations.zones[i]); _this._zones.push(zoneId); _this._zonesMap[String(zoneId)] = true; } }); if (scrollState) { scrollState.restore(editor); } // decorations this._decorations = editor.deltaDecorations(this._decorations, newDecorations.decorations); // overview ruler if (overviewRuler) { overviewRuler.setZones(newDecorations.overviewZones); } }; return VisualEditorState; }()); var DIFF_EDITOR_ID = 0; var DiffEditorWidget = /** @class */ (function (_super) { __extends(DiffEditorWidget, _super); function DiffEditorWidget(domElement, options, editorWorkerService, contextKeyService, instantiationService, codeEditorService, themeService, notificationService) { var _this = _super.call(this) || this; _this._onDidDispose = _this._register(new Emitter()); _this.onDidDispose = _this._onDidDispose.event; _this._onDidUpdateDiff = _this._register(new Emitter()); _this.onDidUpdateDiff = _this._onDidUpdateDiff.event; _this._lastOriginalWarning = null; _this._lastModifiedWarning = null; _this._editorWorkerService = editorWorkerService; _this._codeEditorService = codeEditorService; _this._contextKeyService = _this._register(contextKeyService.createScoped(domElement)); _this._contextKeyService.createKey('isInDiffEditor', true); _this._themeService = themeService; _this._notificationService = notificationService; _this.id = (++DIFF_EDITOR_ID); _this._domElement = domElement; options = options || {}; // renderSideBySide _this._renderSideBySide = true; if (typeof options.renderSideBySide !== 'undefined') { _this._renderSideBySide = options.renderSideBySide; } // ignoreTrimWhitespace _this._ignoreTrimWhitespace = true; if (typeof options.ignoreTrimWhitespace !== 'undefined') { _this._ignoreTrimWhitespace = options.ignoreTrimWhitespace; } // renderIndicators _this._renderIndicators = true; if (typeof options.renderIndicators !== 'undefined') { _this._renderIndicators = options.renderIndicators; } _this._originalIsEditable = false; if (typeof options.originalEditable !== 'undefined') { _this._originalIsEditable = Boolean(options.originalEditable); } _this._updateDecorationsRunner = _this._register(new RunOnceScheduler(function () { return _this._updateDecorations(); }, 0)); _this._containerDomElement = document.createElement('div'); _this._containerDomElement.className = DiffEditorWidget._getClassName(_this._themeService.getTheme(), _this._renderSideBySide); _this._containerDomElement.style.position = 'relative'; _this._containerDomElement.style.height = '100%'; _this._domElement.appendChild(_this._containerDomElement); _this._overviewViewportDomElement = createFastDomNode(document.createElement('div')); _this._overviewViewportDomElement.setClassName('diffViewport'); _this._overviewViewportDomElement.setPosition('absolute'); _this._overviewDomElement = document.createElement('div'); _this._overviewDomElement.className = 'diffOverview'; _this._overviewDomElement.style.position = 'absolute'; _this._overviewDomElement.appendChild(_this._overviewViewportDomElement.domNode); _this._register(dom.addStandardDisposableListener(_this._overviewDomElement, 'mousedown', function (e) { _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._reviewHeight = 0; _this._diffComputationResult = null; var leftContextKeyService = _this._contextKeyService.createScoped(); leftContextKeyService.createKey('isInDiffLeftEditor', true); var leftServices = new ServiceCollection(); leftServices.set(IContextKeyService, leftContextKeyService); var leftScopedInstantiationService = instantiationService.createChild(leftServices); var rightContextKeyService = _this._contextKeyService.createScoped(); rightContextKeyService.createKey('isInDiffRightEditor', true); var rightServices = new ServiceCollection(); rightServices.set(IContextKeyService, rightContextKeyService); var rightScopedInstantiationService = instantiationService.createChild(rightServices); _this._createLeftHandSideEditor(options, leftScopedInstantiationService); _this._createRightHandSideEditor(options, rightScopedInstantiationService); _this._reviewPane = new DiffReview(_this); _this._containerDomElement.appendChild(_this._reviewPane.domNode.domNode); _this._containerDomElement.appendChild(_this._reviewPane.shadow.domNode); _this._containerDomElement.appendChild(_this._reviewPane.actionBarContainer.domNode); if (options.automaticLayout) { _this._measureDomElementToken = window.setInterval(function () { return _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)); } _this._register(themeService.onThemeChange(function (t) { if (_this._strategy && _this._strategy.applyColors(t)) { _this._updateDecorationsRunner.schedule(); } _this._containerDomElement.className = DiffEditorWidget._getClassName(_this._themeService.getTheme(), _this._renderSideBySide); })); _this._codeEditorService.addDiffEditor(_this); return _this; } DiffEditorWidget.prototype.hasWidgetFocus = function () { return dom.isAncestor(document.activeElement, this._domElement); }; DiffEditorWidget.prototype.diffReviewNext = function () { this._reviewPane.next(); }; DiffEditorWidget.prototype.diffReviewPrev = function () { this._reviewPane.prev(); }; DiffEditorWidget._getClassName = function (theme, renderSideBySide) { var result = 'monaco-diff-editor monaco-editor-background '; if (renderSideBySide) { result += 'side-by-side '; } result += getThemeTypeSelector(theme.type); return result; }; DiffEditorWidget.prototype._recreateOverviewRulers = function () { if (this._originalOverviewRuler) { this._overviewDomElement.removeChild(this._originalOverviewRuler.getDomNode()); this._originalOverviewRuler.dispose(); } this._originalOverviewRuler = this.originalEditor.createOverviewRuler('original diffOverviewRuler'); this._overviewDomElement.appendChild(this._originalOverviewRuler.getDomNode()); if (this._modifiedOverviewRuler) { this._overviewDomElement.removeChild(this._modifiedOverviewRuler.getDomNode()); this._modifiedOverviewRuler.dispose(); } this._modifiedOverviewRuler = this.modifiedEditor.createOverviewRuler('modified diffOverviewRuler'); this._overviewDomElement.appendChild(this._modifiedOverviewRuler.getDomNode()); this._layoutOverviewRulers(); }; DiffEditorWidget.prototype._createLeftHandSide = function () { 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); }; DiffEditorWidget.prototype._createRightHandSide = function () { 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); }; DiffEditorWidget.prototype._createLeftHandSideEditor = function (options, instantiationService) { var _this = this; this.originalEditor = this._createInnerEditor(instantiationService, this._originalDomNode, this._adjustOptionsForLeftHandSide(options, this._originalIsEditable)); this._register(this.originalEditor.onDidScrollChange(function (e) { if (_this._isHandlingScrollEvent) { return; } if (!e.scrollTopChanged && !e.scrollLeftChanged && !e.scrollHeightChanged) { return; } _this._isHandlingScrollEvent = true; _this.modifiedEditor.setScrollPosition({ scrollLeft: e.scrollLeft, scrollTop: e.scrollTop }); _this._isHandlingScrollEvent = false; _this._layoutOverviewViewport(); })); this._register(this.originalEditor.onDidChangeViewZones(function () { _this._onViewZonesChanged(); })); this._register(this.originalEditor.onDidChangeModelContent(function () { if (_this._isVisible) { _this._beginUpdateDecorationsSoon(); } })); }; DiffEditorWidget.prototype._createRightHandSideEditor = function (options, instantiationService) { var _this = this; this.modifiedEditor = this._createInnerEditor(instantiationService, this._modifiedDomNode, this._adjustOptionsForRightHandSide(options)); this._register(this.modifiedEditor.onDidScrollChange(function (e) { if (_this._isHandlingScrollEvent) { return; } if (!e.scrollTopChanged && !e.scrollLeftChanged && !e.scrollHeightChanged) { return; } _this._isHandlingScrollEvent = true; _this.originalEditor.setScrollPosition({ scrollLeft: e.scrollLeft, scrollTop: e.scrollTop }); _this._isHandlingScrollEvent = false; _this._layoutOverviewViewport(); })); this._register(this.modifiedEditor.onDidChangeViewZones(function () { _this._onViewZonesChanged(); })); this._register(this.modifiedEditor.onDidChangeConfiguration(function (e) { if (e.fontInfo && _this.modifiedEditor.getModel()) { _this._onViewZonesChanged(); } })); this._register(this.modifiedEditor.onDidChangeModelContent(function () { if (_this._isVisible) { _this._beginUpdateDecorationsSoon(); } })); }; DiffEditorWidget.prototype._createInnerEditor = function (instantiationService, container, options) { return instantiationService.createInstance(CodeEditorWidget, container, options, {}); }; DiffEditorWidget.prototype.dispose = function () { this._codeEditorService.removeDiffEditor(this); if (this._beginUpdateDecorationsTimeout !== -1) { window.clearTimeout(this._beginUpdateDecorationsTimeout); this._beginUpdateDecorationsTimeout = -1; } window.clearInterval(this._measureDomElementToken); this._cleanViewZonesAndDecorations(); if (this._originalOverviewRuler) { this._overviewDomElement.removeChild(this._originalOverviewRuler.getDomNode()); this._originalOverviewRuler.dispose(); } if (this._modifiedOverviewRuler) { this._overviewDomElement.removeChild(this._modifiedOverviewRuler.getDomNode()); this._modifiedOverviewRuler.dispose(); } this._overviewDomElement.removeChild(this._overviewViewportDomElement.domNode); this._containerDomElement.removeChild(this._overviewDomElement); this._containerDomElement.removeChild(this._originalDomNode); this.originalEditor.dispose(); this._containerDomElement.removeChild(this._modifiedDomNode); this.modifiedEditor.dispose(); this._strategy.dispose(); this._containerDomElement.removeChild(this._reviewPane.domNode.domNode); this._containerDomElement.removeChild(this._reviewPane.shadow.domNode); this._containerDomElement.removeChild(this._reviewPane.actionBarContainer.domNode); this._reviewPane.dispose(); this._domElement.removeChild(this._containerDomElement); this._onDidDispose.fire(); _super.prototype.dispose.call(this); }; //------------ begin IDiffEditor methods DiffEditorWidget.prototype.getId = function () { return this.getEditorType() + ':' + this.id; }; DiffEditorWidget.prototype.getEditorType = function () { return editorCommon.EditorType.IDiffEditor; }; DiffEditorWidget.prototype.getLineChanges = function () { if (!this._diffComputationResult) { return null; } return this._diffComputationResult.changes; }; DiffEditorWidget.prototype.getOriginalEditor = function () { return this.originalEditor; }; DiffEditorWidget.prototype.getModifiedEditor = function () { return this.modifiedEditor; }; DiffEditorWidget.prototype.updateOptions = function (newOptions) { // Handle side by side var renderSideBySideChanged = false; if (typeof newOptions.renderSideBySide !== 'undefined') { if (this._renderSideBySide !== newOptions.renderSideBySide) { this._renderSideBySide = newOptions.renderSideBySide; renderSideBySideChanged = true; } } var beginUpdateDecorations = false; if (typeof newOptions.ignoreTrimWhitespace !== 'undefined') { if (this._ignoreTrimWhitespace !== newOptions.ignoreTrimWhitespace) { this._ignoreTrimWhitespace = newOptions.ignoreTrimWhitespace; // Begin comparing beginUpdateDecorations = true; } } if (typeof newOptions.renderIndicators !== 'undefined') { if (this._renderIndicators !== newOptions.renderIndicators) { this._renderIndicators = newOptions.renderIndicators; beginUpdateDecorations = true; } } if (beginUpdateDecorations) { this._beginUpdateDecorations(); } if (typeof newOptions.originalEditable !== 'undefined') { this._originalIsEditable = Boolean(newOptions.originalEditable); } this.modifiedEditor.updateOptions(this._adjustOptionsForRightHandSide(newOptions)); this.originalEditor.updateOptions(this._adjustOptionsForLeftHandSide(newOptions, this._originalIsEditable)); // 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)); } // Update class name this._containerDomElement.className = DiffEditorWidget._getClassName(this._themeService.getTheme(), this._renderSideBySide); } }; DiffEditorWidget.prototype.getModel = function () { return { original: this.originalEditor.getModel(), modified: this.modifiedEditor.getModel() }; }; DiffEditorWidget.prototype.setModel = function (model) { // 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._diffComputationResult = null; this._diffComputationToken++; if (model) { this._recreateOverviewRulers(); // Begin comparing this._beginUpdateDecorations(); } else { this._diffComputationResult = null; } this._layoutOverviewViewport(); }; DiffEditorWidget.prototype.getDomNode = function () { return this._domElement; }; DiffEditorWidget.prototype.getVisibleColumnFromPosition = function (position) { return this.modifiedEditor.getVisibleColumnFromPosition(position); }; DiffEditorWidget.prototype.getPosition = function () { return this.modifiedEditor.getPosition(); }; DiffEditorWidget.prototype.setPosition = function (position) { this.modifiedEditor.setPosition(position); }; DiffEditorWidget.prototype.revealLine = function (lineNumber, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealLine(lineNumber, scrollType); }; DiffEditorWidget.prototype.revealLineInCenter = function (lineNumber, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealLineInCenter(lineNumber, scrollType); }; DiffEditorWidget.prototype.revealLineInCenterIfOutsideViewport = function (lineNumber, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealLineInCenterIfOutsideViewport(lineNumber, scrollType); }; DiffEditorWidget.prototype.revealPosition = function (position, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealPosition(position, scrollType); }; DiffEditorWidget.prototype.revealPositionInCenter = function (position, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealPositionInCenter(position, scrollType); }; DiffEditorWidget.prototype.revealPositionInCenterIfOutsideViewport = function (position, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealPositionInCenterIfOutsideViewport(position, scrollType); }; DiffEditorWidget.prototype.getSelection = function () { return this.modifiedEditor.getSelection(); }; DiffEditorWidget.prototype.getSelections = function () { return this.modifiedEditor.getSelections(); }; DiffEditorWidget.prototype.setSelection = function (something) { this.modifiedEditor.setSelection(something); }; DiffEditorWidget.prototype.setSelections = function (ranges) { this.modifiedEditor.setSelections(ranges); }; DiffEditorWidget.prototype.revealLines = function (startLineNumber, endLineNumber, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealLines(startLineNumber, endLineNumber, scrollType); }; DiffEditorWidget.prototype.revealLinesInCenter = function (startLineNumber, endLineNumber, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealLinesInCenter(startLineNumber, endLineNumber, scrollType); }; DiffEditorWidget.prototype.revealLinesInCenterIfOutsideViewport = function (startLineNumber, endLineNumber, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealLinesInCenterIfOutsideViewport(startLineNumber, endLineNumber, scrollType); }; DiffEditorWidget.prototype.revealRange = function (range, scrollType, revealVerticalInCenter, revealHorizontal) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } if (revealVerticalInCenter === void 0) { revealVerticalInCenter = false; } if (revealHorizontal === void 0) { revealHorizontal = true; } this.modifiedEditor.revealRange(range, scrollType, revealVerticalInCenter, revealHorizontal); }; DiffEditorWidget.prototype.revealRangeInCenter = function (range, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealRangeInCenter(range, scrollType); }; DiffEditorWidget.prototype.revealRangeInCenterIfOutsideViewport = function (range, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealRangeInCenterIfOutsideViewport(range, scrollType); }; DiffEditorWidget.prototype.revealRangeAtTop = function (range, scrollType) { if (scrollType === void 0) { scrollType = 0 /* Smooth */; } this.modifiedEditor.revealRangeAtTop(range, scrollType); }; DiffEditorWidget.prototype.getSupportedActions = function () { return this.modifiedEditor.getSupportedActions(); }; DiffEditorWidget.prototype.saveViewState = function () { var originalViewState = this.originalEditor.saveViewState(); var modifiedViewState = this.modifiedEditor.saveViewState(); return { original: originalViewState, modified: modifiedViewState }; }; DiffEditorWidget.prototype.restoreViewState = function (s) { if (s.original && s.original) { var diffEditorState = s; this.originalEditor.restoreViewState(diffEditorState.original); this.modifiedEditor.restoreViewState(diffEditorState.modified); } }; DiffEditorWidget.prototype.layout = function (dimension) { this._measureDomElement(false, dimension); }; DiffEditorWidget.prototype.focus = function () { this.modifiedEditor.focus(); }; DiffEditorWidget.prototype.hasTextFocus = function () { return this.originalEditor.hasTextFocus() || this.modifiedEditor.hasTextFocus(); }; DiffEditorWidget.prototype.trigger = function (source, handlerId, payload) { this.modifiedEditor.trigger(source, handlerId, payload); }; DiffEditorWidget.prototype.changeDecorations = function (callback) { return this.modifiedEditor.changeDecorations(callback); }; //------------ end IDiffEditor methods //------------ begin layouting methods DiffEditorWidget.prototype._measureDomElement = function (forceDoLayoutCall, dimensions) { dimensions = dimensions || { width: this._containerDomElement.clientWidth, height: this._containerDomElement.clientHeight }; if (dimensions.width <= 0) { this._width = 0; this._height = 0; this._reviewHeight = 0; return; } if (!forceDoLayoutCall && dimensions.width === this._width && dimensions.height === this._height) { // Nothing has changed return; } this._width = dimensions.width; this._height = dimensions.height; this._reviewHeight = this._reviewPane.isVisible() ? this._height : 0; this._doLayout(); }; DiffEditorWidget.prototype._layoutOverviewRulers = function () { var freeSpace = DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH - 2 * DiffEditorWidget.ONE_OVERVIEW_WIDTH; var layoutInfo = this.modifiedEditor.getLayoutInfo(); if (layoutInfo) { this._originalOverviewRuler.setLayout({ top: 0, width: DiffEditorWidget.ONE_OVERVIEW_WIDTH, right: freeSpace + DiffEditorWidget.ONE_OVERVIEW_WIDTH, height: (this._height - this._reviewHeight) }); this._modifiedOverviewRuler.setLayout({ top: 0, right: 0, width: DiffEditorWidget.ONE_OVERVIEW_WIDTH, height: (this._height - this._reviewHeight) }); } }; //------------ end layouting methods DiffEditorWidget.prototype._onViewZonesChanged = function () { if (this._currentlyChangingViewZones) { return; } this._updateDecorationsRunner.schedule(); }; DiffEditorWidget.prototype._beginUpdateDecorationsSoon = function () { var _this = this; // Clear previous timeout if necessary if (this._beginUpdateDecorationsTimeout !== -1) { window.clearTimeout(this._beginUpdateDecorationsTimeout); this._beginUpdateDecorationsTimeout = -1; } this._beginUpdateDecorationsTimeout = window.setTimeout(function () { return _this._beginUpdateDecorations(); }, DiffEditorWidget.UPDATE_DIFF_DECORATIONS_DELAY); }; DiffEditorWidget._equals = function (a, b) { if (!a && !b) { return true; } if (!a || !b) { return false; } return (a.toString() === b.toString()); }; DiffEditorWidget.prototype._beginUpdateDecorations = function () { var _this = this; this._beginUpdateDecorationsTimeout = -1; var currentOriginalModel = this.originalEditor.getModel(); var currentModifiedModel = this.modifiedEditor.getModel(); if (!currentOriginalModel || !currentModifiedModel) { 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; if (!this._editorWorkerService.canComputeDiff(currentOriginalModel.uri, currentModifiedModel.uri)) { if (!DiffEditorWidget._equals(currentOriginalModel.uri, this._lastOriginalWarning) || !DiffEditorWidget._equals(currentModifiedModel.uri, this._lastModifiedWarning)) { this._lastOriginalWarning = currentOriginalModel.uri; this._lastModifiedWarning = currentModifiedModel.uri; this._notificationService.warn(nls.localize("diff.tooLarge", "Cannot compare files because one file is too large.")); } return; } this._editorWorkerService.computeDiff(currentOriginalModel.uri, currentModifiedModel.uri, this._ignoreTrimWhitespace).then(function (result) { if (currentToken === _this._diffComputationToken && currentOriginalModel === _this.originalEditor.getModel() && currentModifiedModel === _this.modifiedEditor.getModel()) { _this._diffComputationResult = result; _this._updateDecorationsRunner.schedule(); _this._onDidUpdateDiff.fire(); } }, function (error) { if (currentToken === _this._diffComputationToken && currentOriginalModel === _this.originalEditor.getModel() && currentModifiedModel === _this.modifiedEditor.getModel()) { _this._diffComputationResult = null; _this._updateDecorationsRunner.schedule(); } }); }; DiffEditorWidget.prototype._cleanViewZonesAndDecorations = function () { this._originalEditorState.clean(this.originalEditor); this._modifiedEditorState.clean(this.modifiedEditor); }; DiffEditorWidget.prototype._updateDecorations = function () { if (!this.originalEditor.getModel() || !this.modifiedEditor.getModel()) { return; } var lineChanges = (this._diffComputationResult ? this._diffComputationResult.changes : []); var foreignOriginal = this._originalEditorState.getForeignViewZones(this.originalEditor.getWhitespaces()); var foreignModified = this._modifiedEditorState.getForeignViewZones(this.modifiedEditor.getWhitespaces()); var diffDecorations = this._strategy.getEditorsDiffDecorations(lineChanges, this._ignoreTrimWhitespace, this._renderIndicators, foreignOriginal, foreignModified, this.originalEditor, this.modifiedEditor); try { this._currentlyChangingViewZones = true; this._originalEditorState.apply(this.originalEditor, this._originalOverviewRuler, diffDecorations.original, false); this._modifiedEditorState.apply(this.modifiedEditor, this._modifiedOverviewRuler, diffDecorations.modified, true); } finally { this._currentlyChangingViewZones = false; } }; DiffEditorWidget.prototype._adjustOptionsForSubEditor = function (options) { var clonedOptions = objects.deepClone(options || {}); clonedOptions.inDiffEditor = true; clonedOptions.wordWrap = 'off'; clonedOptions.wordWrapMinified = false; clonedOptions.automaticLayout = false; clonedOptions.scrollbar = clonedOptions.scrollbar || {}; clonedOptions.scrollbar.vertical = 'visible'; clonedOptions.folding = false; clonedOptions.codeLens = false; clonedOptions.fixedOverflowWidgets = true; // clonedOptions.lineDecorationsWidth = '2ch'; if (!clonedOptions.minimap) { clonedOptions.minimap = {}; } clonedOptions.minimap.enabled = false; return clonedOptions; }; DiffEditorWidget.prototype._adjustOptionsForLeftHandSide = function (options, isEditable) { var result = this._adjustOptionsForSubEditor(options); result.readOnly = !isEditable; result.overviewRulerLanes = 1; result.extraEditorClassName = 'original-in-monaco-diff-editor'; return result; }; DiffEditorWidget.prototype._adjustOptionsForRightHandSide = function (options) { var result = this._adjustOptionsForSubEditor(options); result.revealHorizontalRightPadding = editorOptions.EDITOR_DEFAULTS.viewInfo.revealHorizontalRightPadding + DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH; result.scrollbar.verticalHasArrows = false; result.extraEditorClassName = 'modified-in-monaco-diff-editor'; return result; }; DiffEditorWidget.prototype.doLayout = function () { this._measureDomElement(true); }; DiffEditorWidget.prototype._doLayout = function () { 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.height = (this._height - this._reviewHeight) + 'px'; this._overviewDomElement.style.width = DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH + 'px'; this._overviewDomElement.style.left = (this._width - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH) + 'px'; this._overviewViewportDomElement.setWidth(DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH); this._overviewViewportDomElement.setHeight(30); this.originalEditor.layout({ width: splitPoint, height: (this._height - this._reviewHeight) }); this.modifiedEditor.layout({ width: this._width - splitPoint - DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH, height: (this._height - this._reviewHeight) }); if (this._originalOverviewRuler || this._modifiedOverviewRuler) { this._layoutOverviewRulers(); } this._reviewPane.layout(this._height - this._reviewHeight, this._width, this._reviewHeight); this._layoutOverviewViewport(); }; DiffEditorWidget.prototype._layoutOverviewViewport = function () { var layout = this._computeOverviewViewport(); if (!layout) { this._overviewViewportDomElement.setTop(0); this._overviewViewportDomElement.setHeight(0); } else { this._overviewViewportDomElement.setTop(layout.top); this._overviewViewportDomElement.setHeight(layout.height); } }; DiffEditorWidget.prototype._computeOverviewViewport = function () { var layoutInfo = this.modifiedEditor.getLayoutInfo(); if (!layoutInfo) { return null; } var scrollTop = this.modifiedEditor.getScrollTop(); var scrollHeight = this.modifiedEditor.getScrollHeight(); var computedAvailableSize = Math.max(0, layoutInfo.contentHeight); var computedRepresentableSize = Math.max(0, computedAvailableSize - 2 * 0); var computedRatio = scrollHeight > 0 ? (computedRepresentableSize / scrollHeight) : 0; var computedSliderSize = Math.max(0, Math.floor(layoutInfo.contentHeight * computedRatio)); var computedSliderPosition = Math.floor(scrollTop * computedRatio); return { height: computedSliderSize, top: computedSliderPosition }; }; DiffEditorWidget.prototype._createDataSource = function () { var _this = this; return { getWidth: function () { return _this._width; }, getHeight: function () { return (_this._height - _this._reviewHeight); }, getContainerDomNode: function () { return _this._containerDomElement; }, relayoutEditors: function () { _this._doLayout(); }, getOriginalEditor: function () { return _this.originalEditor; }, getModifiedEditor: function () { return _this.modifiedEditor; } }; }; DiffEditorWidget.prototype._setStrategy = function (newStrategy) { if (this._strategy) { this._strategy.dispose(); } this._strategy = newStrategy; newStrategy.applyColors(this._themeService.getTheme()); if (this._diffComputationResult) { this._updateDecorations(); } // Just do a layout, the strategy might need it this._measureDomElement(true); }; DiffEditorWidget.prototype._getLineChangeAtOrBeforeLineNumber = function (lineNumber, startLineNumberExtractor) { var lineChanges = (this._diffComputationResult ? this._diffComputationResult.changes : []); if (lineChanges.length === 0 || lineNumber < startLineNumberExtractor(lineChanges[0])) { // There are no changes or `lineNumber` is before the first change return null; } var min = 0, max = lineChanges.length - 1; while (min < max) { var mid = Math.floor((min + max) / 2); var midStart = startLineNumberExtractor(lineChanges[mid]); var midEnd = (mid + 1 <= max ? startLineNumberExtractor(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 lineChanges[min]; }; DiffEditorWidget.prototype._getEquivalentLineForOriginalLineNumber = function (lineNumber) { var lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, function (lineChange) { return 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; }; DiffEditorWidget.prototype._getEquivalentLineForModifiedLineNumber = function (lineNumber) { var lineChange = this._getLineChangeAtOrBeforeLineNumber(lineNumber, function (lineChange) { return 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; }; DiffEditorWidget.prototype.getDiffLineInformationForOriginal = function (lineNumber) { if (!this._diffComputationResult) { // Cannot answer that which I don't know return null; } return { equivalentLineNumber: this._getEquivalentLineForOriginalLineNumber(lineNumber) }; }; DiffEditorWidget.prototype.getDiffLineInformationForModified = function (lineNumber) { if (!this._diffComputationResult) { // Cannot answer that which I don't know return null; } return { equivalentLineNumber: this._getEquivalentLineForModifiedLineNumber(lineNumber) }; }; DiffEditorWidget.ONE_OVERVIEW_WIDTH = 15; DiffEditorWidget.ENTIRE_DIFF_OVERVIEW_WIDTH = 30; DiffEditorWidget.UPDATE_DIFF_DECORATIONS_DELAY = 200; // ms DiffEditorWidget = __decorate([ __param(2, IEditorWorkerService), __param(3, IContextKeyService), __param(4, IInstantiationService), __param(5, ICodeEditorService), __param(6, IThemeService), __param(7, INotificationService) ], DiffEditorWidget); return DiffEditorWidget; }(Disposable)); export { DiffEditorWidget }; var DiffEditorWidgetStyle = /** @class */ (function (_super) { __extends(DiffEditorWidgetStyle, _super); function DiffEditorWidgetStyle(dataSource) { var _this = _super.call(this) || this; _this._dataSource = dataSource; return _this; } DiffEditorWidgetStyle.prototype.applyColors = function (theme) { var newInsertColor = (theme.getColor(diffInserted) || defaultInsertColor).transparent(2); var newRemoveColor = (theme.getColor(diffRemoved) || defaultRemoveColor).transparent(2); var hasChanges = !newInsertColor.equals(this._insertColor) || !newRemoveColor.equals(this._removeColor); this._insertColor = newInsertColor; this._removeColor = newRemoveColor; return hasChanges; }; DiffEditorWidgetStyle.prototype.getEditorsDiffDecorations = function (lineChanges, ignoreTrimWhitespace, renderIndicators, originalWhitespaces, modifiedWhitespaces, originalEditor, modifiedEditor) { // Get view zones modifiedWhitespaces = modifiedWhitespaces.sort(function (a, b) { return a.afterLineNumber - b.afterLineNumber; }); originalWhitespaces = originalWhitespaces.sort(function (a, b) { return a.afterLineNumber - b.afterLineNumber; }); var zones = this._getViewZones(lineChanges, originalWhitespaces, modifiedWhitespaces, originalEditor, modifiedEditor, renderIndicators); // Get decorations & overview ruler zones var originalDecorations = this._getOriginalEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor); var modifiedDecorations = this._getModifiedEditorDecorations(lineChanges, ignoreTrimWhitespace, renderIndicators, originalEditor, modifiedEditor); return { original: { decorations: originalDecorations.decorations, overviewZones: originalDecorations.overviewZones, zones: zones.original }, modified: { decorations: modifiedDecorations.decorations, overviewZones: modifiedDecorations.overviewZones, zones: zones.modified } }; }; return DiffEditorWidgetStyle; }(Disposable)); var ForeignViewZonesIterator = /** @class */ (function () { function ForeignViewZonesIterator(source) { this._source = source; this._index = -1; this.advance(); } ForeignViewZonesIterator.prototype.advance = function () { this._index++; if (this._index < this._source.length) { this.current = this._source[this._index]; } else { this.current = null; } }; return ForeignViewZonesIterator; }