From 73eaecdabef1109e4ab7bfb97bbc508ebb456264 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Tue, 28 Dec 2021 16:27:11 +0100 Subject: [PATCH] Simplify creation of `ITextModelService` --- .../standalone/browser/simpleServices.ts | 37 +------ .../standalone/browser/standaloneEditor.ts | 98 +++++++------------ .../standalone/browser/standaloneServices.ts | 13 +-- 3 files changed, 44 insertions(+), 104 deletions(-) diff --git a/src/vs/editor/standalone/browser/simpleServices.ts b/src/vs/editor/standalone/browser/simpleServices.ts index 02e86c90abc..6e1beaf03f8 100644 --- a/src/vs/editor/standalone/browser/simpleServices.ts +++ b/src/vs/editor/standalone/browser/simpleServices.ts @@ -12,13 +12,11 @@ import { IDisposable, IReference, ImmortalReference, toDisposable, DisposableSto import { OS, isLinux, isMacintosh } from 'vs/base/common/platform'; import Severity from 'vs/base/common/severity'; import { URI } from 'vs/base/common/uri'; -import { ICodeEditor, IDiffEditor, isCodeEditor } from 'vs/editor/browser/editorBrowser'; import { IBulkEditOptions, IBulkEditResult, IBulkEditService, ResourceEdit, ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService'; import { isDiffEditorConfigurationKey, isEditorConfigurationKey } from 'vs/editor/common/config/commonEditorConfig'; import { EditOperation } from 'vs/editor/common/core/editOperation'; import { IPosition, Position as Pos } from 'vs/editor/common/core/position'; import { Range } from 'vs/editor/common/core/range'; -import { IEditor } from 'vs/editor/common/editorCommon'; import { IIdentifiedSingleEditOperation, ITextModel, ITextSnapshot } from 'vs/editor/common/model'; import { IModelService } from 'vs/editor/common/services/modelService'; import { IResolvedTextEditorModel, ITextModelContentProvider, ITextModelService } from 'vs/editor/common/services/resolverService'; @@ -103,37 +101,15 @@ export interface IOpenEditorDelegate { (url: string): boolean; } -function withTypedEditor(widget: IEditor, codeEditorCallback: (editor: ICodeEditor) => T, diffEditorCallback: (editor: IDiffEditor) => T): T { - if (isCodeEditor(widget)) { - // Single Editor - return codeEditorCallback(widget); - } else { - // Diff Editor - return diffEditorCallback(widget); - } -} - -export class SimpleEditorModelResolverService implements ITextModelService { +export class SimpleTextModelService implements ITextModelService { public _serviceBrand: undefined; - private editor?: IEditor; - constructor( @IModelService private readonly modelService: IModelService ) { } - public setEditor(editor: IEditor): void { - this.editor = editor; - } - public createModelReference(resource: URI): Promise> { - let model: ITextModel | null = null; - if (this.editor) { - model = withTypedEditor(this.editor, - (editor) => this.findModel(editor, resource), - (diffEditor) => this.findModel(diffEditor.getOriginalEditor(), resource) || this.findModel(diffEditor.getModifiedEditor(), resource) - ); - } + const model = this.modelService.getModel(resource); if (!model) { return Promise.reject(new Error(`Model not found`)); @@ -151,15 +127,6 @@ export class SimpleEditorModelResolverService implements ITextModelService { public canHandleResource(resource: URI): boolean { return false; } - - private findModel(editor: ICodeEditor, resource: URI): ITextModel | null { - let model = this.modelService.getModel(resource); - if (model && model.uri.toString() !== resource.toString()) { - return null; - } - - return model; - } } export class SimpleEditorProgressService implements IEditorProgressService { diff --git a/src/vs/editor/standalone/browser/standaloneEditor.ts b/src/vs/editor/standalone/browser/standaloneEditor.ts index 2b5c0089449..2f66c56778d 100644 --- a/src/vs/editor/standalone/browser/standaloneEditor.ts +++ b/src/vs/editor/standalone/browser/standaloneEditor.ts @@ -12,17 +12,15 @@ import { DiffNavigator, IDiffNavigator } from 'vs/editor/browser/widget/diffNavi import { EditorOptions, ConfigurationChangedEvent, ApplyUpdateResult } from 'vs/editor/common/config/editorOptions'; import { BareFontInfo, FontInfo } from 'vs/editor/common/config/fontInfo'; import { Token } from 'vs/editor/common/core/token'; -import { IEditor, EditorType } from 'vs/editor/common/editorCommon'; +import { EditorType } from 'vs/editor/common/editorCommon'; import { FindMatch, ITextModel, TextModelResolvedOptions } from 'vs/editor/common/model'; import * as modes from 'vs/editor/common/modes'; import { NULL_STATE, nullTokenize } from 'vs/editor/common/modes/nullMode'; import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService'; import { ILanguageService } from 'vs/editor/common/services/languageService'; -import { ITextModelService } from 'vs/editor/common/services/resolverService'; import { IWebWorkerOptions, MonacoWebWorker, createWebWorker as actualCreateWebWorker } from 'vs/editor/common/services/webWorker'; import * as standaloneEnums from 'vs/editor/common/standalone/standaloneEnums'; import { Colorizer, IColorizerElementOptions, IColorizerOptions } from 'vs/editor/standalone/browser/colorizer'; -import { SimpleEditorModelResolverService } from 'vs/editor/standalone/browser/simpleServices'; import { IStandaloneEditorConstructionOptions, IStandaloneCodeEditor, IStandaloneDiffEditor, StandaloneDiffEditor, StandaloneEditor, createTextModel, IStandaloneDiffEditorConstructionOptions } from 'vs/editor/standalone/browser/standaloneCodeEditor'; import { DynamicStandaloneServices, IEditorOverrideServices, StaticServices } from 'vs/editor/standalone/browser/standaloneServices'; import { IStandaloneThemeData, IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneThemeService'; @@ -43,50 +41,31 @@ import { splitLines } from 'vs/base/common/strings'; import { IModelService } from 'vs/editor/common/services/modelService'; import { ILanguageConfigurationService } from 'vs/editor/common/modes/languageConfigurationRegistry'; -function withAllStandaloneServices(domElement: HTMLElement, override: IEditorOverrideServices, callback: (services: DynamicStandaloneServices) => T): T { - let services = new DynamicStandaloneServices(domElement, override); - - let simpleEditorModelResolverService: SimpleEditorModelResolverService | null = null; - if (!services.has(ITextModelService)) { - simpleEditorModelResolverService = new SimpleEditorModelResolverService(StaticServices.modelService.get()); - services.set(ITextModelService, simpleEditorModelResolverService); - } - - let result = callback(services); - - if (simpleEditorModelResolverService) { - simpleEditorModelResolverService.setEditor(result); - } - - return result; -} - /** * Create a new editor under `domElement`. * `domElement` should be empty (not contain other dom nodes). * The editor will read the size of `domElement`. */ export function create(domElement: HTMLElement, options?: IStandaloneEditorConstructionOptions, override?: IEditorOverrideServices): IStandaloneCodeEditor { - return withAllStandaloneServices(domElement, override || {}, (services) => { - return new StandaloneEditor( - domElement, - options, - services, - services.get(IInstantiationService), - services.get(ICodeEditorService), - services.get(ICommandService), - services.get(IContextKeyService), - services.get(IKeybindingService), - services.get(IContextViewService), - services.get(IStandaloneThemeService), - services.get(INotificationService), - services.get(IConfigurationService), - services.get(IAccessibilityService), - services.get(IModelService), - services.get(ILanguageService), - services.get(ILanguageConfigurationService), - ); - }); + const services = new DynamicStandaloneServices(domElement, override || {}); + return new StandaloneEditor( + domElement, + options, + services, + services.get(IInstantiationService), + services.get(ICodeEditorService), + services.get(ICommandService), + services.get(IContextKeyService), + services.get(IKeybindingService), + services.get(IContextViewService), + services.get(IStandaloneThemeService), + services.get(INotificationService), + services.get(IConfigurationService), + services.get(IAccessibilityService), + services.get(IModelService), + services.get(ILanguageService), + services.get(ILanguageConfigurationService), + ); } /** @@ -106,25 +85,24 @@ export function onDidCreateEditor(listener: (codeEditor: ICodeEditor) => void): * The editor will read the size of `domElement`. */ export function createDiffEditor(domElement: HTMLElement, options?: IStandaloneDiffEditorConstructionOptions, override?: IEditorOverrideServices): IStandaloneDiffEditor { - return withAllStandaloneServices(domElement, override || {}, (services) => { - return new StandaloneDiffEditor( - domElement, - options, - services, - services.get(IInstantiationService), - services.get(IContextKeyService), - services.get(IKeybindingService), - services.get(IContextViewService), - services.get(IEditorWorkerService), - services.get(ICodeEditorService), - services.get(IStandaloneThemeService), - services.get(INotificationService), - services.get(IConfigurationService), - services.get(IContextMenuService), - services.get(IEditorProgressService), - services.get(IClipboardService) - ); - }); + const services = new DynamicStandaloneServices(domElement, override || {}); + return new StandaloneDiffEditor( + domElement, + options, + services, + services.get(IInstantiationService), + services.get(IContextKeyService), + services.get(IKeybindingService), + services.get(IContextViewService), + services.get(IEditorWorkerService), + services.get(ICodeEditorService), + services.get(IStandaloneThemeService), + services.get(INotificationService), + services.get(IConfigurationService), + services.get(IContextMenuService), + services.get(IEditorProgressService), + services.get(IClipboardService) + ); } export interface IDiffNavigatorOptions { diff --git a/src/vs/editor/standalone/browser/standaloneServices.ts b/src/vs/editor/standalone/browser/standaloneServices.ts index 6e6c2032f4a..a45b185aeb4 100644 --- a/src/vs/editor/standalone/browser/standaloneServices.ts +++ b/src/vs/editor/standalone/browser/standaloneServices.ts @@ -13,7 +13,7 @@ import { LanguageService } from 'vs/editor/common/services/languageServiceImpl'; import { IModelService } from 'vs/editor/common/services/modelService'; import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl'; import { ITextResourceConfigurationService, ITextResourcePropertiesService } from 'vs/editor/common/services/textResourceConfigurationService'; -import { SimpleBulkEditService, SimpleConfigurationService, SimpleDialogService, SimpleNotificationService, SimpleEditorProgressService, SimpleResourceConfigurationService, SimpleResourcePropertiesService, SimpleUriLabelService, SimpleWorkspaceContextService, StandaloneCommandService, StandaloneKeybindingService, StandaloneTelemetryService, SimpleLayoutService, SimpleWorkspaceTrustManagementService } from 'vs/editor/standalone/browser/simpleServices'; +import { SimpleBulkEditService, SimpleConfigurationService, SimpleDialogService, SimpleNotificationService, SimpleEditorProgressService, SimpleResourceConfigurationService, SimpleResourcePropertiesService, SimpleUriLabelService, SimpleWorkspaceContextService, StandaloneCommandService, StandaloneKeybindingService, StandaloneTelemetryService, SimpleLayoutService, SimpleWorkspaceTrustManagementService, SimpleTextModelService } from 'vs/editor/standalone/browser/simpleServices'; import { StandaloneCodeEditorServiceImpl } from 'vs/editor/standalone/browser/standaloneCodeServiceImpl'; import { StandaloneThemeServiceImpl } from 'vs/editor/standalone/browser/standaloneThemeServiceImpl'; import { IStandaloneThemeService } from 'vs/editor/standalone/common/standaloneThemeService'; @@ -58,6 +58,7 @@ import { ILanguageConfigurationService, LanguageConfigurationService } from 'vs/ import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust'; import { IOpenerService } from 'vs/platform/opener/common/opener'; import { OpenerService } from 'vs/editor/browser/services/openerService'; +import { ITextModelService } from 'vs/editor/common/services/resolverService'; export interface IEditorOverrideServices { [index: string]: any; @@ -252,6 +253,8 @@ export class DynamicStandaloneServices extends Disposable { ensure(IBulkEditService, () => new SimpleBulkEditService(modelService)); ensure(IWorkspaceTrustManagementService, () => new SimpleWorkspaceTrustManagementService()); + + ensure(ITextModelService, () => new SimpleTextModelService(modelService)); } public get(serviceId: ServiceIdentifier): T { @@ -261,12 +264,4 @@ export class DynamicStandaloneServices extends Disposable { } return r; } - - public set(serviceId: ServiceIdentifier, instance: T): void { - this._serviceCollection.set(serviceId, instance); - } - - public has(serviceId: ServiceIdentifier): boolean { - return this._serviceCollection.has(serviceId); - } }