diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs index 8c19f584f0..ca42bf321e 100644 --- a/crates/rust-analyzer/src/caps.rs +++ b/crates/rust-analyzer/src/caps.rs @@ -112,11 +112,10 @@ pub fn server_capabilities(config: &Config) -> ServerCapabilities { .into(), ), moniker_provider: None, - inlay_hint_provider: None, + inlay_hint_provider: Some(OneOf::Left(true)), experimental: Some(json!({ "externalDocs": true, "hoverRange": true, - "inlayHints": true, "joinLines": true, "matchingBrace": true, "moveItem": true, diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 78118764fa..fb64eeea4f 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -19,11 +19,11 @@ use lsp_types::{ CallHierarchyIncomingCall, CallHierarchyIncomingCallsParams, CallHierarchyItem, CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams, CodeLens, CompletionItem, Diagnostic, DiagnosticTag, DocumentFormattingParams, FoldingRange, - FoldingRangeParams, HoverContents, Location, LocationLink, NumberOrString, Position, - PrepareRenameResponse, Range, RenameParams, SemanticTokensDeltaParams, - SemanticTokensFullDeltaResult, SemanticTokensParams, SemanticTokensRangeParams, - SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, SymbolTag, - TextDocumentIdentifier, Url, WorkspaceEdit, + FoldingRangeParams, HoverContents, InlayHint, InlayHintParams, Location, LocationLink, + NumberOrString, Position, PrepareRenameResponse, Range, RenameParams, + SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams, + SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation, + SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit, }; use project_model::{ManifestPath, ProjectWorkspace, TargetKind}; use serde_json::json; @@ -38,10 +38,7 @@ use crate::{ from_proto, global_state::{GlobalState, GlobalStateSnapshot}, line_index::LineEndings, - lsp_ext::{ - self, InlayHint, InlayHintsParams, PositionOrRange, ViewCrateGraphParams, - WorkspaceSymbolParams, - }, + lsp_ext::{self, PositionOrRange, ViewCrateGraphParams, WorkspaceSymbolParams}, lsp_utils::{all_edits_are_disjoint, invalid_params_error}, to_proto, LspError, Result, }; @@ -1322,29 +1319,25 @@ pub(crate) fn publish_diagnostics( pub(crate) fn handle_inlay_hints( snap: GlobalStateSnapshot, - params: InlayHintsParams, -) -> Result> { + params: InlayHintParams, +) -> Result>> { let _p = profile::span("handle_inlay_hints"); let document_uri = ¶ms.text_document.uri; let file_id = from_proto::file_id(&snap, document_uri)?; let line_index = snap.file_line_index(file_id)?; - let range = params - .range - .map(|range| { - from_proto::file_range( - &snap, - TextDocumentIdentifier::new(document_uri.to_owned()), - range, - ) - }) - .transpose()?; + let range = from_proto::file_range( + &snap, + TextDocumentIdentifier::new(document_uri.to_owned()), + params.range, + )?; let inlay_hints_config = snap.config.inlay_hints(); - Ok(snap - .analysis - .inlay_hints(&inlay_hints_config, file_id, range)? - .into_iter() - .map(|it| to_proto::inlay_hint(inlay_hints_config.render_colons, &line_index, it)) - .collect()) + Ok(Some( + 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)) + .collect(), + )) } pub(crate) fn handle_call_hierarchy_prepare( diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index b638a57111..489a233a0a 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -236,14 +236,6 @@ pub struct TestInfo { pub runnable: Runnable, } -pub enum InlayHints {} - -impl Request for InlayHints { - type Params = InlayHintsParams; - type Result = Vec; - const METHOD: &'static str = "experimental/inlayHints"; -} - #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct InlayHintsParams { @@ -251,44 +243,6 @@ pub struct InlayHintsParams { pub range: Option, } -#[derive(Eq, PartialEq, Debug, Copy, Clone, Serialize, Deserialize)] -#[serde(transparent)] -pub struct InlayHintKind(u8); - -impl InlayHintKind { - pub const TYPE: InlayHintKind = InlayHintKind(1); - pub const PARAMETER: InlayHintKind = InlayHintKind(2); -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct InlayHint { - pub label: InlayHintLabel, - pub position: Position, - pub kind: Option, - pub tooltip: Option, - pub padding_left: Option, - pub padding_right: Option, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(untagged)] -pub enum InlayHintLabel { - String(String), - Parts(Vec), -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct InlayHintLabelPart { - pub value: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub tooltip: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub location: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub command: Option, -} pub enum Ssr {} impl Request for Ssr { diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 2d898d76a6..27ec32b1f4 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -597,7 +597,6 @@ impl GlobalState { .on::(handlers::handle_parent_module) .on::(handlers::handle_runnables) .on::(handlers::handle_related_tests) - .on::(handlers::handle_inlay_hints) .on::(handlers::handle_code_action) .on::(handlers::handle_code_action_resolve) .on::(handlers::handle_hover) @@ -611,6 +610,7 @@ impl GlobalState { .on::(handlers::handle_goto_declaration) .on::(handlers::handle_goto_implementation) .on::(handlers::handle_goto_type_definition) + .on::(handlers::handle_inlay_hints) .on::(handlers::handle_completion) .on::(handlers::handle_completion_resolve) .on::(handlers::handle_code_lens) diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index e52505d8a1..0588f00678 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -418,14 +418,8 @@ pub(crate) fn inlay_hint( render_colons: bool, line_index: &LineIndex, inlay_hint: InlayHint, -) -> lsp_ext::InlayHint { - lsp_ext::InlayHint { - label: lsp_ext::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(), - }), +) -> lsp_types::InlayHint { + lsp_types::InlayHint { position: match inlay_hint.kind { // before annotated thing InlayKind::ParameterHint | InlayKind::ImplicitReborrow => { @@ -438,10 +432,16 @@ pub(crate) fn inlay_hint( | InlayKind::GenericParamListHint | InlayKind::LifetimeHint => 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_ext::InlayHintKind::PARAMETER), + InlayKind::ParameterHint => Some(lsp_types::InlayHintKind::PARAMETER), InlayKind::ClosureReturnTypeHint | InlayKind::TypeHint | InlayKind::ChainingHint => { - Some(lsp_ext::InlayHintKind::TYPE) + Some(lsp_types::InlayHintKind::TYPE) } InlayKind::GenericParamListHint | InlayKind::LifetimeHint @@ -465,6 +465,7 @@ pub(crate) fn inlay_hint( InlayKind::GenericParamListHint => false, InlayKind::ImplicitReborrow => false, }), + text_edits: None, } } diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md index 516d1caccc..46193a6ff3 100644 --- a/docs/dev/lsp-extensions.md +++ b/docs/dev/lsp-extensions.md @@ -1,5 +1,5 @@