Skip to content

Commit c8df0c8

Browse files
authored
Linting
1 parent 1efd350 commit c8df0c8

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

‎Build/src/editor.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Compartment, EditorState, Extension } from "@codemirror/state";
22
import { EditorView, keymap, lineNumbers } from "@codemirror/view";
3-
import { defaultKeymap, standardKeymap, toggleComment } from "@codemirror/commands";
3+
import { defaultKeymap, toggleComment } from "@codemirror/commands";
44
import { html } from "@codemirror/lang-html";
55
import { css } from "@codemirror/lang-css";
66
import { javascript } from "@codemirror/lang-javascript";
77
import { autocompletion } from "@codemirror/autocomplete";
8-
import { foldGutter, foldKeymap } from "@codemirror/language";
8+
import { foldGutter, foldKeymap, syntaxTree } from "@codemirror/language";
9+
import { linter, lintGutter } from "@codemirror/lint";
910
import { formatCode } from "./runner";
1011
import { monokai } from "@uiw/codemirror-theme-monokai";
1112
import { bbedit } from "@uiw/codemirror-theme-bbedit";
@@ -65,6 +66,28 @@ function createEditorConfig(
6566
extensions: [
6667
lineNumbers(),
6768
foldGutter(),
69+
// Linting: use syntax tree to surface parse errors from the language parser
70+
linter((view) => {
71+
const diags: any[] = [];
72+
try {
73+
syntaxTree(view.state).iterate({
74+
enter: (node) => {
75+
if (node.type.isError) {
76+
diags.push({
77+
from: node.from,
78+
to: node.to,
79+
severity: "error",
80+
message: "Syntax error",
81+
});
82+
}
83+
},
84+
});
85+
} catch (e) {
86+
// If iterate isn't supported for some tree shapes, fail silently
87+
}
88+
return diags;
89+
}),
90+
lintGutter(),
6891
language,
6992
themeCompartment.of(darkModeState.get() ? monokai : bbedit),
7093
EditorView.lineWrapping,

0 commit comments

Comments
 (0)