From ac0d8c48f7a277d4a43448fe7dd4279383bc53f0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Jul 2018 21:58:49 +0300 Subject: JS plugin --- code/native/Cargo.toml | 19 ++++++++++++ code/native/build.rs | 7 +++++ code/native/src/lib.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 code/native/Cargo.toml create mode 100644 code/native/build.rs create mode 100644 code/native/src/lib.rs (limited to 'code/native') diff --git a/code/native/Cargo.toml b/code/native/Cargo.toml new file mode 100644 index 000000000..1648dfb33 --- /dev/null +++ b/code/native/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "backend" +version = "0.1.0" +authors = ["Aleksey Kladov "] +license = "MIT" +build = "build.rs" +exclude = ["artifacts.json", "index.node"] +[workspace] + +[lib] +name = "backend" +crate-type = ["dylib"] + +[build-dependencies] +neon-build = "0.2.0" + +[dependencies] +neon = "0.2.0" +libsyntax2 = { path = "../../" } diff --git a/code/native/build.rs b/code/native/build.rs new file mode 100644 index 000000000..687a66194 --- /dev/null +++ b/code/native/build.rs @@ -0,0 +1,7 @@ +extern crate neon_build; + +fn main() { + neon_build::setup(); // must be called in build.rs + + // add project-specific build logic here... +} diff --git a/code/native/src/lib.rs b/code/native/src/lib.rs new file mode 100644 index 000000000..dcf478cf5 --- /dev/null +++ b/code/native/src/lib.rs @@ -0,0 +1,80 @@ +#[macro_use] +extern crate neon; +extern crate libsyntax2; + +use libsyntax2::{ + TextRange, + File, + utils::dump_tree, + SyntaxKind::*, +}; +use neon::prelude::*; + +pub struct Wrapper { + inner: File, +} + +impl Wrapper { + fn highlight(&self) -> Vec<(TextRange, &'static str)> { + let mut res = Vec::new(); + self.inner.for_each_node(|node| { + if node.kind() == ERROR { + res.push((node.range(), "error")) + } + }); + res + } +} + + + +declare_types! { + /// A class for generating greeting strings. + pub class RustFile for Wrapper { + init(mut cx) { + let text = cx.argument::(0)?.value(); + Ok(Wrapper { + inner: File::parse(&text) + }) + } + + method syntaxTree(mut cx) { + let this = cx.this(); + let tree = { + let guard = cx.lock(); + let wrapper = this.borrow(&guard); + dump_tree(&wrapper.inner.syntax()) + }; + Ok(cx.string(tree.as_str()).upcast()) + } + + method highlight(mut cx) { + let this = cx.this(); + let highlights = { + let guard = cx.lock(); + let wrapper = this.borrow(&guard); + wrapper.highlight() + }; + let res = cx.empty_array(); + for (i, (range, tag)) in highlights.into_iter().enumerate() { + let start: u32 = range.start().into(); + let end: u32 = range.end().into(); + let start = cx.number(start); + let end = cx.number(end); + let tag = cx.string(tag); + let hl = cx.empty_array(); + hl.set(&mut cx, 0, start)?; + hl.set(&mut cx, 1, end)?; + hl.set(&mut cx, 2, tag)?; + res.set(&mut cx, i as u32, hl)?; + } + + Ok(res.upcast()) + } + } + +} + +register_module!(mut cx, { + cx.export_class::("RustFile") +}); -- cgit v1.2.3