-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathConcertoEditor.tsx
More file actions
224 lines (207 loc) · 5.89 KB
/
ConcertoEditor.tsx
File metadata and controls
224 lines (207 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { useMonaco } from "@monaco-editor/react";
import { lazy, Suspense, useCallback, useEffect, useMemo } from "react";
import * as monaco from "monaco-editor";
import useAppStore from "../store/store";
import { useCodeSelection } from "../components/CodeSelectionMenu";
import { registerAutocompletion } from "../ai-assistant/autocompletion";
const MonacoEditor = lazy(() =>
import("@monaco-editor/react").then((mod) => ({ default: mod.Editor }))
);
const concertoKeywords = [
"map",
"concept",
"from",
"optional",
"default",
"range",
"regex",
"length",
"abstract",
"namespace",
"import",
"enum",
"scalar",
"extends",
"default",
"participant",
"asset",
"o",
"identified by",
"transaction",
"event",
];
const concertoTypes = [
"String",
"Integer",
"Double",
"DateTime",
"Long",
"Boolean",
];
const handleEditorWillMount = (monacoInstance: typeof monaco) => {
monacoInstance.languages.register({
id: "concerto",
extensions: [".cto"],
aliases: ["Concerto", "concerto"],
mimetypes: ["application/vnd.accordproject.concerto"],
});
monacoInstance.languages.setLanguageConfiguration("concerto", {
brackets: [
["{", "}"],
["[", "]"],
["(", ")"],
],
autoClosingPairs: [
{ open: "{", close: "}" },
{ open: "[", close: "]" },
{ open: "(", close: ")" },
{ open: "\"", close: "\"" },
],
surroundingPairs: [
{ open: "{", close: "}" },
{ open: "[", close: "]" },
{ open: "(", close: ")" },
{ open: "\"", close: "\"" },
],
});
monacoInstance.languages.setMonarchTokensProvider("concerto", {
keywords: concertoKeywords,
typeKeywords: concertoTypes,
operators: ["=", "{", "}", "@", '"'],
symbols: /[=}{@"]+/,
escapes: /\\(?:[btnfru"'\\]|\\u[0-9A-Fa-f]{4})/,
tokenizer: {
root: [
{ include: "@whitespace" },
[
/[a-zA-Z_]\w*/,
{
cases: {
"@keywords": "keyword",
"@typeKeywords": "type",
"@default": "identifier",
},
},
],
[/"([^"\\]|\\.)*$/, "string.invalid"], // non-terminated string
[/"/, "string", "@string"],
],
string: [
[/[^\\"]+/, "string"],
[/@escapes/, "string.escape"],
[/\\./, "string.escape.invalid"],
[/"/, "string", "@pop"],
],
whitespace: [
[/\s+/, "white"],
[/(\/\/.*)/, "comment"],
],
},
});
if (monacoInstance) {
registerAutocompletion('concerto', monacoInstance);
}
};
interface ConcertoEditorProps {
value: string;
onChange?: (value: string | undefined) => void;
}
export default function ConcertoEditor({
value,
onChange,
}: ConcertoEditorProps) {
const { handleSelection, MenuComponent } = useCodeSelection("concerto");
const monacoInstance = useMonaco();
const { error, backgroundColor, aiConfig, showLineNumbers, editorFontSize, editorWordWrap } = useAppStore((state) => ({
error: state.error,
backgroundColor: state.backgroundColor,
aiConfig: state.aiConfig,
showLineNumbers: state.showLineNumbers,
editorFontSize: state.editorFontSize,
editorWordWrap: state.editorWordWrap,
}));
const ctoErr = error?.startsWith("c:") ? error : undefined;
const themeName = useMemo(
() => (backgroundColor ? "darkTheme" : "lightTheme"),
[backgroundColor]
);
const options: monaco.editor.IStandaloneEditorConstructionOptions = useMemo(() => ({
minimap: { enabled: false },
wordWrap: editorWordWrap ? "on" as const : "off" as const,
automaticLayout: true,
scrollBeyondLastLine: false,
fontSize: editorFontSize,
lineNumbers: showLineNumbers ? 'on' : 'off',
autoClosingBrackets: "languageDefined",
autoSurround: "languageDefined",
bracketPairColorization: { enabled: true },
inlineSuggest: {
enabled: aiConfig?.enableInlineSuggestions !== false,
mode: "prefix",
suppressSuggestions: false,
fontFamily: "inherit",
keepOnBlur: true,
},
suggest: {
preview: true,
showInlineDetails: true,
},
quickSuggestions: false,
suggestOnTriggerCharacters: false,
acceptSuggestionOnCommitCharacter: false,
acceptSuggestionOnEnter: "off",
tabCompletion: "off",
}), [aiConfig?.enableInlineSuggestions, showLineNumbers, editorFontSize, editorWordWrap]);
const handleEditorDidMount = (editor: monaco.editor.IStandaloneCodeEditor) => {
editor.onDidChangeCursorSelection(() => {
handleSelection(editor);
});
};
const handleChange = useCallback(
(val: string | undefined) => {
if (onChange) onChange(val);
},
[onChange]
);
useEffect(() => {
if (!monacoInstance) return;
const model = monacoInstance.editor.getModels()?.[0];
if (!model) return;
if (ctoErr) {
const match = ctoErr.match(/Line (\d+) column (\d+)/);
if (match) {
const lineNumber = parseInt(match[1], 10);
const columnNumber = parseInt(match[2], 10);
monacoInstance.editor.setModelMarkers(model, "customMarker", [
{
startLineNumber: lineNumber,
startColumn: columnNumber - 1,
endLineNumber: lineNumber,
endColumn: columnNumber + 1,
message: ctoErr,
severity: monaco.MarkerSeverity.Error,
},
]);
}
} else {
monacoInstance.editor.setModelMarkers(model, "customMarker", []);
}
}, [ctoErr, monacoInstance]);
return (
<div className="editorwrapper h-full w-full">
<Suspense fallback={<div>Loading Editor...</div>}>
<MonacoEditor
options={options}
language="concerto"
height="100%"
value={value}
beforeMount={handleEditorWillMount}
onMount={handleEditorDidMount}
onChange={handleChange}
theme={themeName}
/>
</Suspense>
{MenuComponent}
</div>
);
}