aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs7
-rw-r--r--crates/ra_ide_api/src/lib.rs9
-rw-r--r--crates/ra_ide_api/src/matching_brace.rs45
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__highlighting.snap34
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs81
-rw-r--r--crates/ra_ide_api_light/src/lib.rs122
-rw-r--r--crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap32
7 files changed, 166 insertions, 164 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 @@
1use itertools::Itertools; 1use itertools::Itertools;
2use hir::{Problem, source_binder}; 2use hir::{Problem, source_binder};
3use ra_ide_api_light::Severity;
4use ra_db::SourceDatabase; 3use ra_db::SourceDatabase;
5use ra_syntax::{ 4use 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
12use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase}; 11use crate::{Diagnostic, FileId, FileSystemEdit, SourceChange, SourceFileEdit, db::RootDatabase};
13 12
13#[derive(Debug, Copy, Clone)]
14pub enum Severity {
15 Error,
16 WeakWarning,
17}
18
14pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> { 19pub(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;
38mod line_index_utils; 38mod line_index_utils;
39mod join_lines; 39mod join_lines;
40mod typing; 40mod typing;
41mod matching_brace;
41 42
42#[cfg(test)] 43#[cfg(test)]
43mod marks; 44mod 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};
74pub use ra_ide_api_light::{ 77pub use ra_ide_api_light::StructureNode;
75 HighlightedRange, Severity, StructureNode,
76};
77pub use ra_db::{ 78pub 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 @@
1use ra_syntax::{
2 SourceFile, TextUnit,
3 algo::find_leaf_at_offset,
4 SyntaxKind::{self, *},
5 ast::AstNode,
6};
7
8pub 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)]
24mod 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---
2created: "2019-03-23T16:20:31.394314144Z"
3creator: [email protected]
4source: crates/ra_ide_api/src/syntax_highlighting.rs
5expression: result
6---
7Ok(
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 @@
1use ra_syntax::AstNode; 1use rustc_hash::FxHashSet;
2
3use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind::*};
2use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
3 5
4use crate::{ 6use crate::{FileId, db::RootDatabase};
5 FileId, HighlightedRange, 7
6 db::RootDatabase, 8#[derive(Debug)]
7}; 9pub struct HighlightedRange {
10 pub range: TextRange,
11 pub tag: &'static str,
12}
8 13
9pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { 14pub(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)]
64mod 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
74fn main() {}
75 println!("Hello, {}!", 92);
76"#,
77 );
78 let result = analysis.highlight(file_id);
79 assert_debug_snapshot_matches!("highlighting", result);
80 }
12} 81}
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]