aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api_light/src')
-rw-r--r--crates/ra_ide_api_light/src/lib.rs122
-rw-r--r--crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap32
2 files changed, 1 insertions, 153 deletions
diff --git a/crates/ra_ide_api_light/src/lib.rs b/crates/ra_ide_api_light/src/lib.rs
index 1c5fa0837..df7f144b6 100644
--- a/crates/ra_ide_api_light/src/lib.rs
+++ b/crates/ra_ide_api_light/src/lib.rs
@@ -5,128 +5,8 @@
5 5
6mod structure; 6mod structure;
7 7
8use rustc_hash::FxHashSet; 8use ra_syntax::TextRange;
9use ra_syntax::{
10 SourceFile, SyntaxNode, TextRange, TextUnit, Direction,
11 algo::find_leaf_at_offset,
12 SyntaxKind::{self, *},
13 ast::{self, AstNode},
14};
15 9
16pub use crate::{ 10pub use crate::{
17 structure::{file_structure, StructureNode}, 11 structure::{file_structure, StructureNode},
18}; 12};
19
20#[derive(Debug)]
21pub struct HighlightedRange {
22 pub range: TextRange,
23 pub tag: &'static str,
24}
25
26#[derive(Debug, Copy, Clone)]
27pub enum Severity {
28 Error,
29 WeakWarning,
30}
31
32pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> {
33 const BRACES: &[SyntaxKind] =
34 &[L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE];
35 let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset)
36 .filter_map(|node| {
37 let idx = BRACES.iter().position(|&brace| brace == node.kind())?;
38 Some((node, idx))
39 })
40 .next()?;
41 let parent = brace_node.parent()?;
42 let matching_kind = BRACES[brace_idx ^ 1];
43 let matching_node = parent.children().find(|node| node.kind() == matching_kind)?;
44 Some(matching_node.range().start())
45}
46
47pub fn highlight(root: &SyntaxNode) -> Vec<HighlightedRange> {
48 // Visited nodes to handle highlighting priorities
49 let mut highlighted = FxHashSet::default();
50 let mut res = Vec::new();
51 for node in root.descendants() {
52 if highlighted.contains(&node) {
53 continue;
54 }
55 let tag = match node.kind() {
56 COMMENT => "comment",
57 STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
58 ATTR => "attribute",
59 NAME_REF => "text",
60 NAME => "function",
61 INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
62 LIFETIME => "parameter",
63 k if k.is_keyword() => "keyword",
64 _ => {
65 if let Some(macro_call) = ast::MacroCall::cast(node) {
66 if let Some(path) = macro_call.path() {
67 if let Some(segment) = path.segment() {
68 if let Some(name_ref) = segment.name_ref() {
69 highlighted.insert(name_ref.syntax());
70 let range_start = name_ref.syntax().range().start();
71 let mut range_end = name_ref.syntax().range().end();
72 for sibling in path.syntax().siblings(Direction::Next) {
73 match sibling.kind() {
74 EXCL | IDENT => range_end = sibling.range().end(),
75 _ => (),
76 }
77 }
78 res.push(HighlightedRange {
79 range: TextRange::from_to(range_start, range_end),
80 tag: "macro",
81 })
82 }
83 }
84 }
85 }
86 continue;
87 }
88 };
89 res.push(HighlightedRange { range: node.range(), tag })
90 }
91 res
92}
93
94#[cfg(test)]
95mod tests {
96 use ra_syntax::AstNode;
97 use insta::assert_debug_snapshot_matches;
98
99 use test_utils::{add_cursor, assert_eq_text, extract_offset};
100
101 use super::*;
102
103 #[test]
104 fn test_highlighting() {
105 let file = SourceFile::parse(
106 r#"
107// comment
108fn main() {}
109 println!("Hello, {}!", 92);
110"#,
111 );
112 let hls = highlight(file.syntax());
113 assert_debug_snapshot_matches!("highlighting", hls);
114 }
115
116 #[test]
117 fn test_matching_brace() {
118 fn do_check(before: &str, after: &str) {
119 let (pos, before) = extract_offset(before);
120 let file = SourceFile::parse(&before);
121 let new_pos = match matching_brace(&file, pos) {
122 None => pos,
123 Some(pos) => pos,
124 };
125 let actual = add_cursor(&before, new_pos);
126 assert_eq_text!(after, &actual);
127 }
128
129 do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }");
130 }
131
132}
diff --git a/crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap b/crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap
deleted file mode 100644
index ef306a7a0..000000000
--- a/crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap
+++ /dev/null
@@ -1,32 +0,0 @@
1---
2created: "2019-01-22T14:45:01.959724300+00:00"
3creator: [email protected]
4expression: hls
5source: "crates\\ra_ide_api_light\\src\\lib.rs"
6---
7[
8 HighlightedRange {
9 range: [1; 11),
10 tag: "comment"
11 },
12 HighlightedRange {
13 range: [12; 14),
14 tag: "keyword"
15 },
16 HighlightedRange {
17 range: [15; 19),
18 tag: "function"
19 },
20 HighlightedRange {
21 range: [29; 37),
22 tag: "macro"
23 },
24 HighlightedRange {
25 range: [38; 50),
26 tag: "string"
27 },
28 HighlightedRange {
29 range: [52; 54),
30 tag: "literal"
31 }
32]