aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libeditor/Cargo.toml1
-rw-r--r--libeditor/src/lib.rs20
-rw-r--r--libeditor/tests/test.rs47
3 files changed, 49 insertions, 19 deletions
diff --git a/libeditor/Cargo.toml b/libeditor/Cargo.toml
index 92abd3289..750d80038 100644
--- a/libeditor/Cargo.toml
+++ b/libeditor/Cargo.toml
@@ -6,3 +6,4 @@ publish = false
6 6
7[dependencies] 7[dependencies]
8libsyntax2 = { path = "../" } 8libsyntax2 = { path = "../" }
9itertools = "0.7.8"
diff --git a/libeditor/src/lib.rs b/libeditor/src/lib.rs
index a0c003fb5..ef31efe35 100644
--- a/libeditor/src/lib.rs
+++ b/libeditor/src/lib.rs
@@ -13,6 +13,7 @@ pub struct File {
13 inner: libsyntax2::File 13 inner: libsyntax2::File
14} 14}
15 15
16#[derive(Debug)]
16pub struct HighlightedRange { 17pub struct HighlightedRange {
17 pub range: TextRange, 18 pub range: TextRange,
18 pub tag: &'static str, 19 pub tag: &'static str,
@@ -102,22 +103,3 @@ impl<'f> Declaration<'f> {
102 self.0.range() 103 self.0.range()
103 } 104 }
104} 105}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn test_extend_selection() {
112 let text = r#"fn foo() {
113 1 + 1
114}
115"#;
116 let file = File::new(text);
117 let range = TextRange::offset_len(18.into(), 0.into());
118 let range = file.extend_selection(range).unwrap();
119 assert_eq!(range, TextRange::from_to(17.into(), 18.into()));
120 let range = file.extend_selection(range).unwrap();
121 assert_eq!(range, TextRange::from_to(15.into(), 20.into()));
122 }
123}
diff --git a/libeditor/tests/test.rs b/libeditor/tests/test.rs
new file mode 100644
index 000000000..e1c8aee4b
--- /dev/null
+++ b/libeditor/tests/test.rs
@@ -0,0 +1,47 @@
1extern crate libeditor;
2extern crate itertools;
3
4use std::fmt;
5use itertools::Itertools;
6use libeditor::{File, TextRange};
7
8#[test]
9fn test_extend_selection() {
10 let file = file(r#"fn foo() {
11 1 + 1
12}
13"#);
14 let range = TextRange::offset_len(18.into(), 0.into());
15 let range = file.extend_selection(range).unwrap();
16 assert_eq!(range, TextRange::from_to(17.into(), 18.into()));
17 let range = file.extend_selection(range).unwrap();
18 assert_eq!(range, TextRange::from_to(15.into(), 20.into()));
19}
20
21#[test]
22fn test_highlighting() {
23 let file = file(r#"
24// comment
25fn main() {}
26 println!("Hello, {}!", 92);
27"#);
28 let hls = file.highlight();
29 dbg_eq(
30 &hls,
31 r#"[HighlightedRange { range: [1; 11), tag: "comment" },
32 HighlightedRange { range: [12; 14), tag: "keyword" },
33 HighlightedRange { range: [15; 19), tag: "function" },
34 HighlightedRange { range: [29; 36), tag: "text" },
35 HighlightedRange { range: [38; 50), tag: "string" },
36 HighlightedRange { range: [52; 54), tag: "literal" }]"#
37 );
38}
39
40fn file(text: &str) -> File {
41 File::new(text)
42}
43
44fn dbg_eq(actual: &impl fmt::Debug, expected: &str) {
45 let expected = expected.lines().map(|l| l.trim()).join(" ");
46 assert_eq!(actual, expected);
47}