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 | |
parent | a656b891fba4b89775adbc93114a20c99afe5f36 (diff) |
Move highlighting and matching_brace
Diffstat (limited to 'crates/ra_ide_api')
-rw-r--r-- | crates/ra_ide_api/src/diagnostics.rs | 7 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 9 | ||||
-rw-r--r-- | crates/ra_ide_api/src/matching_brace.rs | 45 | ||||
-rw-r--r-- | crates/ra_ide_api/src/snapshots/tests__highlighting.snap | 34 | ||||
-rw-r--r-- | crates/ra_ide_api/src/syntax_highlighting.rs | 81 |
5 files changed, 165 insertions, 11 deletions
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 069092528..aabb614b9 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | use itertools::Itertools; | 1 | use itertools::Itertools; |
2 | use hir::{Problem, source_binder}; | 2 | use hir::{Problem, source_binder}; |
3 | use ra_ide_api_light::Severity; | ||
4 | use ra_db::SourceDatabase; | 3 | use ra_db::SourceDatabase; |
5 | use ra_syntax::{ | 4 | use ra_syntax::{ |
6 | Location, SourceFile, SyntaxKind, TextRange, SyntaxNode, | 5 | Location, SourceFile, SyntaxKind, TextRange, SyntaxNode, |
@@ -11,6 +10,12 @@ use ra_text_edit::{TextEdit, TextEditBuilder}; | |||
11 | 10 | ||
12 | use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase}; | 11 | use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase}; |
13 | 12 | ||
13 | #[derive(Debug, Copy, Clone)] | ||
14 | pub enum Severity { | ||
15 | Error, | ||
16 | WeakWarning, | ||
17 | } | ||
18 | |||
14 | pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> { | 19 | pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> { |
15 | let source_file = db.parse(file_id); | 20 | let source_file = db.parse(file_id); |
16 | let mut res = Vec::new(); | 21 | let mut res = Vec::new(); |
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 0e7b47e3c..99f18b6b8 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -38,6 +38,7 @@ mod folding_ranges; | |||
38 | mod line_index_utils; | 38 | mod line_index_utils; |
39 | mod join_lines; | 39 | mod join_lines; |
40 | mod typing; | 40 | mod typing; |
41 | mod matching_brace; | ||
41 | 42 | ||
42 | #[cfg(test)] | 43 | #[cfg(test)] |
43 | mod marks; | 44 | mod marks; |
@@ -70,10 +71,10 @@ pub use crate::{ | |||
70 | line_index::{LineIndex, LineCol}, | 71 | line_index::{LineIndex, LineCol}, |
71 | line_index_utils::translate_offset_with_edit, | 72 | line_index_utils::translate_offset_with_edit, |
72 | folding_ranges::{Fold, FoldKind}, | 73 | folding_ranges::{Fold, FoldKind}, |
74 | syntax_highlighting::HighlightedRange, | ||
75 | diagnostics::Severity, | ||
73 | }; | 76 | }; |
74 | pub use ra_ide_api_light::{ | 77 | pub use ra_ide_api_light::StructureNode; |
75 | HighlightedRange, Severity, StructureNode, | ||
76 | }; | ||
77 | pub use ra_db::{ | 78 | pub use ra_db::{ |
78 | Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, | 79 | Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, |
79 | Edition | 80 | Edition |
@@ -267,7 +268,7 @@ impl Analysis { | |||
267 | /// supported). | 268 | /// supported). |
268 | pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> { | 269 | pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> { |
269 | let file = self.db.parse(position.file_id); | 270 | let file = self.db.parse(position.file_id); |
270 | ra_ide_api_light::matching_brace(&file, position.offset) | 271 | matching_brace::matching_brace(&file, position.offset) |
271 | } | 272 | } |
272 | 273 | ||
273 | /// Returns a syntax tree represented as `String`, for debug purposes. | 274 | /// Returns a syntax tree represented as `String`, for debug purposes. |
diff --git a/crates/ra_ide_api/src/matching_brace.rs b/crates/ra_ide_api/src/matching_brace.rs new file mode 100644 index 000000000..d1405f14f --- /dev/null +++ b/crates/ra_ide_api/src/matching_brace.rs | |||
@@ -0,0 +1,45 @@ | |||
1 | use ra_syntax::{ | ||
2 | SourceFile, TextUnit, | ||
3 | algo::find_leaf_at_offset, | ||
4 | SyntaxKind::{self, *}, | ||
5 | ast::AstNode, | ||
6 | }; | ||
7 | |||
8 | pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option<TextUnit> { | ||
9 | const BRACES: &[SyntaxKind] = | ||
10 | &[L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE]; | ||
11 | let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset) | ||
12 | .filter_map(|node| { | ||
13 | let idx = BRACES.iter().position(|&brace| brace == node.kind())?; | ||
14 | Some((node, idx)) | ||
15 | }) | ||
16 | .next()?; | ||
17 | let parent = brace_node.parent()?; | ||
18 | let matching_kind = BRACES[brace_idx ^ 1]; | ||
19 | let matching_node = parent.children().find(|node| node.kind() == matching_kind)?; | ||
20 | Some(matching_node.range().start()) | ||
21 | } | ||
22 | |||
23 | #[cfg(test)] | ||
24 | mod tests { | ||
25 | use test_utils::{add_cursor, assert_eq_text, extract_offset}; | ||
26 | |||
27 | use super::*; | ||
28 | |||
29 | #[test] | ||
30 | fn test_matching_brace() { | ||
31 | fn do_check(before: &str, after: &str) { | ||
32 | let (pos, before) = extract_offset(before); | ||
33 | let file = SourceFile::parse(&before); | ||
34 | let new_pos = match matching_brace(&file, pos) { | ||
35 | None => pos, | ||
36 | Some(pos) => pos, | ||
37 | }; | ||
38 | let actual = add_cursor(&before, new_pos); | ||
39 | assert_eq_text!(after, &actual); | ||
40 | } | ||
41 | |||
42 | do_check("struct Foo { a: i32, }<|>", "struct Foo <|>{ a: i32, }"); | ||
43 | } | ||
44 | |||
45 | } | ||
diff --git a/crates/ra_ide_api/src/snapshots/tests__highlighting.snap b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap new file mode 100644 index 000000000..72029e0ed --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap | |||
@@ -0,0 +1,34 @@ | |||
1 | --- | ||
2 | created: "2019-03-23T16:20:31.394314144Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/syntax_highlighting.rs | ||
5 | expression: result | ||
6 | --- | ||
7 | Ok( | ||
8 | [ | ||
9 | HighlightedRange { | ||
10 | range: [1; 11), | ||
11 | tag: "comment" | ||
12 | }, | ||
13 | HighlightedRange { | ||
14 | range: [12; 14), | ||
15 | tag: "keyword" | ||
16 | }, | ||
17 | HighlightedRange { | ||
18 | range: [15; 19), | ||
19 | tag: "function" | ||
20 | }, | ||
21 | HighlightedRange { | ||
22 | range: [29; 37), | ||
23 | tag: "macro" | ||
24 | }, | ||
25 | HighlightedRange { | ||
26 | range: [38; 50), | ||
27 | tag: "string" | ||
28 | }, | ||
29 | HighlightedRange { | ||
30 | range: [52; 54), | ||
31 | tag: "literal" | ||
32 | } | ||
33 | ] | ||
34 | ) | ||
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index fdd87bcff..a0c5e78ad 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs | |||
@@ -1,12 +1,81 @@ | |||
1 | use ra_syntax::AstNode; | 1 | use rustc_hash::FxHashSet; |
2 | |||
3 | use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*}; | ||
2 | use ra_db::SourceDatabase; | 4 | use ra_db::SourceDatabase; |
3 | 5 | ||
4 | use crate::{ | 6 | use crate::{FileId, db::RootDatabase}; |
5 | FileId, HighlightedRange, | 7 | |
6 | db::RootDatabase, | 8 | #[derive(Debug)] |
7 | }; | 9 | pub struct HighlightedRange { |
10 | pub range: TextRange, | ||
11 | pub tag: &'static str, | ||
12 | } | ||
8 | 13 | ||
9 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { | 14 | pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { |
10 | let source_file = db.parse(file_id); | 15 | let source_file = db.parse(file_id); |
11 | ra_ide_api_light::highlight(source_file.syntax()) | 16 | |
17 | // Visited nodes to handle highlighting priorities | ||
18 | let mut highlighted = FxHashSet::default(); | ||
19 | let mut res = Vec::new(); | ||
20 | for node in source_file.syntax().descendants() { | ||
21 | if highlighted.contains(&node) { | ||
22 | continue; | ||
23 | } | ||
24 | let tag = match node.kind() { | ||
25 | COMMENT => "comment", | ||
26 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", | ||
27 | ATTR => "attribute", | ||
28 | NAME_REF => "text", | ||
29 | NAME => "function", | ||
30 | INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal", | ||
31 | LIFETIME => "parameter", | ||
32 | k if k.is_keyword() => "keyword", | ||
33 | _ => { | ||
34 | if let Some(macro_call) = ast::MacroCall::cast(node) { | ||
35 | if let Some(path) = macro_call.path() { | ||
36 | if let Some(segment) = path.segment() { | ||
37 | if let Some(name_ref) = segment.name_ref() { | ||
38 | highlighted.insert(name_ref.syntax()); | ||
39 | let range_start = name_ref.syntax().range().start(); | ||
40 | let mut range_end = name_ref.syntax().range().end(); | ||
41 | for sibling in path.syntax().siblings(Direction::Next) { | ||
42 | match sibling.kind() { | ||
43 | EXCL | IDENT => range_end = sibling.range().end(), | ||
44 | _ => (), | ||
45 | } | ||
46 | } | ||
47 | res.push(HighlightedRange { | ||
48 | range: TextRange::from_to(range_start, range_end), | ||
49 | tag: "macro", | ||
50 | }) | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | continue; | ||
56 | } | ||
57 | }; | ||
58 | res.push(HighlightedRange { range: node.range(), tag }) | ||
59 | } | ||
60 | res | ||
61 | } | ||
62 | |||
63 | #[cfg(test)] | ||
64 | mod tests { | ||
65 | use insta::assert_debug_snapshot_matches; | ||
66 | |||
67 | use crate::mock_analysis::single_file; | ||
68 | |||
69 | #[test] | ||
70 | fn test_highlighting() { | ||
71 | let (analysis, file_id) = single_file( | ||
72 | r#" | ||
73 | // comment | ||
74 | fn main() {} | ||
75 | println!("Hello, {}!", 92); | ||
76 | "#, | ||
77 | ); | ||
78 | let result = analysis.highlight(file_id); | ||
79 | assert_debug_snapshot_matches!("highlighting", result); | ||
80 | } | ||
12 | } | 81 | } |