refactor(code-editor): update to Codemirror 6#421
Conversation
b19a47f to
7c0472e
Compare
|
heya @justinfagnani @aomarks, I've added a summary to my description in the order of what files I think are important in this PR |
f20aba8 to
c1c8a98
Compare
aomarks
left a comment
There was a problem hiding this comment.
Sorry for the very slow review.
Code looks really nice, appreciate all the comments!
Just some small nits.
|
Updated Ci settings for Wireit to use v2 caching |
|
thanks for the review @aomarks, addressed comments. |
fff3844 to
8a8358e
Compare
| const parsers: Record<string, LRParser> = { | ||
| html: htmlLanguage.parser, | ||
| css: cssLanguage.parser, | ||
| svg: xmlLanguage.parser, |
There was a problem hiding this comment.
btw, I wasn't 100% sure that svg and mathml should use the xml parser, or if the HTML mode handles that, but I think XML is correct since there's no containing <svg> tag to kick the parser into XML mode.
This issues is that I think we may be missing out on code completion in these sections though, since the parser doesn't know that we're specifically in SVG or MathML languages. Maybe there's something we can pull from the HTML language, or maybe there are SVG and MathML languages we can use?
There was a problem hiding this comment.
I think HTML uses the XML parser – I don't know if we can do any better. When I added the XML package, the tree-shaken / minified bundle didn't change in size
| } | ||
|
|
||
| // Map from CM6 token names to CSS property base names | ||
| // This maintains backward compatibility with the CSS custom property names |
There was a problem hiding this comment.
I like this 👍
but I would discuss whether it's needed, or if consumers can update their custom themes easily enough along with a new major version of playground elements.
I suspect we'll be on CM6 and its class names for a long while after this transition.
There was a problem hiding this comment.
We can maybe do two major releases. this one (engine breaking) and a proper breaking one? I'm just thinking of our own migration on lit.dev being annoying
| const themeFilenames = fs | ||
| .readdirSync( | ||
| pathlib.resolve(rootDir, 'node_modules', 'codemirror', 'theme') | ||
| pathlib.resolve(rootDir, 'node_modules', 'codemirror-5', 'theme') |
There was a problem hiding this comment.
I forget the details, but did we talk about using native CM6 themes instead, like https://github.com/fsegurai/codemirror-themes?
There was a problem hiding this comment.
I have a sub-PR that we can merge into this branch or after this one that allows for arbitrarily setting extensions
feat(editor): enable adding codemirror extensions
Upgrade to codemirror 6
per-file summary
Major rewrite to adapt to CodeMirror 6:src/playground-code-editor.tsEditorState,EditorView,Extension,Compartment, etc.).playgroundStylesimport and added the newstylesimport fromplayground-styles.ts..tok-*classes withsyntaxHighlighting(classHighlighter, {fallback: true})Editorinstance to CM6'sEditorViewandEditorState.cursorPosition,cursorIndex,tokenUnderCursor) to use the CM6 API.updatemethod to use CM6Compartments andStateEffects for dynamic configuration changes for the following features:EditorViewinstantiation and state creation (_createEditorState)._getLanguageExtensionand the_languageCompartment.autocompletionextension_showDiagnosticsto use CM6Decorations andStateField.Decorations,WidgetType, andAnnotations._onRenderLine).createRenderRootto handle shadow root adoption for theEditorViewconnectedCallback()playground-styles.cssDeleted this file entirely. Instead manually wrote the
.css.tsfile's style overrides.src/playground-styles.tsplayground-styles.cssbut use CodeMirror 6 class namespackage.json@types/codemirror,codemirror-grammar-mode,google_modes).@codemirror/*,@lezer/*).codemirrordependency to^6.0.1.codemirror-5as a git dependency, for theme generation compatibility.build:styles-module,build:codemirror).wireitconfigurations to remove dependencies on the deleted build scripts.
Deleted this file. It was responsible for bundling CodeMirror 5 and its addons.rollup.config.codemirror.jsrollup.config.styles.jsDeleted this file. It was used to process
playground-styles.cssinto a TypeScript module.scripts/theme-generator.jsUpdated the script to read theme files from the
codemirror-5/themedirectory instead ofcodemirror/theme, reflecting the aliased package name for the old version.src/_codemirrorDeleted this directory. This was the entry point for the custom CodeMirror 5 ESM-ified bundle.
src/cm-lang-lit.tshtml,css,svg,mathmlwithin JavaScript and TypeScriptlitLanguagelitJsxLanguagelit()language support function.src/configurator/highlight-tokens.tscmClassproperty for each token definition to match the new token class names generated by CodeMirror 6'sclassHighlightercm-keywordchanged totok-keywordsynBooltoken.src/configurator/playground-theme-detector.tsviewportMargin).tokenToCssProperty) to translate CM6 token names back to the existing CSS custom property names for compatibility.cm-andtok-prefixes.#idin CSS).src/internal/codemirror.tsDeleted this file. It previously exported the CodeMirror 5 global object.