-
-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathConcertoEditor.tsx
More file actions
196 lines (178 loc) · 5.96 KB
/
ConcertoEditor.tsx
File metadata and controls
196 lines (178 loc) · 5.96 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
import { useMonaco } from "@monaco-editor/react";
import { lazy, Suspense, useEffect, useMemo, useRef } from "react";
import * as monaco from "monaco-editor";
import useAppStore from "../store/store";
import { shallow } from "zustand/shallow";
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"],
[/"/, "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 editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null);
const decorationsCollectionRef = useRef<monaco.editor.IEditorDecorationsCollection | null>(null);
const { error, backgroundColor, aiConfig, showLineNumbers } = useAppStore(
(state) => ({
error: state.error,
backgroundColor: state.backgroundColor,
aiConfig: state.aiConfig,
showLineNumbers: state.showLineNumbers,
}),
shallow
);
const themeName = useMemo(
() => (backgroundColor === "#121212" ? "darkTheme" : "lightTheme"),
[backgroundColor]
);
const options: monaco.editor.IStandaloneEditorConstructionOptions = useMemo(() => ({
minimap: { enabled: false },
wordWrap: "on",
automaticLayout: true,
scrollBeyondLastLine: false,
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",
}), [showLineNumbers, aiConfig?.enableInlineSuggestions]);
const handleEditorDidMount = (editor: monaco.editor.IStandaloneCodeEditor) => {
editorRef.current = editor;
decorationsCollectionRef.current = editor.createDecorationsCollection();
editor.onDidChangeCursorSelection(() => {
handleSelection(editor);
});
};
useEffect(() => {
if (!monacoInstance || !editorRef.current) return;
const model = editorRef.current.getModel();
if (!model) return;
if (error) {
const match = error.match(/Line (\d+)(?::| )Col(?:umn)? (\d+)/i);
if (match) {
const line = parseInt(match[1], 10);
const col = parseInt(match[2], 10);
monacoInstance.editor.setModelMarkers(model, "customMarker", [{
startLineNumber: line,
startColumn: col,
endLineNumber: line,
endColumn: model.getLineMaxColumn(line),
message: error,
severity: monaco.MarkerSeverity.Error,
}]);
decorationsCollectionRef.current?.set([
{
range: new monaco.Range(line, 1, line, 1),
options: {
isWholeLine: true,
className: 'errorLineHighlight',
}
}
]);
}
} else {
monacoInstance.editor.setModelMarkers(model, "customMarker", []);
decorationsCollectionRef.current?.clear();
}
}, [error, 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={(val) => onChange?.(val)}
theme={themeName}
/>
</Suspense>
{MenuComponent}
</div>
);
}