I try to use the CodeMirror with custom language in a Vaadin Dialog, but I ran into errors.
import {EditorView, lineNumbers} from "@codemirror/view";
import {EditorState} from "@codemirror/state";
import {HighlightStyle, StreamLanguage, syntaxHighlighting} from "@codemirror/language";
import {tags} from "@lezer/highlight"
...
const dialog = document.createElement('vaadin-dialog', {});
document.body.appendChild(dialog);
dialog.renderer = (root, dialog) => {
const editorContainer = document.createElement('div');
root.appendChild(editorContainer);
const customLanguage = StreamLanguage.define({...});
const myHighlightStyle = HighlightStyle.define([ { tag: tags.keyword, color: "#E06C75" }, ...]);
const editorState = EditorState.create({
doc: "The custom language content",
extensions: [
lineNumbers(),
customLanguage,
syntaxHighlighting(myHighlightStyle)
],
});
const editorView = new EditorView({
state: editorState,
parent: editorContainer,
// give the shadow root context
root: root.getRootNode()
});
If I add the syntax highlighting to the extensions, then I get the following error:
CodeMirror plugin crashed: TypeError: e is not iterable
at zy.style (index.js:261:29)
at index.js:278:33
at iy.highlightRange (index.js:365:22)
at ty (index.js:306:13)
at Wy.buildDeco (index.js:1784:13)
at new Wy (index.js:1762:33)
at wf.create (index.js:2502:49)
at xf.update (index.js:2522:51)
at new vg (index.js:7557:20)
at t.renderer [as _oldRenderer]
Can you reproduce this without @vaadin/vaadin-core? If so, could you set up a minimal, self-contained script that produces the error, and tell me which browser(s) you’re seeing it in?
Yes you are right. I did not noticed that no error thrown with this example, but as you can see, no highlighting either in the editor.
So probably the exception is cached somewhere.
If you follow the TODO comments, you will see, that without the ShadowDOM the code will be highlighted, but if pasted into the shadowDOM, the syntaxHighlighting() doesn’t work.
The class names are there in the editor content. The reason they don’t have a color is that you defined their styles in the outer document, and they won’t take effect inside the shadow DOM.
Well, seems that was a code bug on my side. I replaced the old stream/highlight code with the one from the example, and it worked in the Vaadin dialog as well.