aboutsummaryrefslogtreecommitdiff
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
parent5bf3e949e8470a138a61c806769e1a329761cab6 (diff)
Hash based on binding name and shadow counter
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__highlighting.snap14
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__rainbow_highlighting.snap (renamed from crates/ra_ide_api/src/snapshots/tests__sematic_highlighting.snap)55
-rw-r--r--crates/ra_ide_api/src/syntax_highlighting.rs26
-rw-r--r--crates/ra_syntax/src/syntax_text.rs7
4 files changed, 81 insertions, 21 deletions
diff --git a/crates/ra_ide_api/src/snapshots/tests__highlighting.snap b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap
index 208681f10..e50003b3c 100644
--- a/crates/ra_ide_api/src/snapshots/tests__highlighting.snap
+++ b/crates/ra_ide_api/src/snapshots/tests__highlighting.snap
@@ -1,5 +1,5 @@
1--- 1---
2created: "2019-05-25T10:53:54.439877Z" 2created: "2019-05-25T11:24:53.486036Z"
3creator: [email protected] 3creator: [email protected]
4source: crates/ra_ide_api/src/syntax_highlighting.rs 4source: crates/ra_ide_api/src/syntax_highlighting.rs
5expression: result 5expression: result
@@ -20,7 +20,7 @@ Ok(
20 range: [32; 35), 20 range: [32; 35),
21 tag: "variable", 21 tag: "variable",
22 id: Some( 22 id: Some(
23 461893210254723387, 23 8465336196764640996,
24 ), 24 ),
25 }, 25 },
26 HighlightedRange { 26 HighlightedRange {
@@ -32,7 +32,7 @@ Ok(
32 range: [46; 47), 32 range: [46; 47),
33 tag: "variable", 33 tag: "variable",
34 id: Some( 34 id: Some(
35 8312289520117458465, 35 176272420896316891,
36 ), 36 ),
37 }, 37 },
38 HighlightedRange { 38 HighlightedRange {
@@ -49,7 +49,7 @@ Ok(
49 range: [62; 63), 49 range: [62; 63),
50 tag: "variable", 50 tag: "variable",
51 id: Some( 51 id: Some(
52 4497542318236667727, 52 15061637676198917049,
53 ), 53 ),
54 }, 54 },
55 HighlightedRange { 55 HighlightedRange {
@@ -66,7 +66,7 @@ Ok(
66 range: [76; 79), 66 range: [76; 79),
67 tag: "variable", 67 tag: "variable",
68 id: Some( 68 id: Some(
69 4506850079084802999, 69 14077410872302487760,
70 ), 70 ),
71 }, 71 },
72 HighlightedRange { 72 HighlightedRange {
@@ -78,7 +78,7 @@ Ok(
78 range: [80; 81), 78 range: [80; 81),
79 tag: "variable", 79 tag: "variable",
80 id: Some( 80 id: Some(
81 16968185728268100018, 81 8379786015941272633,
82 ), 82 ),
83 }, 83 },
84 HighlightedRange { 84 HighlightedRange {
@@ -105,7 +105,7 @@ Ok(
105 range: [131; 135), 105 range: [131; 135),
106 tag: "variable", 106 tag: "variable",
107 id: Some( 107 id: Some(
108 14467718814232352107, 108 5766414492220109266,
109 ), 109 ),
110 }, 110 },
111 HighlightedRange { 111 HighlightedRange {
diff --git a/crates/ra_ide_api/src/snapshots/tests__sematic_highlighting.snap b/crates/ra_ide_api/src/snapshots/tests__rainbow_highlighting.snap
index 3b3fe32e9..84cd521a2 100644
--- a/crates/ra_ide_api/src/snapshots/tests__sematic_highlighting.snap
+++ b/crates/ra_ide_api/src/snapshots/tests__rainbow_highlighting.snap
@@ -1,5 +1,5 @@
1--- 1---
2created: "2019-05-25T10:25:13.898113Z" 2created: "2019-05-25T11:21:56.117898Z"
3creator: [email protected] 3creator: [email protected]
4source: crates/ra_ide_api/src/syntax_highlighting.rs 4source: crates/ra_ide_api/src/syntax_highlighting.rs
5expression: result 5expression: result
@@ -15,7 +15,7 @@ Ok(
15 range: [4; 8), 15 range: [4; 8),
16 tag: "variable", 16 tag: "variable",
17 id: Some( 17 id: Some(
18 17119830160611610240, 18 5766414492220109266,
19 ), 19 ),
20 }, 20 },
21 HighlightedRange { 21 HighlightedRange {
@@ -27,7 +27,7 @@ Ok(
27 range: [21; 26), 27 range: [21; 26),
28 tag: "variable", 28 tag: "variable",
29 id: Some( 29 id: Some(
30 2744494144922727377, 30 15975256018338854530,
31 ), 31 ),
32 }, 32 },
33 HighlightedRange { 33 HighlightedRange {
@@ -44,14 +44,14 @@ Ok(
44 range: [46; 47), 44 range: [46; 47),
45 tag: "variable", 45 tag: "variable",
46 id: Some( 46 id: Some(
47 10375904121795371996, 47 176272420896316891,
48 ), 48 ),
49 }, 49 },
50 HighlightedRange { 50 HighlightedRange {
51 range: [50; 55), 51 range: [50; 55),
52 tag: "variable", 52 tag: "variable",
53 id: Some( 53 id: Some(
54 2744494144922727377, 54 15975256018338854530,
55 ), 55 ),
56 }, 56 },
57 HighlightedRange { 57 HighlightedRange {
@@ -68,14 +68,14 @@ Ok(
68 range: [77; 78), 68 range: [77; 78),
69 tag: "variable", 69 tag: "variable",
70 id: Some( 70 id: Some(
71 8228548264153724449, 71 15061637676198917049,
72 ), 72 ),
73 }, 73 },
74 HighlightedRange { 74 HighlightedRange {
75 range: [81; 86), 75 range: [81; 86),
76 tag: "variable", 76 tag: "variable",
77 id: Some( 77 id: Some(
78 2744494144922727377, 78 15975256018338854530,
79 ), 79 ),
80 }, 80 },
81 HighlightedRange { 81 HighlightedRange {
@@ -83,5 +83,46 @@ Ok(
83 tag: "text", 83 tag: "text",
84 id: None, 84 id: None,
85 }, 85 },
86 HighlightedRange {
87 range: [105; 108),
88 tag: "keyword",
89 id: None,
90 },
91 HighlightedRange {
92 range: [109; 110),
93 tag: "variable",
94 id: Some(
95 1714508680417729339,
96 ),
97 },
98 HighlightedRange {
99 range: [113; 134),
100 tag: "string",
101 id: None,
102 },
103 HighlightedRange {
104 range: [140; 143),
105 tag: "keyword",
106 id: None,
107 },
108 HighlightedRange {
109 range: [144; 145),
110 tag: "variable",
111 id: Some(
112 15953336624848413466,
113 ),
114 },
115 HighlightedRange {
116 range: [148; 149),
117 tag: "variable",
118 id: Some(
119 1714508680417729339,
120 ),
121 },
122 HighlightedRange {
123 range: [150; 159),
124 tag: "text",
125 id: None,
126 },
86 ], 127 ],
87) 128)
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}
diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs
index b013164c4..bff1ed5a0 100644
--- a/crates/ra_syntax/src/syntax_text.rs
+++ b/crates/ra_syntax/src/syntax_text.rs
@@ -1,6 +1,6 @@
1use std::{fmt, ops::{self, Bound}}; 1use std::{fmt, ops::{self, Bound}};
2 2
3use crate::{SyntaxNode, TextRange, TextUnit, SyntaxElement}; 3use crate::{SmolStr, SyntaxNode, TextRange, TextUnit, SyntaxElement};
4 4
5#[derive(Clone)] 5#[derive(Clone)]
6pub struct SyntaxText<'a> { 6pub struct SyntaxText<'a> {
@@ -34,6 +34,11 @@ impl<'a> SyntaxText<'a> {
34 self.chunks().collect() 34 self.chunks().collect()
35 } 35 }
36 36
37 pub fn to_smol_string(&self) -> SmolStr {
38 // TODO: `impl iter::FromIterator<&str> for SmolStr`
39 self.to_string().into()
40 }
41
37 pub fn contains(&self, c: char) -> bool { 42 pub fn contains(&self, c: char) -> bool {
38 self.chunks().any(|it| it.contains(c)) 43 self.chunks().any(|it| it.contains(c))
39 } 44 }