@@ -44,6 +44,7 @@ pub(crate) fn highlight(
4444 db : & RootDatabase ,
4545 file_id : FileId ,
4646 range_to_highlight : Option < TextRange > ,
47+ syntactic_name_ref_highlighting : bool ,
4748) -> Vec < HighlightedRange > {
4849 let _p = profile ( "highlight" ) ;
4950 let sema = Semantics :: new ( db) ;
@@ -104,6 +105,7 @@ pub(crate) fn highlight(
104105 if let Some ( ( highlight, binding_hash) ) = highlight_element (
105106 & sema,
106107 & mut bindings_shadow_count,
108+ syntactic_name_ref_highlighting,
107109 name. syntax ( ) . clone ( ) . into ( ) ,
108110 ) {
109111 stack. add ( HighlightedRange {
@@ -200,9 +202,12 @@ pub(crate) fn highlight(
200202
201203 let is_format_string = format_string. as_ref ( ) == Some ( & element_to_highlight) ;
202204
203- if let Some ( ( highlight, binding_hash) ) =
204- highlight_element ( & sema, & mut bindings_shadow_count, element_to_highlight. clone ( ) )
205- {
205+ if let Some ( ( highlight, binding_hash) ) = highlight_element (
206+ & sema,
207+ & mut bindings_shadow_count,
208+ syntactic_name_ref_highlighting,
209+ element_to_highlight. clone ( ) ,
210+ ) {
206211 stack. add ( HighlightedRange { range, highlight, binding_hash } ) ;
207212 if let Some ( string) =
208213 element_to_highlight. as_token ( ) . cloned ( ) . and_then ( ast:: String :: cast)
@@ -410,6 +415,7 @@ fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
410415fn highlight_element (
411416 sema : & Semantics < RootDatabase > ,
412417 bindings_shadow_count : & mut FxHashMap < Name , u32 > ,
418+ syntactic_name_ref_highlighting : bool ,
413419 element : SyntaxElement ,
414420) -> Option < ( Highlight , Option < u64 > ) > {
415421 let db = sema. db ;
@@ -463,6 +469,7 @@ fn highlight_element(
463469 }
464470 NameRefClass :: FieldShorthand { .. } => HighlightTag :: Field . into ( ) ,
465471 } ,
472+ None if syntactic_name_ref_highlighting => highlight_name_ref_by_syntax ( name_ref) ,
466473 None => HighlightTag :: UnresolvedReference . into ( ) ,
467474 }
468475 }
@@ -614,3 +621,53 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
614621
615622 tag. into ( )
616623}
624+
625+ fn highlight_name_ref_by_syntax ( name : ast:: NameRef ) -> Highlight {
626+ let default = HighlightTag :: UnresolvedReference ;
627+
628+ let parent = match name. syntax ( ) . parent ( ) {
629+ Some ( it) => it,
630+ _ => return default. into ( ) ,
631+ } ;
632+
633+ let tag = match parent. kind ( ) {
634+ METHOD_CALL_EXPR => HighlightTag :: Function ,
635+ FIELD_EXPR => HighlightTag :: Field ,
636+ PATH_SEGMENT => {
637+ let path = match parent. parent ( ) . and_then ( ast:: Path :: cast) {
638+ Some ( it) => it,
639+ _ => return default. into ( ) ,
640+ } ;
641+ let expr = match path. syntax ( ) . parent ( ) . and_then ( ast:: PathExpr :: cast) {
642+ Some ( it) => it,
643+ _ => {
644+ // within path, decide whether it is module or adt by checking for uppercase name
645+ return if name. text ( ) . chars ( ) . next ( ) . unwrap_or_default ( ) . is_uppercase ( ) {
646+ HighlightTag :: Struct
647+ } else {
648+ HighlightTag :: Module
649+ }
650+ . into ( ) ;
651+ }
652+ } ;
653+ let parent = match expr. syntax ( ) . parent ( ) {
654+ Some ( it) => it,
655+ None => return default. into ( ) ,
656+ } ;
657+
658+ match parent. kind ( ) {
659+ CALL_EXPR => HighlightTag :: Function ,
660+ _ => {
661+ if name. text ( ) . chars ( ) . next ( ) . unwrap_or_default ( ) . is_uppercase ( ) {
662+ HighlightTag :: Struct
663+ } else {
664+ HighlightTag :: Constant
665+ }
666+ }
667+ }
668+ }
669+ _ => default,
670+ } ;
671+
672+ tag. into ( )
673+ }
0 commit comments