From 974ed7155acccb5da0c2aeac09d7052c4f75902d Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Tue, 10 Mar 2020 18:59:49 -0700 Subject: Deduplicate some Inlay definitions - Remove match conversion for InlayKind since we're using remote --- crates/rust-analyzer/src/config.rs | 31 ++------------------------ crates/rust-analyzer/src/main_loop/handlers.rs | 7 ++---- crates/rust-analyzer/src/req.rs | 29 ++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 36 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 2f407723b..22ff0845c 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -7,40 +7,13 @@ //! configure the server itself, feature flags are passed into analysis, and //! tweak things like automatic insertion of `()` in completions. -use ra_ide::{InlayConfig, InlayKind}; +use crate::req::InlayConfigDef; +use ra_ide::InlayConfig; use rustc_hash::FxHashMap; use ra_project_model::CargoFeatures; use serde::{Deserialize, Deserializer}; -#[derive(Deserialize)] -#[serde(remote = "InlayKind")] -pub enum InlayKindDef { - TypeHint, - ParameterHint, -} - -// Work-around until better serde support is added -// https://github.com/serde-rs/serde/issues/723#issuecomment-382501277 -fn vec_inlay_kind<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - #[derive(Deserialize)] - struct Wrapper(#[serde(with = "InlayKindDef")] InlayKind); - - let v = Vec::deserialize(deserializer)?; - Ok(v.into_iter().map(|Wrapper(a)| a).collect()) -} - -#[derive(Deserialize)] -#[serde(remote = "InlayConfig")] -pub struct InlayConfigDef { - #[serde(deserialize_with = "vec_inlay_kind")] - pub display_type: Vec, - pub max_length: Option, -} - /// Client provided initialization options #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] #[serde(rename_all = "camelCase", default)] diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 4f58bcb0a..23463b28e 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -37,7 +37,7 @@ use crate::{ }, diagnostics::DiagnosticTask, from_json, - req::{self, Decoration, InlayHint, InlayHintsParams, InlayKind}, + req::{self, Decoration, InlayHint, InlayHintsParams}, semantic_tokens::SemanticTokensBuilder, world::WorldSnapshot, LspError, Result, @@ -1002,10 +1002,7 @@ pub fn handle_inlay_hints( .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::InlayKind::TypeHint => InlayKind::TypeHint, - ra_ide::InlayKind::ParameterHint => InlayKind::ParameterHint, - }, + kind: api_type.kind, }) .collect()) } diff --git a/crates/rust-analyzer/src/req.rs b/crates/rust-analyzer/src/req.rs index a3efe3b9f..dab05405e 100644 --- a/crates/rust-analyzer/src/req.rs +++ b/crates/rust-analyzer/src/req.rs @@ -2,7 +2,9 @@ use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url}; use rustc_hash::FxHashMap; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize}; + +use ra_ide::{InlayConfig, InlayKind}; pub use lsp_types::{ notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens, @@ -196,14 +198,37 @@ pub struct InlayHintsParams { } #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] -pub enum InlayKind { +#[serde(remote = "InlayKind")] +pub enum InlayKindDef { TypeHint, ParameterHint, } +// Work-around until better serde support is added +// https://github.com/serde-rs/serde/issues/723#issuecomment-382501277 +fn vec_inlay_kind<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + #[derive(Deserialize)] + struct Wrapper(#[serde(with = "InlayKindDef")] InlayKind); + + let v = Vec::deserialize(deserializer)?; + Ok(v.into_iter().map(|Wrapper(a)| a).collect()) +} + +#[derive(Deserialize)] +#[serde(remote = "InlayConfig")] +pub struct InlayConfigDef { + #[serde(deserialize_with = "vec_inlay_kind")] + pub display_type: Vec, + pub max_length: Option, +} + #[derive(Debug, Deserialize, Serialize)] pub struct InlayHint { pub range: Range, + #[serde(with = "InlayKindDef")] pub kind: InlayKind, pub label: String, } -- cgit v1.2.3