Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Make resource configuration configureable and make all extra services…
Browse files Browse the repository at this point in the history
… fully configurable
  • Loading branch information
kaisalmen committed Mar 9, 2023
1 parent 5e03d9e commit 8b40bc2
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 34 deletions.
28 changes: 28 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
"@typescript-eslint/eslint-plugin": "~5.54.0",
"@typescript-eslint/parser": "~5.54.0",
"@vitest/coverage-c8": "~0.29.2",
"@types/shelljs": "~0.8.11",
"@vitest/ui": "~0.29.2",
"path-browserify": "~1.0.1",
"typescript": "~4.9.5",
"eslint": "~8.35.0",
"editorconfig": "~1.0.2",
"shx": "~0.3.4",
"shelljs": "~0.8.5",
"tslib": "~2.5.0",
"vite": "~4.1.4",
"vite-node": "~0.29.2",
Expand Down
14 changes: 1 addition & 13 deletions packages/examples/src/langium/wrapperLangium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BrowserMessageReader, BrowserMessageWriter, Message } from 'vscode-lang
const client = new MonacoEditorLanguageClientWrapper({
useVscodeConfig: true,
vscodeActivationConfig: {
basePath: '../monaco-editor-wrapper',
enableModelEditorService: true,
enableConfigurationService: true,
enableKeybindingsService: true,
Expand Down Expand Up @@ -64,19 +65,6 @@ async function startEditor() {
content: responseStatemachineTm.text()
}
);
/*
vscodeApiConfig.setGrammars([{
language: languageId,
scopeName: 'source.statemachine',
path: './statemachine-grammar.json'
}], (grammar) => {
switch (grammar.language) {
case languageId:
return responseStatemachineTm.text();
default:
return Promise.reject(new Error(`Grammar language ${grammar.language} not found!`));
}
}); */

vscodeApiConfig.setUserConfiguration(`{
"workbench.colorTheme": "Dark+ (Experimental)",
Expand Down
1 change: 1 addition & 0 deletions packages/monaco-editor-wrapper/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
resources/themes
resources/wasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, mkdirSync, readFile, writeFileSync } from 'fs';
import { fetchAllThemesFromGitHub } from 'monaco-languageclient/themeRemoteHelper';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import shell from 'shelljs';

/**
* Solves: __dirname is not defined in ES module scope
Expand Down Expand Up @@ -31,7 +32,10 @@ readFile(resolve(getLocalDirectory(), '../../../../node_modules/monaco-editor/mi
});
});

mkdirSync(resolve(getLocalDirectory(), '../../resources/themes'), {
recursive: true
});
fetchAllThemesFromGitHub(resolve(getLocalDirectory(), '../../resources/themes'));
shell.mkdir('-p', resolve(getLocalDirectory(), '../../resources/wasm'));
const onigSource = resolve(getLocalDirectory(), '../../../../node_modules/vscode-oniguruma/release/onig.wasm');
const onigTarget = resolve(getLocalDirectory(), '../../resources/wasm');
shell.cp(onigSource, onigTarget);

shell.mkdir('-p', resolve(getLocalDirectory(), '../../resources/themes'));
await fetchAllThemesFromGitHub(resolve(getLocalDirectory(), '../../resources/themes'));
13 changes: 13 additions & 0 deletions packages/monaco-editor-wrapper/build/tsconfig.src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"declarationDir": "dist",
"noEmit": true
},
"include": [
"src/**/*.ts",
"src/**/*.mts"
]
}
4 changes: 2 additions & 2 deletions packages/monaco-editor-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
],
"scripts": {
"clean": "npx shx rm -rf ./dist ./bundle ./src/generated *.tsbuildinfo",
"create:assets": "node ./build/src/buildAssets.mjs",
"compile": "npm run create:assets && tsc --build tsconfig.src.json",
"handle:assets": "vite-node ./build/src/buildAssets.mts",
"compile": "npm run handle:assets && tsc --build tsconfig.src.json",
"build:bundle:main": "npx shx rm -rf bundle/generated && vite --config vite.bundle.config.ts build",
"build:bundle:languages": "vite --config vite.languages.config.ts build",
"build:bundle": "npm run build:bundle:main && npm run build:bundle:languages",
Expand Down
37 changes: 24 additions & 13 deletions packages/monaco-editor-wrapper/src/vscodeApiConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import getNotificationServiceOverride from 'vscode/service-override/notification
import getDialogsServiceOverride from 'vscode/service-override/dialogs';
import getConfigurationServiceOverride, { updateUserConfiguration as vscodeUpdateUserConfiguration } from 'vscode/service-override/configuration';
import getKeybindingsServiceOverride, { updateUserKeybindings } from 'vscode/service-override/keybindings';
import getTextmateServiceOverride, { ITMSyntaxExtensionPoint, setGrammars as vscodeSetGrammars } from 'vscode/service-override/textmate';
import getTextmateServiceOverride, { ITMSyntaxExtensionPoint, setGrammars } from 'vscode/service-override/textmate';
import getLanguagesServiceOverride, { IRawLanguageExtensionPoint, setLanguages as vscodeSetLanguages } from 'vscode/service-override/languages';
import getTokenClassificationServiceOverride from 'vscode/service-override/tokenClassification';
import getLanguageConfigurationServiceOverride, { setLanguageConfiguration as vscodeSetLanguageConfiguration } from 'vscode/service-override/languageConfiguration';
Expand All @@ -13,6 +13,7 @@ import getThemeServiceOverride from 'vscode/service-override/theme';
import { loadAllDefaultThemes } from 'monaco-languageclient/themeLocalHelper';

export type MonacoVscodeApiActivtion = {
basePath: string,
enableModelEditorService: boolean;
// notificationService and dialogsService are enabled by default
enableConfigurationService: boolean;
Expand All @@ -31,9 +32,6 @@ export class VscodeApiConfig {

private activationConfig: MonacoVscodeApiActivtion | undefined;

private onigFileUrl = new URL('../../node_modules/vscode-oniguruma/release/onig.wasm', window.location.href).href;
private themesUrl = new URL('../monaco-editor-wrapper/resources/themes', window.location.href).href;

private userConfigurationJson: string | undefined;
private keybindingsJson: string | undefined;

Expand All @@ -49,7 +47,9 @@ export class VscodeApiConfig {
} = {};

async init(input?: MonacoVscodeApiActivtion) {
console.log(window.location.href);
this.activationConfig = {
basePath: input?.basePath ?? '.',
enableModelEditorService: input?.enableModelEditorService ?? true,
enableConfigurationService: input?.enableConfigurationService ?? true,
enableKeybindingsService: input?.enableKeybindingsService ?? true,
Expand All @@ -58,27 +58,37 @@ export class VscodeApiConfig {
enableLanguageConfigurationService: input?.enableLanguageConfigurationService ?? true,
};

const responseOnig = await fetch(this.onigFileUrl);
const onigFileUrl = new URL(this.activationConfig?.basePath + '/resources/wasm/onig.wasm', window.location.href).href;
const responseOnig = await fetch(onigFileUrl);
const modelService = this.activationConfig.enableModelEditorService ? getModelEditorServiceOverride(async (model, options) => {
console.log('trying to open a model', model, options);
return undefined;
}) : undefined;
const configurationService = this.activationConfig.enableModelEditorService ? getConfigurationServiceOverride() : undefined;
const keybindingsService = this.activationConfig.enableKeybindingsService ? getKeybindingsServiceOverride() : undefined;

const textmateService = this.activationConfig.enableTextmateService ? getTextmateServiceOverride(async () => {
return await responseOnig.arrayBuffer();
}) : undefined;
const tokenClassificationService = this.activationConfig.enableTokenClassificationService ? getTokenClassificationServiceOverride() : undefined;
let languageConfigurationService;
let languageService;
if (tokenClassificationService) {
languageConfigurationService = getLanguageConfigurationServiceOverride();
languageService = getLanguagesServiceOverride();
}

StandaloneServices.initialize({
...modelService,
...getNotificationServiceOverride(),
...getDialogsServiceOverride(),
...configurationService,
...keybindingsService,
...getTextmateServiceOverride(async () => {
return await responseOnig.arrayBuffer();
}),
...textmateService,
...getThemeServiceOverride(),
...getTokenClassificationServiceOverride(),
...getLanguageConfigurationServiceOverride(),
...getLanguagesServiceOverride()
...tokenClassificationService,
...languageConfigurationService,
...languageService
});
console.log('Basic init of VscodeApiConfig was completed.');
}
Expand All @@ -101,10 +111,11 @@ export class VscodeApiConfig {
return Promise.reject(new Error(`Grammar language ${grammar.language} not found!`));
}
};
vscodeSetGrammars(Array.from(this.grammarsConfig.grammarMap.values()), contentFunc);
setGrammars(Array.from(this.grammarsConfig.grammarMap.values()), contentFunc);
}

await loadAllDefaultThemes(this.themesUrl);
const themesUrl = new URL(this.activationConfig?.basePath + '/resources/themes', window.location.href).href;
await loadAllDefaultThemes(themesUrl);

if (this.userConfigurationJson) {
void vscodeUpdateUserConfiguration(this.userConfigurationJson);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"files": [],
"references": [
{ "path": "packages/monaco-editor-wrapper/tsconfig.src.json" },
{ "path": "packages/monaco-editor-wrapper/build/tsconfig.src.json" },
{ "path": "packages/monaco-editor-comp/tsconfig.src.json" },
{ "path": "packages/monaco-editor-react/tsconfig.src.json" },
{ "path": "packages/monaco-editor-workers/tsconfig.src.json" },
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"exclude": [
"**/node_modules/**/*",
"**/dist/**/*",
"**/lib/**/*",
"**/build/**/*"
"**/lib/**/*"
]
}

0 comments on commit 8b40bc2

Please sign in to comment.