From ea70cda687e460ae21bce98cd91a600122a7c047 Mon Sep 17 00:00:00 2001 From: Kai Salmen Date: Wed, 27 Sep 2023 21:54:18 +0200 Subject: [PATCH] Simplify monaco-vscode-api config --- package-lock.json | 129 +++++++++++ .../client/src/monaco-vscode-api-services.ts | 207 +----------------- packages/examples/package.json | 12 + packages/examples/src/browser/main.ts | 4 +- packages/examples/src/common.ts | 34 ++- .../src/langium/statemachineClient.ts | 41 ++-- packages/examples/src/minimal.ts | 7 +- packages/examples/src/python/client/main.ts | 25 ++- vite.config.ts | 38 +++- 9 files changed, 236 insertions(+), 261 deletions(-) diff --git a/package-lock.json b/package-lock.json index 84d00d015..ea0ca9e15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -73,6 +73,113 @@ "resolved": "https://registry.npmjs.org/@chevrotain/utils/-/utils-11.0.3.tgz", "integrity": "sha512-YslZMgtJUyuMbZ+aKvfF3x1f5liK4mWNxghFRv7jqRR9C3R3fAOGTTKvxXDa2Y1s9zSbcpuO0cAxDYsc9SrXoQ==" }, + "node_modules/@codingame/monaco-vscode-accessibility-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-accessibility-service-override/-/monaco-vscode-accessibility-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-KFrOWnwWc6GOBfugtbVBmmnNkUy/hgXCOPgzw/t/whUjfdy49/w0xO8xCewsBFbfmtZ4AwmEEwSeSjd+Rc0cPg==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-configuration-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-configuration-service-override/-/monaco-vscode-configuration-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-i4zIc1xN4tLHXNEtLMdqzXTgewWqzsu32KRpdhKIATbFDhG8f+NKQzpWrFgKU2Y4Wg9QFx1UwNScB6A0LrsLZg==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-editor-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-editor-service-override/-/monaco-vscode-editor-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-q7vWUU5uOdSFiJ6C6TRMZHxPUfMoLiBrBZoTLToc9AQM29mRbGJKtzwfJt4xSMxtzaEJkz4yACfaJEA3HVwNvg==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-json-default-extension": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-json-default-extension/-/monaco-vscode-json-default-extension-1.82.0-next.0.tgz", + "integrity": "sha512-rQvM1IagjyZaglnCaInk+2I/o8u7Kk+bvB0h78Os3NPzWGTFJ2nudSAtkipS/VNc920/r7YS0m8tvo17tyBrNQ==", + "dependencies": { + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-keybindings-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-keybindings-service-override/-/monaco-vscode-keybindings-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-jgvvi55m9fS8nq/MUmlWkm8EEuX6PWrbtpdijh+/ZOJJXExI0lFIyGUpnspuRCLwmEmz3zuOgK2d2fCWh3Hq4Q==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-languages-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-languages-service-override/-/monaco-vscode-languages-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-0eg9s5j+Bu6BEgqUhjJa4aDLqhz3eGLBR1tQbQuQdtHkHM5ynRq4bQJ5ZzoQdqhAbO1fuN6gvBquNX8dazczRQ==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-model-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-model-service-override/-/monaco-vscode-model-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-v38Rq6x+xkk+RguVQ+HL3uMlOqzQzwFMdfG8LaWPRTGx2zhJzpdc1Ojr1p2UFSPdiZzSRJ6q5XEC5yLFqtYR9Q==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-python-default-extension": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-python-default-extension/-/monaco-vscode-python-default-extension-1.82.0-next.0.tgz", + "integrity": "sha512-AkG/7ZHWFUxa8M5j6byCVGQky5f08ik718YKfVdgBVZgveoQu8kFzp6Nhr3anQB+eX6NTmAfo2l4bCwmCcQLmQ==", + "dependencies": { + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-textmate-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-textmate-service-override/-/monaco-vscode-textmate-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-S/86kJCwoEec9btbtA2qkCvKPajcFC5hfVxOgbccaiYGprFK+3fBQ0jw1ccex5yKkhFl9Wvq8AcbCpg+lcDTtA==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0", + "vscode-oniguruma": "^2.0.0", + "vscode-textmate": "^9.0.0" + } + }, + "node_modules/@codingame/monaco-vscode-theme-defaults-default-extension": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-theme-defaults-default-extension/-/monaco-vscode-theme-defaults-default-extension-1.82.0-next.0.tgz", + "integrity": "sha512-sQJOu75oSod10wlZs446OCEGO6sR3s7T7M66830c91gFdGELLbQrGoG0JeDTJZECIfuKMkzMtagwNjKHa+cetg==", + "dependencies": { + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-theme-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-theme-service-override/-/monaco-vscode-theme-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-mD0IngvnMlbHgWZvdoGp0zlLii0YnDxuXm0rRTuBdzQoWSOKgyrZNFnaSZtoeuvEWHJBw19yclnd8jFbQ//Tcw==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, + "node_modules/@codingame/monaco-vscode-views-service-override": { + "version": "1.82.0-next.0", + "resolved": "https://registry.npmjs.org/@codingame/monaco-vscode-views-service-override/-/monaco-vscode-views-service-override-1.82.0-next.0.tgz", + "integrity": "sha512-cK+c1abptSXN1PV+F3KWWn72RQmq1GQOqRVmEgbvWkH9nj3/Wv4LK6O0VZO705ukjEH68e1GHrRYGpSSKRidOw==", + "dependencies": { + "monaco-editor": "0.43.0", + "vscode": "npm:@codingame/monaco-vscode-api@1.82.0-next.0" + } + }, "node_modules/@cspotcode/source-map-support": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", @@ -5970,6 +6077,16 @@ "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.17.3.tgz", "integrity": "sha512-SYU4z1dL0PyIMd4Vj8YOqFvHu7Hz/enbWtpfnVbJHU4Nd1YNYx8u0ennumc6h48GQNeOLxmwySmnADouT/AuZA==" }, + "node_modules/vscode-oniguruma": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/vscode-oniguruma/-/vscode-oniguruma-2.0.1.tgz", + "integrity": "sha512-poJU8iHIWnC3vgphJnrLZyI3YdqRlR27xzqDmpPXYzA93R4Gk8z7T6oqDzDoHjoikA2aS82crdXFkjELCdJsjQ==" + }, + "node_modules/vscode-textmate": { + "version": "9.0.0", + "resolved": "https://registry.npmjs.org/vscode-textmate/-/vscode-textmate-9.0.0.tgz", + "integrity": "sha512-Cl65diFGxz7gpwbav10HqiY/eVYTO1sjQpmRmV991Bj7wAoOAjGQ97PpQcXorDE2Uc4hnGWLY17xme+5t6MlSg==" + }, "node_modules/vscode-uri": { "version": "3.0.7", "resolved": "https://registry.npmjs.org/vscode-uri/-/vscode-uri-3.0.7.tgz", @@ -6328,6 +6445,18 @@ "version": "6.5.0-next.0", "license": "MIT", "dependencies": { + "@codingame/monaco-vscode-accessibility-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-configuration-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-editor-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-json-default-extension": "1.82.0-next.0", + "@codingame/monaco-vscode-keybindings-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-languages-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-model-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-python-default-extension": "1.82.0-next.0", + "@codingame/monaco-vscode-textmate-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-theme-defaults-default-extension": "1.82.0-next.0", + "@codingame/monaco-vscode-theme-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-views-service-override": "1.82.0-next.0", "express": "~4.18.2", "langium": "~2.0.2", "langium-statemachine-dsl": "~2.0.0", diff --git a/packages/client/src/monaco-vscode-api-services.ts b/packages/client/src/monaco-vscode-api-services.ts index 8a762c17d..5ab8d0b05 100644 --- a/packages/client/src/monaco-vscode-api-services.ts +++ b/packages/client/src/monaco-vscode-api-services.ts @@ -6,61 +6,12 @@ import { editor, Environment } from 'monaco-editor'; import { ILogService, initialize as initializeMonacoService, LogLevel, StandaloneServices } from 'vscode/services'; import { initialize as initializeVscodeExtensions } from 'vscode/extensions'; -import type { OpenEditor } from 'vscode/service-override/editor'; -import { ITerminalBackend, SimpleTerminalBackend } from 'vscode/service-override/terminal'; -import { IStorageProvider } from 'vscode/service-override/storage'; -import { IAnyWorkspaceIdentifier } from 'vscode/service-override/configuration'; -import { IWorkbenchConstructionOptions } from 'vscode/service-override/environment'; -import { URI } from 'vscode-uri'; interface MonacoEnvironmentEnhanced extends Environment { vscodeApiInitialised: boolean; } export type InitializeServiceConfig = { - enableDialogService?: boolean; - enableNotificationService?: boolean; - enableModelService?: boolean; - /** - * editor service is the default. If you want to use the views service, set enableViewsService to true. - */ - configureEditorOrViewsService?: { - enableViewsService?: boolean; - openEditorFunc?: OpenEditor - }; - configureConfigurationService?: { - defaultWorkspaceUri: URI | IAnyWorkspaceIdentifier; - }; - enableThemeService?: boolean; - enableKeybindingsService?: boolean; - enableTextmateService?: boolean; - enableLanguagesService?: boolean; - enableAudioCueService?: boolean; - enableDebugService?: boolean; - enablePreferencesService?: boolean; - enableSnippetsService?: boolean; - enableOutputService?: boolean; - configureTerminalService?: { - backendImpl: SimpleTerminalBackend | ITerminalBackend - } - enableSearchService?: boolean; - enableMarkersService?: boolean; - enableAccessibilityService?: boolean; - enableLanguageDetectionWorkerService?: boolean; - /** - * If no provider is specified, the default BrowserStorageService is used. - */ - configureStorageService?: { - provider?: IStorageProvider - }, - configureRemoteAgentService?: { - connectionToken?: Promise | string; - resourceUriProvider?: ((uri: URI) => URI); - }, - enableLifecycleService?: boolean, - configureEnvironmentService?: { - options: IWorkbenchConstructionOptions - }, userServices?: editor.IEditorOverrideServices; debugLogging?: boolean; logLevel?: LogLevel @@ -81,11 +32,7 @@ export const initServices = async (config?: InitializeServiceConfig) => { } if (!window.MonacoEnvironment) { - window.MonacoEnvironment = { - createTrustedTypesPolicy: (_policyName: string) => { - return undefined; - } - }; + window.MonacoEnvironment = {}; } (window.MonacoEnvironment as MonacoEnvironmentEnhanced).vscodeApiInitialised = true; }; @@ -104,88 +51,8 @@ export type ModuleWithDefaultExport = { */ export const importAllServices = async (config?: InitializeServiceConfig) => { const serviceNames: string[] = []; - const promises: Promise[] = []; const lc: InitializeServiceConfig = config ?? {}; - const userServices = lc.userServices ?? {}; - - const addService = (name: string, promise: Promise) => { - serviceNames.push(name); - promises.push(promise); - }; - - if (lc.enableModelService === true) { - addService('model', import('vscode/service-override/model')); - } - if (lc.configureEditorOrViewsService !== undefined) { - if (lc.configureEditorOrViewsService.enableViewsService === true) { - addService('views', import('vscode/service-override/views')); - } else { - addService('editor', import('vscode/service-override/editor')); - } - } - if (lc.configureConfigurationService !== undefined) { - addService('configuration', import('vscode/service-override/configuration')); - } - if (lc.enableDialogService === true) { - addService('dialogs', import('vscode/service-override/dialogs')); - } - if (lc.enableNotificationService === true) { - addService('notifications', import('vscode/service-override/notifications')); - } - if (lc.enableThemeService === true) { - addService('theme', import('vscode/service-override/theme')); - } - if (lc.enableTextmateService === true) { - addService('textmate', import('vscode/service-override/textmate')); - } - if (lc.enableKeybindingsService === true) { - addService('keybindings', import('vscode/service-override/keybindings')); - } - if (lc.enableLanguagesService === true) { - addService('languages', import('vscode/service-override/languages')); - } - if (lc.enableAudioCueService === true) { - addService('audioCue', import('vscode/service-override/audioCue')); - } - if (lc.enableDebugService === true) { - addService('debug', import('vscode/service-override/debug')); - } - if (lc.enablePreferencesService === true) { - addService('preferences', import('vscode/service-override/preferences')); - } - if (lc.enableSnippetsService === true) { - addService('snippets', import('vscode/service-override/snippets')); - } - if (lc.enableOutputService === true) { - addService('output', import('vscode/service-override/output')); - } - if (lc.configureTerminalService !== undefined) { - addService('terminal', import('vscode/service-override/terminal')); - } - if (lc.enableSearchService === true) { - addService('search', import('vscode/service-override/search')); - } - if (lc.enableMarkersService === true) { - addService('markers', import('vscode/service-override/markers')); - } - if (lc.enableAccessibilityService === true) { - addService('accessibility', import('vscode/service-override/accessibility')); - } - if (lc.enableLanguageDetectionWorkerService === true) { - addService('languageDetectionWorker', import('vscode/service-override/languageDetectionWorker')); - } - if (lc.configureStorageService !== undefined) { - addService('storage', import('vscode/service-override/storage')); - } - if (lc.configureRemoteAgentService !== undefined) { - addService('remoteAgent', import('vscode/service-override/remoteAgent')); - } - if (lc.enableLifecycleService === true) { - addService('lifecycle', import('vscode/service-override/lifecycle')); - } - if (lc.configureEnvironmentService !== undefined) { - addService('environment', import('vscode/service-override/environment')); - } + const userServices: editor.IEditorOverrideServices = lc.userServices ?? {}; const reportServiceLoading = (services: editor.IEditorOverrideServices, debugLogging: boolean, origin?: string) => { for (const serviceName of Object.keys(services)) { @@ -198,80 +65,24 @@ export const importAllServices = async (config?: InitializeServiceConfig) => { } } }; + reportServiceLoading(userServices, lc.debugLogging === true, 'user'); - const mergeServices = (services: editor.IEditorOverrideServices, overrideServices: editor.IEditorOverrideServices) => { - for (const [name, service] of Object.entries(services)) { - overrideServices[name] = service; - } - }; - - let count = 0; - const loadedImports = await Promise.all(Object.values(promises)); - const overrideServices: editor.IEditorOverrideServices = {}; - if (userServices) { - mergeServices(userServices, overrideServices); - reportServiceLoading(userServices, lc.debugLogging === true, 'user'); - } - - const haveThemeService = serviceNames.includes('theme') || Object.keys(overrideServices).includes('themeService'); - const haveTextmateService = serviceNames.includes('textmate') || Object.keys(overrideServices).includes('textMateTokenizationFeature'); + const haveThemeService = serviceNames.includes('theme') || Object.keys(userServices).includes('themeService'); + const haveTextmateService = serviceNames.includes('textmate') || Object.keys(userServices).includes('textMateTokenizationFeature'); const haveMarkersService = serviceNames.includes('markers'); - const haveViewsService = serviceNames.includes('views') || Object.keys(overrideServices).includes('viewsService'); + const haveViewsService = serviceNames.includes('views') || Object.keys(userServices).includes('viewsService'); // theme requires textmate if (haveThemeService && !haveTextmateService) { - throw new Error('"theme" requires "textmate" service. Please add it to the "initServices" config.'); + throw new Error('"theme" requires "textmate" service. Please add it to the "userServices".'); } // markers service requires views service if (haveMarkersService && !haveViewsService) { - throw new Error('"markers" requires "views" service. Please add it to the "initServices" config.'); - } - - for (const loadedImport of loadedImports) { - const serviceName = serviceNames[count]; - if (lc.debugLogging === true) { - console.log(`Initialising provided service: ${serviceName}`); - } - - let services: editor.IEditorOverrideServices = {}; - if (serviceName === 'editor' || serviceName === 'views') { - if (lc.configureEditorOrViewsService?.openEditorFunc) { - services = loadedImport.default(lc.configureEditorOrViewsService.openEditorFunc); - } else { - const defaultOpenEditorFunc: OpenEditor = async (model, options, sideBySide) => { - console.log('Trying to open a model', model, options, sideBySide); - return undefined; - }; - services = loadedImport.default(defaultOpenEditorFunc); - } - } else if (serviceName === 'configuration') { - if (lc.configureConfigurationService?.defaultWorkspaceUri) { - services = loadedImport.default(lc.configureConfigurationService!.defaultWorkspaceUri); - } - } else if (serviceName === 'terminal') { - if (lc.configureTerminalService?.backendImpl) { - services = loadedImport.default(lc.configureTerminalService.backendImpl); - } - } else if (serviceName === 'storage') { - services = loadedImport.default(lc.configureStorageService?.provider); - } else if (serviceName === 'remoteAgent') { - services = loadedImport.default(lc.configureRemoteAgentService?.connectionToken, lc.configureRemoteAgentService?.resourceUriProvider); - } else if (serviceName === 'environment') { - if (lc.configureEnvironmentService?.options) { - services = loadedImport.default(lc.configureEnvironmentService?.options); - } - } else { - services = loadedImport.default(); - } - - mergeServices(services, overrideServices); - reportServiceLoading(services, lc.debugLogging === true); - - count++; + throw new Error('"markers" requires "views" service. Please add it to the "userServices".'); } - await initializeMonacoService(overrideServices); + await initializeMonacoService(userServices); if (lc.logLevel) { StandaloneServices.get(ILogService).setLevel(lc.logLevel); } diff --git a/packages/examples/package.json b/packages/examples/package.json index 4de4572f2..2d0e07e70 100644 --- a/packages/examples/package.json +++ b/packages/examples/package.json @@ -43,6 +43,18 @@ } }, "dependencies": { + "@codingame/monaco-vscode-accessibility-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-configuration-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-editor-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-json-default-extension": "1.82.0-next.0", + "@codingame/monaco-vscode-keybindings-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-languages-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-model-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-python-default-extension": "1.82.0-next.0", + "@codingame/monaco-vscode-textmate-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-theme-defaults-default-extension": "1.82.0-next.0", + "@codingame/monaco-vscode-theme-service-override": "1.82.0-next.0", + "@codingame/monaco-vscode-views-service-override": "1.82.0-next.0", "express": "~4.18.2", "langium": "~2.0.2", "langium-statemachine-dsl": "~2.0.0", diff --git a/packages/examples/src/browser/main.ts b/packages/examples/src/browser/main.ts index 2314b56ca..1eee23e78 100644 --- a/packages/examples/src/browser/main.ts +++ b/packages/examples/src/browser/main.ts @@ -7,8 +7,8 @@ import { getLanguageService, TextDocument } from 'vscode-json-languageservice'; import { createConverter as createCodeConverter } from 'vscode-languageclient/lib/common/codeConverter.js'; import { createConverter as createProtocolConverter } from 'vscode-languageclient/lib/common/protocolConverter.js'; import { createDefaultJsonContent, createJsonEditor, performInit } from '../common.js'; -import 'vscode/default-extensions/theme-defaults'; -import 'vscode/default-extensions/json'; +import '@codingame/monaco-vscode-theme-defaults-default-extension'; +import '@codingame/monaco-vscode-json-default-extension'; import { buildWorkerDefinition } from 'monaco-editor-workers'; buildWorkerDefinition('../../node_modules/monaco-editor-workers/dist/workers/', new URL('', window.location.href).href, false); diff --git a/packages/examples/src/common.ts b/packages/examples/src/common.ts index 6b96f9a3a..fb47635c0 100644 --- a/packages/examples/src/common.ts +++ b/packages/examples/src/common.ts @@ -5,8 +5,16 @@ import { editor, languages, Uri } from 'monaco-editor'; import { createConfiguredEditor, createModelReference, IReference, ITextFileEditorModel } from 'vscode/monaco'; -import 'vscode/default-extensions/theme-defaults'; -import 'vscode/default-extensions/json'; +import '@codingame/monaco-vscode-theme-defaults-default-extension'; +import '@codingame/monaco-vscode-json-default-extension'; +import getAccessibilityServiceOverride from '@codingame/monaco-vscode-accessibility-service-override'; +import getConfigurationServiceOverride from '@codingame/monaco-vscode-configuration-service-override'; +import getEditorServiceOverride from '@codingame/monaco-vscode-editor-service-override'; +import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override'; +import getLanguagesServiceOverride from '@codingame/monaco-vscode-languages-service-override'; +import getModelServiceOverride from '@codingame/monaco-vscode-model-service-override'; +import getThemeServiceOverride from '@codingame/monaco-vscode-theme-service-override'; +import getTextmateServiceOverride from '@codingame/monaco-vscode-textmate-service-override'; import { initServices, MonacoLanguageClient } from 'monaco-languageclient'; import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient'; import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc'; @@ -81,17 +89,19 @@ export type ExampleJsonEditor = { export const performInit = async (vscodeApiInit: boolean) => { if (vscodeApiInit === true) { await initServices({ - enableThemeService: true, - enableTextmateService: true, - enableModelService: true, - configureEditorOrViewsService: { + userServices: { + ...getThemeServiceOverride(), + ...getTextmateServiceOverride(), + ...getConfigurationServiceOverride(URI.file('/workspace')), + ...getEditorServiceOverride(async (model, options, sideBySide) => { + console.log('Trying to open a model', model, options, sideBySide); + return undefined; + }), + ...getModelServiceOverride(), + ...getLanguagesServiceOverride(), + ...getKeybindingsServiceOverride(), + ...getAccessibilityServiceOverride() }, - configureConfigurationService: { - defaultWorkspaceUri: URI.file('/workspace') - }, - enableKeybindingsService: true, - enableLanguagesService: true, - enableAccessibilityService: true, debugLogging: true }); diff --git a/packages/examples/src/langium/statemachineClient.ts b/packages/examples/src/langium/statemachineClient.ts index 6e0463ab2..0be75e8b1 100644 --- a/packages/examples/src/langium/statemachineClient.ts +++ b/packages/examples/src/langium/statemachineClient.ts @@ -10,10 +10,16 @@ import { BrowserMessageReader, BrowserMessageWriter } from 'vscode-languageserve import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient'; import { createConfiguredEditor } from 'vscode/monaco'; import { ExtensionHostKind, registerExtension } from 'vscode/extensions'; -import { updateUserConfiguration } from 'vscode/service-override/configuration'; +import getConfigurationServiceOverride, { updateUserConfiguration } from '@codingame/monaco-vscode-configuration-service-override'; +import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override'; +import getLanguagesServiceOverride from '@codingame/monaco-vscode-languages-service-override'; +import getModelServiceOverride from '@codingame/monaco-vscode-model-service-override'; +import getThemeServiceOverride from '@codingame/monaco-vscode-theme-service-override'; +import getTextmateServiceOverride from '@codingame/monaco-vscode-textmate-service-override'; +import getViewsServiceOverride from '@codingame/monaco-vscode-views-service-override'; import getAccessibilityServiceOverride from 'vscode/service-override/accessibility'; import { LogLevel } from 'vscode/services'; -import 'vscode/default-extensions/theme-defaults'; +import '@codingame/monaco-vscode-theme-defaults-default-extension'; import { URI } from 'vscode-uri'; import { buildWorkerDefinition } from 'monaco-editor-workers'; @@ -24,31 +30,14 @@ const languageId = 'statemachine'; export const setupStatemachineClient = async () => { // use this to demonstrate all possible services made available by the monaco-vscode-api await initServices({ - enableThemeService: true, - enableTextmateService: true, - enableModelService: true, - configureEditorOrViewsService: { - enableViewsService: true - }, - configureConfigurationService: { - defaultWorkspaceUri: URI.file('/tmp') - }, - enableKeybindingsService: true, - enableLanguagesService: true, - enableAudioCueService: true, - enableDebugService: true, - enableDialogService: true, - enableNotificationService: true, - enablePreferencesService: true, - enableSnippetsService: true, - enableOutputService: true, - enableSearchService: true, - enableLanguageDetectionWorkerService: true, - // This should demonstrate that you can chose to not use the built-in loading mechanism, - // but do it manually, see below - enableAccessibilityService: false, userServices: { - // manually add the accessibility service + ...getThemeServiceOverride(), + ...getTextmateServiceOverride(), + ...getConfigurationServiceOverride(URI.file('/workspace')), + ...getViewsServiceOverride(), + ...getModelServiceOverride(), + ...getLanguagesServiceOverride(), + ...getKeybindingsServiceOverride(), ...getAccessibilityServiceOverride() }, debugLogging: true, diff --git a/packages/examples/src/minimal.ts b/packages/examples/src/minimal.ts index dad7964a2..c78acc0b4 100644 --- a/packages/examples/src/minimal.ts +++ b/packages/examples/src/minimal.ts @@ -8,6 +8,7 @@ import 'monaco-editor/esm/vs/editor/standalone/browser/iPadShowKeyboard/iPadShow import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js'; import * as vscode from 'vscode'; import { createConfiguredEditor, createModelReference } from 'vscode/monaco'; +import getModelServiceOverride from '@codingame/monaco-vscode-model-service-override'; import { initServices, MonacoLanguageClient } from 'monaco-languageclient'; import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient'; import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc'; @@ -62,8 +63,10 @@ const createLanguageClient = (transports: MessageTransports): MonacoLanguageClie const run = async () => { // init vscode-api await initServices({ - // you require the model service to be able use createModelReference, otherwise nothing else is needed - enableModelService: true, + userServices: { + // you require the model service to be able use createModelReference, otherwise nothing else is needed + ...getModelServiceOverride() + }, debugLogging: true }); diff --git a/packages/examples/src/python/client/main.ts b/packages/examples/src/python/client/main.ts index db9b3f3ab..63b33e4be 100644 --- a/packages/examples/src/python/client/main.ts +++ b/packages/examples/src/python/client/main.ts @@ -5,12 +5,17 @@ import * as monaco from 'monaco-editor'; import * as vscode from 'vscode'; -import 'vscode/default-extensions/theme-defaults'; -import 'vscode/default-extensions/python'; -import { updateUserConfiguration } from 'vscode/service-override/configuration'; +import '@codingame/monaco-vscode-theme-defaults-default-extension'; +import '@codingame/monaco-vscode-python-default-extension'; import { LogLevel } from 'vscode/services'; import { createConfiguredEditor, createModelReference } from 'vscode/monaco'; import { ExtensionHostKind, registerExtension } from 'vscode/extensions'; +import getConfigurationServiceOverride, { updateUserConfiguration } from '@codingame/monaco-vscode-configuration-service-override'; +import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override'; +import getLanguagesServiceOverride from '@codingame/monaco-vscode-languages-service-override'; +import getModelServiceOverride from '@codingame/monaco-vscode-model-service-override'; +import getThemeServiceOverride from '@codingame/monaco-vscode-theme-service-override'; +import getTextmateServiceOverride from '@codingame/monaco-vscode-textmate-service-override'; import { initServices, MonacoLanguageClient } from 'monaco-languageclient'; import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient'; import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc'; @@ -73,14 +78,14 @@ const createLanguageClient = (transports: MessageTransports): MonacoLanguageClie export const startPythonClient = async () => { // init vscode-api await initServices({ - enableModelService: true, - enableThemeService: true, - enableTextmateService: true, - configureConfigurationService: { - defaultWorkspaceUri: URI.file('/tmp') + userServices: { + ...getThemeServiceOverride(), + ...getTextmateServiceOverride(), + ...getConfigurationServiceOverride(URI.file('/tmp')), + ...getModelServiceOverride(), + ...getLanguagesServiceOverride(), + ...getKeybindingsServiceOverride() }, - enableLanguagesService: true, - enableKeybindingsService: true, debugLogging: true, logLevel: LogLevel.Debug }); diff --git a/vite.config.ts b/vite.config.ts index c5996801d..582be49a3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -19,7 +19,8 @@ export default defineConfig(() => { resolve: { alias: { path: 'path-browserify' - } + }, + dedupe: ['monaco-editor', 'vscode'] }, server: { port: 8080, @@ -28,22 +29,37 @@ export default defineConfig(() => { optimizeDeps: { esbuildOptions: { plugins: [ - // copied from "https://github.com/CodinGame/monaco-vscode-api/blob/run-ext-host-in-worker/demo/vite.config.ts" + // copied from "https://github.com/CodinGame/monaco-vscode-api/blob/main/demo/vite.config.ts" { name: 'import.meta.url', setup({ onLoad }) { - // Help vite that bundles/move files without touching `import.meta.url` which breaks asset urls - onLoad({ filter: /default-extensions\/.*\.js/, namespace: 'file' }, args => { - let code = readFileSync(args.path, 'utf8'); - code = code.replace( - /\bimport\.meta\.url\b/g, - `new URL('/@fs/${args.path}', window.location.origin)` - ); - return { contents: code }; - }); + // Help vite that bundles/move files in dev mode without touching `import.meta.url` which breaks asset urls + onLoad({ filter: /.*\.js/, namespace: 'file' }, async args => { + const code = fs.readFileSync(args.path, 'utf8') + + const assetImportMetaUrlRE = /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/g + let i = 0 + let newCode = '' + for (let match = assetImportMetaUrlRE.exec(code); match != null; match = assetImportMetaUrlRE.exec(code)) { + newCode += code.slice(i, match.index) + + const path = match[1].slice(1, -1) + const resolved = await import.meta.resolve!(path, url.pathToFileURL(args.path)) + + newCode += `new URL(${JSON.stringify(url.fileURLToPath(resolved))}, import.meta.url)` + + i = assetImportMetaUrlRE.lastIndex + } + newCode += code.slice(i) + + return { contents: newCode } + }) } }] } + }, + define: { + rootDirectory: JSON.stringify(__dirname) } }; return config;