-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathneo4jSetup.tsx
197 lines (174 loc) · 4.77 KB
/
neo4jSetup.tsx
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
import {
acceptCompletion,
autocompletion,
clearSnippet,
closeBrackets,
closeBracketsKeymap,
closeCompletion,
completionKeymap,
nextSnippetField,
prevSnippetField,
snippetKeymap,
} from '@codemirror/autocomplete';
import {
defaultKeymap,
history,
historyKeymap,
indentLess,
indentMore,
} from '@codemirror/commands';
import {
bracketMatching,
defaultHighlightStyle,
foldKeymap,
indentOnInput,
syntaxHighlighting,
} from '@codemirror/language';
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
import { EditorState, Extension, StateCommand } from '@codemirror/state';
import {
Command,
crosshairCursor,
drawSelection,
dropCursor,
EditorView,
highlightSpecialChars,
KeyBinding,
keymap,
rectangularSelection,
} from '@codemirror/view';
import { lintKeymap } from '@codemirror/lint';
import { getIconForType } from './icons';
import resizableEditor from './resizableEditor';
const insertTab: StateCommand = (cmd) => {
// if there is a selection we should indent the selected text, but if not insert
// two spaces as per the cypher style guide
if (cmd.state.selection.main.from === cmd.state.selection.main.to) {
cmd.dispatch(
cmd.state.update({
changes: {
from: cmd.state.selection.main.to,
to: cmd.state.selection.main.to,
insert: ' ',
},
selection: { anchor: cmd.state.selection.main.to + 2 },
}),
);
} else {
indentMore(cmd);
}
return true;
};
type SetupProps = {
moveFocusOnTab?: boolean;
resizeable?: boolean;
};
export const basicNeo4jSetup = ({
moveFocusOnTab = false,
resizeable = false
}: SetupProps): Extension[] => {
const keymaps: KeyBinding[] = [
closeBracketsKeymap,
defaultKeymap,
searchKeymap,
historyKeymap,
foldKeymap,
completionKeymap,
lintKeymap,
].flat();
if (!moveFocusOnTab) {
keymaps.push(
{
key: 'Tab',
preventDefault: true,
run: acceptCompletion,
},
{
key: 'Tab',
preventDefault: true,
run: insertTab,
},
{
key: 'Shift-Tab',
preventDefault: true,
run: indentLess,
},
);
}
const extensions: Extension[] = [];
extensions.push(highlightSpecialChars());
extensions.push(history());
extensions.push(drawSelection());
extensions.push(dropCursor());
extensions.push(EditorState.allowMultipleSelections.of(true));
extensions.push(indentOnInput());
extensions.push(
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
);
extensions.push(bracketMatching());
extensions.push(closeBrackets());
extensions.push(
autocompletion({
icons: false,
interactionDelay: 5,
addToOptions: [
{
render(completion, state) {
const isDarkTheme = state.facet(EditorView.darkTheme);
const icon = document.createElement('span');
icon.innerHTML = getIconForType(completion.type, isDarkTheme);
const svgElement = icon.children[0] as SVGElement;
svgElement.style.display = 'inline';
svgElement.style.marginRight = '5px';
return icon;
},
position: 20,
},
],
}),
);
extensions.push(rectangularSelection());
extensions.push(crosshairCursor());
extensions.push(highlightSelectionMatches());
extensions.push(keymap.of(keymaps));
extensions.push(
snippetKeymap.of([
{
key: 'Tab',
run: acceptCompletionOrGotoNextSnippet,
shift: acceptCompletionOrGotoPrevSnippet,
},
{ key: 'Escape', run: closeCompletionsOrClearSnippets },
]),
);
if (resizeable) {
extensions.push(resizableEditor())
}
return extensions;
};
// The logic to check if there's a completion open is surprisingly complex
// https://github.com/codemirror/autocomplete/blob/5ad2ebc861f2f61cdc943fc087a5bfb756a7d0fa/src/view.ts#L31
// For example it respects an interaction delay, so we can't just check if the completion is open
// instead we just run the acceptCompletion command which returns true if a completion was accepted
// in that case we know that we shouldn't move to the next snippet field
const acceptCompletionOrGotoNextSnippet: Command = (view: EditorView) => {
const didCompletion = acceptCompletion(view);
if (didCompletion) {
return true;
}
return nextSnippetField(view);
};
const acceptCompletionOrGotoPrevSnippet: Command = (view: EditorView) => {
const didCompletion = acceptCompletion(view);
if (didCompletion) {
return true;
}
return prevSnippetField(view);
};
const closeCompletionsOrClearSnippets: Command = (view: EditorView) => {
const closedCompletions = closeCompletion(view);
if (closedCompletions) {
return true;
}
return clearSnippet(view);
};