mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-10-31 03:54:42 +00:00 
			
		
		
		
	Improve hover module path rendering
This commit is contained in:
		
							parent
							
								
									1b52a6680f
								
							
						
					
					
						commit
						3bae1f0a1b
					
				
					 5 changed files with 133 additions and 23 deletions
				
			
		|  | @ -3916,6 +3916,10 @@ impl ToolModule { | ||||||
|             db.crate_def_map(self.krate).registered_tools()[self.idx as usize].clone(), |             db.crate_def_map(self.krate).registered_tools()[self.idx as usize].clone(), | ||||||
|         ) |         ) | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     pub fn krate(&self) -> Crate { | ||||||
|  |         Crate { id: self.krate } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||||||
|  |  | ||||||
|  | @ -92,11 +92,11 @@ impl Definition { | ||||||
|             Definition::ExternCrateDecl(it) => it.module(db), |             Definition::ExternCrateDecl(it) => it.module(db), | ||||||
|             Definition::DeriveHelper(it) => it.derive().module(db), |             Definition::DeriveHelper(it) => it.derive().module(db), | ||||||
|             Definition::InlineAsmOperand(it) => it.parent(db).module(db), |             Definition::InlineAsmOperand(it) => it.parent(db).module(db), | ||||||
|  |             Definition::ToolModule(t) => t.krate().root_module(), | ||||||
|             Definition::BuiltinAttr(_) |             Definition::BuiltinAttr(_) | ||||||
|             | Definition::BuiltinType(_) |             | Definition::BuiltinType(_) | ||||||
|             | Definition::BuiltinLifetime(_) |             | Definition::BuiltinLifetime(_) | ||||||
|             | Definition::TupleField(_) |             | Definition::TupleField(_) | ||||||
|             | Definition::ToolModule(_) |  | ||||||
|             | Definition::InlineAsmRegOrRegClass(_) => return None, |             | Definition::InlineAsmRegOrRegClass(_) => return None, | ||||||
|         }; |         }; | ||||||
|         Some(module) |         Some(module) | ||||||
|  |  | ||||||
|  | @ -3,7 +3,7 @@ use std::{env, mem, ops::Not}; | ||||||
| 
 | 
 | ||||||
| use either::Either; | use either::Either; | ||||||
| use hir::{ | use hir::{ | ||||||
|     db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, AssocItemContainer, CaptureKind, |     db::ExpandDatabase, Adt, AsAssocItem, AsExternAssocItem, CaptureKind, | ||||||
|     DynCompatibilityViolation, HasCrate, HasSource, HirDisplay, Layout, LayoutError, |     DynCompatibilityViolation, HasCrate, HasSource, HirDisplay, Layout, LayoutError, | ||||||
|     MethodViolationCode, Name, Semantics, Symbol, Trait, Type, TypeInfo, VariantDef, |     MethodViolationCode, Name, Semantics, Symbol, Trait, Type, TypeInfo, VariantDef, | ||||||
| }; | }; | ||||||
|  | @ -376,7 +376,7 @@ pub(super) fn process_markup( | ||||||
|     Markup::from(markup) |     Markup::from(markup) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn definition_owner_name(db: &RootDatabase, def: &Definition, edition: Edition) -> Option<String> { | fn definition_owner_name(db: &RootDatabase, def: Definition, edition: Edition) -> Option<String> { | ||||||
|     match def { |     match def { | ||||||
|         Definition::Field(f) => { |         Definition::Field(f) => { | ||||||
|             let parent = f.parent_def(db); |             let parent = f.parent_def(db); | ||||||
|  | @ -390,9 +390,52 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition, edition: Edition) | ||||||
|                 _ => Some(parent_name), |                 _ => Some(parent_name), | ||||||
|             }; |             }; | ||||||
|         } |         } | ||||||
|         Definition::Local(l) => l.parent(db).name(db), |  | ||||||
|         Definition::Variant(e) => Some(e.parent_enum(db).name(db)), |         Definition::Variant(e) => Some(e.parent_enum(db).name(db)), | ||||||
|  |         Definition::GenericParam(generic_param) => match generic_param.parent() { | ||||||
|  |             hir::GenericDef::Adt(it) => Some(it.name(db)), | ||||||
|  |             hir::GenericDef::Trait(it) => Some(it.name(db)), | ||||||
|  |             hir::GenericDef::TraitAlias(it) => Some(it.name(db)), | ||||||
|  |             hir::GenericDef::TypeAlias(it) => Some(it.name(db)), | ||||||
| 
 | 
 | ||||||
|  |             hir::GenericDef::Impl(i) => i.self_ty(db).as_adt().map(|adt| adt.name(db)), | ||||||
|  |             hir::GenericDef::Function(it) => { | ||||||
|  |                 let container = it.as_assoc_item(db).and_then(|assoc| match assoc.container(db) { | ||||||
|  |                     hir::AssocItemContainer::Trait(t) => Some(t.name(db)), | ||||||
|  |                     hir::AssocItemContainer::Impl(i) => { | ||||||
|  |                         i.self_ty(db).as_adt().map(|adt| adt.name(db)) | ||||||
|  |                     } | ||||||
|  |                 }); | ||||||
|  |                 match container { | ||||||
|  |                     Some(name) => { | ||||||
|  |                         return Some(format!( | ||||||
|  |                             "{}::{}", | ||||||
|  |                             name.display(db, edition), | ||||||
|  |                             it.name(db).display(db, edition) | ||||||
|  |                         )) | ||||||
|  |                     } | ||||||
|  |                     None => Some(it.name(db)), | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             hir::GenericDef::Const(it) => { | ||||||
|  |                 let container = it.as_assoc_item(db).and_then(|assoc| match assoc.container(db) { | ||||||
|  |                     hir::AssocItemContainer::Trait(t) => Some(t.name(db)), | ||||||
|  |                     hir::AssocItemContainer::Impl(i) => { | ||||||
|  |                         i.self_ty(db).as_adt().map(|adt| adt.name(db)) | ||||||
|  |                     } | ||||||
|  |                 }); | ||||||
|  |                 match container { | ||||||
|  |                     Some(name) => { | ||||||
|  |                         return Some(format!( | ||||||
|  |                             "{}::{}", | ||||||
|  |                             name.display(db, edition), | ||||||
|  |                             it.name(db)?.display(db, edition) | ||||||
|  |                         )) | ||||||
|  |                     } | ||||||
|  |                     None => it.name(db), | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }, | ||||||
|  |         Definition::DeriveHelper(derive_helper) => Some(derive_helper.derive().name(db)), | ||||||
|         d => { |         d => { | ||||||
|             if let Some(assoc_item) = d.as_assoc_item(db) { |             if let Some(assoc_item) = d.as_assoc_item(db) { | ||||||
|                 match assoc_item.container(db) { |                 match assoc_item.container(db) { | ||||||
|  | @ -436,7 +479,7 @@ pub(super) fn definition( | ||||||
|     config: &HoverConfig, |     config: &HoverConfig, | ||||||
|     edition: Edition, |     edition: Edition, | ||||||
| ) -> Markup { | ) -> Markup { | ||||||
|     let mod_path = definition_mod_path(db, &def, edition); |     let mod_path = definition_path(db, &def, edition); | ||||||
|     let label = match def { |     let label = match def { | ||||||
|         Definition::Trait(trait_) => { |         Definition::Trait(trait_) => { | ||||||
|             trait_.display_limited(db, config.max_trait_assoc_items_count, edition).to_string() |             trait_.display_limited(db, config.max_trait_assoc_items_count, edition).to_string() | ||||||
|  | @ -915,19 +958,22 @@ fn closure_ty( | ||||||
|     Some(res) |     Some(res) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) -> Option<String> { | fn definition_path(db: &RootDatabase, &def: &Definition, edition: Edition) -> Option<String> { | ||||||
|     if matches!(def, Definition::GenericParam(_) | Definition::Local(_) | Definition::Label(_)) { |     if matches!( | ||||||
|  |         def, | ||||||
|  |         Definition::TupleField(_) | ||||||
|  |             | Definition::Label(_) | ||||||
|  |             | Definition::Local(_) | ||||||
|  |             | Definition::BuiltinAttr(_) | ||||||
|  |             | Definition::BuiltinLifetime(_) | ||||||
|  |             | Definition::BuiltinType(_) | ||||||
|  |             | Definition::InlineAsmRegOrRegClass(_) | ||||||
|  |             | Definition::InlineAsmOperand(_) | ||||||
|  |     ) { | ||||||
|         return None; |         return None; | ||||||
|     } |     } | ||||||
|     let container: Option<Definition> = |     let rendered_parent = definition_owner_name(db, def, edition); | ||||||
|         def.as_assoc_item(db).and_then(|assoc| match assoc.container(db) { |     def.module(db).map(|module| path(db, module, rendered_parent, edition)) | ||||||
|             AssocItemContainer::Trait(trait_) => Some(trait_.into()), |  | ||||||
|             AssocItemContainer::Impl(impl_) => impl_.self_ty(db).as_adt().map(|adt| adt.into()), |  | ||||||
|         }); |  | ||||||
|     container |  | ||||||
|         .unwrap_or(*def) |  | ||||||
|         .module(db) |  | ||||||
|         .map(|module| path(db, module, definition_owner_name(db, def, edition), edition)) |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn markup( | fn markup( | ||||||
|  |  | ||||||
|  | @ -4699,6 +4699,10 @@ fn hover_lifetime() { | ||||||
|         expect![[r#" |         expect![[r#" | ||||||
|             *'lifetime* |             *'lifetime* | ||||||
| 
 | 
 | ||||||
|  |             ```rust | ||||||
|  |             ra_test_fixture::foo | ||||||
|  |             ``` | ||||||
|  | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             'lifetime |             'lifetime | ||||||
|             ``` |             ``` | ||||||
|  | @ -4729,6 +4733,10 @@ impl<T: TraitA + TraitB> Foo<T$0> where T: Sized {} | ||||||
|         expect![[r#" |         expect![[r#" | ||||||
|             *T* |             *T* | ||||||
| 
 | 
 | ||||||
|  |             ```rust | ||||||
|  |             ra_test_fixture::Foo | ||||||
|  |             ``` | ||||||
|  | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             T: TraitA + TraitB |             T: TraitA + TraitB | ||||||
|             ``` |             ``` | ||||||
|  | @ -4743,6 +4751,10 @@ impl<T> Foo<T$0> {} | ||||||
|         expect![[r#" |         expect![[r#" | ||||||
|             *T* |             *T* | ||||||
| 
 | 
 | ||||||
|  |             ```rust | ||||||
|  |             ra_test_fixture::Foo | ||||||
|  |             ``` | ||||||
|  | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             T |             T | ||||||
|             ``` |             ``` | ||||||
|  | @ -4757,6 +4769,10 @@ impl<T: 'static> Foo<T$0> {} | ||||||
|         expect![[r#" |         expect![[r#" | ||||||
|             *T* |             *T* | ||||||
| 
 | 
 | ||||||
|  |             ```rust | ||||||
|  |             ra_test_fixture::Foo | ||||||
|  |             ``` | ||||||
|  | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             T: 'static |             T: 'static | ||||||
|             ``` |             ``` | ||||||
|  | @ -4777,6 +4793,10 @@ impl<T$0: Trait> Foo<T> {} | ||||||
|         expect![[r#" |         expect![[r#" | ||||||
|             *T* |             *T* | ||||||
| 
 | 
 | ||||||
|  |             ```rust | ||||||
|  |             ra_test_fixture::Foo | ||||||
|  |             ``` | ||||||
|  | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             T: Trait |             T: Trait | ||||||
|             ``` |             ``` | ||||||
|  | @ -4792,6 +4812,10 @@ impl<T$0: Trait + ?Sized> Foo<T> {} | ||||||
|         expect![[r#" |         expect![[r#" | ||||||
|             *T* |             *T* | ||||||
| 
 | 
 | ||||||
|  |             ```rust | ||||||
|  |             ra_test_fixture::Foo | ||||||
|  |             ``` | ||||||
|  | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             T: Trait + ?Sized |             T: Trait + ?Sized | ||||||
|             ``` |             ``` | ||||||
|  | @ -4812,6 +4836,10 @@ fn foo<T$0>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T |                 T | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -4833,6 +4861,10 @@ fn foo<T$0: Sized>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T |                 T | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -4854,6 +4886,10 @@ fn foo<T$0: ?Sized>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T: ?Sized |                 T: ?Sized | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -4876,6 +4912,10 @@ fn foo<T$0: Trait>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T: Trait |                 T: Trait | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -4898,6 +4938,10 @@ fn foo<T$0: Trait + Sized>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T: Trait |                 T: Trait | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -4920,6 +4964,10 @@ fn foo<T$0: Trait + ?Sized>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T: Trait + ?Sized |                 T: Trait + ?Sized | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -4941,6 +4989,10 @@ fn foo<T$0: ?Sized + Sized + Sized>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T |                 T | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -4963,6 +5015,10 @@ fn foo<T$0: Sized + ?Sized + Sized + Trait>() {} | ||||||
|             expect![[r#" |             expect![[r#" | ||||||
|                 *T* |                 *T* | ||||||
| 
 | 
 | ||||||
|  |                 ```rust | ||||||
|  |                 ra_test_fixture::foo | ||||||
|  |                 ``` | ||||||
|  | 
 | ||||||
|                 ```rust |                 ```rust | ||||||
|                 T: Trait |                 T: Trait | ||||||
|                 ``` |                 ``` | ||||||
|  | @ -5010,6 +5066,10 @@ impl<const LEN: usize> Foo<LEN$0> {} | ||||||
|         expect![[r#" |         expect![[r#" | ||||||
|             *LEN* |             *LEN* | ||||||
| 
 | 
 | ||||||
|  |             ```rust | ||||||
|  |             ra_test_fixture::Foo | ||||||
|  |             ``` | ||||||
|  | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             const LEN: usize |             const LEN: usize | ||||||
|             ``` |             ``` | ||||||
|  | @ -7857,7 +7917,7 @@ fn test() { | ||||||
|             *foo* |             *foo* | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             ra_test_fixture::S |             ra_test_fixture::m::S | ||||||
|             ``` |             ``` | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|  | @ -7886,7 +7946,7 @@ fn test() { | ||||||
|             *foo* |             *foo* | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             ra_test_fixture::S |             ra_test_fixture::m::S | ||||||
|             ``` |             ``` | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|  | @ -7916,7 +7976,7 @@ mod m { | ||||||
|             *foo* |             *foo* | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             ra_test_fixture::S |             ra_test_fixture::m::inner::S | ||||||
|             ``` |             ``` | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|  | @ -7946,7 +8006,7 @@ fn test() { | ||||||
|             *A* |             *A* | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             ra_test_fixture::S |             ra_test_fixture::m::S | ||||||
|             ``` |             ``` | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|  | @ -7975,7 +8035,7 @@ fn test() { | ||||||
|             *A* |             *A* | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             ra_test_fixture::S |             ra_test_fixture::m::S | ||||||
|             ``` |             ``` | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|  | @ -8005,7 +8065,7 @@ mod m { | ||||||
|             *A* |             *A* | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|             ra_test_fixture::S |             ra_test_fixture::m::inner::S | ||||||
|             ``` |             ``` | ||||||
| 
 | 
 | ||||||
|             ```rust |             ```rust | ||||||
|  |  | ||||||
|  | @ -46,7 +46,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | ||||||
| .unresolved_reference    { color: #FC5555; text-decoration: wavy underline; } | .unresolved_reference    { color: #FC5555; text-decoration: wavy underline; } | ||||||
| </style> | </style> | ||||||
| <pre><code><span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">allow</span><span class="parenthesis attribute">(</span><span class="none attribute">dead_code</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span> | <pre><code><span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="builtin_attr attribute">allow</span><span class="parenthesis attribute">(</span><span class="none attribute">dead_code</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span> | ||||||
| <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="tool_module attribute library">rustfmt</span><span class="operator attribute">::</span><span class="tool_module attribute library">skip</span><span class="attribute_bracket attribute">]</span> | <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="tool_module attribute">rustfmt</span><span class="operator attribute">::</span><span class="tool_module attribute">skip</span><span class="attribute_bracket attribute">]</span> | ||||||
| <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">identity</span><span class="attribute_bracket attribute">]</span> | <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">identity</span><span class="attribute_bracket attribute">]</span> | ||||||
| <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="attribute attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="derive attribute default_library library">Default</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span> | <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="attribute attribute default_library library">derive</span><span class="parenthesis attribute">(</span><span class="derive attribute default_library library">Default</span><span class="parenthesis attribute">)</span><span class="attribute_bracket attribute">]</span> | ||||||
| <span class="comment documentation">/// This is a doc comment</span> | <span class="comment documentation">/// This is a doc comment</span> | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lukas Wirth
						Lukas Wirth