Skip to content

Commit 3cf505c

Browse files
committed
monaco-1.0.0 - new monaco plugin
1 parent 60f0d4b commit 3cf505c

21 files changed

+7654
-294
lines changed

docs/package-lock.json

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"@mdx-js/react": "3.1.0",
2121
"@monaco-editor/react": "4.6.0",
2222
"@nlighten/json-transform": "^1.3.3",
23-
"@nlighten/json-transform-core": "^1.3.4",
23+
"@nlighten/json-transform-core": "^1.5.0",
24+
"@nlighten/monaco-json-transform": "^1.0.0",
2425
"buffer": "^6.0.3",
2526
"clsx": "2.1.0",
2627
"crypto-browserify": "^3.12.1",

docs/src/components/TransformerTester.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {useEffect, useMemo, useState} from "react";
22
import { } from "@docusaurus/BrowserOnly"
33
import { JSONSchemaUtils } from '@nlighten/json-schema-utils'
4-
import {jsonpathJoin} from "@nlighten/json-transform-core";
4+
import {ContextVariablesSchemas, jsonpathJoin} from "@nlighten/json-transform-core";
55
import {DebuggableTransformerFunctions, JsonTransformer} from "@nlighten/json-transform";
66
import {useLocation, useHistory} from "@docusaurus/router";
77
import MonacoEditor from "./monaco/MonacoEditor";
@@ -71,15 +71,15 @@ const TransformerTester = () => {
7171
const generatedSchema = JSONSchemaUtils.generate(parsedInput);
7272
const result = JSONSchemaUtils.parse(generatedSchema);
7373
setSuggestions("/transformer",
74-
["#now", "#null", "#uuid"].concat(
74+
Object.keys(ContextVariablesSchemas).concat(
7575
result?.paths.map(x => jsonpathJoin("$", x.$path)) ?? []
7676
),
7777
result?.paths.reduce((a, c) => {
7878
a[jsonpathJoin("$", c.$path)] = c;
7979
return a;
8080
}, {} as any),)
8181
} else {
82-
setSuggestions("/transformer", ["#now", "#null", "#uuid"], {});
82+
setSuggestions("/transformer", Object.keys(ContextVariablesSchemas), {});
8383
}
8484
}, [parsedInput]);
8585

docs/src/components/monaco/Monaco.init.ts

Lines changed: 25 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { loader, type Monaco } from "@monaco-editor/react";
21
import * as monaco from 'monaco-editor';
3-
import type { IRange, languages } from "monaco-editor";
4-
import { functionsParser, getFunctionInlineSignature, getFunctionObjectSignature } from "@nlighten/json-transform-core";
5-
import {formatSchemaType} from "@nlighten/json-schema-utils";
6-
import jsonVariablesTokensProvider from "./jsonVariablesTokensProvider";
7-
import registerHoverProvider from "./jsonHoverProvider";
8-
import {getSuggestions, resolveSuggestions} from "./suggestionsProvider";
2+
import { loader, type Monaco } from "@monaco-editor/react";
3+
import {getSuggestions} from "./suggestionsProvider";
4+
5+
import {
6+
registerJsonTransformItemCompletionProvider,
7+
registerJsonTransformDSTProvider,
8+
registerJsonTransformHoverProvider,
9+
defineThemeVsDarkCustom
10+
} from "@nlighten/monaco-json-transform"
911

1012
const glob = window as unknown as { monaco: Monaco };
1113

@@ -34,116 +36,29 @@ export const getModel = (value: string, uri: string, language = "json", path?: s
3436
};
3537

3638
const initMonaco = (monaco: Monaco) => {
37-
jsonVariablesTokensProvider(monaco);
38-
39-
// add suggestions
40-
const jsonVariableMapper = (v: any, paths: any, range: IRange, inline: boolean): languages.CompletionItem => {
41-
let label = v[0] === "<" ? v.replace(/^<[^>]*>/, "") : v;
42-
let insertText: string = label.replace(/"/g, '\\"');
43-
let insertTextRules: any = undefined;
44-
let documentation: string | undefined = undefined;
45-
let detail = paths?.[v]
46-
? `(${paths[v].type}${paths[v].format ? ":" + paths[v].format : ""})${
47-
paths[v].description ? " " + paths[v].description : ""
48-
}`
49-
: undefined;
50-
let kind = monaco.languages.CompletionItemKind.Field;
51-
let tags: any = undefined;
52-
if (inline && v[0] === "$" && v[1] === "$") {
53-
const inlineFunction = functionsParser.get(v.substring(2));
54-
if (inlineFunction) {
55-
if (inlineFunction.deprecated) {
56-
tags = [monaco.languages.CompletionItemTag.Deprecated];
57-
}
58-
let counter = 1;
59-
label = v + " (inline)";
60-
61-
insertText = getFunctionInlineSignature(v.substring(2), inlineFunction, true).replace("$$", "\\$\\$"); // escape the first $$
62-
if (insertText.includes("{")) {
63-
insertText = insertText.replace(/{/g, () => `$\{${counter++}:`);
64-
}
65-
insertText += `:$\{${counter}:input}`;
66-
insertTextRules = monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet;
67-
kind = monaco.languages.CompletionItemKind.Function;
68-
documentation = inlineFunction.description;
69-
}
70-
} else if (!inline && v[0] === "$" && v[1] === "$") {
71-
const objectFunction = functionsParser.get(v.substring(2));
72-
if (objectFunction) {
73-
let counter = 1;
74-
label = v + " (object)";
75-
insertText = getFunctionObjectSignature(v.substring(2), objectFunction);
76-
insertText = insertText.replace(/\{\w+}/g, m => `$\{${counter++}:"${m.substring(1)}"`);
77-
insertTextRules = monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet;
78-
kind = monaco.languages.CompletionItemKind.Module;
79-
detail = objectFunction.outputSchema
80-
? formatSchemaType(objectFunction.outputSchema)
81-
: objectFunction.description;
82-
documentation = objectFunction.description;
83-
}
84-
}
85-
return {
86-
label,
87-
kind,
88-
insertText,
89-
insertTextRules,
90-
range,
91-
detail,
92-
documentation,
93-
tags,
94-
};
95-
};
96-
const functionSuggestions = functionsParser.getNames()
97-
.filter(x => !functionsParser.get(x).deprecated)
98-
.map(x => `$$${x}`);
39+
registerJsonTransformDSTProvider(monaco);
9940

100-
monaco.languages.registerCompletionItemProvider("json", {
101-
provideCompletionItems: (model, position) => {
41+
registerJsonTransformItemCompletionProvider(monaco, {
42+
getTypeMap: (model) => {
10243
const path = model.uri.path.replace(/\.\w+$/, "");
103-
const [suggestions, paths] = getSuggestions(path);
104-
const word = model.getWordUntilPosition(position);
105-
const range = {
106-
startLineNumber: position.lineNumber,
107-
endLineNumber: position.lineNumber,
108-
startColumn: word.startColumn,
109-
endColumn: word.endColumn,
110-
};
111-
// find out if we are inside a string or after a key inside an object
112-
// we count (") before position on same line
113-
// if even number then we are not in a string, otherwise a string (this is not precise, could have been escaped)
114-
const quotesCount =
115-
model
116-
.getLineContent(position.lineNumber)
117-
.substring(0, position.column - 1)
118-
.match(/^"|[^\\]"/g)?.length ?? 0; // ignore escaped double quotes
119-
const inline = quotesCount % 2 !== 0; // if inside a string, suggest inline functions otherwise object functions
120-
return {
121-
suggestions: suggestions.concat(functionSuggestions).map(s => jsonVariableMapper(s, paths, range, inline)),
122-
};
44+
const [, paths] = getSuggestions(path);
45+
return paths;
12346
},
47+
getSuggestions: (model) => {
48+
const path = model.uri.path.replace(/\.\w+$/, "");
49+
return getSuggestions(path)[0];
50+
}
12451
});
12552

126-
monaco.editor.registerCommand("docs", function (accessor: any, arg: any) {
127-
window.open(functionsParser.resolveDocsUrl(arg.func, arg.type), "_blank");
53+
registerJsonTransformHoverProvider(monaco, {
54+
getTypeMap: (model) => {
55+
const path = model.uri.path.replace(/\.\w+$/, "");
56+
console.log('looking for: ' + path);
57+
return getSuggestions(path, true)[1];
58+
}
12859
});
12960

130-
registerHoverProvider(monaco, { resolveSuggestions });
131-
132-
monaco.editor.defineTheme("vs-dark-custom", {
133-
base: "vs-dark",
134-
colors: {},
135-
inherit: true,
136-
rules: [
137-
{ token: "variable", foreground: "#80ff80".substring(1) },
138-
{ token: "variable_deprecated", foreground: "#80ff80".substring(1), fontStyle: "strikethrough" },
139-
{ token: "member", foreground: "#30ffee".substring(1) },
140-
{ token: "context", foreground: "#d7a6ff".substring(1) },
141-
{ token: "annotation", foreground: "#ff8080".substring(1) },
142-
{ token: "function", foreground: "#ffff80".substring(1) },
143-
{ token: "function_context", foreground: "#7d7df3".substring(1) },
144-
{ token: "function_deprecated", foreground: "#ffff80".substring(1), fontStyle: "strikethrough" },
145-
],
146-
});
61+
defineThemeVsDarkCustom(monaco);
14762
};
14863

14964
loader.config({ monaco })

docs/src/components/monaco/jsonHoverProvider.ts

Lines changed: 0 additions & 99 deletions
This file was deleted.

docs/src/components/monaco/jsonVariablesTokensProvider.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)