Skip to content

Commit

Permalink
WIP: Update to latest monaco-editor-wrapper and monaco-editor-react
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisalmen committed Oct 16, 2023
1 parent 0faf6ac commit 320d353
Show file tree
Hide file tree
Showing 18 changed files with 691 additions and 655 deletions.
2 changes: 1 addition & 1 deletion hugo/assets/scripts/arithmetics/arithmetics-tools.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { monaco } from "monaco-editor-wrapper/.";
import { monaco } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { Pos } from "../langium-utils/langium-ast";

export interface Evaluation {
Expand Down
4 changes: 1 addition & 3 deletions hugo/assets/scripts/arithmetics/arithmetics.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { MonacoEditorReactComp } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { createUserConfig, MonacoEditorReactComp, UserConfig } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { buildWorkerDefinition } from "monaco-editor-workers";
import React from "react";
import { createRoot } from "react-dom/client";
import { Diagnostic, DocumentChangeResponse } from "../langium-utils/langium-ast";
import { Evaluation, examples, syntaxHighlighting } from "./arithmetics-tools";
import { UserConfig } from "monaco-editor-wrapper";
import { createUserConfig } from "../utils";

buildWorkerDefinition(
"../../libs/monaco-editor-workers/workers",
Expand Down
4 changes: 1 addition & 3 deletions hugo/assets/scripts/domainmodel/domainmodel.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { MonacoEditorReactComp } from "@typefox/monaco-editor-react/bundle";
import { createUserConfig, MonacoEditorReactComp, UserConfig } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { buildWorkerDefinition } from "monaco-editor-workers";
import React from "react";
import { createRoot } from "react-dom/client";
import { Diagnostic, DocumentChangeResponse, LangiumAST } from "../langium-utils/langium-ast";
import { DomainModelAstNode, example, getTreeNode, syntaxHighlighting } from "./domainmodel-tools";
import { UserConfig } from "monaco-editor-wrapper";
import { createUserConfig } from "../utils";
import D3Tree from "./d3tree";

buildWorkerDefinition(
Expand Down
2 changes: 1 addition & 1 deletion hugo/assets/scripts/minilogo/minilogo-tools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { monaco } from "monaco-editor-wrapper/.";
import { monaco } from "./static/libs/monaco-editor-react/monaco-editor-react.js";

export interface Command {
name: 'penUp' | 'penDown' | 'move' | 'color';
Expand Down
4 changes: 1 addition & 3 deletions hugo/assets/scripts/minilogo/minilogo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { MonacoEditorReactComp } from "@typefox/monaco-editor-react/bundle";
import { createUserConfig, MonacoEditorReactComp, UserConfig } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { buildWorkerDefinition } from "monaco-editor-workers";
import React, { createRef } from "react";
import { createRoot } from "react-dom/client";
import { Diagnostic, DocumentChangeResponse, LangiumAST } from "../langium-utils/langium-ast";
import { ColorArgs, Command, MoveArgs, examples, syntaxHighlighting } from "./minilogo-tools";
import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from "lz-string";
import { UserConfig } from "monaco-editor-wrapper";
import { createUserConfig } from "../utils";

buildWorkerDefinition(
"../../libs/monaco-editor-workers/workers",
Expand Down
19 changes: 17 additions & 2 deletions hugo/assets/scripts/monaco-editor-react.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
export * from "@typefox/monaco-editor-react";
import 'vscode/default-extensions/theme-defaults';
import * as monaco from "monaco-editor";
import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override';
import type { MonacoEditorProps } from "@typefox/monaco-editor-react";
import { MonacoEditorReactComp } from "@typefox/monaco-editor-react";
import { addMonacoStyles } from 'monaco-editor-wrapper/styles';

export * from "monaco-editor-wrapper";
export type * from "monaco-editor-wrapper";
export * from "./monaco-editor-wrapper-utils";

export {
monaco,
MonacoEditorProps,
MonacoEditorReactComp,
addMonacoStyles,
getKeybindingsServiceOverride
}
176 changes: 176 additions & 0 deletions hugo/assets/scripts/monaco-editor-wrapper-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import * as monaco from "monaco-editor";
import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override';
import { EditorAppConfigClassic, EditorAppConfigExtended, LanguageClientConfig, UserConfig } from "monaco-editor-wrapper";

export type WorkerUrl = string;

/**
* Generalized configuration used with 'getMonacoEditorReactConfig' to generate a working configuration for monaco-editor-react
*/
export interface MonacoReactConfig {
code: string,
languageId: string,
worker: WorkerUrl | Worker,
readonly?: boolean // whether to make the editor readonly or not (by default is false)
}

/**
* Extended config, specifically for textmate usage
*/
export interface MonacoExtendedReactConfig extends MonacoReactConfig {
textmateGrammar: any;
}

/**
* Editor config, specifically for monarch grammar usage
*/
export interface MonacoEditorReactConfig extends MonacoReactConfig {
monarchGrammar?: monaco.languages.IMonarchLanguage;
}

/**
* Helper to identify a Extended config, for use with TextMate
*/
function isMonacoExtendedReactConfig(config: unknown): config is MonacoExtendedReactConfig {
return (config as MonacoExtendedReactConfig).textmateGrammar !== undefined;
}


/**
* Default language configuration, common to most Langium DSLs
*/
export const defaultLanguageConfig = {
"comments": {
"lineComment": "//",
"blockComment": ["/*", "*/"]
},
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
],
"autoClosingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
],
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
]
};

/**
* Generates a UserConfig for a given Langium example, which is then passed to the monaco-editor-react component
*
* @param config A Extended or classic editor config to generate a UserConfig from
* @returns A completed UserConfig
*/
export function createUserConfig(config: MonacoExtendedReactConfig | MonacoEditorReactConfig): UserConfig {
// setup urls for config & grammar
const id = config.languageId;

// check whether to use extended config (Textmate) or the classic editor config (Monarch)
let editorAppConfig: EditorAppConfigClassic | EditorAppConfigExtended;
const useExtendedConfig = isMonacoExtendedReactConfig(config);
if (useExtendedConfig) {
// setup extension contents
const languageConfigUrl = `/${id}-configuration.json`;
const languageGrammarUrl = `/${id}-grammar.json`;
const extensionContents = new Map<string, string>();
extensionContents.set(languageConfigUrl, JSON.stringify(defaultLanguageConfig));
extensionContents.set(languageGrammarUrl, JSON.stringify(config.textmateGrammar));

editorAppConfig = {
$type: 'extended',
languageId: id,
code: config.code,
useDiffEditor: false,
extensions: [{
config: {
name: id,
publisher: 'TypeFox',
version: '1.0.0',
engines: {
vscode: '*'
},
contributes: {
languages: [{
id: id,
extensions: [ `.${id}` ],
configuration: languageConfigUrl
}],
grammars: [{
language: id,
scopeName: `source.${id}`,
path: languageGrammarUrl
}]
}
},
filesOrContents: extensionContents,
}],
userConfiguration: {
json: JSON.stringify({
'workbench.colorTheme': 'Default Dark Modern',
'editor.semanticHighlighting.enabled': true,
'editor.lightbulb.enabled': true,
'editor.guides.bracketPairsHorizontal': 'active'
})
}
};
} else {
editorAppConfig = {
$type: 'classic',
languageId: id,
code: config.code,
useDiffEditor: false,
languageExtensionConfig: { id },
languageDef: config.monarchGrammar,
editorOptions: {
'semanticHighlighting.enabled': true,
readOnly: config.readonly,
theme: 'vs-dark'
}
};
}

let languageClientConfig: LanguageClientConfig;
if (typeof config.worker === 'string') {
languageClientConfig = {
options: {
$type: 'WorkerConfig',
url: new URL(config.worker, window.location.href),
type: 'module',
name: `${id}-language-server-worker`,
}
};
} else {
languageClientConfig = {
options: {
$type: 'WorkerDirect',
worker: config.worker
}
};
}

// generate user config for langium based language
const userConfig: UserConfig = {
wrapperConfig: {
serviceConfig: {
// only use keyboard service in addition to the default services from monaco-editor-wrapper
userServices: {
...getKeybindingsServiceOverride()
},
debugLogging: true
},
editorAppConfig
},
languageClientConfig
}
return userConfig;
}
2 changes: 1 addition & 1 deletion hugo/assets/scripts/sql/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { monaco } from "@typefox/monaco-editor-react/.";
import { monaco } from "./static/libs/monaco-editor-react/monaco-editor-react.js";

export const syntaxHighlighting: monaco.languages.IMonarchLanguage = {
tokenizer: {
Expand Down
4 changes: 1 addition & 3 deletions hugo/assets/scripts/sql/ui.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MonacoEditorReactComp } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { createUserConfig, MonacoEditorReactComp, UserConfig } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { buildWorkerDefinition } from "monaco-editor-workers";
import React from "react";
import { createRoot } from "react-dom/client";
Expand All @@ -9,8 +9,6 @@ import {
defaultText,
syntaxHighlighting,
} from "./constants";
import { UserConfig } from "monaco-editor-wrapper";
import { createUserConfig } from '../utils';

buildWorkerDefinition(
"../../libs/monaco-editor-workers/workers",
Expand Down
4 changes: 1 addition & 3 deletions hugo/assets/scripts/statemachine/statemachine.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { MonacoEditorReactComp } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { createUserConfig, MonacoEditorReactComp, UserConfig } from "./static/libs/monaco-editor-react/monaco-editor-react.js";
import { buildWorkerDefinition } from "monaco-editor-workers";
import React from "react";
import { createRoot } from "react-dom/client";
import { Diagnostic, DocumentChangeResponse, LangiumAST } from "../langium-utils/langium-ast";
import { defaultText, StateMachineAstNode, StateMachineState, StateMachineTools } from "./statemachine-tools";
import { UserConfig } from "monaco-editor-wrapper";
import { createUserConfig } from '../utils';
import statemachineGrammar from 'langium-statemachine-dsl/syntaxes/statemachine.tmLanguage.json';

buildWorkerDefinition(
Expand Down
Loading

0 comments on commit 320d353

Please sign in to comment.