Skip to content

Commit 6465868

Browse files
committed
Make inlay hints work in attributed items
1 parent 11a17c8 commit 6465868

File tree

2 files changed

+147
-19
lines changed

2 files changed

+147
-19
lines changed

crates/hir/src/semantics.rs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ use itertools::Itertools;
1616
use rustc_hash::{FxHashMap, FxHashSet};
1717
use smallvec::{smallvec, SmallVec};
1818
use syntax::{
19+
algo::skip_trivia_token,
1920
ast::{self, GenericParamsOwner, LoopBodyOwner},
20-
match_ast, AstNode, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize,
21+
match_ast, AstNode, Direction, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange, TextSize,
2122
};
2223

2324
use crate::{
@@ -184,6 +185,11 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
184185
self.imp.descend_into_macros(token)
185186
}
186187

188+
/// Maps a node down by mapping its first and last token down.
189+
pub fn descend_node_into_attributes<N: AstNode>(&self, node: N) -> SmallVec<[N; 1]> {
190+
self.imp.descend_node_into_attributes(node)
191+
}
192+
187193
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
188194
self.imp.find_file(syntax_node.clone()).file_id
189195
}
@@ -192,6 +198,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
192198
self.imp.original_range(node)
193199
}
194200

201+
pub fn original_range_opt(&self, node: &SyntaxNode) -> Option<FileRange> {
202+
self.imp.original_range_opt(node)
203+
}
204+
195205
pub fn diagnostics_display_range(&self, diagnostics: InFile<SyntaxNodePtr>) -> FileRange {
196206
self.imp.diagnostics_display_range(diagnostics)
197207
}
@@ -471,16 +481,69 @@ impl<'db> SemanticsImpl<'db> {
471481
)
472482
}
473483

484+
// This might not be the correct way to due this, but it works for now
485+
fn descend_node_into_attributes<N: AstNode>(&self, node: N) -> SmallVec<[N; 1]> {
486+
let mut res = smallvec![];
487+
let tokens = (|| {
488+
let first = skip_trivia_token(node.syntax().first_token()?, Direction::Next)?;
489+
let last = skip_trivia_token(node.syntax().last_token()?, Direction::Prev)?;
490+
Some((first, last))
491+
})();
492+
let (first, last) = match tokens {
493+
Some(it) => it,
494+
None => return res,
495+
};
496+
497+
if first == last {
498+
self.descend_into_macros_impl(first, |InFile { value, .. }| {
499+
if let Some(node) = value.ancestors().find_map(N::cast) {
500+
res.push(node)
501+
}
502+
});
503+
} else {
504+
// Descend first and last token, then zip them to look for the node they belong to
505+
let mut scratch: SmallVec<[_; 1]> = smallvec![];
506+
self.descend_into_macros_impl(first, |token| {
507+
scratch.push(token);
508+
});
509+
510+
let mut scratch = scratch.into_iter();
511+
self.descend_into_macros_impl(last, |InFile { value: last, file_id: last_fid }| {
512+
if let Some(InFile { value: first, file_id: first_fid }) = scratch.next() {
513+
if first_fid == last_fid {
514+
if let Some(p) = first.parent() {
515+
let range = first.text_range().cover(last.text_range());
516+
let node = find_root(&p)
517+
.covering_element(range)
518+
.ancestors()
519+
.take_while(|it| it.text_range() == range)
520+
.find_map(N::cast);
521+
if let Some(node) = node {
522+
res.push(node);
523+
}
524+
}
525+
}
526+
}
527+
});
528+
}
529+
res
530+
}
531+
474532
fn descend_into_macros(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
533+
let mut res = smallvec![];
534+
self.descend_into_macros_impl(token, |InFile { value, .. }| res.push(value));
535+
res
536+
}
537+
538+
fn descend_into_macros_impl(&self, token: SyntaxToken, mut f: impl FnMut(InFile<SyntaxToken>)) {
475539
let _p = profile::span("descend_into_macros");
476540
let parent = match token.parent() {
477541
Some(it) => it,
478-
None => return smallvec![token],
542+
None => return,
479543
};
480544
let sa = self.analyze(&parent);
481545
let mut queue = vec![InFile::new(sa.file_id, token)];
482546
let mut cache = self.expansion_info_cache.borrow_mut();
483-
let mut res = smallvec![];
484547
// Remap the next token in the queue into a macro call its in, if it is not being remapped
485548
// either due to not being in a macro-call or because its unused push it into the result vec,
486549
// otherwise push the remapped tokens back into the queue as they can potentially be remapped again.
@@ -546,10 +609,9 @@ impl<'db> SemanticsImpl<'db> {
546609
.is_none();
547610

548611
if was_not_remapped {
549-
res.push(token.value)
612+
f(token)
550613
}
551614
}
552-
res
553615
}
554616

555617
// Note this return type is deliberate as [`find_nodes_at_offset_with_descend`] wants to stop
@@ -580,6 +642,11 @@ impl<'db> SemanticsImpl<'db> {
580642
node.as_ref().original_file_range(self.db.upcast())
581643
}
582644

645+
fn original_range_opt(&self, node: &SyntaxNode) -> Option<FileRange> {
646+
let node = self.find_file(node.clone());
647+
node.as_ref().original_file_range_opt(self.db.upcast())
648+
}
649+
583650
fn diagnostics_display_range(&self, src: InFile<SyntaxNodePtr>) -> FileRange {
584651
let root = self.db.parse_or_expand(src.file_id).unwrap();
585652
let node = src.value.to_node(&root);

crates/ide/src/inlay_hints.rs

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use either::Either;
22
use hir::{known, Callable, HasVisibility, HirDisplay, Semantics, TypeInfo};
3-
use ide_db::helpers::FamousDefs;
43
use ide_db::RootDatabase;
4+
use ide_db::{base_db::FileRange, helpers::FamousDefs};
55
use stdx::to_lower_snake_case;
66
use syntax::{
77
ast::{self, ArgListOwner, AstNode, NameOwner},
@@ -79,7 +79,7 @@ pub(crate) fn inlay_hints(
7979
_ => (),
8080
}
8181
} else if let Some(it) = ast::IdentPat::cast(node.clone()) {
82-
get_bind_pat_hints(&mut res, &sema, config, it);
82+
get_bind_pat_hints(&mut res, &sema, config, &it);
8383
}
8484
}
8585
res
@@ -99,7 +99,9 @@ fn get_chaining_hints(
9999
return None;
100100
}
101101

102-
let krate = sema.scope(expr.syntax()).module().map(|it| it.krate());
102+
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
103+
let desc_expr = descended.as_ref().unwrap_or(expr);
104+
let krate = sema.scope(desc_expr.syntax()).module().map(|it| it.krate());
103105
let famous_defs = FamousDefs(sema, krate);
104106

105107
let mut tokens = expr
@@ -121,7 +123,7 @@ fn get_chaining_hints(
121123
next_next = tokens.next()?.kind();
122124
}
123125
if next_next == T![.] {
124-
let ty = sema.type_of_expr(expr)?.original;
126+
let ty = sema.type_of_expr(desc_expr)?.original;
125127
if ty.is_unknown() {
126128
return None;
127129
}
@@ -133,7 +135,7 @@ fn get_chaining_hints(
133135
}
134136
}
135137
acc.push(InlayHint {
136-
range: sema.original_range(expr.syntax()).range,
138+
range: expr.syntax().text_range(),
137139
kind: InlayKind::ChainingHint,
138140
label: hint_iterator(sema, &famous_defs, config, &ty).unwrap_or_else(|| {
139141
ty.display_truncated(sema.db, config.max_length).to_string().into()
@@ -160,18 +162,22 @@ fn get_param_name_hints(
160162
.into_iter()
161163
.zip(arg_list.args())
162164
.filter_map(|((param, _ty), arg)| {
165+
// Only annotate hints for expressions that exist in the original file
166+
let range = sema.original_range_opt(arg.syntax())?;
163167
let param_name = match param? {
164168
Either::Left(_) => "self".to_string(),
165169
Either::Right(pat) => match pat {
166170
ast::Pat::IdentPat(it) => it.name()?.to_string(),
167171
_ => return None,
168172
},
169173
};
170-
Some((param_name, arg))
174+
Some((param_name, arg, range))
171175
})
172-
.filter(|(param_name, arg)| !should_hide_param_name_hint(sema, &callable, param_name, arg))
173-
.map(|(param_name, arg)| InlayHint {
174-
range: sema.original_range(arg.syntax()).range,
176+
.filter(|(param_name, arg, _)| {
177+
!should_hide_param_name_hint(sema, &callable, param_name, arg)
178+
})
179+
.map(|(param_name, _, FileRange { range, .. })| InlayHint {
180+
range,
175181
kind: InlayKind::ParameterHint,
176182
label: param_name.into(),
177183
});
@@ -184,25 +190,27 @@ fn get_bind_pat_hints(
184190
acc: &mut Vec<InlayHint>,
185191
sema: &Semantics<RootDatabase>,
186192
config: &InlayHintsConfig,
187-
pat: ast::IdentPat,
193+
pat: &ast::IdentPat,
188194
) -> Option<()> {
189195
if !config.type_hints {
190196
return None;
191197
}
192198

193-
let krate = sema.scope(pat.syntax()).module().map(|it| it.krate());
199+
let descended = sema.descend_node_into_attributes(pat.clone()).pop();
200+
let desc_pat = descended.as_ref().unwrap_or(pat);
201+
let krate = sema.scope(desc_pat.syntax()).module().map(|it| it.krate());
194202
let famous_defs = FamousDefs(sema, krate);
195203

196-
let ty = sema.type_of_pat(&pat.clone().into())?.original;
204+
let ty = sema.type_of_pat(&desc_pat.clone().into())?.original;
197205

198206
if should_not_display_type_hint(sema, &pat, &ty) {
199207
return None;
200208
}
201209

202210
acc.push(InlayHint {
203211
range: match pat.name() {
204-
Some(name) => sema.original_range(name.syntax()).range,
205-
None => sema.original_range(pat.syntax()).range,
212+
Some(name) => name.syntax().text_range(),
213+
None => pat.syntax().text_range(),
206214
},
207215
kind: InlayKind::TypeHint,
208216
label: hint_iterator(sema, &famous_defs, config, &ty)
@@ -435,9 +443,13 @@ fn get_callable(
435443
) -> Option<(hir::Callable, ast::ArgList)> {
436444
match expr {
437445
ast::Expr::CallExpr(expr) => {
446+
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
447+
let expr = descended.as_ref().unwrap_or(expr);
438448
sema.type_of_expr(&expr.expr()?)?.original.as_callable(sema.db).zip(expr.arg_list())
439449
}
440450
ast::Expr::MethodCallExpr(expr) => {
451+
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
452+
let expr = descended.as_ref().unwrap_or(expr);
441453
sema.resolve_method_call_as_callable(expr).zip(expr.arg_list())
442454
}
443455
_ => None,
@@ -1471,4 +1483,53 @@ fn main() {
14711483
"#]],
14721484
);
14731485
}
1486+
1487+
#[test]
1488+
fn hints_in_attr_call() {
1489+
check_expect(
1490+
TEST_CONFIG,
1491+
r#"
1492+
//- proc_macros: identity, input_replace
1493+
struct Struct;
1494+
impl Struct {
1495+
fn chain(self) -> Self {
1496+
self
1497+
}
1498+
}
1499+
#[proc_macros::identity]
1500+
fn main() {
1501+
let strukt = Struct;
1502+
strukt
1503+
.chain()
1504+
.chain()
1505+
.chain();
1506+
Struct::chain(strukt);
1507+
}
1508+
"#,
1509+
expect![[r#"
1510+
[
1511+
InlayHint {
1512+
range: 124..130,
1513+
kind: TypeHint,
1514+
label: "Struct",
1515+
},
1516+
InlayHint {
1517+
range: 145..185,
1518+
kind: ChainingHint,
1519+
label: "Struct",
1520+
},
1521+
InlayHint {
1522+
range: 145..168,
1523+
kind: ChainingHint,
1524+
label: "Struct",
1525+
},
1526+
InlayHint {
1527+
range: 222..228,
1528+
kind: ParameterHint,
1529+
label: "self",
1530+
},
1531+
]
1532+
"#]],
1533+
);
1534+
}
14741535
}

0 commit comments

Comments
 (0)