diff options
author | Aleksey Kladov <[email protected]> | 2018-08-01 08:40:07 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-01 08:40:07 +0100 |
commit | 966e9db2b83802dfb55d55bd3a26e69dced1bbd7 (patch) | |
tree | 6d3bda084eab1221bcad7602aa26a2c307850a72 /libeditor | |
parent | b9189ed2db8cb1934e677a17fcc6282c66306df1 (diff) |
Extract libeditor
Diffstat (limited to 'libeditor')
-rw-r--r-- | libeditor/Cargo.toml | 9 | ||||
-rw-r--r-- | libeditor/src/lib.rs | 53 |
2 files changed, 62 insertions, 0 deletions
diff --git a/libeditor/Cargo.toml b/libeditor/Cargo.toml new file mode 100644 index 000000000..1a532ce2f --- /dev/null +++ b/libeditor/Cargo.toml | |||
@@ -0,0 +1,9 @@ | |||
1 | [package] | ||
2 | name = "libeditor" | ||
3 | version = "0.1.0" | ||
4 | authors = ["Aleksey Kladov <[email protected]>"] | ||
5 | publish = false | ||
6 | |||
7 | [dependencies] | ||
8 | libsyntax2 = { path = "../" } | ||
9 | text_unit = "0.1.2" | ||
diff --git a/libeditor/src/lib.rs b/libeditor/src/lib.rs new file mode 100644 index 000000000..119bdb2d6 --- /dev/null +++ b/libeditor/src/lib.rs | |||
@@ -0,0 +1,53 @@ | |||
1 | extern crate libsyntax2; | ||
2 | extern crate text_unit; | ||
3 | |||
4 | use libsyntax2::{ | ||
5 | algo::walk, | ||
6 | SyntaxKind::*, | ||
7 | }; | ||
8 | use text_unit::TextRange; | ||
9 | |||
10 | pub struct File { | ||
11 | inner: libsyntax2::File | ||
12 | } | ||
13 | |||
14 | pub struct HighlightedRange { | ||
15 | pub range: TextRange, | ||
16 | pub tag: &'static str, | ||
17 | } | ||
18 | |||
19 | impl File { | ||
20 | pub fn new(text: &str) -> File { | ||
21 | File { | ||
22 | inner: libsyntax2::File::parse(text) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | pub fn highlight(&self) -> Vec<HighlightedRange> { | ||
27 | let syntax = self.inner.syntax(); | ||
28 | let mut res = Vec::new(); | ||
29 | for node in walk::preorder(syntax.as_ref()) { | ||
30 | let tag = match node.kind() { | ||
31 | ERROR => "error", | ||
32 | COMMENT | DOC_COMMENT => "comment", | ||
33 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", | ||
34 | ATTR => "attribute", | ||
35 | NAME_REF => "text", | ||
36 | NAME => "function", | ||
37 | INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", | ||
38 | LIFETIME => "parameter", | ||
39 | k if k.is_keyword() => "keyword", | ||
40 | _ => continue, | ||
41 | }; | ||
42 | res.push(HighlightedRange { | ||
43 | range: node.range(), | ||
44 | tag | ||
45 | }) | ||
46 | } | ||
47 | res | ||
48 | } | ||
49 | |||
50 | pub fn syntax_tree(&self) -> String { | ||
51 | ::libsyntax2::utils::dump_tree(&self.inner.syntax()) | ||
52 | } | ||
53 | } | ||