diff options
author | Wilco Kusee <[email protected]> | 2019-03-23 16:34:49 +0000 |
---|---|---|
committer | Wilco Kusee <[email protected]> | 2019-03-23 16:34:49 +0000 |
commit | a3711e08dc4e393957dff136218c47d8b77da14f (patch) | |
tree | 862ad28652de70aac75cf6e619850ae013500694 /crates/ra_ide_api_light/src/lib.rs | |
parent | a656b891fba4b89775adbc93114a20c99afe5f36 (diff) |
Move highlighting and matching_brace
Diffstat (limited to 'crates/ra_ide_api_light/src/lib.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/lib.rs | 122 |
1 files changed, 1 insertions, 121 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 | ||
6 | mod structure; | 6 | mod structure; |
7 | 7 | ||
8 | use rustc_hash::FxHashSet; | 8 | use ra_syntax::TextRange; |
9 | use ra_syntax::{ | ||
10 | SourceFile, SyntaxNode, TextRange, TextUnit, Direction, | ||
11 | algo::find_leaf_at_offset, | ||
12 | SyntaxKind::{self, *}, | ||
13 | ast::{self, AstNode}, | ||
14 | }; | ||
15 | 9 | ||
16 | pub use crate::{ | 10 | pub use crate::{ |
17 | structure::{file_structure, StructureNode}, | 11 | structure::{file_structure, StructureNode}, |
18 | }; | 12 | }; |
19 | |||
20 | #[derive(Debug)] | ||
21 | pub struct HighlightedRange { | ||
22 | pub range: TextRange, | ||
23 | pub tag: &'static str, | ||
24 | } | ||
25 | |||
26 | #[derive(Debug, Copy, Clone)] | ||
27 | pub enum Severity { | ||
28 | Error, | ||
29 | WeakWarning, | ||
30 | } | ||
31 | |||
32 | pub 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 | |||
47 | pub 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)] | ||
95 | mod 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 | ||
108 | fn 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 | } | ||