Skip to content

Commit 3aa6306

Browse files
committed
Auto merge of #16089 - SomeoneToIgnore:fix-parameter-inlay-hints-resolution, r=Veykril
Query for nearest parent block around the hint to resolve This way, parameter hints will be found for resolution and #15522 (comment) will be fixed Hopefully that also helps with whatever else (lifetimes', etc.) hints in #13962 > hints are resolved by querying for textDocument/inlayHint with hint's position turned into a [position-1, position+1] range (instead of the original, much wider document range). > > This might lead to issues in the future, with e.g. lifetime hints (currently there's nothing to resolve for them and it's fine) that belong to a certain position, but need to have textDocument/inlayHint query for much bigger range than their position+/-1
2 parents 457b966 + 8ae42b5 commit 3aa6306

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ fn ty_to_text_edit(
422422
Some(builder.finish())
423423
}
424424

425+
pub enum RangeLimit {
426+
Fixed(TextRange),
427+
NearestParent(TextSize),
428+
}
429+
425430
// Feature: Inlay Hints
426431
//
427432
// rust-analyzer shows additional information inline with the source code.
@@ -443,7 +448,7 @@ fn ty_to_text_edit(
443448
pub(crate) fn inlay_hints(
444449
db: &RootDatabase,
445450
file_id: FileId,
446-
range_limit: Option<TextRange>,
451+
range_limit: Option<RangeLimit>,
447452
config: &InlayHintsConfig,
448453
) -> Vec<InlayHint> {
449454
let _p = profile::span("inlay_hints");
@@ -458,13 +463,31 @@ pub(crate) fn inlay_hints(
458463

459464
let hints = |node| hints(&mut acc, &famous_defs, config, file_id, node);
460465
match range_limit {
461-
Some(range) => match file.covering_element(range) {
466+
Some(RangeLimit::Fixed(range)) => match file.covering_element(range) {
462467
NodeOrToken::Token(_) => return acc,
463468
NodeOrToken::Node(n) => n
464469
.descendants()
465470
.filter(|descendant| range.intersect(descendant.text_range()).is_some())
466471
.for_each(hints),
467472
},
473+
Some(RangeLimit::NearestParent(position)) => {
474+
match file.token_at_offset(position).left_biased() {
475+
Some(token) => {
476+
if let Some(parent_block) =
477+
token.parent_ancestors().find_map(ast::BlockExpr::cast)
478+
{
479+
parent_block.syntax().descendants().for_each(hints)
480+
} else if let Some(parent_item) =
481+
token.parent_ancestors().find_map(ast::Item::cast)
482+
{
483+
parent_item.syntax().descendants().for_each(hints)
484+
} else {
485+
return acc;
486+
}
487+
}
488+
None => return acc,
489+
}
490+
}
468491
None => file.descendants().for_each(hints),
469492
};
470493
}

crates/ide/src/inlay_hints/bind_pat.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ mod tests {
177177
use syntax::{TextRange, TextSize};
178178
use test_utils::extract_annotations;
179179

180-
use crate::{fixture, inlay_hints::InlayHintsConfig, ClosureReturnTypeHints};
180+
use crate::{
181+
fixture,
182+
inlay_hints::{InlayHintsConfig, RangeLimit},
183+
ClosureReturnTypeHints,
184+
};
181185

182186
use crate::inlay_hints::tests::{
183187
check, check_edit, check_no_edit, check_with_config, DISABLED_CONFIG, TEST_CONFIG,
@@ -400,7 +404,7 @@ fn main() {
400404
.inlay_hints(
401405
&InlayHintsConfig { type_hints: true, ..DISABLED_CONFIG },
402406
file_id,
403-
Some(TextRange::new(TextSize::from(500), TextSize::from(600))),
407+
Some(RangeLimit::Fixed(TextRange::new(TextSize::from(500), TextSize::from(600)))),
404408
)
405409
.unwrap();
406410
let actual =

crates/ide/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub use crate::{
9494
inlay_hints::{
9595
AdjustmentHints, AdjustmentHintsMode, ClosureReturnTypeHints, DiscriminantHints,
9696
InlayFieldsToResolve, InlayHint, InlayHintLabel, InlayHintLabelPart, InlayHintPosition,
97-
InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints,
97+
InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints, RangeLimit,
9898
},
9999
join_lines::JoinLinesConfig,
100100
markup::Markup,
@@ -397,7 +397,7 @@ impl Analysis {
397397
&self,
398398
config: &InlayHintsConfig,
399399
file_id: FileId,
400-
range: Option<TextRange>,
400+
range: Option<RangeLimit>,
401401
) -> Cancellable<Vec<InlayHint>> {
402402
self.with_db(|db| inlay_hints::inlay_hints(db, file_id, range, config))
403403
}

crates/rust-analyzer/src/handlers/request.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use anyhow::Context;
1212

1313
use ide::{
1414
AnnotationConfig, AssistKind, AssistResolveStrategy, Cancellable, FilePosition, FileRange,
15-
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, ReferenceCategory,
16-
Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit,
15+
HoverAction, HoverGotoTypeData, InlayFieldsToResolve, Query, RangeInfo, RangeLimit,
16+
ReferenceCategory, Runnable, RunnableKind, SingleResolve, SourceChange, TextEdit,
1717
};
1818
use ide_db::SymbolKind;
1919
use lsp_server::ErrorCode;
@@ -1409,7 +1409,7 @@ pub(crate) fn handle_inlay_hints(
14091409
let inlay_hints_config = snap.config.inlay_hints();
14101410
Ok(Some(
14111411
snap.analysis
1412-
.inlay_hints(&inlay_hints_config, file_id, Some(range))?
1412+
.inlay_hints(&inlay_hints_config, file_id, Some(RangeLimit::Fixed(range)))?
14131413
.into_iter()
14141414
.map(|it| {
14151415
to_proto::inlay_hint(
@@ -1440,22 +1440,13 @@ pub(crate) fn handle_inlay_hints_resolve(
14401440
anyhow::ensure!(snap.file_exists(file_id), "Invalid LSP resolve data");
14411441

14421442
let line_index = snap.file_line_index(file_id)?;
1443-
let range = from_proto::text_range(
1444-
&line_index,
1445-
lsp_types::Range { start: original_hint.position, end: original_hint.position },
1446-
)?;
1447-
let range_start = range.start();
1448-
let range_end = range.end();
1449-
let large_range = TextRange::new(
1450-
range_start.checked_sub(1.into()).unwrap_or(range_start),
1451-
range_end.checked_add(1.into()).unwrap_or(range_end),
1452-
);
1443+
let hint_position = from_proto::offset(&line_index, original_hint.position)?;
14531444
let mut forced_resolve_inlay_hints_config = snap.config.inlay_hints();
14541445
forced_resolve_inlay_hints_config.fields_to_resolve = InlayFieldsToResolve::empty();
14551446
let resolve_hints = snap.analysis.inlay_hints(
14561447
&forced_resolve_inlay_hints_config,
14571448
file_id,
1458-
Some(large_range),
1449+
Some(RangeLimit::NearestParent(hint_position)),
14591450
)?;
14601451

14611452
let mut resolved_hints = resolve_hints

0 commit comments

Comments
 (0)