-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathstateUtils.js
More file actions
471 lines (408 loc) · 11.8 KB
/
stateUtils.js
File metadata and controls
471 lines (408 loc) · 11.8 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import { EditorState, Compartment } from '@codemirror/state';
import {
EditorView,
lineNumbers as lineNumbersExt,
highlightActiveLine,
highlightActiveLineGutter,
gutters,
keymap,
highlightSpecialChars,
drawSelection,
dropCursor,
rectangularSelection,
crosshairCursor
} from '@codemirror/view';
import {
foldGutter,
foldKeymap,
bracketMatching,
indentOnInput,
syntaxHighlighting
} from '@codemirror/language';
import {
autocompletion,
closeBrackets,
closeBracketsKeymap
} from '@codemirror/autocomplete';
import {
highlightSelectionMatches,
search,
searchKeymap
} from '@codemirror/search';
import {
defaultKeymap,
history,
historyKeymap,
insertTab,
indentLess
} from '@codemirror/commands';
import { lintGutter } from '@codemirror/lint';
import {
expandAbbreviation,
abbreviationTracker
} from '@emmetio/codemirror6-plugin';
import { css } from '@codemirror/lang-css';
import { html } from '@codemirror/lang-html';
import { json } from '@codemirror/lang-json';
import { xml } from '@codemirror/lang-xml';
import { linter } from '@codemirror/lint';
import { JSHINT } from 'jshint';
import { HTMLHint } from 'htmlhint';
import { CSSLint } from 'csslint';
import { emmetConfig } from '@emmetio/codemirror6-plugin';
import { color as colorPicker } from '@connieye/codemirror-color-picker';
import p5JavaScript from './p5JavaScript';
import { tidyCodeWithPrettier } from './tidier';
import { highlightStyle } from './highlightStyle';
import { errorDecorationStateField } from './consoleErrorDecoration';
// ----- TODOS -----
// - JSON linter
// - shader syntax highlighting
// - add docstrings for all exported functions
/** Detects what mode the file is based on the name. */
export function getFileMode(fileName) {
let mode;
if (fileName.match(/.+\.js$/i)) {
mode = 'javascript';
} else if (fileName.match(/.+\.css$/i)) {
mode = 'css';
} else if (fileName.match(/.+\.(html)$/i)) {
mode = 'html';
} else if (fileName.match(/.+\.(xml)$/i)) {
mode = 'xml';
} else if (fileName.match(/.+\.json$/i)) {
mode = 'application/json';
} else if (fileName.match(/.+\.(frag|glsl)$/i)) {
mode = 'x-shader/x-fragment';
} else if (fileName.match(/.+\.(vert|stl|mtl)$/i)) {
mode = 'x-shader/x-vertex';
} else {
mode = 'text/plain';
}
return mode;
}
function getFileLanguage(fileName) {
const fileMode = getFileMode(fileName);
switch (fileMode) {
case 'javascript':
return p5JavaScript;
case 'css':
return css;
case 'html':
return html;
case 'xml':
return xml;
case 'application/json':
return json;
default:
return null;
}
}
function makeCssLinter(callback) {
return (view) => {
const documentContent = view.state.doc.toString();
const { messages } = CSSLint.verify(documentContent, {});
const diagnostics = [];
messages.forEach((message) => {
if (!message) return;
const {
line: messageLine,
col: messageCharacter,
type: messageType,
message: messageText
} = message;
const cmLine = view.state.doc.line(messageLine);
// TODO: Can we to do the to/from smarter?
diagnostics.push({
from: cmLine.from + messageCharacter - 1,
to: cmLine.from + messageCharacter,
severity: messageType,
message: messageText
});
});
if (callback) callback(diagnostics);
return diagnostics;
};
}
// https://github.com/codemirror/codemirror5/blob/master/addon/lint/html-lint.js
const HTMLHINT_OPTIONS = {
'tagname-lowercase': true,
'attr-lowercase': true,
'attr-value-double-quotes': true,
'doctype-first': false,
'tag-pair': true,
'spec-char-escape': true,
'id-unique': true,
'src-not-empty': true,
'attr-no-duplication': true
};
function makeHtmlLinter(callback) {
return (view) => {
const documentContent = view.state.doc.toString();
const messages = HTMLHint.verify(documentContent, HTMLHINT_OPTIONS) || [];
const diagnostics = [];
messages.forEach((message) => {
if (!message) return;
const {
line: messageLine,
col: messageCharacter,
type: messageType,
message: messageText
} = message;
const cmLine = view.state.doc.line(messageLine);
// TODO: Can we to do the to/from smarter?
diagnostics.push({
from: cmLine.from + messageCharacter - 1,
to: cmLine.from + messageCharacter,
severity: messageType,
message: messageText
});
});
if (callback) callback(diagnostics);
return diagnostics;
};
}
const JSHINT_OPTIONS = {
asi: true,
eqeqeq: false,
'-W041': false,
esversion: 11
};
// TODO: Consider using ESLINT instead
function makeJsLinter(callback) {
return (view) => {
const documentContent = view.state.doc.toString();
// Run JSHINT
JSHINT(documentContent, JSHINT_OPTIONS);
const { errors } = JSHINT;
// Return errors
const diagnostics = [];
errors.forEach((error) => {
if (!error) return;
const { line: errorLine, character: errorCharacter, evidence } = error;
const cmLine = view.state.doc.line(errorLine);
// https://github.com/codemirror/codemirror5/blob/master/addon/lint/javascript-lint.js
const start = errorCharacter - 1;
let end = start + 1;
if (evidence) {
const index = evidence.substring(start).search(/.\b/);
if (index > -1) {
end += index;
}
}
diagnostics.push({
from: cmLine.from + start,
to: cmLine.from + end,
severity: error.code.startsWith('W') ? 'warning' : 'error',
message: error.reason
});
});
if (callback) callback(diagnostics);
return diagnostics;
};
}
function getFileLinter(fileName, callback) {
const fileMode = getFileMode(fileName);
switch (fileMode) {
case 'javascript':
return linter(makeJsLinter(callback));
case 'html':
return linter(makeHtmlLinter(callback));
case 'css':
return linter(makeCssLinter(callback));
default:
return null;
}
}
function getFileEmmetConfig(fileName) {
const fileMode = getFileMode(fileName);
switch (fileMode) {
case 'html':
return emmetConfig.of({ syntax: 'html' });
case 'css':
return emmetConfig.of({ syntax: 'css' });
default:
return null;
}
}
function getColorPickerAtSelection(view) {
const { head } = view.state.selection.main;
const { node } = view.domAtPos(head);
const startEl =
node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
const lineEl = startEl?.closest('.cm-line');
return (
lineEl?.querySelector('input[type="color"]:not(:disabled)') ||
view.contentDOM.querySelector('input[type="color"]:not(:disabled)')
);
}
function openColorPickerWithKeyboard(view) {
const picker = getColorPickerAtSelection(view);
if (!picker || picker.disabled) {
return false;
}
picker.focus();
if (typeof picker.showPicker === 'function') {
picker.showPicker();
} else {
picker.click();
}
return true;
}
// Extra custom keymaps.
// TODO: We need to add sublime mappings + other missing extra mappings here.
const extraKeymaps = [{ key: 'Tab', run: insertTab, shift: indentLess }];
const emmetKeymaps = [{ key: 'Tab', run: expandAbbreviation }];
export const AUTOCOMPLETE_OPTIONS = {
tooltipClass: () => 'CodeMirror-hints',
optionClass: () => 'CodeMirror-hint',
closeOnBlur: false
};
/**
* Creates a new CodeMirror editor state with configurations,
* extensions, and keymaps tailored to the file type and settings.
*
* Returns a "file state" object containing the CodeMirror state and compartments.
*/
export function createNewFileState(filename, document, settings) {
const {
linewrap,
lineNumbers,
autocomplete,
autocloseBracketsQuotes,
onUpdateLinting,
onViewUpdate
} = settings;
const lineNumbersCpt = new Compartment();
const lineWrappingCpt = new Compartment();
const closeBracketsCpt = new Compartment();
const autocompleteCpt = new Compartment();
// Depending on the file mode, we have a different tidier function.
const mode = getFileMode(filename);
const fileExtraKeymaps = [...extraKeymaps];
fileExtraKeymaps.push({
key: 'Mod-k',
run: (view) => {
if (mode !== 'css') {
return false;
}
return openColorPickerWithKeyboard(view);
}
});
fileExtraKeymaps.push({
key: `Shift-Mod-F`,
run: (cmView) => tidyCodeWithPrettier(cmView, mode)
});
const keymaps = [
fileExtraKeymaps,
closeBracketsKeymap,
defaultKeymap,
historyKeymap,
foldKeymap,
searchKeymap
];
// https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts
const extensions = [
// The first few extensions can be toggled on or off.
lineNumbersCpt.of(lineNumbers ? lineNumbersExt() : []),
lineWrappingCpt.of(linewrap ? EditorView.lineWrapping : []),
closeBracketsCpt.of(autocloseBracketsQuotes ? closeBrackets() : []),
autocompleteCpt.of(
autocomplete ? autocompletion(AUTOCOMPLETE_OPTIONS) : []
),
// Everything below here should always be on.
history(),
search(),
// Highlight extensions
highlightActiveLine(),
highlightActiveLineGutter(),
highlightSpecialChars(),
highlightSelectionMatches(),
syntaxHighlighting(highlightStyle),
// Selection extensions
drawSelection(),
rectangularSelection(),
dropCursor(),
crosshairCursor(),
EditorState.allowMultipleSelections.of(true),
// Gutter extensions
gutters({ fixed: false }),
foldGutter(),
// Misc extensions
indentOnInput(),
bracketMatching(),
errorDecorationStateField,
// Setup the event listeners on the CodeMirror instance.
EditorView.updateListener.of(onViewUpdate)
];
// Only enable the color picker for Javascript and CSS, which
// have both been tested.
const fileMode = getFileMode(filename);
if (fileMode === 'javascript' || fileMode === 'css') {
extensions.push(colorPicker);
}
const fileLanguage = getFileLanguage(filename);
const fileLinter = getFileLinter(filename, onUpdateLinting);
const fileEmmetConfig = getFileEmmetConfig(filename);
if (fileLanguage) {
extensions.push(fileLanguage());
}
if (fileLinter) {
extensions.push(fileLinter);
extensions.push(lintGutter());
}
// If it's HTML or CSS, we add some emmet-specific configs.
if (fileEmmetConfig) {
extensions.push(fileEmmetConfig);
extensions.push(abbreviationTracker());
keymaps.push(emmetKeymaps);
}
// Now add the keymaps...
extensions.push(keymap.of(keymaps.flat()));
// Create the state with document content if we have it.
const stateOptions = {
extensions
};
if (document) {
stateOptions.doc = document;
}
const cmState = EditorState.create(stateOptions);
return {
cmState,
lineNumbersCpt,
lineWrappingCpt,
closeBracketsCpt,
autocompleteCpt
};
}
/**
* Given a reconfigure effect, this function will update all
* of the file states.
*
* We need to do this whenever the settings like line numbers
* change, so it will get called in the useEffect hooks.
*/
export function updateFileStates({
fileStates,
cmView,
file: currentFile,
reconfigureEffect
}) {
if (!fileStates) return;
Object.entries(fileStates).forEach(([fileId, fileState]) => {
// Either grab the current state from the view or saved in the fileStates.
let { cmState } = fileState;
if (fileId === currentFile.id) {
cmState = cmView.state;
}
// Apply the new effects and grab the new state.
const { state: newCmState } = cmState.update({
effects: reconfigureEffect(fileState)
});
// Save the new states and update the view for the currently open file.
fileStates[fileId].cmState = newCmState;
if (fileId === currentFile.id) {
cmView.setState(newCmState);
}
});
}