|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import { Emitter } from 'vs/base/common/event'; |
| 6 | +import { Disposable } from 'vs/base/common/lifecycle'; |
| 7 | +import { autorunHandleChanges } from 'vs/base/common/observableImpl/autorun'; |
| 8 | +import { IEditorConstructionOptions } from 'vs/editor/browser/config/editorConfiguration'; |
| 9 | +import { IDiffEditorConstructionOptions } from 'vs/editor/browser/editorBrowser'; |
| 10 | +import { CodeEditorWidget, ICodeEditorWidgetOptions } from 'vs/editor/browser/widget/codeEditorWidget'; |
| 11 | +import { IDiffCodeEditorWidgetOptions } from 'vs/editor/browser/widget/diffEditorWidget'; |
| 12 | +import { OverviewRulerPart } from 'vs/editor/browser/widget/diffEditorWidget2/overviewRulerPart'; |
| 13 | +import { EditorOptions, IEditorOptions } from 'vs/editor/common/config/editorOptions'; |
| 14 | +import { IContentSizeChangedEvent } from 'vs/editor/common/editorCommon'; |
| 15 | +import { localize } from 'vs/nls'; |
| 16 | +import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; |
| 17 | +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; |
| 18 | +import { DiffEditorOptions } from './diffEditorOptions'; |
| 19 | + |
| 20 | +export class DiffEditorEditors extends Disposable { |
| 21 | + public readonly modified: CodeEditorWidget; |
| 22 | + public readonly original: CodeEditorWidget; |
| 23 | + |
| 24 | + private readonly _onDidContentSizeChange = this._register(new Emitter<IContentSizeChangedEvent>()); |
| 25 | + public get onDidContentSizeChange() { return this._onDidContentSizeChange.event; } |
| 26 | + |
| 27 | + constructor( |
| 28 | + private readonly originalEditorElement: HTMLElement, |
| 29 | + private readonly modifiedEditorElement: HTMLElement, |
| 30 | + private readonly _options: DiffEditorOptions, |
| 31 | + codeEditorWidgetOptions: IDiffCodeEditorWidgetOptions, |
| 32 | + private readonly _createInnerEditor: (instantiationService: IInstantiationService, container: HTMLElement, options: Readonly<IEditorOptions>, editorWidgetOptions: ICodeEditorWidgetOptions) => CodeEditorWidget, |
| 33 | + @IContextKeyService private readonly _contextKeyService: IContextKeyService, |
| 34 | + @IInstantiationService private readonly _instantiationService: IInstantiationService |
| 35 | + ) { |
| 36 | + super(); |
| 37 | + |
| 38 | + this.original = this._createLeftHandSideEditor(_options.editorOptions.get(), codeEditorWidgetOptions.originalEditor || {}); |
| 39 | + this.modified = this._createRightHandSideEditor(_options.editorOptions.get(), codeEditorWidgetOptions.modifiedEditor || {}); |
| 40 | + |
| 41 | + this._register(autorunHandleChanges('update editor options', { |
| 42 | + createEmptyChangeSummary: () => ({}), |
| 43 | + handleChange: (ctx, changeSummary) => { |
| 44 | + if (ctx.didChange(_options.editorOptions)) { |
| 45 | + Object.assign(changeSummary, ctx.change); |
| 46 | + } |
| 47 | + return true; |
| 48 | + } |
| 49 | + }, (reader, changeSummary) => { |
| 50 | + _options.editorOptions.read(reader); |
| 51 | + |
| 52 | + this.modified.updateOptions(this._adjustOptionsForRightHandSide(changeSummary)); |
| 53 | + this.original.updateOptions(this._adjustOptionsForLeftHandSide(changeSummary)); |
| 54 | + })); |
| 55 | + } |
| 56 | + |
| 57 | + private _createLeftHandSideEditor(options: Readonly<IDiffEditorConstructionOptions>, codeEditorWidgetOptions: ICodeEditorWidgetOptions): CodeEditorWidget { |
| 58 | + const editor = this._constructInnerEditor(this._instantiationService, this.originalEditorElement, this._adjustOptionsForLeftHandSide(options), codeEditorWidgetOptions); |
| 59 | + const isInDiffLeftEditorKey = this._contextKeyService.createKey<boolean>('isInDiffLeftEditor', editor.hasWidgetFocus()); |
| 60 | + this._register(editor.onDidFocusEditorWidget(() => isInDiffLeftEditorKey.set(true))); |
| 61 | + this._register(editor.onDidBlurEditorWidget(() => isInDiffLeftEditorKey.set(false))); |
| 62 | + return editor; |
| 63 | + } |
| 64 | + |
| 65 | + private _createRightHandSideEditor(options: Readonly<IDiffEditorConstructionOptions>, codeEditorWidgetOptions: ICodeEditorWidgetOptions): CodeEditorWidget { |
| 66 | + const editor = this._constructInnerEditor(this._instantiationService, this.modifiedEditorElement, this._adjustOptionsForRightHandSide(options), codeEditorWidgetOptions); |
| 67 | + const isInDiffRightEditorKey = this._contextKeyService.createKey<boolean>('isInDiffRightEditor', editor.hasWidgetFocus()); |
| 68 | + this._register(editor.onDidFocusEditorWidget(() => isInDiffRightEditorKey.set(true))); |
| 69 | + this._register(editor.onDidBlurEditorWidget(() => isInDiffRightEditorKey.set(false))); |
| 70 | + return editor; |
| 71 | + } |
| 72 | + |
| 73 | + private _constructInnerEditor(instantiationService: IInstantiationService, container: HTMLElement, options: Readonly<IEditorConstructionOptions>, editorWidgetOptions: ICodeEditorWidgetOptions): CodeEditorWidget { |
| 74 | + const editor = this._createInnerEditor(instantiationService, container, options, editorWidgetOptions); |
| 75 | + |
| 76 | + this._register(editor.onDidContentSizeChange(e => { |
| 77 | + const width = this.original.getContentWidth() + this.modified.getContentWidth() + OverviewRulerPart.ENTIRE_DIFF_OVERVIEW_WIDTH; |
| 78 | + const height = Math.max(this.modified.getContentHeight(), this.original.getContentHeight()); |
| 79 | + |
| 80 | + this._onDidContentSizeChange.fire({ |
| 81 | + contentHeight: height, |
| 82 | + contentWidth: width, |
| 83 | + contentHeightChanged: e.contentHeightChanged, |
| 84 | + contentWidthChanged: e.contentWidthChanged |
| 85 | + }); |
| 86 | + })); |
| 87 | + return editor; |
| 88 | + } |
| 89 | + |
| 90 | + private _adjustOptionsForLeftHandSide(changedOptions: Readonly<IDiffEditorConstructionOptions>): IEditorConstructionOptions { |
| 91 | + const result = this._adjustOptionsForSubEditor(changedOptions); |
| 92 | + if (!this._options.renderSideBySide.get()) { |
| 93 | + // never wrap hidden editor |
| 94 | + result.wordWrapOverride1 = 'off'; |
| 95 | + result.wordWrapOverride2 = 'off'; |
| 96 | + result.stickyScroll = { enabled: false }; |
| 97 | + } else { |
| 98 | + result.wordWrapOverride1 = this._options.diffWordWrap.get(); |
| 99 | + } |
| 100 | + if (changedOptions.originalAriaLabel) { |
| 101 | + result.ariaLabel = changedOptions.originalAriaLabel; |
| 102 | + } |
| 103 | + result.ariaLabel = this._updateAriaLabel(result.ariaLabel); |
| 104 | + result.readOnly = !this._options.originalEditable.get(); |
| 105 | + result.dropIntoEditor = { enabled: !result.readOnly }; |
| 106 | + result.extraEditorClassName = 'original-in-monaco-diff-editor'; |
| 107 | + return result; |
| 108 | + } |
| 109 | + |
| 110 | + private _adjustOptionsForRightHandSide(changedOptions: Readonly<IDiffEditorConstructionOptions>): IEditorConstructionOptions { |
| 111 | + const result = this._adjustOptionsForSubEditor(changedOptions); |
| 112 | + if (changedOptions.modifiedAriaLabel) { |
| 113 | + result.ariaLabel = changedOptions.modifiedAriaLabel; |
| 114 | + } |
| 115 | + result.ariaLabel = this._updateAriaLabel(result.ariaLabel); |
| 116 | + result.wordWrapOverride1 = this._options.diffWordWrap.get(); |
| 117 | + result.revealHorizontalRightPadding = EditorOptions.revealHorizontalRightPadding.defaultValue + OverviewRulerPart.ENTIRE_DIFF_OVERVIEW_WIDTH; |
| 118 | + result.scrollbar!.verticalHasArrows = false; |
| 119 | + result.extraEditorClassName = 'modified-in-monaco-diff-editor'; |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + private _adjustOptionsForSubEditor(options: Readonly<IDiffEditorConstructionOptions>): IEditorConstructionOptions { |
| 124 | + const clonedOptions = { |
| 125 | + ...options, |
| 126 | + dimension: { |
| 127 | + height: 0, |
| 128 | + width: 0 |
| 129 | + }, |
| 130 | + }; |
| 131 | + clonedOptions.inDiffEditor = true; |
| 132 | + clonedOptions.automaticLayout = false; |
| 133 | + // Clone scrollbar options before changing them |
| 134 | + clonedOptions.scrollbar = { ...(clonedOptions.scrollbar || {}) }; |
| 135 | + clonedOptions.scrollbar.vertical = 'visible'; |
| 136 | + clonedOptions.folding = false; |
| 137 | + clonedOptions.codeLens = this._options.diffCodeLens.get(); |
| 138 | + clonedOptions.fixedOverflowWidgets = true; |
| 139 | + // clonedOptions.lineDecorationsWidth = '2ch'; |
| 140 | + // Clone minimap options before changing them |
| 141 | + clonedOptions.minimap = { ...(clonedOptions.minimap || {}) }; |
| 142 | + clonedOptions.minimap.enabled = false; |
| 143 | + |
| 144 | + if (this._options.collapseUnchangedRegions.get()) { |
| 145 | + clonedOptions.stickyScroll = { enabled: false }; |
| 146 | + } else { |
| 147 | + clonedOptions.stickyScroll = this._options.editorOptions.get().stickyScroll; |
| 148 | + } |
| 149 | + return clonedOptions; |
| 150 | + } |
| 151 | + |
| 152 | + private _updateAriaLabel(ariaLabel: string | undefined): string | undefined { |
| 153 | + const ariaNavigationTip = localize('diff-aria-navigation-tip', ' use Shift + F7 to navigate changes'); |
| 154 | + if (this._options.accessibilityVerbose.get()) { |
| 155 | + return ariaLabel + ariaNavigationTip; |
| 156 | + } else if (ariaLabel) { |
| 157 | + return ariaLabel.replaceAll(ariaNavigationTip, ''); |
| 158 | + } |
| 159 | + return undefined; |
| 160 | + } |
| 161 | +} |
0 commit comments