aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/hover.rs40
-rw-r--r--crates/ide/src/inlay_hints.rs38
2 files changed, 60 insertions, 18 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 00745238a..69b828f47 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -2,8 +2,8 @@ use hir::{
2 Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, 2 Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource,
3 HirDisplay, Module, ModuleDef, ModuleSource, Semantics, 3 HirDisplay, Module, ModuleDef, ModuleSource, Semantics,
4}; 4};
5use ide_db::base_db::SourceDatabase;
6use ide_db::{ 5use ide_db::{
6 base_db::SourceDatabase,
7 defs::{Definition, NameClass, NameRefClass}, 7 defs::{Definition, NameClass, NameRefClass},
8 RootDatabase, 8 RootDatabase,
9}; 9};
@@ -94,7 +94,12 @@ pub(crate) fn hover(
94 let node = token.parent(); 94 let node = token.parent();
95 let definition = match_ast! { 95 let definition = match_ast! {
96 match node { 96 match node {
97 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)), 97 // we don't use NameClass::referenced_or_defined here as we do not want to resolve
98 // field pattern shorthands to their definition
99 ast::Name(name) => NameClass::classify(&sema, &name).and_then(|class| match class {
100 NameClass::ConstReference(def) => Some(def),
101 def => def.defined(sema.db),
102 }),
98 ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), 103 ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)),
99 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime) 104 ast::Lifetime(lifetime) => NameClass::classify_lifetime(&sema, &lifetime)
100 .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)), 105 .map_or_else(|| NameRefClass::classify_lifetime(&sema, &lifetime).map(|d| d.referenced(sema.db)), |d| d.defined(sema.db)),
@@ -3446,6 +3451,37 @@ impl<const LEN: usize> Foo<LEN$0> {}
3446 } 3451 }
3447 3452
3448 #[test] 3453 #[test]
3454 fn hover_const_pat() {
3455 check(
3456 r#"
3457/// This is a doc
3458const FOO: usize = 3;
3459fn foo() {
3460 match 5 {
3461 FOO$0 => (),
3462 _ => ()
3463 }
3464}
3465"#,
3466 expect![[r#"
3467 *FOO*
3468
3469 ```rust
3470 test
3471 ```
3472
3473 ```rust
3474 const FOO: usize = 3
3475 ```
3476
3477 ---
3478
3479 This is a doc
3480 "#]],
3481 );
3482 }
3483
3484 #[test]
3449 fn hover_mod_def() { 3485 fn hover_mod_def() {
3450 check( 3486 check(
3451 r#" 3487 r#"
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index 42d0a38e8..4ceb20742 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -109,26 +109,31 @@ fn get_chaining_hints(
109 // Chaining can be defined as an expression whose next sibling tokens are newline and dot 109 // Chaining can be defined as an expression whose next sibling tokens are newline and dot
110 // Ignoring extra whitespace and comments 110 // Ignoring extra whitespace and comments
111 let next = tokens.next()?.kind(); 111 let next = tokens.next()?.kind();
112 let next_next = tokens.next()?.kind(); 112 if next == SyntaxKind::WHITESPACE {
113 if next == SyntaxKind::WHITESPACE && next_next == T![.] { 113 let mut next_next = tokens.next()?.kind();
114 let ty = sema.type_of_expr(&expr)?; 114 while next_next == SyntaxKind::WHITESPACE {
115 if ty.is_unknown() { 115 next_next = tokens.next()?.kind();
116 return None;
117 } 116 }
118 if matches!(expr, ast::Expr::PathExpr(_)) { 117 if next_next == T![.] {
119 if let Some(hir::Adt::Struct(st)) = ty.as_adt() { 118 let ty = sema.type_of_expr(&expr)?;
120 if st.fields(sema.db).is_empty() { 119 if ty.is_unknown() {
121 return None; 120 return None;
121 }
122 if matches!(expr, ast::Expr::PathExpr(_)) {
123 if let Some(hir::Adt::Struct(st)) = ty.as_adt() {
124 if st.fields(sema.db).is_empty() {
125 return None;
126 }
122 } 127 }
123 } 128 }
129 acc.push(InlayHint {
130 range: expr.syntax().text_range(),
131 kind: InlayKind::ChainingHint,
132 label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| {
133 ty.display_truncated(sema.db, config.max_length).to_string().into()
134 }),
135 });
124 } 136 }
125 acc.push(InlayHint {
126 range: expr.syntax().text_range(),
127 kind: InlayKind::ChainingHint,
128 label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| {
129 ty.display_truncated(sema.db, config.max_length).to_string().into()
130 }),
131 });
132 } 137 }
133 Some(()) 138 Some(())
134} 139}
@@ -983,6 +988,7 @@ struct C;
983fn main() { 988fn main() {
984 let c = A(B(C)) 989 let c = A(B(C))
985 .into_b() // This is a comment 990 .into_b() // This is a comment
991 // This is another comment
986 .into_c(); 992 .into_c();
987} 993}
988"#, 994"#,