aboutsummaryrefslogtreecommitdiff
path: root/code/native
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-01 08:40:07 +0100
committerAleksey Kladov <[email protected]>2018-08-01 08:40:07 +0100
commit966e9db2b83802dfb55d55bd3a26e69dced1bbd7 (patch)
tree6d3bda084eab1221bcad7602aa26a2c307850a72 /code/native
parentb9189ed2db8cb1934e677a17fcc6282c66306df1 (diff)
Extract libeditor
Diffstat (limited to 'code/native')
-rw-r--r--code/native/Cargo.toml2
-rw-r--r--code/native/src/lib.rs40
2 files changed, 10 insertions, 32 deletions
diff --git a/code/native/Cargo.toml b/code/native/Cargo.toml
index 1648dfb33..3a27051b2 100644
--- a/code/native/Cargo.toml
+++ b/code/native/Cargo.toml
@@ -16,4 +16,4 @@ neon-build = "0.2.0"
16 16
17[dependencies] 17[dependencies]
18neon = "0.2.0" 18neon = "0.2.0"
19libsyntax2 = { path = "../../" } 19libeditor = { path = "../../libeditor" }
diff --git a/code/native/src/lib.rs b/code/native/src/lib.rs
index 068767fab..aae7ad2f3 100644
--- a/code/native/src/lib.rs
+++ b/code/native/src/lib.rs
@@ -1,42 +1,20 @@
1#[macro_use] 1#[macro_use]
2extern crate neon; 2extern crate neon;
3extern crate libsyntax2; 3extern crate libeditor;
4 4
5use libsyntax2::{
6 TextRange,
7 File,
8 utils::dump_tree,
9 SyntaxKind::*,
10 algo,
11};
12use neon::prelude::*; 5use neon::prelude::*;
13 6
14pub struct Wrapper { 7pub struct Wrapper {
15 inner: File, 8 inner: libeditor::File,
16} 9}
17 10
18impl Wrapper {
19 fn highlight(&self) -> Vec<(TextRange, &'static str)> {
20 let mut res = Vec::new();
21 let syntax = self.inner.syntax();
22 for node in algo::walk::preorder(syntax.as_ref()) {
23 if node.kind() == ERROR {
24 res.push((node.range(), "error"))
25 }
26 }
27 res
28 }
29}
30
31
32
33declare_types! { 11declare_types! {
34 /// A class for generating greeting strings. 12 /// A class for generating greeting strings.
35 pub class RustFile for Wrapper { 13 pub class RustFile for Wrapper {
36 init(mut cx) { 14 init(mut cx) {
37 let text = cx.argument::<JsString>(0)?.value(); 15 let text = cx.argument::<JsString>(0)?.value();
38 Ok(Wrapper { 16 Ok(Wrapper {
39 inner: File::parse(&text) 17 inner: libeditor::File::new(&text)
40 }) 18 })
41 } 19 }
42 20
@@ -45,7 +23,7 @@ declare_types! {
45 let tree = { 23 let tree = {
46 let guard = cx.lock(); 24 let guard = cx.lock();
47 let wrapper = this.borrow(&guard); 25 let wrapper = this.borrow(&guard);
48 dump_tree(&wrapper.inner.syntax()) 26 wrapper.inner.syntax_tree()
49 }; 27 };
50 Ok(cx.string(tree.as_str()).upcast()) 28 Ok(cx.string(tree.as_str()).upcast())
51 } 29 }
@@ -55,15 +33,15 @@ declare_types! {
55 let highlights = { 33 let highlights = {
56 let guard = cx.lock(); 34 let guard = cx.lock();
57 let wrapper = this.borrow(&guard); 35 let wrapper = this.borrow(&guard);
58 wrapper.highlight() 36 wrapper.inner.highlight()
59 }; 37 };
60 let res = cx.empty_array(); 38 let res = cx.empty_array();
61 for (i, (range, tag)) in highlights.into_iter().enumerate() { 39 for (i, hl) in highlights.into_iter().enumerate() {
62 let start: u32 = range.start().into(); 40 let start: u32 = hl.range.start().into();
63 let end: u32 = range.end().into(); 41 let end: u32 = hl.range.end().into();
64 let start = cx.number(start); 42 let start = cx.number(start);
65 let end = cx.number(end); 43 let end = cx.number(end);
66 let tag = cx.string(tag); 44 let tag = cx.string(hl.tag);
67 let hl = cx.empty_array(); 45 let hl = cx.empty_array();
68 hl.set(&mut cx, 0, start)?; 46 hl.set(&mut cx, 0, start)?;
69 hl.set(&mut cx, 1, end)?; 47 hl.set(&mut cx, 1, end)?;