11//! Describes items defined or visible (ie, imported) in a certain scope.
22//! This is shared between modules and blocks.
33
4- use std:: sync:: LazyLock ;
4+ use std:: { fmt , sync:: LazyLock } ;
55
66use base_db:: Crate ;
7- use hir_expand:: { AstId , MacroCallId , attrs:: AttrId , db :: ExpandDatabase , name:: Name } ;
7+ use hir_expand:: { AstId , MacroCallId , attrs:: AttrId , name:: Name } ;
88use indexmap:: map:: Entry ;
99use itertools:: Itertools ;
1010use la_arena:: Idx ;
@@ -19,6 +19,7 @@ use crate::{
1919 AdtId , BuiltinType , ConstId , ExternBlockId , ExternCrateId , FxIndexMap , HasModule , ImplId ,
2020 LocalModuleId , Lookup , MacroId , ModuleDefId , ModuleId , TraitId , UseId ,
2121 db:: DefDatabase ,
22+ nameres:: MacroSubNs ,
2223 per_ns:: { Item , MacrosItem , PerNs , TypesItem , ValuesItem } ,
2324 visibility:: Visibility ,
2425} ;
@@ -735,40 +736,47 @@ impl ItemScope {
735736 }
736737 }
737738
738- pub ( crate ) fn dump ( & self , db : & dyn ExpandDatabase , buf : & mut String ) {
739+ pub ( crate ) fn dump ( & self , db : & dyn DefDatabase , buf : & mut String ) {
739740 let mut entries: Vec < _ > = self . resolutions ( ) . collect ( ) ;
740741 entries. sort_by_key ( |( name, _) | name. clone ( ) ) ;
741742
743+ let print_macro_sub_ns =
744+ |buf : & mut String , macro_id : MacroId | match MacroSubNs :: from_id ( db, macro_id) {
745+ MacroSubNs :: Bang => buf. push ( '!' ) ,
746+ MacroSubNs :: Attr => buf. push ( '#' ) ,
747+ } ;
748+
742749 for ( name, def) in entries {
743- format_to ! (
744- buf ,
745- "{}: ",
746- name . map_or ( "_" . to_owned ( ) , |name| name . display ( db , Edition :: LATEST ) . to_string ( ) )
747- ) ;
750+ let display_name : & dyn fmt :: Display = match & name {
751+ Some ( name ) => & name . display ( db , Edition :: LATEST ) ,
752+ None => & "_ ",
753+ } ;
754+ format_to ! ( buf , "- {display_name} :" ) ;
748755
749756 if let Some ( Item { import, .. } ) = def. types {
750- buf. push_str ( " t " ) ;
757+ buf. push_str ( " type " ) ;
751758 match import {
752- Some ( ImportOrExternCrate :: Import ( _) ) => buf. push ( 'i' ) ,
753- Some ( ImportOrExternCrate :: Glob ( _) ) => buf. push ( 'g' ) ,
754- Some ( ImportOrExternCrate :: ExternCrate ( _) ) => buf. push ( 'e' ) ,
759+ Some ( ImportOrExternCrate :: Import ( _) ) => buf. push_str ( " (import)" ) ,
760+ Some ( ImportOrExternCrate :: Glob ( _) ) => buf. push_str ( " (glob)" ) ,
761+ Some ( ImportOrExternCrate :: ExternCrate ( _) ) => buf. push_str ( " (extern)" ) ,
755762 None => ( ) ,
756763 }
757764 }
758765 if let Some ( Item { import, .. } ) = def. values {
759- buf. push_str ( " v " ) ;
766+ buf. push_str ( " value " ) ;
760767 match import {
761- Some ( ImportOrGlob :: Import ( _) ) => buf. push ( 'i' ) ,
762- Some ( ImportOrGlob :: Glob ( _) ) => buf. push ( 'g' ) ,
768+ Some ( ImportOrGlob :: Import ( _) ) => buf. push_str ( " (import)" ) ,
769+ Some ( ImportOrGlob :: Glob ( _) ) => buf. push_str ( " (glob)" ) ,
763770 None => ( ) ,
764771 }
765772 }
766- if let Some ( Item { import, .. } ) = def. macros {
767- buf. push_str ( " m" ) ;
773+ if let Some ( Item { def : macro_id, import, .. } ) = def. macros {
774+ buf. push_str ( " macro" ) ;
775+ print_macro_sub_ns ( buf, macro_id) ;
768776 match import {
769- Some ( ImportOrExternCrate :: Import ( _) ) => buf. push ( 'i' ) ,
770- Some ( ImportOrExternCrate :: Glob ( _) ) => buf. push ( 'g' ) ,
771- Some ( ImportOrExternCrate :: ExternCrate ( _) ) => buf. push ( 'e' ) ,
777+ Some ( ImportOrExternCrate :: Import ( _) ) => buf. push_str ( " (import)" ) ,
778+ Some ( ImportOrExternCrate :: Glob ( _) ) => buf. push_str ( " (glob)" ) ,
779+ Some ( ImportOrExternCrate :: ExternCrate ( _) ) => buf. push_str ( " (extern)" ) ,
772780 None => ( ) ,
773781 }
774782 }
@@ -778,6 +786,21 @@ impl ItemScope {
778786
779787 buf. push ( '\n' ) ;
780788 }
789+
790+ // Also dump legacy-textual-scope macros visible at the _end_ of the scope.
791+ //
792+ // For tests involving a cursor position, this might include macros that
793+ // are _not_ visible at the cursor position.
794+ let mut legacy_macros = self . legacy_macros ( ) . collect :: < Vec < _ > > ( ) ;
795+ legacy_macros. sort_by ( |( a, _) , ( b, _) | Ord :: cmp ( a, b) ) ;
796+ for ( name, macros) in legacy_macros {
797+ format_to ! ( buf, "- (legacy) {} :" , name. display( db, Edition :: LATEST ) ) ;
798+ for & macro_id in macros {
799+ buf. push_str ( " macro" ) ;
800+ print_macro_sub_ns ( buf, macro_id) ;
801+ }
802+ buf. push ( '\n' ) ;
803+ }
781804 }
782805
783806 pub ( crate ) fn shrink_to_fit ( & mut self ) {
0 commit comments