Skip to content

Commit

Permalink
Auto merge of #12285 - Veykril:inlay-hints, r=Veykril
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed May 17, 2022
2 parents f8fc0ea + 58a2411 commit c5c442e
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 60 deletions.
162 changes: 127 additions & 35 deletions crates/ide/src/inlay_hints.rs

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ pub use crate::{
folding_ranges::{Fold, FoldKind},
highlight_related::{HighlightRelatedConfig, HighlightedRange},
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
inlay_hints::{InlayHint, InlayHintsConfig, InlayKind, LifetimeElisionHints, ReborrowHints},
inlay_hints::{
InlayHint, InlayHintsConfig, InlayKind, LifetimeElisionHints, RangeOrOffset, ReborrowHints,
},
join_lines::JoinLinesConfig,
markup::Markup,
moniker::{MonikerKind, MonikerResult, PackageInformation},
Expand Down
10 changes: 8 additions & 2 deletions crates/rust-analyzer/src/caps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use lsp_types::{
CodeActionProviderCapability, CodeLensOptions, CompletionOptions, DeclarationCapability,
DocumentOnTypeFormattingOptions, FileOperationFilter, FileOperationPattern,
FileOperationPatternKind, FileOperationRegistrationOptions, FoldingRangeProviderCapability,
HoverProviderCapability, ImplementationProviderCapability, OneOf, RenameOptions, SaveOptions,
HoverProviderCapability, ImplementationProviderCapability, InlayHintOptions,
InlayHintServerCapabilities, OneOf, RenameOptions, SaveOptions,
SelectionRangeProviderCapability, SemanticTokensFullOptions, SemanticTokensLegend,
SemanticTokensOptions, ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability,
TextDocumentSyncKind, TextDocumentSyncOptions, TypeDefinitionProviderCapability,
Expand Down Expand Up @@ -112,7 +113,12 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities {
.into(),
),
moniker_provider: None,
inlay_hint_provider: Some(OneOf::Left(true)),
inlay_hint_provider: Some(OneOf::Right(InlayHintServerCapabilities::Options(
InlayHintOptions {
work_done_progress_options: Default::default(),
resolve_provider: Some(true),
},
))),
experimental: Some(json!({
"externalDocs": true,
"hoverRange": true,
Expand Down
45 changes: 44 additions & 1 deletion crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,11 +1343,54 @@ pub(crate) fn handle_inlay_hints(
snap.analysis
.inlay_hints(&inlay_hints_config, file_id, Some(range))?
.into_iter()
.map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it))
.map(|it| {
to_proto::inlay_hint(
&line_index,
&params.text_document,
inlay_hints_config.render_colons,
it,
)
})
.collect(),
))
}

pub(crate) fn handle_inlay_hints_resolve(
snap: GlobalStateSnapshot,
mut hint: InlayHint,
) -> Result<InlayHint> {
let _p = profile::span("handle_inlay_hints_resolve");
let data = match hint.data.take() {
Some(it) => it,
None => return Ok(hint),
};

let resolve_data: lsp_ext::InlayHintResolveData = serde_json::from_value(data)?;

let file_range = from_proto::file_range(
&snap,
resolve_data.text_document,
match resolve_data.position {
PositionOrRange::Position(pos) => Range::new(pos, pos),
PositionOrRange::Range(range) => range,
},
)?;
let info = match snap.analysis.hover(&snap.config.hover(), file_range)? {
None => return Ok(hint),
Some(info) => info,
};

let markup_kind =
snap.config.hover().documentation.map_or(ide::HoverDocFormat::Markdown, |kind| kind);

// FIXME: hover actions?
hint.tooltip = Some(lsp_types::InlayHintTooltip::MarkupContent(to_proto::markup_content(
info.info.markup,
markup_kind,
)));
Ok(hint)
}

pub(crate) fn handle_call_hierarchy_prepare(
snap: GlobalStateSnapshot,
params: CallHierarchyPrepareParams,
Expand Down
6 changes: 6 additions & 0 deletions crates/rust-analyzer/src/lsp_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,12 @@ pub struct CompletionResolveData {
pub imports: Vec<CompletionImport>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct InlayHintResolveData {
pub text_document: TextDocumentIdentifier,
pub position: PositionOrRange,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct CompletionImport {
pub full_import_path: String,
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ impl GlobalState {
.on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
.on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
.on::<lsp_types::request::InlayHintRequest>(handlers::handle_inlay_hints)
.on::<lsp_types::request::InlayHintResolveRequest>(handlers::handle_inlay_hints_resolve)
.on::<lsp_types::request::Completion>(handlers::handle_completion)
.on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
.on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)
Expand Down
54 changes: 34 additions & 20 deletions crates/rust-analyzer/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,9 @@ pub(crate) fn signature_help(
}

pub(crate) fn inlay_hint(
render_colons: bool,
line_index: &LineIndex,
text_document: &lsp_types::TextDocumentIdentifier,
render_colons: bool,
inlay_hint: InlayHint,
) -> lsp_types::InlayHint {
lsp_types::InlayHint {
Expand All @@ -433,24 +434,6 @@ pub(crate) fn inlay_hint(
| InlayKind::LifetimeHint
| InlayKind::ClosingBraceHint => position(line_index, inlay_hint.range.end()),
},
label: lsp_types::InlayHintLabel::String(match inlay_hint.kind {
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
_ => inlay_hint.label.to_string(),
}),
kind: match inlay_hint.kind {
InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER),
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
Some(lsp_types::InlayHintKind::TYPE)
}
InlayKind::BindingModeHint
| InlayKind::GenericParamListHint
| InlayKind::LifetimeHint
| InlayKind::ImplicitReborrowHint
| InlayKind::ClosingBraceHint => None,
},
tooltip: None,
padding_left: Some(match inlay_hint.kind {
InlayKind::TypeHint => !render_colons,
InlayKind::ChainingHint | InlayKind::ClosingBraceHint => true,
Expand All @@ -471,8 +454,39 @@ pub(crate) fn inlay_hint(
InlayKind::BindingModeHint => inlay_hint.label != "&",
InlayKind::ParameterHint | InlayKind::LifetimeHint => true,
}),
label: lsp_types::InlayHintLabel::String(match inlay_hint.kind {
InlayKind::ParameterHint if render_colons => format!("{}:", inlay_hint.label),
InlayKind::TypeHint if render_colons => format!(": {}", inlay_hint.label),
InlayKind::ClosureReturnTypeHint => format!(" -> {}", inlay_hint.label),
_ => inlay_hint.label.clone(),
}),
kind: match inlay_hint.kind {
InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER),
InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => {
Some(lsp_types::InlayHintKind::TYPE)
}
InlayKind::BindingModeHint
| InlayKind::GenericParamListHint
| InlayKind::LifetimeHint
| InlayKind::ImplicitReborrowHint
| InlayKind::ClosingBraceHint => None,
},
text_edits: None,
data: None,
tooltip: Some(lsp_types::InlayHintTooltip::String(inlay_hint.label)),
data: inlay_hint.hover_trigger.map(|range_or_offset| {
to_value(lsp_ext::InlayHintResolveData {
text_document: text_document.clone(),
position: match range_or_offset {
ide::RangeOrOffset::Offset(offset) => {
lsp_ext::PositionOrRange::Position(position(line_index, offset))
}
ide::RangeOrOffset::Range(text_range) => {
lsp_ext::PositionOrRange::Range(range(line_index, text_range))
}
},
})
.unwrap()
}),
}
}

Expand Down
2 changes: 1 addition & 1 deletion docs/dev/lsp-extensions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!---
lsp_ext.rs hash: 7a34bc3f38e2a7d8
lsp_ext.rs hash: 44e8238e4fbd4128
If you need to change the above hash to make the test pass, please check if you
need to adjust this doc as well and ping this issue:
Expand Down

0 comments on commit c5c442e

Please sign in to comment.