aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/syntax_highlighting.rs
diff options
context:
space:
mode:
authorPascal Hertleif <[email protected]>2019-05-25 11:56:52 +0100
committerPascal Hertleif <[email protected]>2019-05-27 10:26:35 +0100
commited89b0638b1dbf8f9a33d9a95e829e602142bb05 (patch)
treeb0b6b5f33fb25d79744662e1c17b6c610de67b95 /crates/ra_ide_api/src/syntax_highlighting.rs
parent5bf3e949e8470a138a61c806769e1a329761cab6 (diff)
Hash based on binding name and shadow counter
Diffstat (limited to 'crates/ra_ide_api/src/syntax_highlighting.rs')
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs26
1 files changed, 20 insertions, 6 deletions
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs
index da000c0c3..407fcda4a 100644
--- a/crates/ra_ide_api/src/syntax_highlighting.rs
+++ b/crates/ra_ide_api/src/syntax_highlighting.rs
@@ -1,6 +1,6 @@
1use rustc_hash::FxHashSet; 1use rustc_hash::{FxHashSet, FxHashMap};
2 2
3use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind, SyntaxKind::*, SyntaxElement, T}; 3use ra_syntax::{ast, AstNode, TextRange, Direction, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxElement, T};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_prof::profile; 5use ra_prof::profile;
6 6
@@ -43,6 +43,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
43 43
44 // Visited nodes to handle highlighting priorities 44 // Visited nodes to handle highlighting priorities
45 let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default(); 45 let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default();
46 let mut bindings_shadow_count: FxHashMap<SmolStr, u32> = FxHashMap::default();
47
46 let mut res = Vec::new(); 48 let mut res = Vec::new();
47 for node in source_file.syntax().descendants_with_tokens() { 49 for node in source_file.syntax().descendants_with_tokens() {
48 if highlighted.contains(&node) { 50 if highlighted.contains(&node) {
@@ -77,7 +79,11 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
77 Some(Def(ModuleDef::Trait(_))) => ("type", None), 79 Some(Def(ModuleDef::Trait(_))) => ("type", None),
78 Some(Def(ModuleDef::TypeAlias(_))) => ("type", None), 80 Some(Def(ModuleDef::TypeAlias(_))) => ("type", None),
79 Some(SelfType(_)) => ("type", None), 81 Some(SelfType(_)) => ("type", None),
80 Some(Pat(ptr)) => ("variable", Some(hash(ptr.syntax_node_ptr().range()))), 82 Some(Pat(ptr)) => ("variable", Some(hash({
83 let text = ptr.syntax_node_ptr().to_node(&source_file.syntax()).text().to_smol_string();
84 let shadow_count = bindings_shadow_count.entry(text.clone()).or_default();
85 (text, shadow_count)
86 }))),
81 Some(SelfParam(_)) => ("type", None), 87 Some(SelfParam(_)) => ("type", None),
82 Some(GenericParam(_)) => ("type", None), 88 Some(GenericParam(_)) => ("type", None),
83 None => ("text", None), 89 None => ("text", None),
@@ -88,7 +94,12 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
88 } 94 }
89 NAME => { 95 NAME => {
90 if let Some(name) = node.as_ast_node::<ast::Name>() { 96 if let Some(name) = node.as_ast_node::<ast::Name>() {
91 ("variable", Some(hash(name.syntax().range()))) 97 ("variable", Some(hash({
98 let text = name.syntax().text().to_smol_string();
99 let shadow_count = bindings_shadow_count.entry(text.clone()).or_insert(1);
100 *shadow_count += 1;
101 (text, shadow_count)
102 })))
92 } else { 103 } else {
93 ("text", None) 104 ("text", None)
94 } 105 }
@@ -240,16 +251,19 @@ fn main() {
240 } 251 }
241 252
242 #[test] 253 #[test]
243 fn test_sematic_highlighting() { 254 fn test_rainbow_highlighting() {
244 let (analysis, file_id) = single_file( 255 let (analysis, file_id) = single_file(
245 r#" 256 r#"
246fn main() { 257fn main() {
247 let hello = "hello"; 258 let hello = "hello";
248 let x = hello.to_string(); 259 let x = hello.to_string();
249 let y = hello.to_string(); 260 let y = hello.to_string();
261
262 let x = "other color please!";
263 let y = x.to_string();
250}"#, 264}"#,
251 ); 265 );
252 let result = analysis.highlight(file_id); 266 let result = analysis.highlight(file_id);
253 assert_debug_snapshot_matches!("sematic_highlighting", result); 267 assert_debug_snapshot_matches!("rainbow_highlighting", result);
254 } 268 }
255} 269}