From 73177af78f61975a0eaef9a88758d59db4ccc473 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 12 Jun 2021 21:01:51 +0530 Subject: syntax tree <-> dom tree --- src/lib.rs | 36 ++++++++++++++++++++++++------ www/index.html | 15 ++++++++++--- www/index.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 99 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f9a6a26..a1e085f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,7 +53,16 @@ impl SynNode { } pub fn to_string(&self) -> String { - format!("{:?}@{:?}", self.node.kind(), self.node.text_range(),) + format!("{:?} {:?}", self.node.kind(), self.node.text_range(),) + } + + pub fn range(&self) -> TextRange { + let r = self.node.text_range(); + (r.start().into(), r.end().into()).into() + } + + pub fn text(&self) -> String { + format!("{:?}", self.node.kind()) } pub fn from_str(s: &str) -> Result { @@ -95,7 +104,18 @@ impl SynErr { } #[wasm_bindgen] -struct TextRange { +impl SynErr { + pub fn range(&self) -> TextRange { + let r = self.err.range(); + (r.start().into(), r.end().into()).into() + } + pub fn to_string(&self) -> String { + self.err.to_string() + } +} + +#[wasm_bindgen] +pub struct TextRange { start: u32, end: u32, } @@ -107,12 +127,14 @@ impl From<(u32, u32)> for TextRange { } #[wasm_bindgen] -impl SynErr { - pub fn range(&self) -> TextRange { - let r = self.err.range(); - (r.start().into(), r.end().into()).into() +impl TextRange { + pub fn start(&self) -> u32 { + self.start + } + pub fn end(&self) -> u32 { + self.end } pub fn to_string(&self) -> String { - self.err.to_string() + format!("{}..{}", self.start, self.end) } } diff --git a/www/index.html b/www/index.html index 01ba57d..1404993 100644 --- a/www/index.html +++ b/www/index.html @@ -9,6 +9,17 @@ display: grid; grid-template-columns: 1fr 1fr; grid-gap: 20px; +} +.syntax-node { + padding: 0px; + padding-left: 20px; +} +.syntax-err { + color: red; +} +pre { + padding: 0px; + margin: 0px } @@ -16,9 +27,7 @@
-
-
-        
+
diff --git a/www/index.js b/www/index.js index 2665873..52253c0 100644 --- a/www/index.js +++ b/www/index.js @@ -1,5 +1,7 @@ import {SynNode, put_cst} from "cstea"; import {EditorState, EditorView, basicSetup} from "@codemirror/basic-setup" +import {Decoration, DecorationSet} from "@codemirror/view" +import {StateField, StateEffect} from "@codemirror/state" import {rust} from "@codemirror/lang-rust" let cst = document.getElementById('cst'); @@ -18,32 +20,77 @@ let view = new EditorView({ parent: document.getElementById('source-code') }) +const doHighlight = StateEffect.define() + +const highlightField = StateField.define({ + create() { + return Decoration.none; + }, + update(highlight, tr) { + for (let e of tr.effects) if (e.is(doHighlight)) { + return (Decoration.none).update({ + add: [hlMark.range(e.value.from, e.value.to)] + }); + } + }, + provide: f => EditorView.decorations.from(f) +}) + +const hlMark = Decoration.mark({class: "cm-highlight"}) + +const hlTheme = EditorView.baseTheme({ + ".cm-highlight": { textDecoration: "underline 3px red" } +}) + +function highlightArea(view, textRange) { + let effects = [doHighlight.of({from: textRange.start(), to: textRange.end()})]; + if (!view.state.field(highlightField, false)) { + effects.push(StateEffect.appendConfig.of([highlightField, hlTheme])); + } + view.dispatch({effects}); +} + function render_cst(synRoot) { - cst.innerText += "\n" + synRoot.to_string(); + let nodeDiv = document.createElement("div"); + nodeDiv.className = "syntax-node"; + let r = synRoot.range(); + let synText = wrap(synRoot.text() + synRoot.range().to_string(), "pre"); + synText.onmouseover = () => { + console.log(r.to_string()); + highlightArea(view, r); + } + nodeDiv.appendChild(synText); if (!synRoot.is_token()) { synRoot.children().forEach(node => { - render_cst(node); - return; + nodeDiv.appendChild(render_cst(node)); }); - } else { - return; } + return nodeDiv; +} + +function wrap(s, tag) { + let t = document.createElement(tag); + t.innerText = s; + return t; } function render_err(errorList) { - cst.innerText = ""; + let errDiv = document.createElement("div"); + errDiv.className = "syntax-err"; errorList.forEach(err => { - cst.innerText += "\n" + err.to_string(); + errDiv.appendChild(wrap(err.to_string(), "pre")); + highlightArea(view, err.range()); }); + return errDiv; } function doRender() { let sourceFile = view.state.doc.toString();; - cst.innerText = ""; + cst.innerHTML = ""; try { - var synRoot = SynNode.from_str(sourceFile); - render_cst(synRoot); + let synRoot = SynNode.from_str(sourceFile); + cst.appendChild(render_cst(synRoot)); } catch (synError) { - render_err(synError) + cst.appendChild(render_err(synError)); } } -- cgit v1.2.3