From ed89b0638b1dbf8f9a33d9a95e829e602142bb05 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Sat, 25 May 2019 12:56:52 +0200 Subject: Hash based on binding name and shadow counter --- .../src/snapshots/tests__highlighting.snap | 14 +-- .../src/snapshots/tests__rainbow_highlighting.snap | 128 +++++++++++++++++++++ .../src/snapshots/tests__sematic_highlighting.snap | 87 -------------- crates/ra_ide_api/src/syntax_highlighting.rs | 26 ++++- crates/ra_syntax/src/syntax_text.rs | 7 +- 5 files changed, 161 insertions(+), 101 deletions(-) create mode 100644 crates/ra_ide_api/src/snapshots/tests__rainbow_highlighting.snap delete mode 100644 crates/ra_ide_api/src/snapshots/tests__sematic_highlighting.snap 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 @@ --- -created: "2019-05-25T10:53:54.439877Z" +created: "2019-05-25T11:24:53.486036Z" creator: insta@0.8.1 source: crates/ra_ide_api/src/syntax_highlighting.rs expression: result @@ -20,7 +20,7 @@ Ok( range: [32; 35), tag: "variable", id: Some( - 461893210254723387, + 8465336196764640996, ), }, HighlightedRange { @@ -32,7 +32,7 @@ Ok( range: [46; 47), tag: "variable", id: Some( - 8312289520117458465, + 176272420896316891, ), }, HighlightedRange { @@ -49,7 +49,7 @@ Ok( range: [62; 63), tag: "variable", id: Some( - 4497542318236667727, + 15061637676198917049, ), }, HighlightedRange { @@ -66,7 +66,7 @@ Ok( range: [76; 79), tag: "variable", id: Some( - 4506850079084802999, + 14077410872302487760, ), }, HighlightedRange { @@ -78,7 +78,7 @@ Ok( range: [80; 81), tag: "variable", id: Some( - 16968185728268100018, + 8379786015941272633, ), }, HighlightedRange { @@ -105,7 +105,7 @@ Ok( range: [131; 135), tag: "variable", id: Some( - 14467718814232352107, + 5766414492220109266, ), }, HighlightedRange { diff --git a/crates/ra_ide_api/src/snapshots/tests__rainbow_highlighting.snap b/crates/ra_ide_api/src/snapshots/tests__rainbow_highlighting.snap new file mode 100644 index 000000000..84cd521a2 --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__rainbow_highlighting.snap @@ -0,0 +1,128 @@ +--- +created: "2019-05-25T11:21:56.117898Z" +creator: insta@0.8.1 +source: crates/ra_ide_api/src/syntax_highlighting.rs +expression: result +--- +Ok( + [ + HighlightedRange { + range: [1; 3), + tag: "keyword", + id: None, + }, + HighlightedRange { + range: [4; 8), + tag: "variable", + id: Some( + 5766414492220109266, + ), + }, + HighlightedRange { + range: [17; 20), + tag: "keyword", + id: None, + }, + HighlightedRange { + range: [21; 26), + tag: "variable", + id: Some( + 15975256018338854530, + ), + }, + HighlightedRange { + range: [29; 36), + tag: "string", + id: None, + }, + HighlightedRange { + range: [42; 45), + tag: "keyword", + id: None, + }, + HighlightedRange { + range: [46; 47), + tag: "variable", + id: Some( + 176272420896316891, + ), + }, + HighlightedRange { + range: [50; 55), + tag: "variable", + id: Some( + 15975256018338854530, + ), + }, + HighlightedRange { + range: [56; 65), + tag: "text", + id: None, + }, + HighlightedRange { + range: [73; 76), + tag: "keyword", + id: None, + }, + HighlightedRange { + range: [77; 78), + tag: "variable", + id: Some( + 15061637676198917049, + ), + }, + HighlightedRange { + range: [81; 86), + tag: "variable", + id: Some( + 15975256018338854530, + ), + }, + HighlightedRange { + range: [87; 96), + tag: "text", + 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, + }, + ], +) diff --git a/crates/ra_ide_api/src/snapshots/tests__sematic_highlighting.snap b/crates/ra_ide_api/src/snapshots/tests__sematic_highlighting.snap deleted file mode 100644 index 3b3fe32e9..000000000 --- a/crates/ra_ide_api/src/snapshots/tests__sematic_highlighting.snap +++ /dev/null @@ -1,87 +0,0 @@ ---- -created: "2019-05-25T10:25:13.898113Z" -creator: insta@0.8.1 -source: crates/ra_ide_api/src/syntax_highlighting.rs -expression: result ---- -Ok( - [ - HighlightedRange { - range: [1; 3), - tag: "keyword", - id: None, - }, - HighlightedRange { - range: [4; 8), - tag: "variable", - id: Some( - 17119830160611610240, - ), - }, - HighlightedRange { - range: [17; 20), - tag: "keyword", - id: None, - }, - HighlightedRange { - range: [21; 26), - tag: "variable", - id: Some( - 2744494144922727377, - ), - }, - HighlightedRange { - range: [29; 36), - tag: "string", - id: None, - }, - HighlightedRange { - range: [42; 45), - tag: "keyword", - id: None, - }, - HighlightedRange { - range: [46; 47), - tag: "variable", - id: Some( - 10375904121795371996, - ), - }, - HighlightedRange { - range: [50; 55), - tag: "variable", - id: Some( - 2744494144922727377, - ), - }, - HighlightedRange { - range: [56; 65), - tag: "text", - id: None, - }, - HighlightedRange { - range: [73; 76), - tag: "keyword", - id: None, - }, - HighlightedRange { - range: [77; 78), - tag: "variable", - id: Some( - 8228548264153724449, - ), - }, - HighlightedRange { - range: [81; 86), - tag: "variable", - id: Some( - 2744494144922727377, - ), - }, - HighlightedRange { - range: [87; 96), - tag: "text", - id: None, - }, - ], -) 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 @@ -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_prof::profile; @@ -43,6 +43,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec = FxHashSet::default(); + let mut bindings_shadow_count: FxHashMap = FxHashMap::default(); + let mut res = Vec::new(); for node in source_file.syntax().descendants_with_tokens() { if highlighted.contains(&node) { @@ -77,7 +79,11 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec ("type", None), Some(Def(ModuleDef::TypeAlias(_))) => ("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(GenericParam(_)) => ("type", None), None => ("text", None), @@ -88,7 +94,12 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec { if let Some(name) = node.as_ast_node::() { - ("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 { ("text", None) } @@ -240,16 +251,19 @@ fn main() { } #[test] - fn test_sematic_highlighting() { + fn test_rainbow_highlighting() { let (analysis, file_id) = single_file( r#" fn main() { let hello = "hello"; let x = hello.to_string(); let y = hello.to_string(); + + let x = "other color please!"; + let y = x.to_string(); }"#, ); let result = analysis.highlight(file_id); - assert_debug_snapshot_matches!("sematic_highlighting", result); + assert_debug_snapshot_matches!("rainbow_highlighting", result); } } 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 @@ use std::{fmt, ops::{self, Bound}}; -use crate::{SyntaxNode, TextRange, TextUnit, SyntaxElement}; +use crate::{SmolStr, SyntaxNode, TextRange, TextUnit, SyntaxElement}; #[derive(Clone)] pub struct SyntaxText<'a> { @@ -34,6 +34,11 @@ impl<'a> SyntaxText<'a> { 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 { self.chunks().any(|it| it.contains(c)) } -- cgit v1.2.3