aboutsummaryrefslogtreecommitdiff
path: root/libeditor/src/lib.rs
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 /libeditor/src/lib.rs
parentb9189ed2db8cb1934e677a17fcc6282c66306df1 (diff)
Extract libeditor
Diffstat (limited to 'libeditor/src/lib.rs')
-rw-r--r--libeditor/src/lib.rs53
1 files changed, 53 insertions, 0 deletions
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 @@
1extern crate libsyntax2;
2extern crate text_unit;
3
4use libsyntax2::{
5 algo::walk,
6 SyntaxKind::*,
7};
8use text_unit::TextRange;
9
10pub struct File {
11 inner: libsyntax2::File
12}
13
14pub struct HighlightedRange {
15 pub range: TextRange,
16 pub tag: &'static str,
17}
18
19impl 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}