mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-25 19:35:06 +00:00
Hash based on binding name and shadow counter
This commit is contained in:
parent
5bf3e949e8
commit
ed89b0638b
4 changed files with 81 additions and 21 deletions
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
created: "2019-05-25T10:53:54.439877Z"
|
created: "2019-05-25T11:24:53.486036Z"
|
||||||
creator: insta@0.8.1
|
creator: insta@0.8.1
|
||||||
source: crates/ra_ide_api/src/syntax_highlighting.rs
|
source: crates/ra_ide_api/src/syntax_highlighting.rs
|
||||||
expression: result
|
expression: result
|
||||||
|
@ -20,7 +20,7 @@ Ok(
|
||||||
range: [32; 35),
|
range: [32; 35),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
461893210254723387,
|
8465336196764640996,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -32,7 +32,7 @@ Ok(
|
||||||
range: [46; 47),
|
range: [46; 47),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
8312289520117458465,
|
176272420896316891,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -49,7 +49,7 @@ Ok(
|
||||||
range: [62; 63),
|
range: [62; 63),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
4497542318236667727,
|
15061637676198917049,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -66,7 +66,7 @@ Ok(
|
||||||
range: [76; 79),
|
range: [76; 79),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
4506850079084802999,
|
14077410872302487760,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -78,7 +78,7 @@ Ok(
|
||||||
range: [80; 81),
|
range: [80; 81),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
16968185728268100018,
|
8379786015941272633,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -105,7 +105,7 @@ Ok(
|
||||||
range: [131; 135),
|
range: [131; 135),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
14467718814232352107,
|
5766414492220109266,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
created: "2019-05-25T10:25:13.898113Z"
|
created: "2019-05-25T11:21:56.117898Z"
|
||||||
creator: insta@0.8.1
|
creator: insta@0.8.1
|
||||||
source: crates/ra_ide_api/src/syntax_highlighting.rs
|
source: crates/ra_ide_api/src/syntax_highlighting.rs
|
||||||
expression: result
|
expression: result
|
||||||
|
@ -15,7 +15,7 @@ Ok(
|
||||||
range: [4; 8),
|
range: [4; 8),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
17119830160611610240,
|
5766414492220109266,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -27,7 +27,7 @@ Ok(
|
||||||
range: [21; 26),
|
range: [21; 26),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
2744494144922727377,
|
15975256018338854530,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -44,14 +44,14 @@ Ok(
|
||||||
range: [46; 47),
|
range: [46; 47),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
10375904121795371996,
|
176272420896316891,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
range: [50; 55),
|
range: [50; 55),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
2744494144922727377,
|
15975256018338854530,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -68,14 +68,14 @@ Ok(
|
||||||
range: [77; 78),
|
range: [77; 78),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
8228548264153724449,
|
15061637676198917049,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
range: [81; 86),
|
range: [81; 86),
|
||||||
tag: "variable",
|
tag: "variable",
|
||||||
id: Some(
|
id: Some(
|
||||||
2744494144922727377,
|
15975256018338854530,
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
HighlightedRange {
|
HighlightedRange {
|
||||||
|
@ -83,5 +83,46 @@ Ok(
|
||||||
tag: "text",
|
tag: "text",
|
||||||
id: None,
|
id: None,
|
||||||
},
|
},
|
||||||
|
HighlightedRange {
|
||||||
|
range: [105; 108),
|
||||||
|
tag: "keyword",
|
||||||
|
id: None,
|
||||||
|
},
|
||||||
|
HighlightedRange {
|
||||||
|
range: [109; 110),
|
||||||
|
tag: "variable",
|
||||||
|
id: Some(
|
||||||
|
1714508680417729339,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
HighlightedRange {
|
||||||
|
range: [113; 134),
|
||||||
|
tag: "string",
|
||||||
|
id: None,
|
||||||
|
},
|
||||||
|
HighlightedRange {
|
||||||
|
range: [140; 143),
|
||||||
|
tag: "keyword",
|
||||||
|
id: None,
|
||||||
|
},
|
||||||
|
HighlightedRange {
|
||||||
|
range: [144; 145),
|
||||||
|
tag: "variable",
|
||||||
|
id: Some(
|
||||||
|
15953336624848413466,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
HighlightedRange {
|
||||||
|
range: [148; 149),
|
||||||
|
tag: "variable",
|
||||||
|
id: Some(
|
||||||
|
1714508680417729339,
|
||||||
|
),
|
||||||
|
},
|
||||||
|
HighlightedRange {
|
||||||
|
range: [150; 159),
|
||||||
|
tag: "text",
|
||||||
|
id: None,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
)
|
)
|
|
@ -1,6 +1,6 @@
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::{FxHashSet, FxHashMap};
|
||||||
|
|
||||||
use ra_syntax::{ast, AstNode, TextRange, Direction, SyntaxKind, SyntaxKind::*, SyntaxElement, T};
|
use ra_syntax::{ast, AstNode, TextRange, Direction, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxElement, T};
|
||||||
use ra_db::SourceDatabase;
|
use ra_db::SourceDatabase;
|
||||||
use ra_prof::profile;
|
use ra_prof::profile;
|
||||||
|
|
||||||
|
@ -43,6 +43,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
|
|
||||||
// Visited nodes to handle highlighting priorities
|
// Visited nodes to handle highlighting priorities
|
||||||
let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default();
|
let mut highlighted: FxHashSet<SyntaxElement> = FxHashSet::default();
|
||||||
|
let mut bindings_shadow_count: FxHashMap<SmolStr, u32> = FxHashMap::default();
|
||||||
|
|
||||||
let mut res = Vec::new();
|
let mut res = Vec::new();
|
||||||
for node in source_file.syntax().descendants_with_tokens() {
|
for node in source_file.syntax().descendants_with_tokens() {
|
||||||
if highlighted.contains(&node) {
|
if highlighted.contains(&node) {
|
||||||
|
@ -77,7 +79,11 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
Some(Def(ModuleDef::Trait(_))) => ("type", None),
|
Some(Def(ModuleDef::Trait(_))) => ("type", None),
|
||||||
Some(Def(ModuleDef::TypeAlias(_))) => ("type", None),
|
Some(Def(ModuleDef::TypeAlias(_))) => ("type", None),
|
||||||
Some(SelfType(_)) => ("type", None),
|
Some(SelfType(_)) => ("type", None),
|
||||||
Some(Pat(ptr)) => ("variable", Some(hash(ptr.syntax_node_ptr().range()))),
|
Some(Pat(ptr)) => ("variable", Some(hash({
|
||||||
|
let text = ptr.syntax_node_ptr().to_node(&source_file.syntax()).text().to_smol_string();
|
||||||
|
let shadow_count = bindings_shadow_count.entry(text.clone()).or_default();
|
||||||
|
(text, shadow_count)
|
||||||
|
}))),
|
||||||
Some(SelfParam(_)) => ("type", None),
|
Some(SelfParam(_)) => ("type", None),
|
||||||
Some(GenericParam(_)) => ("type", None),
|
Some(GenericParam(_)) => ("type", None),
|
||||||
None => ("text", None),
|
None => ("text", None),
|
||||||
|
@ -88,7 +94,12 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
}
|
}
|
||||||
NAME => {
|
NAME => {
|
||||||
if let Some(name) = node.as_ast_node::<ast::Name>() {
|
if let Some(name) = node.as_ast_node::<ast::Name>() {
|
||||||
("variable", Some(hash(name.syntax().range())))
|
("variable", Some(hash({
|
||||||
|
let text = name.syntax().text().to_smol_string();
|
||||||
|
let shadow_count = bindings_shadow_count.entry(text.clone()).or_insert(1);
|
||||||
|
*shadow_count += 1;
|
||||||
|
(text, shadow_count)
|
||||||
|
})))
|
||||||
} else {
|
} else {
|
||||||
("text", None)
|
("text", None)
|
||||||
}
|
}
|
||||||
|
@ -240,16 +251,19 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sematic_highlighting() {
|
fn test_rainbow_highlighting() {
|
||||||
let (analysis, file_id) = single_file(
|
let (analysis, file_id) = single_file(
|
||||||
r#"
|
r#"
|
||||||
fn main() {
|
fn main() {
|
||||||
let hello = "hello";
|
let hello = "hello";
|
||||||
let x = hello.to_string();
|
let x = hello.to_string();
|
||||||
let y = hello.to_string();
|
let y = hello.to_string();
|
||||||
|
|
||||||
|
let x = "other color please!";
|
||||||
|
let y = x.to_string();
|
||||||
}"#,
|
}"#,
|
||||||
);
|
);
|
||||||
let result = analysis.highlight(file_id);
|
let result = analysis.highlight(file_id);
|
||||||
assert_debug_snapshot_matches!("sematic_highlighting", result);
|
assert_debug_snapshot_matches!("rainbow_highlighting", result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{fmt, ops::{self, Bound}};
|
use std::{fmt, ops::{self, Bound}};
|
||||||
|
|
||||||
use crate::{SyntaxNode, TextRange, TextUnit, SyntaxElement};
|
use crate::{SmolStr, SyntaxNode, TextRange, TextUnit, SyntaxElement};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct SyntaxText<'a> {
|
pub struct SyntaxText<'a> {
|
||||||
|
@ -34,6 +34,11 @@ impl<'a> SyntaxText<'a> {
|
||||||
self.chunks().collect()
|
self.chunks().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn to_smol_string(&self) -> SmolStr {
|
||||||
|
// TODO: `impl iter::FromIterator<&str> for SmolStr`
|
||||||
|
self.to_string().into()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn contains(&self, c: char) -> bool {
|
pub fn contains(&self, c: char) -> bool {
|
||||||
self.chunks().any(|it| it.contains(c))
|
self.chunks().any(|it| it.contains(c))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue