Skip to content

Commit

Permalink
Always use languages and model services, clean-up service usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Sep 28, 2023
1 parent 50fb459 commit 5da5529
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 56 deletions.
17 changes: 0 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions packages/client/src/monaco-vscode-api-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { editor, Environment } from 'monaco-editor';
import { ILogService, initialize, LogLevel, StandaloneServices } from 'vscode/services';
import { initialize as initializeVscodeExtensions } from 'vscode/extensions';
import { OpenEditor } from '@codingame/monaco-vscode-editor-service-override';
import getLanguagesServiceOverride from '@codingame/monaco-vscode-languages-service-override';
import getModelServiceOverride from '@codingame/monaco-vscode-model-service-override';

interface MonacoEnvironmentEnhanced extends Environment {
vscodeApiInitialised: boolean;
Expand Down Expand Up @@ -45,29 +47,39 @@ export const useOpenEditorStub: OpenEditor = async (modelRef, options, sideBySid

/**
* monaco-vscode-api automatically loads the following services:
* - layout
* - environment
* - extension
* - files
* - quickAccess
* - layout
* - environment
* - extension
* - files
* - quickAccess
*
* monaco-languageclient always adds the following services:
* - languages
* - model
*/
export const importAllServices = async (config?: InitializeServiceConfig) => {
const serviceNames: string[] = [];
const lc: InitializeServiceConfig = config ?? {};
const userServices: editor.IEditorOverrideServices = lc.userServices ?? {};

const reportServiceLoading = (services: editor.IEditorOverrideServices, debugLogging: boolean, origin?: string) => {
const reportServiceLoading = (services: editor.IEditorOverrideServices, debugLogging: boolean) => {
for (const serviceName of Object.keys(services)) {
if (debugLogging) {
if (origin) {
console.log(`Loading ${origin} service: ${serviceName}`);
} else {
console.log(`Loading service: ${serviceName}`);
}
console.log(`Loading service: ${serviceName}`);
}
}
};
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;
}
};
const mlcDefautServices = {
...getLanguagesServiceOverride(),
...getModelServiceOverride()
};
mergeServices(mlcDefautServices, userServices);
reportServiceLoading(userServices, lc.debugLogging === true);

const haveThemeService = serviceNames.includes('theme') || Object.keys(userServices).includes('themeService');
const haveTextmateService = serviceNames.includes('textmate') || Object.keys(userServices).includes('textMateTokenizationFeature');
Expand Down
1 change: 0 additions & 1 deletion packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
}
},
"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",
Expand Down
12 changes: 2 additions & 10 deletions packages/examples/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import { editor, languages, Uri } from 'monaco-editor';
import { createConfiguredEditor, createModelReference, IReference, ITextFileEditorModel } from 'vscode/monaco';
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, useOpenEditorStub, MonacoLanguageClient } from 'monaco-languageclient';
import { initServices, MonacoLanguageClient } from 'monaco-languageclient';
import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient';
import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc';
import { URI } from 'vscode-uri';
Expand Down Expand Up @@ -93,11 +89,7 @@ export const performInit = async (vscodeApiInit: boolean) => {
...getThemeServiceOverride(),
...getTextmateServiceOverride(),
...getConfigurationServiceOverride(URI.file('/workspace')),
...getEditorServiceOverride(useOpenEditorStub),
...getModelServiceOverride(),
...getLanguagesServiceOverride(),
...getKeybindingsServiceOverride(),
...getAccessibilityServiceOverride()
},
debugLogging: true
});
Expand All @@ -117,7 +109,7 @@ export const createJsonEditor = async (config: {
content: string
}) => {
// create the model
const uri = Uri.parse('/tmp/model.json');
const uri = Uri.parse('/workspace/model.json');
const modelRef = await createModelReference(uri, config.content);
modelRef.object.setLanguageId('json');

Expand Down
10 changes: 2 additions & 8 deletions packages/examples/src/langium/statemachineClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ import { ExtensionHostKind, registerExtension } from 'vscode/extensions';
import getConfigurationServiceOverride, { updateUserConfiguration } 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 getAccessibilityServiceOverride from 'vscode/service-override/accessibility';
import { LogLevel } from 'vscode/services';
import '@codingame/monaco-vscode-theme-defaults-default-extension';
import { URI } from 'vscode-uri';
Expand All @@ -35,10 +32,7 @@ export const setupStatemachineClient = async () => {
...getTextmateServiceOverride(),
...getConfigurationServiceOverride(URI.file('/workspace')),
...getEditorServiceOverride(useOpenEditorStub),
...getModelServiceOverride(),
...getLanguagesServiceOverride(),
...getKeybindingsServiceOverride(),
...getAccessibilityServiceOverride()
...getKeybindingsServiceOverride()
},
debugLogging: true,
logLevel: LogLevel.Info
Expand Down Expand Up @@ -93,7 +87,7 @@ export const setupStatemachineClient = async () => {
const editorText = await (await fetch(exampleStatemachineUrl)).text();

const editorOptions = {
model: editor.createModel(editorText, languageId, Uri.parse('inmemory://example.statemachine')),
model: editor.createModel(editorText, languageId, Uri.parse('/workspace/example.statemachine')),
automaticLayout: true
};
createConfiguredEditor(document.getElementById('container')!, editorOptions);
Expand Down
12 changes: 4 additions & 8 deletions packages/examples/src/python/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ 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';
Expand Down Expand Up @@ -60,7 +58,7 @@ const createLanguageClient = (transports: MessageTransports): MonacoLanguageClie
workspaceFolder: {
index: 0,
name: 'workspace',
uri: monaco.Uri.parse('/tmp')
uri: monaco.Uri.parse('/workspace')
},
synchronize: {
fileEvents: [vscode.workspace.createFileSystemWatcher('**')]
Expand All @@ -81,9 +79,7 @@ export const startPythonClient = async () => {
userServices: {
...getThemeServiceOverride(),
...getTextmateServiceOverride(),
...getConfigurationServiceOverride(URI.file('/tmp')),
...getModelServiceOverride(),
...getLanguagesServiceOverride(),
...getConfigurationServiceOverride(URI.file('/workspace')),
...getKeybindingsServiceOverride()
},
debugLogging: true,
Expand Down Expand Up @@ -136,7 +132,7 @@ export const startPythonClient = async () => {
}`);

const fileSystemProvider = new RegisteredFileSystemProvider(false);
fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/hello.py'), 'print("Hello, World!")'));
fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/workspace/hello.py'), 'print("Hello, World!")'));
registerFileSystemOverlay(1, fileSystemProvider);

// create the web socket and configure to start the language client on open, can add extra parameters to the url if needed.
Expand All @@ -163,7 +159,7 @@ export const startPythonClient = async () => {
});

// use the file create before
const modelRef = await createModelReference(monaco.Uri.file('/tmp/hello.py'));
const modelRef = await createModelReference(monaco.Uri.file('/workspace/hello.py'));
modelRef.object.setLanguageId(languageId);

// create monaco editor
Expand Down

0 comments on commit 5da5529

Please sign in to comment.