Skip to content

Commit c5c442e

Browse files
committed
2 parents f8fc0ea + 58a2411 commit c5c442e

File tree

8 files changed

+224
-60
lines changed

8 files changed

+224
-60
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 127 additions & 35 deletions
Large diffs are not rendered by default.

crates/ide/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ pub use crate::{
8080
folding_ranges::{Fold, FoldKind},
8181
highlight_related::{HighlightRelatedConfig, HighlightedRange},
8282
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
83-
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind, LifetimeElisionHints, ReborrowHints},
83+
inlay_hints::{
84+
InlayHint, InlayHintsConfig, InlayKind, LifetimeElisionHints, RangeOrOffset, ReborrowHints,
85+
},
8486
join_lines::JoinLinesConfig,
8587
markup::Markup,
8688
moniker::{MonikerKind, MonikerResult, PackageInformation},

crates/rust-analyzer/src/caps.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use lsp_types::{
44
CodeActionProviderCapability, CodeLensOptions, CompletionOptions, DeclarationCapability,
55
DocumentOnTypeFormattingOptions, FileOperationFilter, FileOperationPattern,
66
FileOperationPatternKind, FileOperationRegistrationOptions, FoldingRangeProviderCapability,
7-
HoverProviderCapability, ImplementationProviderCapability, OneOf, RenameOptions, SaveOptions,
7+
HoverProviderCapability, ImplementationProviderCapability, InlayHintOptions,
8+
InlayHintServerCapabilities, OneOf, RenameOptions, SaveOptions,
89
SelectionRangeProviderCapability, SemanticTokensFullOptions, SemanticTokensLegend,
910
SemanticTokensOptions, ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability,
1011
TextDocumentSyncKind, TextDocumentSyncOptions, TypeDefinitionProviderCapability,
@@ -112,7 +113,12 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities {
112113
.into(),
113114
),
114115
moniker_provider: None,
115-
inlay_hint_provider: Some(OneOf::Left(true)),
116+
inlay_hint_provider: Some(OneOf::Right(InlayHintServerCapabilities::Options(
117+
InlayHintOptions {
118+
work_done_progress_options: Default::default(),
119+
resolve_provider: Some(true),
120+
},
121+
))),
116122
experimental: Some(json!({
117123
"externalDocs": true,
118124
"hoverRange": true,

crates/rust-analyzer/src/handlers.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,11 +1343,54 @@ pub(crate) fn handle_inlay_hints(
13431343
snap.analysis
13441344
.inlay_hints(&inlay_hints_config, file_id, Some(range))?
13451345
.into_iter()
1346-
.map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it))
1346+
.map(|it| {
1347+
to_proto::inlay_hint(
1348+
&line_index,
1349+
&params.text_document,
1350+
inlay_hints_config.render_colons,
1351+
it,
1352+
)
1353+
})
13471354
.collect(),
13481355
))
13491356
}
13501357

1358+
pub(crate) fn handle_inlay_hints_resolve(
1359+
snap: GlobalStateSnapshot,
1360+
mut hint: InlayHint,
1361+
) -> Result<InlayHint> {
1362+
let _p = profile::span("handle_inlay_hints_resolve");
1363+
let data = match hint.data.take() {
1364+
Some(it) => it,
1365+
None => return Ok(hint),
1366+
};
1367+
1368+
let resolve_data: lsp_ext::InlayHintResolveData = serde_json::from_value(data)?;
1369+
1370+
let file_range = from_proto::file_range(
1371+
&snap,
1372+
resolve_data.text_document,
1373+
match resolve_data.position {
1374+
PositionOrRange::Position(pos) => Range::new(pos, pos),
1375+
PositionOrRange::Range(range) => range,
1376+
},
1377+
)?;
1378+
let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
1379+
None => return Ok(hint),
1380+
Some(info) => info,
1381+
};
1382+
1383+
let markup_kind =
1384+
snap.config.hover().documentation.map_or(ide::HoverDocFormat::Markdown, |kind| kind);
1385+
1386+
// FIXME: hover actions?
1387+
hint.tooltip = Some(lsp_types::InlayHintTooltip::MarkupContent(to_proto::markup_content(
1388+
info.info.markup,
1389+
markup_kind,
1390+
)));
1391+
Ok(hint)
1392+
}
1393+
13511394
pub(crate) fn handle_call_hierarchy_prepare(
13521395
snap: GlobalStateSnapshot,
13531396
params: CallHierarchyPrepareParams,

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ pub struct CompletionResolveData {
518518
pub imports: Vec<CompletionImport>,
519519
}
520520

521+
#[derive(Debug, Serialize, Deserialize)]
522+
pub struct InlayHintResolveData {
523+
pub text_document: TextDocumentIdentifier,
524+
pub position: PositionOrRange,
525+
}
526+
521527
#[derive(Debug, Serialize, Deserialize)]
522528
pub struct CompletionImport {
523529
pub full_import_path: String,

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ impl GlobalState {
612612
.on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
613613
.on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
614614
.on::<lsp_types::request::InlayHintRequest>(handlers::handle_inlay_hints)
615+
.on::<lsp_types::request::InlayHintResolveRequest>(handlers::handle_inlay_hints_resolve)
615616
.on::<lsp_types::request::Completion>(handlers::handle_completion)
616617
.on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
617618
.on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)

crates/rust-analyzer/src/to_proto.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,9 @@ pub(crate) fn signature_help(
415415
}
416416

417417
pub(crate) fn inlay_hint(
418-
render_colons: bool,
419418
line_index: &LineIndex,
419+
text_document: &lsp_types::TextDocumentIdentifier,
420+
render_colons: bool,
420421
inlay_hint: InlayHint,
421422
) -> lsp_types::InlayHint {
422423
lsp_types::InlayHint {
@@ -433,24 +434,6 @@ pub(crate) fn inlay_hint(
433434
| InlayKind::LifetimeHint
434435
| InlayKind::ClosingBraceHint => position(line_index, inlay_hint.range.end()),
435436
},
436-
label: lsp_types::InlayHintLabel::String(match inlay_hint.kind {
437-
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
438-
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
439-
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
440-
_ => inlay_hint.label.to_string(),
441-
}),
442-
kind: match inlay_hint.kind {
443-
InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER),
444-
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
445-
Some(lsp_types::InlayHintKind::TYPE)
446-
}
447-
InlayKind::BindingModeHint
448-
| InlayKind::GenericParamListHint
449-
| InlayKind::LifetimeHint
450-
| InlayKind::ImplicitReborrowHint
451-
| InlayKind::ClosingBraceHint => None,
452-
},
453-
tooltip: None,
454437
padding_left: Some(match inlay_hint.kind {
455438
InlayKind::TypeHint => !render_colons,
456439
InlayKind::ChainingHint | InlayKind::ClosingBraceHint => true,
@@ -471,8 +454,39 @@ pub(crate) fn inlay_hint(
471454
InlayKind::BindingModeHint => inlay_hint.label != "&",
472455
InlayKind::ParameterHint | InlayKind::LifetimeHint => true,
473456
}),
457+
label: lsp_types::InlayHintLabel::String(match inlay_hint.kind {
458+
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
459+
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
460+
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
461+
_ => inlay_hint.label.clone(),
462+
}),
463+
kind: match inlay_hint.kind {
464+
InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER),
465+
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
466+
Some(lsp_types::InlayHintKind::TYPE)
467+
}
468+
InlayKind::BindingModeHint
469+
| InlayKind::GenericParamListHint
470+
| InlayKind::LifetimeHint
471+
| InlayKind::ImplicitReborrowHint
472+
| InlayKind::ClosingBraceHint => None,
473+
},
474474
text_edits: None,
475-
data: None,
475+
tooltip: Some(lsp_types::InlayHintTooltip::String(inlay_hint.label)),
476+
data: inlay_hint.hover_trigger.map(|range_or_offset| {
477+
to_value(lsp_ext::InlayHintResolveData {
478+
text_document: text_document.clone(),
479+
position: match range_or_offset {
480+
ide::RangeOrOffset::Offset(offset) => {
481+
lsp_ext::PositionOrRange::Position(position(line_index, offset))
482+
}
483+
ide::RangeOrOffset::Range(text_range) => {
484+
lsp_ext::PositionOrRange::Range(range(line_index, text_range))
485+
}
486+
},
487+
})
488+
.unwrap()
489+
}),
476490
}
477491
}
478492

docs/dev/lsp-extensions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: 7a34bc3f38e2a7d8
2+
lsp_ext.rs hash: 44e8238e4fbd4128
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

0 commit comments

Comments
 (0)