Skip to content

Commit

Permalink
fix: monaco theme and language definition
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Dec 2, 2022
1 parent dbec9e4 commit 969373d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
58 changes: 45 additions & 13 deletions src/syntax-highlighters/monaco/language-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,44 @@ export const language = <MonacoEditor.languages.IMonarchLanguage>{
keywords: [],
operators: [],

identifiers: new RegExp(/(?:\w|-[a-zA-Z])*/),
identifiers: new RegExp(/(?!self)(?:\w|-[a-zA-Z])*/),

brackets: [
{ open: "[", close: "]", token: OpenFgaDslThemeToken.DELIMITER_BRACKET_TYPE_RESTRICTIONS },
{ open: "(", close: ")", token: OpenFgaDslThemeToken.DELIMITER_BRACKET_RELATION_DEFINITION },
],

typeRestrictionsDelimiters: [
{ open: "[", close: "]", token: OpenFgaDslThemeToken.DELIMITER_BRACKET_TYPE_RESTRICTIONS },
[new RegExp(/,/), OpenFgaDslThemeToken.DELIMITER_BRACKET_RELATION_DEFINITION],
],

tokenizer: {
root: [
{ include: "@whitespace" },

[new RegExp(/[:]/), OpenFgaDslThemeToken.DELIMITER_DEFINE_COLON],
[new RegExp(/[{}[\]()]/), "@brackets"],
[new RegExp(/^(\s*#).*/), OpenFgaDslThemeToken.COMMENT],

[new RegExp(/@[a-zA-Z]\w*/), "tag"],
[
new RegExp(/(\[)(\s*)(@identifiers)(\s*)(\])/),
["@brackets", "@whitespace", OpenFgaDslThemeToken.VALUE_TYPE_RESTRICTIONS_TYPE, "@whitespace", "@brackets"],
],
[
new RegExp(/(,)(\s*)(@identifiers)(\s*)(\])/),
[
OpenFgaDslThemeToken.DELIMITER_COMMA_TYPE_RESTRICTIONS,
"@whitespace",
OpenFgaDslThemeToken.VALUE_TYPE_RESTRICTIONS_TYPE,
"@whitespace",
"@brackets",
],
],
[
new RegExp(/(\[)(\s*)(@identifiers)(\s*)(,)/),
[
"@brackets",
"@whitespace",
"type.type-restrictions.value",
"@whitespace",
OpenFgaDslThemeToken.DELIMITER_COMMA_TYPE_RESTRICTIONS,
],
],
[new RegExp(/[{}[\]()]/), "@brackets"],

[
new RegExp(/(schema)(\s+)(\d\.\d)/),
Expand All @@ -94,6 +112,15 @@ export const language = <MonacoEditor.languages.IMonarchLanguage>{
new RegExp(/(but not)(\s+)(@identifiers)/),
[OpenFgaDslThemeToken.OPERATOR_BUT_NOT, "@whitespace", OpenFgaDslThemeToken.VALUE_RELATION_COMPUTED],
],

[
new RegExp(/(as)(\s+)(@identifiers)/),
[OpenFgaDslThemeToken.KEYWORD_AS, "@whitespace", OpenFgaDslThemeToken.VALUE_RELATION_COMPUTED],
],
[
new RegExp(/(:)(\s+)(@identifiers)/),
[OpenFgaDslThemeToken.DELIMITER_DEFINE_COLON, "@whitespace", OpenFgaDslThemeToken.VALUE_RELATION_COMPUTED],
],
[
new RegExp(/(@identifiers)(\s+)(from)(\s+)(@identifiers)/),
[
Expand All @@ -120,8 +147,11 @@ export const language = <MonacoEditor.languages.IMonarchLanguage>{
OpenFgaDslThemeToken.VALUE_TYPE_RESTRICTIONS_WILDCARD,
],
],
[new RegExp(/,/), OpenFgaDslThemeToken.DELIMITER_COMMA_TYPE_RESTRICTIONS],
[new RegExp(/but\snot/), OpenFgaDslThemeToken.OPERATOR_BUT_NOT],

[":", OpenFgaDslThemeToken.DELIMITER_DEFINE_COLON],
[",", OpenFgaDslThemeToken.DELIMITER_COMMA_TYPE_RESTRICTIONS],
[Keyword.BUT_NOT, OpenFgaDslThemeToken.OPERATOR_BUT_NOT],
[Keyword.SELF, OpenFgaDslThemeToken.KEYWORD_SELF],
[
new RegExp(/@identifiers/),
{
Expand All @@ -133,10 +163,12 @@ export const language = <MonacoEditor.languages.IMonarchLanguage>{
[Keyword.DEFINE]: OpenFgaDslThemeToken.KEYWORD_DEFINE,
[Keyword.FROM]: OpenFgaDslThemeToken.KEYWORD_FROM,
[Keyword.AS]: OpenFgaDslThemeToken.KEYWORD_AS,
[Keyword.SELF]: OpenFgaDslThemeToken.KEYWORD_SELF,
[Keyword.MODEL]: OpenFgaDslThemeToken.KEYWORD_MODEL,
[Keyword.SCHEMA]: { token: OpenFgaDslThemeToken.KEYWORD_SCHEMA },
"@default": "identifier",
// TODO: This should be "identifier", however because tupleset was not properly
// detected with the rules above, this is the quickiest hacky fix we can do to
// get it out there for people to use
"@default": OpenFgaDslThemeToken.VALUE_RELATION_TUPLE_TO_USERSET_TUPLESET,
},
},
],
Expand Down
7 changes: 1 addition & 6 deletions src/syntax-highlighters/monaco/theme.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { editor } from "monaco-editor";

import { LANGUAGE_NAME } from "../../constants";
import {
OpenFgaDslThemeToken,
OpenFgaThemeConfiguration,
SupportedTheme,
supportedThemes,
} from "../../theme";
import { OpenFgaDslThemeToken, OpenFgaThemeConfiguration, SupportedTheme, supportedThemes } from "../../theme";
import { getThemeTokenStyle } from "../../theme/utils";

function buildMonacoTheme(themeConfig: OpenFgaThemeConfiguration): editor.IStandaloneThemeData {
Expand Down
7 changes: 3 additions & 4 deletions src/syntax-highlighters/prism/language-definition.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { OpenFgaDslThemeTokenType } from "../../theme";

export const languageDefinition = {
[OpenFgaDslThemeTokenType.DEFAULT]: {},
[OpenFgaDslThemeTokenType.COMMENT]: {
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true,
},
[OpenFgaDslThemeTokenType.KEYWORD]: /\b(type|relations|define|and|or|but not|from|as|model|schema)\b/,
[OpenFgaDslThemeTokenType.TYPE]: {
pattern: /(\b(?:type)\s+)[\w.\\]+/
pattern: /(\b(?:type)\s+)(\w)+/,
},
[OpenFgaDslThemeTokenType.RELATION]: {
pattern: /(\b(?:relation)\s+)[\w.\\]+/
pattern: /(\b(?:relation)\s+)(\w)+/,
},
[OpenFgaDslThemeTokenType.DIRECTLY_ASSIGNABLE]: /\bself|(\[.*\])\b/,
[OpenFgaDslThemeTokenType.DIRECTLY_ASSIGNABLE]: /\b\[.*\]\b/,
};
4 changes: 2 additions & 2 deletions src/theme/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export function getThemeTokenStyle(
token: OpenFgaDslThemeToken,
themeConfig: OpenFgaThemeConfiguration,
): {
color?: string;
foreground?: string;
fontStyle?: string;
} {
return {
color:
foreground:
themeConfig.rawColorOverrides?.[token] ||
themeConfig.colors[tokenTypeMap[token || OpenFgaDslThemeTokenType.DEFAULT]],
fontStyle:
Expand Down

0 comments on commit 969373d

Please sign in to comment.