mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-11-03 13:13:18 +00:00 
			
		
		
		
	use Source for module, part 1
This commit is contained in:
		
							parent
							
								
									178d8e96b5
								
							
						
					
					
						commit
						0145d06515
					
				@ -196,16 +196,13 @@ impl Module {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
 | 
					    /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
 | 
				
			||||||
    pub fn definition_source(
 | 
					    pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
 | 
				
			||||||
        self,
 | 
					 | 
				
			||||||
        db: &(impl DefDatabase + AstDatabase),
 | 
					 | 
				
			||||||
    ) -> (HirFileId, ModuleSource) {
 | 
					 | 
				
			||||||
        let def_map = db.crate_def_map(self.krate);
 | 
					        let def_map = db.crate_def_map(self.krate);
 | 
				
			||||||
        let decl_id = def_map[self.module_id].declaration;
 | 
					        let decl_id = def_map[self.module_id].declaration;
 | 
				
			||||||
        let file_id = def_map[self.module_id].definition;
 | 
					        let file_id = def_map[self.module_id].definition;
 | 
				
			||||||
        let module_source = ModuleSource::new(db, file_id, decl_id);
 | 
					        let module_source = ModuleSource::new(db, file_id, decl_id);
 | 
				
			||||||
        let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
 | 
					        let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
 | 
				
			||||||
        (file_id, module_source)
 | 
					        (file_id, module_source).into()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
 | 
					    /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
 | 
				
			||||||
@ -226,9 +223,9 @@ impl Module {
 | 
				
			|||||||
        db: &impl HirDatabase,
 | 
					        db: &impl HirDatabase,
 | 
				
			||||||
        import: ImportId,
 | 
					        import: ImportId,
 | 
				
			||||||
    ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> {
 | 
					    ) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> {
 | 
				
			||||||
        let (file_id, source) = self.definition_source(db);
 | 
					        let src = self.definition_source(db);
 | 
				
			||||||
        let (_, source_map) = db.raw_items_with_source_map(file_id);
 | 
					        let (_, source_map) = db.raw_items_with_source_map(src.file_id);
 | 
				
			||||||
        source_map.get(&source, import)
 | 
					        source_map.get(&src.ast, import)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Returns the crate this module is part of.
 | 
					    /// Returns the crate this module is part of.
 | 
				
			||||||
 | 
				
			|||||||
@ -48,8 +48,8 @@ impl HasSource for ImplBlock {
 | 
				
			|||||||
    type Ast = TreeArc<ast::ImplBlock>;
 | 
					    type Ast = TreeArc<ast::ImplBlock>;
 | 
				
			||||||
    fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
 | 
					    fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
 | 
				
			||||||
        let source_map = db.impls_in_module_with_source_map(self.module).1;
 | 
					        let source_map = db.impls_in_module_with_source_map(self.module).1;
 | 
				
			||||||
        let (file_id, source) = self.module.definition_source(db);
 | 
					        let src = self.module.definition_source(db);
 | 
				
			||||||
        (file_id, source_map.get(&source, self.impl_id)).into()
 | 
					        (src.file_id, source_map.get(&src.ast, self.impl_id)).into()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -69,8 +69,8 @@ impl ImplBlock {
 | 
				
			|||||||
    /// Returns the syntax of the impl block
 | 
					    /// Returns the syntax of the impl block
 | 
				
			||||||
    pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
 | 
					    pub fn source(&self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::ImplBlock>> {
 | 
				
			||||||
        let source_map = db.impls_in_module_with_source_map(self.module).1;
 | 
					        let source_map = db.impls_in_module_with_source_map(self.module).1;
 | 
				
			||||||
        let (file_id, source) = self.module.definition_source(db);
 | 
					        let src = self.module.definition_source(db);
 | 
				
			||||||
        (file_id, source_map.get(&source, self.impl_id)).into()
 | 
					        (src.file_id, source_map.get(&src.ast, self.impl_id)).into()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn id(&self) -> ImplId {
 | 
					    pub fn id(&self) -> ImplId {
 | 
				
			||||||
@ -207,8 +207,8 @@ impl ModuleImplBlocks {
 | 
				
			|||||||
            impls_by_def: FxHashMap::default(),
 | 
					            impls_by_def: FxHashMap::default(),
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let (file_id, module_source) = m.module.definition_source(db);
 | 
					        let src = m.module.definition_source(db);
 | 
				
			||||||
        let node = match &module_source {
 | 
					        let node = match &src.ast {
 | 
				
			||||||
            ModuleSource::SourceFile(node) => node.syntax(),
 | 
					            ModuleSource::SourceFile(node) => node.syntax(),
 | 
				
			||||||
            ModuleSource::Module(node) => {
 | 
					            ModuleSource::Module(node) => {
 | 
				
			||||||
                node.item_list().expect("inline module should have item list").syntax()
 | 
					                node.item_list().expect("inline module should have item list").syntax()
 | 
				
			||||||
@ -216,7 +216,7 @@ impl ModuleImplBlocks {
 | 
				
			|||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) {
 | 
					        for impl_block_ast in node.children().filter_map(ast::ImplBlock::cast) {
 | 
				
			||||||
            let impl_block = ImplData::from_ast(db, file_id, m.module, impl_block_ast);
 | 
					            let impl_block = ImplData::from_ast(db, src.file_id, m.module, impl_block_ast);
 | 
				
			||||||
            let id = m.impls.alloc(impl_block);
 | 
					            let id = m.impls.alloc(impl_block);
 | 
				
			||||||
            for &impl_item in &m.impls[id].items {
 | 
					            for &impl_item in &m.impls[id].items {
 | 
				
			||||||
                m.impls_by_def.insert(impl_item, id);
 | 
					                m.impls_by_def.insert(impl_item, id);
 | 
				
			||||||
 | 
				
			|||||||
@ -84,7 +84,7 @@ impl LangItems {
 | 
				
			|||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
        // Look for impl targets
 | 
					        // Look for impl targets
 | 
				
			||||||
        let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone());
 | 
					        let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone());
 | 
				
			||||||
        let source = module.definition_source(db).1;
 | 
					        let source = module.definition_source(db).ast;
 | 
				
			||||||
        for (impl_id, _) in impl_blocks.impls.iter() {
 | 
					        for (impl_id, _) in impl_blocks.impls.iter() {
 | 
				
			||||||
            let impl_block = source_map.get(&source, impl_id);
 | 
					            let impl_block = source_map.get(&source, impl_id);
 | 
				
			||||||
            let lang_item_name = impl_block
 | 
					            let lang_item_name = impl_block
 | 
				
			||||||
 | 
				
			|||||||
@ -97,13 +97,13 @@ pub fn struct_from_module(
 | 
				
			|||||||
    module: Module,
 | 
					    module: Module,
 | 
				
			||||||
    struct_def: &ast::StructDef,
 | 
					    struct_def: &ast::StructDef,
 | 
				
			||||||
) -> Struct {
 | 
					) -> Struct {
 | 
				
			||||||
    let (file_id, _) = module.definition_source(db);
 | 
					    let file_id = module.definition_source(db).file_id;
 | 
				
			||||||
    let ctx = LocationCtx::new(db, module, file_id);
 | 
					    let ctx = LocationCtx::new(db, module, file_id);
 | 
				
			||||||
    Struct { id: ctx.to_def(struct_def) }
 | 
					    Struct { id: ctx.to_def(struct_def) }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum {
 | 
					pub fn enum_from_module(db: &impl HirDatabase, module: Module, enum_def: &ast::EnumDef) -> Enum {
 | 
				
			||||||
    let (file_id, _) = module.definition_source(db);
 | 
					    let file_id = module.definition_source(db).file_id;
 | 
				
			||||||
    let ctx = LocationCtx::new(db, module, file_id);
 | 
					    let ctx = LocationCtx::new(db, module, file_id);
 | 
				
			||||||
    Enum { id: ctx.to_def(enum_def) }
 | 
					    Enum { id: ctx.to_def(enum_def) }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -113,7 +113,7 @@ pub fn trait_from_module(
 | 
				
			|||||||
    module: Module,
 | 
					    module: Module,
 | 
				
			||||||
    trait_def: &ast::TraitDef,
 | 
					    trait_def: &ast::TraitDef,
 | 
				
			||||||
) -> Trait {
 | 
					) -> Trait {
 | 
				
			||||||
    let (file_id, _) = module.definition_source(db);
 | 
					    let file_id = module.definition_source(db).file_id;
 | 
				
			||||||
    let ctx = LocationCtx::new(db, module, file_id);
 | 
					    let ctx = LocationCtx::new(db, module, file_id);
 | 
				
			||||||
    Trait { id: ctx.to_def(trait_def) }
 | 
					    Trait { id: ctx.to_def(trait_def) }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -129,10 +129,10 @@ impl NavigationTarget {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
 | 
					    pub(crate) fn from_module(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
 | 
				
			||||||
        let (file_id, source) = module.definition_source(db);
 | 
					        let src = module.definition_source(db);
 | 
				
			||||||
        let file_id = file_id.as_original_file();
 | 
					        let file_id = src.file_id.as_original_file();
 | 
				
			||||||
        let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
 | 
					        let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
 | 
				
			||||||
        match source {
 | 
					        match src.ast {
 | 
				
			||||||
            ModuleSource::SourceFile(node) => {
 | 
					            ModuleSource::SourceFile(node) => {
 | 
				
			||||||
                NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None)
 | 
					                NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -115,8 +115,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
 | 
				
			|||||||
            Some(Def(it)) => {
 | 
					            Some(Def(it)) => {
 | 
				
			||||||
                match it {
 | 
					                match it {
 | 
				
			||||||
                    hir::ModuleDef::Module(it) => {
 | 
					                    hir::ModuleDef::Module(it) => {
 | 
				
			||||||
                        let it = it.definition_source(db).1;
 | 
					                        if let hir::ModuleSource::Module(it) = it.definition_source(db).ast {
 | 
				
			||||||
                        if let hir::ModuleSource::Module(it) = it {
 | 
					 | 
				
			||||||
                            res.extend(hover_text(it.doc_comment_text(), it.short_label()))
 | 
					                            res.extend(hover_text(it.doc_comment_text(), it.short_label()))
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
				
			|||||||
@ -148,9 +148,9 @@ fn rename_mod(
 | 
				
			|||||||
    let mut file_system_edits = Vec::new();
 | 
					    let mut file_system_edits = Vec::new();
 | 
				
			||||||
    if let Some(module) = source_binder::module_from_declaration(db, position.file_id, &ast_module)
 | 
					    if let Some(module) = source_binder::module_from_declaration(db, position.file_id, &ast_module)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        let (file_id, module_source) = module.definition_source(db);
 | 
					        let src = module.definition_source(db);
 | 
				
			||||||
        let file_id = file_id.as_original_file();
 | 
					        let file_id = src.file_id.as_original_file();
 | 
				
			||||||
        match module_source {
 | 
					        match src.ast {
 | 
				
			||||||
            ModuleSource::SourceFile(..) => {
 | 
					            ModuleSource::SourceFile(..) => {
 | 
				
			||||||
                let mod_path: RelativePathBuf = db.file_relative_path(file_id);
 | 
					                let mod_path: RelativePathBuf = db.file_relative_path(file_id);
 | 
				
			||||||
                // mod is defined in path/to/dir/mod.rs
 | 
					                // mod is defined in path/to/dir/mod.rs
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user