diff options
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/html.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/injection.rs | 26 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tags.rs | 8 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 36 |
4 files changed, 50 insertions, 22 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 99b6b25ab..0c74f7370 100644 --- a/crates/ra_ide/src/syntax_highlighting/html.rs +++ b/crates/ra_ide/src/syntax_highlighting/html.rs | |||
@@ -64,6 +64,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
64 | 64 | ||
65 | .lifetime { color: #DFAF8F; font-style: italic; } | 65 | .lifetime { color: #DFAF8F; font-style: italic; } |
66 | .comment { color: #7F9F7F; } | 66 | .comment { color: #7F9F7F; } |
67 | .documentation { color: #629755; } | ||
68 | .injected { opacity: 0.65 ; } | ||
67 | .struct, .enum { color: #7CB8BB; } | 69 | .struct, .enum { color: #7CB8BB; } |
68 | .enum_variant { color: #BDE0F3; } | 70 | .enum_variant { color: #BDE0F3; } |
69 | .string_literal { color: #CC9393; } | 71 | .string_literal { color: #CC9393; } |
diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 929a5cc5c..181c21256 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs | |||
@@ -7,7 +7,10 @@ use hir::Semantics; | |||
7 | use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; | 7 | use ra_syntax::{ast, AstToken, SyntaxNode, SyntaxToken, TextRange, TextSize}; |
8 | use stdx::SepBy; | 8 | use stdx::SepBy; |
9 | 9 | ||
10 | use crate::{call_info::ActiveParameter, Analysis, HighlightTag, HighlightedRange, RootDatabase}; | 10 | use crate::{ |
11 | call_info::ActiveParameter, Analysis, Highlight, HighlightModifier, HighlightTag, | ||
12 | HighlightedRange, RootDatabase, | ||
13 | }; | ||
11 | 14 | ||
12 | use super::HighlightedRangeStack; | 15 | use super::HighlightedRangeStack; |
13 | 16 | ||
@@ -118,7 +121,7 @@ pub(super) fn extract_doc_comments( | |||
118 | range.start(), | 121 | range.start(), |
119 | range.start() + TextSize::try_from(pos).unwrap(), | 122 | range.start() + TextSize::try_from(pos).unwrap(), |
120 | ), | 123 | ), |
121 | highlight: HighlightTag::Comment.into(), | 124 | highlight: HighlightTag::Comment | HighlightModifier::Documentation, |
122 | binding_hash: None, | 125 | binding_hash: None, |
123 | }); | 126 | }); |
124 | line_start += range.len() - TextSize::try_from(pos).unwrap(); | 127 | line_start += range.len() - TextSize::try_from(pos).unwrap(); |
@@ -152,18 +155,24 @@ pub(super) fn highlight_doc_comment( | |||
152 | let mut start_offset = None; | 155 | let mut start_offset = None; |
153 | let mut end_offset = None; | 156 | let mut end_offset = None; |
154 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { | 157 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { |
158 | // It's possible for orig_line_start - line_start to be negative. Add h.range.start() | ||
159 | // here and remove it from the end range after the loop below so that the values are | ||
160 | // always non-negative. | ||
161 | let offset = h.range.start() + orig_line_start - line_start; | ||
155 | if line_start <= &h.range.start() { | 162 | if line_start <= &h.range.start() { |
156 | start_offset.get_or_insert(orig_line_start - line_start); | 163 | start_offset.get_or_insert(offset); |
157 | break; | 164 | break; |
158 | } else { | 165 | } else { |
159 | end_offset.get_or_insert(orig_line_start - line_start); | 166 | end_offset.get_or_insert(offset); |
160 | } | 167 | } |
161 | } | 168 | } |
162 | if let Some(start_offset) = start_offset { | 169 | if let Some(start_offset) = start_offset { |
163 | h.range = TextRange::new( | 170 | h.range = TextRange::new( |
164 | h.range.start() + start_offset, | 171 | start_offset, |
165 | h.range.end() + end_offset.unwrap_or(start_offset), | 172 | h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(), |
166 | ); | 173 | ); |
174 | |||
175 | h.highlight |= HighlightModifier::Injected; | ||
167 | stack.add(h); | 176 | stack.add(h); |
168 | } | 177 | } |
169 | } | 178 | } |
@@ -173,6 +182,7 @@ pub(super) fn highlight_doc_comment( | |||
173 | for comment in new_comments { | 182 | for comment in new_comments { |
174 | stack.add(comment); | 183 | stack.add(comment); |
175 | } | 184 | } |
176 | stack.pop_and_inject(false); | 185 | stack.pop_and_inject(None); |
177 | stack.pop_and_inject(true); | 186 | stack |
187 | .pop_and_inject(Some(Highlight::from(HighlightTag::Generic) | HighlightModifier::Injected)); | ||
178 | } | 188 | } |
diff --git a/crates/ra_ide/src/syntax_highlighting/tags.rs b/crates/ra_ide/src/syntax_highlighting/tags.rs index 400d22fb6..e8e251cfc 100644 --- a/crates/ra_ide/src/syntax_highlighting/tags.rs +++ b/crates/ra_ide/src/syntax_highlighting/tags.rs | |||
@@ -27,6 +27,7 @@ pub enum HighlightTag { | |||
27 | Field, | 27 | Field, |
28 | FormatSpecifier, | 28 | FormatSpecifier, |
29 | Function, | 29 | Function, |
30 | Generic, | ||
30 | Keyword, | 31 | Keyword, |
31 | Lifetime, | 32 | Lifetime, |
32 | Macro, | 33 | Macro, |
@@ -56,6 +57,8 @@ pub enum HighlightModifier { | |||
56 | /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is | 57 | /// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is |
57 | /// not. | 58 | /// not. |
58 | Definition, | 59 | Definition, |
60 | Documentation, | ||
61 | Injected, | ||
59 | Mutable, | 62 | Mutable, |
60 | Unsafe, | 63 | Unsafe, |
61 | } | 64 | } |
@@ -76,6 +79,7 @@ impl HighlightTag { | |||
76 | HighlightTag::Field => "field", | 79 | HighlightTag::Field => "field", |
77 | HighlightTag::FormatSpecifier => "format_specifier", | 80 | HighlightTag::FormatSpecifier => "format_specifier", |
78 | HighlightTag::Function => "function", | 81 | HighlightTag::Function => "function", |
82 | HighlightTag::Generic => "generic", | ||
79 | HighlightTag::Keyword => "keyword", | 83 | HighlightTag::Keyword => "keyword", |
80 | HighlightTag::Lifetime => "lifetime", | 84 | HighlightTag::Lifetime => "lifetime", |
81 | HighlightTag::Macro => "macro", | 85 | HighlightTag::Macro => "macro", |
@@ -108,6 +112,8 @@ impl HighlightModifier { | |||
108 | HighlightModifier::Attribute, | 112 | HighlightModifier::Attribute, |
109 | HighlightModifier::ControlFlow, | 113 | HighlightModifier::ControlFlow, |
110 | HighlightModifier::Definition, | 114 | HighlightModifier::Definition, |
115 | HighlightModifier::Documentation, | ||
116 | HighlightModifier::Injected, | ||
111 | HighlightModifier::Mutable, | 117 | HighlightModifier::Mutable, |
112 | HighlightModifier::Unsafe, | 118 | HighlightModifier::Unsafe, |
113 | ]; | 119 | ]; |
@@ -117,6 +123,8 @@ impl HighlightModifier { | |||
117 | HighlightModifier::Attribute => "attribute", | 123 | HighlightModifier::Attribute => "attribute", |
118 | HighlightModifier::ControlFlow => "control", | 124 | HighlightModifier::ControlFlow => "control", |
119 | HighlightModifier::Definition => "declaration", | 125 | HighlightModifier::Definition => "declaration", |
126 | HighlightModifier::Documentation => "documentation", | ||
127 | HighlightModifier::Injected => "injected", | ||
120 | HighlightModifier::Mutable => "mutable", | 128 | HighlightModifier::Mutable => "mutable", |
121 | HighlightModifier::Unsafe => "unsafe", | 129 | HighlightModifier::Unsafe => "unsafe", |
122 | } | 130 | } |
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index b4d56a7a0..b7fad9719 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs | |||
@@ -2,10 +2,7 @@ use std::fs; | |||
2 | 2 | ||
3 | use test_utils::{assert_eq_text, project_dir, read_text}; | 3 | use test_utils::{assert_eq_text, project_dir, read_text}; |
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{mock_analysis::single_file, FileRange, TextRange}; |
6 | mock_analysis::{single_file, MockAnalysis}, | ||
7 | FileRange, TextRange, | ||
8 | }; | ||
9 | 6 | ||
10 | #[test] | 7 | #[test] |
11 | fn test_highlighting() { | 8 | fn test_highlighting() { |
@@ -127,12 +124,10 @@ fn accidentally_quadratic() { | |||
127 | let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic"); | 124 | let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic"); |
128 | let src = fs::read_to_string(file).unwrap(); | 125 | let src = fs::read_to_string(file).unwrap(); |
129 | 126 | ||
130 | let mut mock = MockAnalysis::new(); | 127 | let (analysis, file_id) = single_file(&src); |
131 | let file_id = mock.add_file("/main.rs", &src); | ||
132 | let host = mock.analysis_host(); | ||
133 | 128 | ||
134 | // let t = std::time::Instant::now(); | 129 | // let t = std::time::Instant::now(); |
135 | let _ = host.analysis().highlight(file_id).unwrap(); | 130 | let _ = analysis.highlight(file_id).unwrap(); |
136 | // eprintln!("elapsed: {:?}", t.elapsed()); | 131 | // eprintln!("elapsed: {:?}", t.elapsed()); |
137 | } | 132 | } |
138 | 133 | ||
@@ -140,16 +135,17 @@ fn accidentally_quadratic() { | |||
140 | fn test_ranges() { | 135 | fn test_ranges() { |
141 | let (analysis, file_id) = single_file( | 136 | let (analysis, file_id) = single_file( |
142 | r#" | 137 | r#" |
143 | #[derive(Clone, Debug)] | 138 | #[derive(Clone, Debug)] |
144 | struct Foo { | 139 | struct Foo { |
145 | pub x: i32, | 140 | pub x: i32, |
146 | pub y: i32, | 141 | pub y: i32, |
147 | }"#, | 142 | } |
143 | "#, | ||
148 | ); | 144 | ); |
149 | 145 | ||
150 | // The "x" | 146 | // The "x" |
151 | let highlights = &analysis | 147 | let highlights = &analysis |
152 | .highlight_range(FileRange { file_id, range: TextRange::at(82.into(), 1.into()) }) | 148 | .highlight_range(FileRange { file_id, range: TextRange::at(45.into(), 1.into()) }) |
153 | .unwrap(); | 149 | .unwrap(); |
154 | 150 | ||
155 | assert_eq!(&highlights[0].highlight.to_string(), "field.declaration"); | 151 | assert_eq!(&highlights[0].highlight.to_string(), "field.declaration"); |
@@ -291,6 +287,9 @@ fn main() { | |||
291 | fn test_highlight_doctest() { | 287 | fn test_highlight_doctest() { |
292 | check_highlighting( | 288 | check_highlighting( |
293 | r#" | 289 | r#" |
290 | /// ``` | ||
291 | /// let _ = "early doctests should not go boom"; | ||
292 | /// ``` | ||
294 | struct Foo { | 293 | struct Foo { |
295 | bar: bool, | 294 | bar: bool, |
296 | } | 295 | } |
@@ -344,6 +343,15 @@ impl Foo { | |||
344 | true | 343 | true |
345 | } | 344 | } |
346 | } | 345 | } |
346 | |||
347 | /// ``` | ||
348 | /// noop!(1); | ||
349 | /// ``` | ||
350 | macro_rules! noop { | ||
351 | ($expr:expr) => { | ||
352 | $expr | ||
353 | } | ||
354 | } | ||
347 | "# | 355 | "# |
348 | .trim(), | 356 | .trim(), |
349 | "crates/ra_ide/src/snapshots/highlight_doctest.html", | 357 | "crates/ra_ide/src/snapshots/highlight_doctest.html", |