@@ -11,15 +11,16 @@ extern crate rustc_driver as _;
1111use std:: { any:: Any , collections:: hash_map:: Entry , mem, path:: Path , sync} ;
1212
1313use crossbeam_channel:: { Receiver , unbounded} ;
14- use hir_expand:: proc_macro:: {
15- ProcMacro , ProcMacroExpander , ProcMacroExpansionError , ProcMacroKind , ProcMacroLoadResult ,
16- ProcMacrosBuilder ,
14+ use hir_expand:: {
15+ db:: ExpandDatabase ,
16+ proc_macro:: {
17+ ProcMacro , ProcMacroExpander , ProcMacroExpansionError , ProcMacroKind , ProcMacroLoadResult ,
18+ ProcMacrosBuilder ,
19+ } ,
1720} ;
1821use ide_db:: {
19- ChangeWithProcMacros , FxHashMap , RootDatabase ,
20- base_db:: {
21- CrateGraphBuilder , Env , ProcMacroLoadingError , SourceDatabase , SourceRoot , SourceRootId ,
22- } ,
22+ ChangeWithProcMacros , EditionedFileId , FxHashMap , RootDatabase ,
23+ base_db:: { CrateGraphBuilder , Env , ProcMacroLoadingError , SourceRoot , SourceRootId } ,
2324 prime_caches,
2425} ;
2526use itertools:: Itertools ;
@@ -530,7 +531,7 @@ struct Expander(proc_macro_api::ProcMacro);
530531impl ProcMacroExpander for Expander {
531532 fn expand (
532533 & self ,
533- db : & dyn SourceDatabase ,
534+ db : & dyn ExpandDatabase ,
534535 subtree : & tt:: TopSubtree ,
535536 attrs : Option < & tt:: TopSubtree > ,
536537 env : & Env ,
@@ -541,30 +542,40 @@ impl ProcMacroExpander for Expander {
541542 ) -> Result < tt:: TopSubtree , ProcMacroExpansionError > {
542543 let mut cb = |req| match req {
543544 SubRequest :: LocalFilePath { file_id } => {
544- let file = FileId :: from_raw ( file_id) ;
545- let source_root_id = db. file_source_root ( file ) . source_root_id ( db) ;
545+ let file_id = FileId :: from_raw ( file_id) ;
546+ let source_root_id = db. file_source_root ( file_id ) . source_root_id ( db) ;
546547 let source_root = db. source_root ( source_root_id) . source_root ( db) ;
547-
548548 let name = source_root
549- . path_for_file ( & file )
549+ . path_for_file ( & file_id )
550550 . and_then ( |path| path. as_path ( ) )
551551 . map ( |path| path. to_string ( ) ) ;
552552
553553 Ok ( SubResponse :: LocalFilePathResult { name } )
554554 }
555- SubRequest :: SourceText { file_id, start, end } => {
556- let file = FileId :: from_raw ( file_id) ;
557- let text = db. file_text ( file) . text ( db) ;
558- let slice = text. get ( start as usize ..end as usize ) . map ( ToOwned :: to_owned) ;
559- Ok ( SubResponse :: SourceTextResult { text : slice } )
555+ SubRequest :: SourceText { file_id, ast_id, start, end } => {
556+ let raw_file_id = FileId :: from_raw ( file_id) ;
557+ let editioned_file_id = span:: EditionedFileId :: from_raw ( file_id) ;
558+ let ast_id = span:: ErasedFileAstId :: from_raw ( ast_id) ;
559+ let hir_file_id = EditionedFileId :: from_span_guess_origin ( db, editioned_file_id) ;
560+ let anchor_offset = db
561+ . ast_id_map ( hir_expand:: HirFileId :: FileId ( hir_file_id) )
562+ . get_erased ( ast_id)
563+ . text_range ( )
564+ . start ( ) ;
565+ let anchor_offset = u32:: from ( anchor_offset) ;
566+ let abs_start = start + anchor_offset;
567+ let abs_end = end + anchor_offset;
568+ let source = db. file_text ( raw_file_id) . text ( db) ;
569+ let text = source. get ( abs_start as usize ..abs_end as usize ) . map ( ToOwned :: to_owned) ;
570+
571+ Ok ( SubResponse :: SourceTextResult { text } )
560572 }
561573 SubRequest :: FilePath { file_id } => {
562- let file = FileId :: from_raw ( file_id) ;
563- let source_root_id = db. file_source_root ( file ) . source_root_id ( db) ;
574+ let file_id = FileId :: from_raw ( file_id) ;
575+ let source_root_id = db. file_source_root ( file_id ) . source_root_id ( db) ;
564576 let source_root = db. source_root ( source_root_id) . source_root ( db) ;
565-
566577 let name = source_root
567- . path_for_file ( & file )
578+ . path_for_file ( & file_id )
568579 . and_then ( |path| path. as_path ( ) )
569580 . map ( |path| path. to_string ( ) )
570581 . unwrap_or_default ( ) ;
0 commit comments