diff options
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/syntax_highlighting.rs | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index e46686ab9..dcefb0513 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs | |||
@@ -85,13 +85,18 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
85 | Some(SelfType(_)) => "type", | 85 | Some(SelfType(_)) => "type", |
86 | Some(Pat(ptr)) => { | 86 | Some(Pat(ptr)) => { |
87 | binding_hash = Some({ | 87 | binding_hash = Some({ |
88 | let text = ptr.syntax_node_ptr().to_node(&source_file.syntax()).text().to_smol_string(); | 88 | let text = ptr |
89 | let shadow_count = bindings_shadow_count.entry(text.clone()).or_default(); | 89 | .syntax_node_ptr() |
90 | .to_node(&source_file.syntax()) | ||
91 | .text() | ||
92 | .to_smol_string(); | ||
93 | let shadow_count = | ||
94 | bindings_shadow_count.entry(text.clone()).or_default(); | ||
90 | calc_binding_hash(file_id, &text, *shadow_count) | 95 | calc_binding_hash(file_id, &text, *shadow_count) |
91 | }); | 96 | }); |
92 | 97 | ||
93 | "variable" | 98 | "variable" |
94 | }, | 99 | } |
95 | Some(SelfParam(_)) => "type", | 100 | Some(SelfParam(_)) => "type", |
96 | Some(GenericParam(_)) => "type", | 101 | Some(GenericParam(_)) => "type", |
97 | None => "text", | 102 | None => "text", |
@@ -105,7 +110,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
105 | if name.syntax().ancestors().any(|x| ast::BindPat::cast(x).is_some()) { | 110 | if name.syntax().ancestors().any(|x| ast::BindPat::cast(x).is_some()) { |
106 | binding_hash = Some({ | 111 | binding_hash = Some({ |
107 | let text = name.syntax().text().to_smol_string(); | 112 | let text = name.syntax().text().to_smol_string(); |
108 | let shadow_count = bindings_shadow_count.entry(text.clone()).or_insert(0); | 113 | let shadow_count = |
114 | bindings_shadow_count.entry(text.clone()).or_insert(0); | ||
109 | *shadow_count += 1; | 115 | *shadow_count += 1; |
110 | calc_binding_hash(file_id, &text, *shadow_count) | 116 | calc_binding_hash(file_id, &text, *shadow_count) |
111 | }); | 117 | }); |
@@ -161,7 +167,8 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
161 | fn rainbowify(seed: u64) -> String { | 167 | fn rainbowify(seed: u64) -> String { |
162 | use rand::prelude::*; | 168 | use rand::prelude::*; |
163 | let mut rng = SmallRng::seed_from_u64(seed); | 169 | let mut rng = SmallRng::seed_from_u64(seed); |
164 | format!("hsl({h},{s}%,{l}%)", | 170 | format!( |
171 | "hsl({h},{s}%,{l}%)", | ||
165 | h = rng.gen_range::<u16, _, _>(0, 361), | 172 | h = rng.gen_range::<u16, _, _>(0, 361), |
166 | s = rng.gen_range::<u16, _, _>(42, 99), | 173 | s = rng.gen_range::<u16, _, _>(42, 99), |
167 | l = rng.gen_range::<u16, _, _>(40, 91), | 174 | l = rng.gen_range::<u16, _, _>(40, 91), |
@@ -199,8 +206,12 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo | |||
199 | let classes = ranges.iter().map(|x| x.tag).collect::<Vec<_>>().join(" "); | 206 | let classes = ranges.iter().map(|x| x.tag).collect::<Vec<_>>().join(" "); |
200 | let binding_hash = ranges.first().and_then(|x| x.binding_hash); | 207 | let binding_hash = ranges.first().and_then(|x| x.binding_hash); |
201 | let color = match (rainbow, binding_hash) { | 208 | let color = match (rainbow, binding_hash) { |
202 | (true, Some(hash)) => format!(" data-binding-hash=\"{}\" style=\"color: {};\"", hash, rainbowify(hash)), | 209 | (true, Some(hash)) => format!( |
203 | _ => "".into() | 210 | " data-binding-hash=\"{}\" style=\"color: {};\"", |
211 | hash, | ||
212 | rainbowify(hash) | ||
213 | ), | ||
214 | _ => "".into(), | ||
204 | }; | 215 | }; |
205 | buf.push_str(&format!("<span class=\"{}\"{}>{}</span>", classes, color, text)); | 216 | buf.push_str(&format!("<span class=\"{}\"{}>{}</span>", classes, color, text)); |
206 | } | 217 | } |
@@ -264,10 +275,11 @@ fn main() { | |||
264 | } | 275 | } |
265 | unsafe { vec.set_len(0); } | 276 | unsafe { vec.set_len(0); } |
266 | } | 277 | } |
267 | "#.trim(), | 278 | "# |
279 | .trim(), | ||
268 | ); | 280 | ); |
269 | let dst_file = project_dir().join("crates/ra_ide_api/src/snapshots/highlighting.html"); | 281 | let dst_file = project_dir().join("crates/ra_ide_api/src/snapshots/highlighting.html"); |
270 | let actual_html = &analysis.highlight_as_html(file_id).unwrap(); | 282 | let actual_html = &analysis.highlight_as_html(file_id, true).unwrap(); |
271 | let expected_html = &read_text(&dst_file); | 283 | let expected_html = &read_text(&dst_file); |
272 | std::fs::write(dst_file, &actual_html).unwrap(); | 284 | std::fs::write(dst_file, &actual_html).unwrap(); |
273 | assert_eq_text!(expected_html, actual_html); | 285 | assert_eq_text!(expected_html, actual_html); |
@@ -285,10 +297,12 @@ fn main() { | |||
285 | let x = "other color please!"; | 297 | let x = "other color please!"; |
286 | let y = x.to_string(); | 298 | let y = x.to_string(); |
287 | } | 299 | } |
288 | "#.trim(), | 300 | "# |
301 | .trim(), | ||
289 | ); | 302 | ); |
290 | let dst_file = project_dir().join("crates/ra_ide_api/src/snapshots/rainbow_highlighting.html"); | 303 | let dst_file = |
291 | let actual_html = &analysis.highlight_as_html(file_id).unwrap(); | 304 | project_dir().join("crates/ra_ide_api/src/snapshots/rainbow_highlighting.html"); |
305 | let actual_html = &analysis.highlight_as_html(file_id, true).unwrap(); | ||
292 | let expected_html = &read_text(&dst_file); | 306 | let expected_html = &read_text(&dst_file); |
293 | std::fs::write(dst_file, &actual_html).unwrap(); | 307 | std::fs::write(dst_file, &actual_html).unwrap(); |
294 | assert_eq_text!(expected_html, actual_html); | 308 | assert_eq_text!(expected_html, actual_html); |