Skip to content

Commit 87658c8

Browse files
committed
refactor hover
change struct_rest_pat to guarentee a HoverResult Move token and node matching out of struct_rest_pat into hover
1 parent 8b17681 commit 87658c8

File tree

2 files changed

+66
-61
lines changed

2 files changed

+66
-61
lines changed

crates/ide/src/hover.rs

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub(crate) fn hover(
127127
original_token.parent().and_then(ast::TokenTree::cast),
128128
Some(tt) if tt.syntax().ancestors().any(|it| ast::Meta::can_cast(it.kind()))
129129
);
130+
130131
// prefer descending the same token kind in attribute expansions, in normal macros text
131132
// equivalency is more important
132133
let descended = if in_attr {
@@ -135,54 +136,68 @@ pub(crate) fn hover(
135136
sema.descend_into_macros_with_same_text(original_token.clone())
136137
};
137138

138-
// FIXME: Definition should include known lints and the like instead of having this special case here
139-
let hovered_lint = descended.iter().find_map(|token| {
140-
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
141-
render::try_for_lint(&attr, token)
142-
});
143-
if let Some(res) = hovered_lint {
144-
return Some(RangeInfo::new(original_token.text_range(), res));
145-
}
146-
139+
// try lint hover
147140
let result = descended
148141
.iter()
149-
.filter_map(|token| {
150-
let node = token.parent()?;
151-
let class = IdentClass::classify_token(sema, token)?;
152-
if let IdentClass::Operator(OperatorClass::Await(_)) = class {
153-
// It's better for us to fall back to the keyword hover here,
154-
// rendering poll is very confusing
155-
return None;
156-
}
157-
Some(class.definitions().into_iter().zip(iter::once(node).cycle()))
142+
.find_map(|token| {
143+
// FIXME: Definition should include known lints and the like instead of having this special case here
144+
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
145+
render::try_for_lint(&attr, token)
158146
})
159-
.flatten()
160-
.unique_by(|&(def, _)| def)
161-
.filter_map(|(def, node)| hover_for_definition(sema, file_id, def, &node, config))
162-
.reduce(|mut acc: HoverResult, HoverResult { markup, actions }| {
163-
acc.actions.extend(actions);
164-
acc.markup = Markup::from(format!("{}\n---\n{}", acc.markup, markup));
165-
acc
166-
});
147+
// try item definitions
148+
.or_else(|| {
149+
descended
150+
.iter()
151+
.filter_map(|token| {
152+
let node = token.parent()?;
153+
let class = IdentClass::classify_token(sema, token)?;
154+
if let IdentClass::Operator(OperatorClass::Await(_)) = class {
155+
// It's better for us to fall back to the keyword hover here,
156+
// rendering poll is very confusing
157+
return None;
158+
}
159+
Some(class.definitions().into_iter().zip(iter::once(node).cycle()))
160+
})
161+
.flatten()
162+
.unique_by(|&(def, _)| def)
163+
.filter_map(|(def, node)| hover_for_definition(sema, file_id, def, &node, config))
164+
.reduce(|mut acc: HoverResult, HoverResult { markup, actions }| {
165+
acc.actions.extend(actions);
166+
acc.markup = Markup::from(format!("{}\n---\n{}", acc.markup, markup));
167+
acc
168+
})
169+
})
170+
// try keywords
171+
.or_else(|| {
172+
descended.iter().find_map(|token| render::keyword(sema, config, token))
173+
})
174+
// try rest item hover
175+
.or_else(|| {
176+
descended.iter().find_map(|token| {
177+
if token.kind() != DOT2 {
178+
return None;
179+
}
167180

168-
if result.is_none() {
169-
// fallbacks, show keywords or types
181+
let record_pat_field_list =
182+
token.parent_ancestors().find_map(ast::RecordPatFieldList::cast)?;
170183

171-
let res = descended.iter().find_map(|token| render::keyword(sema, config, token));
172-
if let Some(res) = res {
173-
return Some(RangeInfo::new(original_token.text_range(), res));
174-
}
175-
let res = descended
176-
.iter()
177-
.find_map(|token| hover_type_fallback(sema, config, token, &original_token));
178-
if let Some(_) = res {
179-
return res;
180-
}
181-
}
182-
result.map(|mut res: HoverResult| {
183-
res.actions = dedupe_or_merge_hover_actions(res.actions);
184-
RangeInfo::new(original_token.text_range(), res)
185-
})
184+
let record_pat =
185+
record_pat_field_list.syntax().parent().and_then(ast::RecordPat::cast)?;
186+
187+
Some(render::struct_rest_pat(sema, config, &record_pat))
188+
})
189+
});
190+
191+
result
192+
.map(|mut res: HoverResult| {
193+
res.actions = dedupe_or_merge_hover_actions(res.actions);
194+
RangeInfo::new(original_token.text_range(), res)
195+
})
196+
// fallback to type hover if there aren't any other suggestions
197+
// this finds its own range instead of using the closest token's range
198+
.or_else(|| {
199+
descended.iter().find_map(|token| hover_type_fallback(sema, config, token, &token))
200+
})
186201
}
187202

188203
pub(crate) fn hover_for_definition(
@@ -268,8 +283,7 @@ fn hover_type_fallback(
268283
}
269284
};
270285

271-
let res = render::type_info(sema, config, &expr_or_pat)
272-
.or_else(|| render::struct_rest_pat(sema, config, &expr_or_pat))?;
286+
let res = render::type_info(sema, config, &expr_or_pat)?;
273287

274288
let range = sema
275289
.original_range_opt(&node)

crates/ide/src/hover/render.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,24 +252,15 @@ pub(super) fn keyword(
252252
Some(HoverResult { markup, actions })
253253
}
254254

255+
/// Returns missing types in a record pattern.
256+
/// Only makes sense when there's a rest pattern in the record pattern.
257+
/// i.e. `let S {a, ..} = S {a: 1, b: 2}`
255258
pub(super) fn struct_rest_pat(
256259
sema: &Semantics<'_, RootDatabase>,
257260
config: &HoverConfig,
258-
expr_or_pat: &Either<ast::Expr, ast::Pat>,
259-
) -> Option<HoverResult> {
260-
let pat = expr_or_pat.as_ref().right()?;
261-
262-
let mut ancestors = pat.syntax().ancestors();
263-
let _record_pat_field_list = ancestors.next()?;
264-
let record_pat = ancestors.next()?;
265-
let pattern = sema
266-
.find_nodes_at_offset_with_descend::<RecordPat>(
267-
&record_pat,
268-
record_pat.text_range().start(),
269-
)
270-
.next()?;
271-
272-
let missing_fields = sema.record_pattern_missing_fields(&pattern);
261+
pattern: &RecordPat,
262+
) -> HoverResult {
263+
let missing_fields = sema.record_pattern_missing_fields(pattern);
273264

274265
// if there are no missing fields, the end result is a hover that shows ".."
275266
// should be left in to indicate that there are no more fields in the pattern
@@ -302,7 +293,7 @@ pub(super) fn struct_rest_pat(
302293
}
303294
};
304295
res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets));
305-
Some(res)
296+
res
306297
}
307298

308299
pub(super) fn try_for_lint(attr: &ast::Attr, token: &SyntaxToken) -> Option<HoverResult> {

0 commit comments

Comments
 (0)