codemirror

    A MoonBit port of CodeMirror 5, targeting the JavaScript backend

    editor
    codemirror
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    15 minutes ago
    Downloads
    1

    No dependencies

    #CodeMirror 5 in MoonBit

    CI

    A port of CodeMirror 5 (v5.65.21) to MoonBit, compiled with MoonBit's JavaScript backend. The editor core is a faithful, file-by-file translation of src/ from the upstream repository, and runs in the browser.

    #Status

    • Editor core: complete. Document model (B-tree of lines, marked text, bookmarks, line widgets, linked documents, undo history), highlighting engine, display (viewport rendering, measurement, bidi text, line wrapping, gutters, scrollbars), both input styles (hidden textarea and contenteditable), keymaps, commands, mouse/touch/drag-and-drop handling.
    • JavaScript-compatible API (jsapi + bundle): a drop-in replacement for upstream's lib/codemirror.js (window.CodeMirror). Upstream modes, addons and keymaps written in JavaScript run unmodified on top of the MoonBit core.
    • Native MoonBit modes (120 of upstream's 121, in modes/): apl, asciiarmor, asn1, asterisk, brainfuck, clike, clojure, cmake, cobol, coffeescript, commonlisp, crystal, css, cypher, d, diff, django, dockerfile, dtd, dylan, ebnf, ecl, eiffel, elm, erlang, factor, fcl, forth, fortran, gas, gfm, gherkin, go, groovy, haml, handlebars, haskell, haskell-literate, haxe, htmlembedded, htmlmixed, http, idl, javascript, jinja2, jsx, julia, livescript, lua, markdown, mathematica, mbox, mirc, mllike, modelica, mscgen, mumps, nginx, nsis, ntriples, octave, oz, pascal, pegjs, perl, php, pig, powershell, properties, protobuf, pug, puppet, python, q, r, rpm, rst, ruby, rust, sas, sass, scheme, shell, sieve, slim, smalltalk, smarty, solr, soy, sparql, spreadsheet, sql, stex, stylus, swift, tcl, textile, tiddlywiki, tiki, toml, tornado, troff, ttcn, ttcn-cfg, turtle, twig, vb, vbscript, velocity, verilog, vhdl, vue, wast, webidl, xml, xquery, yacas, yaml, yaml-frontmatter, z80. Notes: clike covers C, C++, Java, C#, Scala, Kotlin, GLSL, nesC, Objective-C/C++, Squirrel, Ceylon, PHP and Dart, and accepts JavaScript hooks (as upstream's Dart mode uses them); sql covers all 14 dialects; modes built on upstream's simple-mode addon are generated by tools/extract_simple_modes.js. The remaining upstream modes run through the JavaScript API. test/compare_tokens.js compares token output of native modes with upstream.
    • Native MoonBit addons (all of upstream's addon/ scripts, in addon/<group>/): comment (comment, continuecomment), dialog, display (autorefresh, fullscreen, panel, placeholder, rulers), edit (closebrackets, closetag, continuelist, matchbrackets, matchtags, trailingspace), fold (foldcode, foldgutter, brace/comment/indent/markdown/xml folding), hint (show-hint, anyword, css, html, javascript, sql, xml), lint (lint plus the javascript, css, html, json, yaml and coffeescript linter wrappers), merge, mode (overlay, multiplex, simple, loadmode), runmode (runmode, colorize), scroll (annotatescrollbar, scrollpastend, simplescrollbars), search (search, searchcursor, jump-to-line, match-highlighter, matchesonscrollbar), selection (active-line, mark-selection, selection-pointer), tern and wrap (hardwrap). Each one registers the same options, commands, helpers and CodeMirror.* functions as its upstream script, so JavaScript code using them is unchanged. External libraries stay external: lint wraps JSHint, CSSLint and so on, merge uses diff_match_patch, and tern uses the Tern server. addon/tern/worker.js stays a plain script: it runs Tern in a Web Worker and never touches CodeMirror.
    • Overlays and multiplexing: @mode.overlay_mode and @mode.multiplexing_mode are native ports of addon/mode/overlay.js and addon/mode/multiplex.js.
    • Simple modes: mode/simple is a native port of addon/mode/simple.js (state machines of regexp rules, nested modes, indentation).
    • Themes: all 65 upstream themes (css/theme/).

    #Verification

    The upstream test suite (test/index.html, 1485 tests covering the core, documents, multiple selections, contenteditable, scrolling, comments, search, keymaps, addons and ~40 modes) runs against this port:

    PageWhat runsResult
    test/index.htmlMoonBit core + upstream JS modes/addons1481 / 1485
    test/native.htmlMoonBit core + native MoonBit modes and addons1481 / 1485
    test/baseline.htmlupstream CodeMirror itself (from src/)1481 / 1485

    The four failures (core_move_bidi_* ×2, core_issue_4878, scroll_movedown_resize) are identical for upstream CodeMirror in the same browser, i.e. they are environment-dependent, not port regressions.

    Beyond the upstream suite, test/addon_cases.js drives every native addon through scripted scenarios (typing, commands, dialogs, hints, folding, merge views, a fake Tern server, ...) and records the results. Running it on test/native.html and on test/baseline.html (which loads upstream's addon scripts) and comparing the two transcripts with diffTranscript gives identical output.

    test/ci.mjs runs all of this headless with Playwright (CI does so on every push): the port's pages may only fail tests that upstream also fails, and the addon transcripts must match.

    #Known differences

    • Position identity. Upstream's clipPos returns an in-range position object unchanged, so getCursor(), listSelections() and similar calls can hand back the very {line, ch} object passed to setSelection and friends. This port always returns fresh CodeMirror.Pos objects (with sticky: null). The difference only shows when comparing a result with === against the object you passed in, or when serializing it and expecting no sticky key. Fresh objects also keep later changes to your own object from silently affecting the editor's selection.

    #Layout

    PackageContents
    js/JavaScript FFI: values, regular expressions, DOM bindings
    mode/Mode API: StringStream, ModeState trait, Mode, registry
    core/The editor (model, display, input, edit)
    modes/<name>/Native language modes
    addon/<group>/Native addons
    jsapi/JS-compatible CodeMirror API (api.js glue, generated into api_js.mbt by gen.py)
    bundle/Entry point producing a codemirror.js replacement
    demo/Demo page using the MoonBit API directly
    css/codemirror.css and themes
    test/Pages running the upstream test suite

    #Installing

    moon add bobzhang/codemirror

    The packages target the JavaScript backend (--target js).

    #Building

    moon build --target js

    This produces _build/js/debug/build/bundle/bundle.js (the window.CodeMirror replacement) and _build/js/debug/build/demo/demo.js. Serve the repository root (e.g. python3 -m http.server) and open demo/index.html or test/index.html. The test pages load upstream sources from .repos/codemirror5 (a checkout of the upstream repository).

    #Using it from MoonBit

    let cm = @core.Editor::create(
    parent_node,
    config={ "value": "let x = 1", "mode": "javascript", "lineNumbers": true },
    )
    cm.on("change", _ => println(cm.get_value()))
    cm.replace_range("// hi\n", @core.Pos::new(0, 0))

    Options are given as JSON, using CodeMirror's option names. Function-valued options have typed setters (set_extra_keys, set_line_number_formatter, …). Events are delivered as arrays of EvArg.

    #Porting notes

    • Modes: a mode's tokenizer, indenter and other state-dependent operations are methods on its state type (ModeState trait). Tokenizer/parser functions that upstream compares by identity are represented as enums.
    • CodeMirror.Pass is the Pass error (raised by commands) or None (from indent).
    • Old-IE and Presto-specific workarounds were dropped.