|
| 1 | +import { StandaloneServices } from 'vs/editor/standalone/browser/standaloneServices' |
| 2 | +import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfiguration' |
| 3 | +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' |
| 4 | +import { IStandaloneEditorConstructionOptions, StandaloneDiffEditor, StandaloneEditor } from 'vs/editor/standalone/browser/standaloneCodeEditor' |
| 5 | +import { IEditorOptions } from 'vs/editor/common/config/editorOptions' |
| 6 | +import { IEditorConfiguration } from 'vs/workbench/browser/parts/editor/textEditor' |
| 7 | +import { isObject } from 'vs/base/common/types' |
| 8 | +import { deepClone, distinct } from 'vs/base/common/objects' |
| 9 | +import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget' |
| 10 | +import type { create as createEditor, createDiffEditor } from 'vs/editor/standalone/browser/standaloneEditor' |
| 11 | +import { createInjectedClass } from './tools/injection' |
| 12 | + |
| 13 | +function computeConfiguration (configuration: IEditorConfiguration, isDiffEditor: boolean, overrides?: Readonly<IEditorOptions>): IEditorOptions { |
| 14 | + const editorConfiguration: IEditorOptions = isObject(configuration.editor) ? deepClone(configuration.editor) : Object.create(null) |
| 15 | + if (isDiffEditor && isObject(configuration.diffEditor)) { |
| 16 | + Object.assign(editorConfiguration, configuration.diffEditor) |
| 17 | + } |
| 18 | + if (isObject(configuration.diffEditor)) { |
| 19 | + Object.assign(editorConfiguration, overrides) |
| 20 | + } |
| 21 | + return editorConfiguration |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * A StandaloneEditor which is plugged on the `textResourceConfigurationService` for its options |
| 26 | + * So instead of just taking the options given by the user via the `updateOptions` method, |
| 27 | + * it fallbacks on the current configurationService configuration |
| 28 | + */ |
| 29 | +class ConfiguredStandaloneEditor extends createInjectedClass(StandaloneEditor) { |
| 30 | + // use createInjectedClass because StandaloneEditor has a lot of injected services and it would be a pain to inject them all here to be able to forward them |
| 31 | + // Also, the injected services may vary so relying on the annotations is more robust (and useful for @codingame/monaco-editor which removes a service from the list) |
| 32 | + |
| 33 | + private optionsOverrides?: Readonly<IEditorOptions> |
| 34 | + private lastAppliedEditorOptions?: IEditorOptions |
| 35 | + |
| 36 | + constructor ( |
| 37 | + domElement: HTMLElement, |
| 38 | + private isDiffEditor: boolean, |
| 39 | + _options: Readonly<IStandaloneEditorConstructionOptions> | undefined, |
| 40 | + @IInstantiationService instantiationService: IInstantiationService, |
| 41 | + @ITextResourceConfigurationService private textResourceConfigurationService: ITextResourceConfigurationService |
| 42 | + ) { |
| 43 | + const computedOptions = computeConfiguration(textResourceConfigurationService.getValue<IEditorConfiguration>(_options?.model?.uri), isDiffEditor, _options) |
| 44 | + super(instantiationService, domElement, computedOptions) |
| 45 | + this.lastAppliedEditorOptions = computedOptions |
| 46 | + |
| 47 | + this.optionsOverrides = _options |
| 48 | + this._register(textResourceConfigurationService.onDidChangeConfiguration(() => this.updateEditorConfiguration())) |
| 49 | + this._register(this.onDidChangeModelLanguage(() => this.updateEditorConfiguration())) |
| 50 | + this._register(this.onDidChangeModel(() => this.updateEditorConfiguration())) |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * This method is widely inspired from vs/workbench/browser/parts/editor/textEditor |
| 55 | + */ |
| 56 | + private updateEditorConfiguration (): void { |
| 57 | + const resource = this.getModel()?.uri |
| 58 | + if (resource == null) { |
| 59 | + return |
| 60 | + } |
| 61 | + const configuration = this.textResourceConfigurationService.getValue<IEditorConfiguration | undefined>(resource) |
| 62 | + if (configuration == null) { |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + const editorConfiguration = computeConfiguration(configuration, this.isDiffEditor, this.optionsOverrides) |
| 67 | + |
| 68 | + // Try to figure out the actual editor options that changed from the last time we updated the editor. |
| 69 | + // We do this so that we are not overwriting some dynamic editor settings (e.g. word wrap) that might |
| 70 | + // have been applied to the editor directly. |
| 71 | + let editorSettingsToApply = editorConfiguration |
| 72 | + if (this.lastAppliedEditorOptions != null) { |
| 73 | + editorSettingsToApply = distinct(this.lastAppliedEditorOptions, editorSettingsToApply) |
| 74 | + } |
| 75 | + |
| 76 | + if (Object.keys(editorSettingsToApply).length > 0) { |
| 77 | + this.lastAppliedEditorOptions = editorConfiguration |
| 78 | + |
| 79 | + super.updateOptions(editorSettingsToApply) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + override updateOptions (newOptions: Readonly<IEditorOptions>): void { |
| 84 | + this.optionsOverrides = newOptions |
| 85 | + this.updateEditorConfiguration() |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class ConfiguredStandaloneDiffEditor extends StandaloneDiffEditor { |
| 90 | + protected override _createInnerEditor (instantiationService: IInstantiationService, container: HTMLElement, options: Readonly<IEditorOptions>): CodeEditorWidget { |
| 91 | + return instantiationService.createInstance(ConfiguredStandaloneEditor, container, true, options) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +export const createConfiguredEditor: typeof createEditor = (domElement, options, override) => { |
| 96 | + const instantiationService = StandaloneServices.initialize(override ?? {}) |
| 97 | + return instantiationService.createInstance(ConfiguredStandaloneEditor, domElement, false, options) |
| 98 | +} |
| 99 | + |
| 100 | +export const createConfiguredDiffEditor: typeof createDiffEditor = (domElement, options, override) => { |
| 101 | + const instantiationService = StandaloneServices.initialize(override ?? {}) |
| 102 | + return instantiationService.createInstance(ConfiguredStandaloneDiffEditor, domElement, options) |
| 103 | +} |
0 commit comments