|
1 | 1 | //! Implementation of inlay hints for generic parameters. |
| 2 | +use either::Either; |
2 | 3 | use ide_db::{active_parameter::generic_def_for_node, famous_defs::FamousDefs}; |
3 | 4 | use syntax::{ |
4 | 5 | AstNode, |
5 | 6 | ast::{self, AnyHasGenericArgs, HasGenericArgs, HasName}, |
6 | 7 | }; |
7 | 8 |
|
8 | 9 | use crate::{ |
9 | | - InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind, inlay_hints::GenericParameterHints, |
| 10 | + InlayHint, InlayHintLabel, InlayHintsConfig, InlayKind, |
| 11 | + inlay_hints::{GenericParameterHints, param_name}, |
10 | 12 | }; |
11 | 13 |
|
12 | 14 | use super::param_name::is_argument_similar_to_param_name; |
@@ -62,8 +64,17 @@ pub(crate) fn hints( |
62 | 64 | let param_name = param.name(sema.db); |
63 | 65 |
|
64 | 66 | let should_hide = { |
65 | | - let argument = get_string_representation(&arg)?; |
66 | | - is_argument_similar_to_param_name(&argument, param_name.as_str()) |
| 67 | + let param_name = param_name.as_str(); |
| 68 | + get_segment_representation(&arg).map_or(false, |seg| match seg { |
| 69 | + Either::Left(Either::Left(argument)) => { |
| 70 | + is_argument_similar_to_param_name(&argument, param_name) |
| 71 | + } |
| 72 | + Either::Left(Either::Right(argument)) => argument |
| 73 | + .segment() |
| 74 | + .and_then(|it| it.name_ref()) |
| 75 | + .is_some_and(|it| it.text().eq_ignore_ascii_case(param_name)), |
| 76 | + Either::Right(lifetime) => lifetime.text().eq_ignore_ascii_case(param_name), |
| 77 | + }) |
67 | 78 | }; |
68 | 79 |
|
69 | 80 | if should_hide { |
@@ -111,32 +122,34 @@ pub(crate) fn hints( |
111 | 122 | Some(()) |
112 | 123 | } |
113 | 124 |
|
114 | | -fn get_string_representation(arg: &ast::GenericArg) -> Option<String> { |
| 125 | +fn get_segment_representation( |
| 126 | + arg: &ast::GenericArg, |
| 127 | +) -> Option<Either<Either<Vec<ast::NameRef>, ast::Path>, ast::Lifetime>> { |
115 | 128 | return match arg { |
116 | 129 | ast::GenericArg::AssocTypeArg(_) => None, |
117 | | - ast::GenericArg::ConstArg(const_arg) => Some(const_arg.to_string()), |
| 130 | + ast::GenericArg::ConstArg(const_arg) => { |
| 131 | + param_name::get_segment_representation(&const_arg.expr()?).map(Either::Left) |
| 132 | + } |
118 | 133 | ast::GenericArg::LifetimeArg(lifetime_arg) => { |
119 | 134 | let lifetime = lifetime_arg.lifetime()?; |
120 | | - Some(lifetime.to_string()) |
| 135 | + Some(Either::Right(lifetime)) |
121 | 136 | } |
122 | 137 | ast::GenericArg::TypeArg(type_arg) => { |
123 | 138 | let ty = type_arg.ty()?; |
124 | | - Some( |
125 | | - type_path_segment(&ty) |
126 | | - .map_or_else(|| type_arg.to_string(), |segment| segment.to_string()), |
127 | | - ) |
| 139 | + type_path(&ty).map(Either::Right).map(Either::Left) |
128 | 140 | } |
129 | 141 | }; |
130 | 142 |
|
131 | | - fn type_path_segment(ty: &ast::Type) -> Option<ast::PathSegment> { |
| 143 | + fn type_path(ty: &ast::Type) -> Option<ast::Path> { |
132 | 144 | match ty { |
133 | | - ast::Type::ArrayType(it) => type_path_segment(&it.ty()?), |
134 | | - ast::Type::ForType(it) => type_path_segment(&it.ty()?), |
135 | | - ast::Type::ParenType(it) => type_path_segment(&it.ty()?), |
136 | | - ast::Type::PathType(path_type) => path_type.path()?.segment(), |
137 | | - ast::Type::PtrType(it) => type_path_segment(&it.ty()?), |
138 | | - ast::Type::RefType(it) => type_path_segment(&it.ty()?), |
139 | | - ast::Type::SliceType(it) => type_path_segment(&it.ty()?), |
| 145 | + ast::Type::ArrayType(it) => type_path(&it.ty()?), |
| 146 | + ast::Type::ForType(it) => type_path(&it.ty()?), |
| 147 | + ast::Type::ParenType(it) => type_path(&it.ty()?), |
| 148 | + ast::Type::PathType(path_type) => path_type.path(), |
| 149 | + ast::Type::PtrType(it) => type_path(&it.ty()?), |
| 150 | + ast::Type::RefType(it) => type_path(&it.ty()?), |
| 151 | + ast::Type::SliceType(it) => type_path(&it.ty()?), |
| 152 | + ast::Type::MacroType(macro_type) => macro_type.macro_call()?.path(), |
140 | 153 | _ => None, |
141 | 154 | } |
142 | 155 | } |
|
0 commit comments