From 8f3377d9f93a256f8e68ae183808fd767b529d18 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 22 Jul 2019 21:52:47 +0300 Subject: [PATCH] Code review fixes --- crates/ra_ide_api/src/inlay_hints.rs | 49 ++++++++----------- crates/ra_lsp_server/src/main_loop.rs | 1 + .../ra_lsp_server/src/main_loop/handlers.rs | 23 ++++++++- crates/ra_lsp_server/src/req.rs | 27 ++++++++++ 4 files changed, 70 insertions(+), 30 deletions(-) diff --git a/crates/ra_ide_api/src/inlay_hints.rs b/crates/ra_ide_api/src/inlay_hints.rs index 1b1f28951a..174662beb5 100644 --- a/crates/ra_ide_api/src/inlay_hints.rs +++ b/crates/ra_ide_api/src/inlay_hints.rs @@ -9,16 +9,15 @@ use ra_syntax::{ #[derive(Debug, PartialEq, Eq)] pub enum InlayKind { - LetBinding, - ClosureParameter, + LetBindingType, + ClosureParameterType, } #[derive(Debug)] pub struct InlayHint { pub range: TextRange, - pub text: SmolStr, - pub inlay_kind: InlayKind, - pub inlay_type_string: SmolStr, + pub kind: InlayKind, + pub label: SmolStr, } pub(crate) fn inlay_hints(db: &RootDatabase, file_id: FileId, file: &SourceFile) -> Vec { @@ -56,9 +55,8 @@ fn get_inlay_hints( Some(vec![InlayHint { range: pat_range, - text: let_syntax.text().to_string().into(), - inlay_kind: InlayKind::LetBinding, - inlay_type_string, + kind: InlayKind::LetBindingType, + label: inlay_type_string, }]) }) .visit(|closure_parameter: ast::LambdaExpr| match closure_parameter.param_list() { @@ -80,9 +78,8 @@ fn get_inlay_hints( Some(InlayHint { range: closure_param_syntax.text_range(), - text: closure_param_syntax.text().to_string().into(), - inlay_kind: InlayKind::ClosureParameter, - inlay_type_string, + kind: InlayKind::ClosureParameterType, + label: inlay_type_string, }) }) .collect(), @@ -149,39 +146,33 @@ fn main() { assert_debug_snapshot_matches!(analysis.inlay_hints(file_id).unwrap(), @r#"[ InlayHint { range: [71; 75), - text: "let test = 54;", - inlay_kind: LetBinding, - inlay_type_string: "i32", + kind: LetBindingType, + label: "i32", }, InlayHint { range: [121; 125), - text: "let test = OuterStruct {};", - inlay_kind: LetBinding, - inlay_type_string: "OuterStruct", + kind: LetBindingType, + label: "OuterStruct", }, InlayHint { range: [297; 305), - text: "let mut test = 33;", - inlay_kind: LetBinding, - inlay_type_string: "i32", + kind: LetBindingType, + label: "i32", }, InlayHint { range: [417; 426), - text: "let i_squared = i * i;", - inlay_kind: LetBinding, - inlay_type_string: "u32", + kind: LetBindingType, + label: "u32", }, InlayHint { range: [496; 502), - text: "let (x, c) = (42, \'a\');", - inlay_kind: LetBinding, - inlay_type_string: "(i32, char)", + kind: LetBindingType, + label: "(i32, char)", }, InlayHint { range: [524; 528), - text: "let test = (42, \'a\');", - inlay_kind: LetBinding, - inlay_type_string: "(i32, char)", + kind: LetBindingType, + label: "(i32, char)", }, ]"# ); diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 668d2fd729..8e830c8b85 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -362,6 +362,7 @@ fn on_request( .on::(handlers::handle_references)? .on::(handlers::handle_formatting)? .on::(handlers::handle_document_highlight)? + .on::(handlers::handle_inlay_hints)? .finish(); Ok(()) } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 68865b7555..5bf950a537 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -21,7 +21,7 @@ use url_serde::Ser; use crate::{ cargo_target_spec::{runnable_args, CargoTargetSpec}, conv::{to_location, Conv, ConvWith, MapConvWith, TryConvWith, TryConvWithToVec}, - req::{self, Decoration}, + req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind}, world::WorldSnapshot, LspError, Result, }; @@ -874,3 +874,24 @@ fn to_diagnostic_severity(severity: Severity) -> DiagnosticSeverity { WeakWarning => DiagnosticSeverity::Hint, } } + +pub fn handle_inlay_hints( + world: WorldSnapshot, + params: InlayHintsParams, +) -> Result> { + let file_id = params.text_document.try_conv_with(&world)?; + let analysis = world.analysis(); + let line_index = analysis.file_line_index(file_id); + Ok(analysis + .inlay_hints(file_id)? + .into_iter() + .map(|api_type| InlayHint { + label: api_type.label.to_string(), + range: api_type.range.conv_with(&line_index), + kind: match api_type.kind { + ra_ide_api::InlayKind::LetBindingType => InlayKind::LetBindingType, + ra_ide_api::InlayKind::ClosureParameterType => InlayKind::ClosureParameterType, + }, + }) + .collect()) +} diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs index 8d39b04a74..916185f99d 100644 --- a/crates/ra_lsp_server/src/req.rs +++ b/crates/ra_lsp_server/src/req.rs @@ -196,3 +196,30 @@ pub struct SourceChange { pub workspace_edit: WorkspaceEdit, pub cursor_position: Option, } + +pub enum InlayHints {} + +impl Request for InlayHints { + type Params = InlayHintsParams; + type Result = Vec; + const METHOD: &'static str = "rust-analyzer/inlayHints"; +} + +#[derive(Serialize, Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct InlayHintsParams { + pub text_document: TextDocumentIdentifier, +} + +#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] +pub enum InlayKind { + LetBindingType, + ClosureParameterType, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct InlayHint { + pub range: Range, + pub kind: InlayKind, + pub label: String, +}