aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting.rs')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs33
1 files changed, 26 insertions, 7 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 56a36f587..0411977b9 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -2,7 +2,7 @@
2 2
3use rustc_hash::{FxHashMap, FxHashSet}; 3use rustc_hash::{FxHashMap, FxHashSet};
4 4
5use hir::{InFile, Name}; 5use hir::{InFile, Name, SourceBinder};
6use ra_db::SourceDatabase; 6use ra_db::SourceDatabase;
7use ra_prof::profile; 7use ra_prof::profile;
8use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T}; 8use ra_syntax::{ast, AstNode, Direction, SyntaxElement, SyntaxKind, SyntaxKind::*, TextRange, T};
@@ -84,6 +84,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
84 hash((file_id, name, shadow_count)) 84 hash((file_id, name, shadow_count))
85 } 85 }
86 86
87 let mut sb = SourceBinder::new(db);
88
87 // Visited nodes to handle highlighting priorities 89 // Visited nodes to handle highlighting priorities
88 // FIXME: retain only ranges here 90 // FIXME: retain only ranges here
89 let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default(); 91 let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default();
@@ -108,8 +110,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
108 NAME_REF if node.ancestors().any(|it| it.kind() == ATTR) => continue, 110 NAME_REF if node.ancestors().any(|it| it.kind() == ATTR) => continue,
109 NAME_REF => { 111 NAME_REF => {
110 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap(); 112 let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
111 let name_kind = 113 let name_kind = classify_name_ref(&mut sb, InFile::new(file_id.into(), &name_ref))
112 classify_name_ref(db, InFile::new(file_id.into(), &name_ref)).map(|d| d.kind); 114 .map(|d| d.kind);
113 match name_kind { 115 match name_kind {
114 Some(name_kind) => { 116 Some(name_kind) => {
115 if let Local(local) = &name_kind { 117 if let Local(local) = &name_kind {
@@ -129,7 +131,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
129 NAME => { 131 NAME => {
130 let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap(); 132 let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap();
131 let name_kind = 133 let name_kind =
132 classify_name(db, InFile::new(file_id.into(), &name)).map(|d| d.kind); 134 classify_name(&mut sb, InFile::new(file_id.into(), &name)).map(|d| d.kind);
133 135
134 if let Some(Local(local)) = &name_kind { 136 if let Some(Local(local)) = &name_kind {
135 if let Some(name) = local.name(db) { 137 if let Some(name) = local.name(db) {
@@ -308,9 +310,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
308 310
309#[cfg(test)] 311#[cfg(test)]
310mod tests { 312mod tests {
311 use crate::mock_analysis::single_file; 313 use std::fs;
314
312 use test_utils::{assert_eq_text, project_dir, read_text}; 315 use test_utils::{assert_eq_text, project_dir, read_text};
313 316
317 use crate::mock_analysis::{single_file, MockAnalysis};
318
314 #[test] 319 #[test]
315 fn test_highlighting() { 320 fn test_highlighting() {
316 let (analysis, file_id) = single_file( 321 let (analysis, file_id) = single_file(
@@ -357,7 +362,7 @@ impl<X> E<X> {
357 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html"); 362 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html");
358 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap(); 363 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
359 let expected_html = &read_text(&dst_file); 364 let expected_html = &read_text(&dst_file);
360 std::fs::write(dst_file, &actual_html).unwrap(); 365 fs::write(dst_file, &actual_html).unwrap();
361 assert_eq_text!(expected_html, actual_html); 366 assert_eq_text!(expected_html, actual_html);
362 } 367 }
363 368
@@ -383,7 +388,21 @@ fn bar() {
383 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html"); 388 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html");
384 let actual_html = &analysis.highlight_as_html(file_id, true).unwrap(); 389 let actual_html = &analysis.highlight_as_html(file_id, true).unwrap();
385 let expected_html = &read_text(&dst_file); 390 let expected_html = &read_text(&dst_file);
386 std::fs::write(dst_file, &actual_html).unwrap(); 391 fs::write(dst_file, &actual_html).unwrap();
387 assert_eq_text!(expected_html, actual_html); 392 assert_eq_text!(expected_html, actual_html);
388 } 393 }
394
395 #[test]
396 fn accidentally_quadratic() {
397 let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic");
398 let src = fs::read_to_string(file).unwrap();
399
400 let mut mock = MockAnalysis::new();
401 let file_id = mock.add_file("/main.rs", &src);
402 let host = mock.analysis_host();
403
404 // let t = std::time::Instant::now();
405 let _ = host.analysis().highlight(file_id).unwrap();
406 // eprintln!("elapsed: {:?}", t.elapsed());
407 }
389} 408}