Skip to content

Commit

Permalink
Custom rules too.
Browse files Browse the repository at this point in the history
  • Loading branch information
thesoftwarephilosopher committed Aug 22, 2024
1 parent 34e7ccc commit 98a3dea
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions site/load-samples.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import monaco from '@imlib/monaco-esm';
import type monacoTypes from 'monaco-editor';
import { setupTheme } from './theme.js';
import { tokenProvider } from './token-provider.js';
import { rules, tokenProvider } from './token-provider.js';
import { Mod, modules } from './vanillajsx/compiler.js';

monaco.languages.typescript.typescriptDefaults.addExtraLib(jsxlib(), `ts:filename/jsx.d.ts`);
Expand All @@ -10,7 +10,7 @@ monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ESNext,
});

setupTheme();
setupTheme(rules);

monaco.languages.setMonarchTokensProvider('typescript', tokenProvider as monacoTypes.languages.IMonarchLanguage);

Expand Down
9 changes: 6 additions & 3 deletions site/monarch/monarch-playground.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import monaco from '@imlib/monaco-esm';
import { setupTheme } from '../theme.js';
import { rules } from '../token-provider.js';

setupTheme();
setupTheme(rules);

// monaco.languages.typescript.typescriptDefaults.addExtraLib(jsxlib(), `ts:filename/jsx.d.ts`);
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
Expand Down Expand Up @@ -50,8 +51,8 @@ const editor2 = monaco.editor.create(editorContainer2, {
tabSize: 2,
});

editor1.layout({ width: 700, height: 900 });
editor2.layout({ width: 700, height: 900 });
editor1.layout({ width: 700, height: 1250 });
editor2.layout({ width: 700, height: 1250 });

updateTokenProvider();
editor1.onDidChangeModelContent(updateTokenProvider);
Expand All @@ -62,5 +63,7 @@ async function updateTokenProvider() {
const url = URL.createObjectURL(blob);
const mod = await import(url);
URL.revokeObjectURL(url);

monaco.languages.setMonarchTokensProvider('typescript', mod.tokenProvider);
setupTheme(mod.rules);
}
11 changes: 3 additions & 8 deletions site/theme.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import monaco from '@imlib/monaco-esm';
import monacoTypes from 'monaco-editor';

export function setupTheme() {
export function setupTheme(rules: monacoTypes.editor.ITokenThemeRule[]) {
monaco.editor.defineTheme('vsc2', {
base: 'vs-dark',
inherit: true,
rules: [
// { token: "identifier.ts", foreground: "9CDCFE" },
{ token: "property.ts", foreground: "9CDCFE" },
{ token: "function.ts", foreground: "DCDCAA" },
{ token: "method.ts", foreground: "DCDCAA" },
// { token: "delimiter.ts", foreground: "569CD6" },
],
rules: rules,
colors: {
"editor.background": '#1b1f25',
},
Expand Down
8 changes: 8 additions & 0 deletions site/token-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
// vs-dark monaco: https://github.com/microsoft/vscode/blob/main/src/vs/editor/standalone/common/themes.ts#L82
// dark-plus vscode: https://github.com/microsoft/vscode/blob/main/extensions/theme-defaults/themes/dark_plus.json

export const rules = [
// { token: "identifier.ts", foreground: "9CDCFE" },
{ token: "property.ts", foreground: "9CDCFE" },
{ token: "function.ts", foreground: "DCDCAA" },
{ token: "method.ts", foreground: "DCDCAA" },
{ token: "delimiter.ts", foreground: "569CD6" },
];

export const tokenProvider = {

defaultToken: 'invalid',
Expand Down

0 comments on commit 98a3dea

Please sign in to comment.