From b3fa7312a77f92b37a5e9b19bdcc89b8510675b4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Mar 2020 18:01:36 +0100 Subject: [PATCH] Simpler deserialization --- crates/rust-analyzer/src/config.rs | 13 ++++++++----- crates/rust-analyzer/src/conv.rs | 18 ++++++++++++++++-- crates/rust-analyzer/src/main_loop.rs | 8 ++++++-- crates/rust-analyzer/src/main_loop/handlers.rs | 6 +----- crates/rust-analyzer/src/req.rs | 14 +------------- editors/code/src/client.ts | 7 ++++++- 6 files changed, 38 insertions(+), 28 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index a6f832ed7c6..084e17b040c 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -7,8 +7,6 @@ //! configure the server itself, feature flags are passed into analysis, and //! tweak things like automatic insertion of `()` in completions. -use crate::req::InlayConfigDef; -use ra_ide::InlayHintsOptions; use rustc_hash::FxHashMap; use ra_project_model::CargoFeatures; @@ -32,8 +30,11 @@ pub struct ServerConfig { pub lru_capacity: Option, - #[serde(with = "InlayConfigDef")] - pub inlay_hints: InlayHintsOptions, + #[serde(deserialize_with = "nullable_bool_true")] + pub inlay_hints_type: bool, + #[serde(deserialize_with = "nullable_bool_true")] + pub inlay_hints_parameter: bool, + pub inlay_hints_max_length: Option, pub cargo_watch_enable: bool, pub cargo_watch_args: Vec, @@ -63,7 +64,9 @@ fn default() -> ServerConfig { exclude_globs: Vec::new(), use_client_watching: false, lru_capacity: None, - inlay_hints: Default::default(), + inlay_hints_type: true, + inlay_hints_parameter: true, + inlay_hints_max_length: None, cargo_watch_enable: true, cargo_watch_args: Vec::new(), cargo_watch_command: "check".to_string(), diff --git a/crates/rust-analyzer/src/conv.rs b/crates/rust-analyzer/src/conv.rs index a2d68c344e4..fd4657d7e50 100644 --- a/crates/rust-analyzer/src/conv.rs +++ b/crates/rust-analyzer/src/conv.rs @@ -11,8 +11,8 @@ use ra_ide::{ translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag, - InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo, ReferenceAccess, Severity, - SourceChange, SourceFileEdit, + InlayHint, InlayKind, InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo, + ReferenceAccess, Severity, SourceChange, SourceFileEdit, }; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_text_edit::{AtomTextEdit, TextEdit}; @@ -323,6 +323,20 @@ fn conv_with(self, ctx: &FoldConvCtx) -> lsp_types::FoldingRange { } } +impl ConvWith<&LineIndex> for InlayHint { + type Output = req::InlayHint; + fn conv_with(self, line_index: &LineIndex) -> Self::Output { + req::InlayHint { + label: self.label.to_string(), + range: self.range.conv_with(line_index), + kind: match self.kind { + InlayKind::ParameterHint => req::InlayKind::ParameterHint, + InlayKind::TypeHint => req::InlayKind::TypeHint, + }, + } + } +} + impl Conv for Highlight { type Output = (u32, u32); diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 495056da3e1..2b3b16d356b 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -18,7 +18,7 @@ use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response}; use lsp_types::{ClientCapabilities, NumberOrString}; use ra_cargo_watch::{url_from_path_with_drive_lowercasing, CheckOptions, CheckTask}; -use ra_ide::{Canceled, FileId, LibraryData, SourceRootId}; +use ra_ide::{Canceled, FileId, InlayHintsOptions, LibraryData, SourceRootId}; use ra_prof::profile; use ra_vfs::{VfsFile, VfsTask, Watch}; use relative_path::RelativePathBuf; @@ -177,7 +177,11 @@ pub fn main_loop( .and_then(|it| it.folding_range.as_ref()) .and_then(|it| it.line_folding_only) .unwrap_or(false), - inlay_hints: config.inlay_hints, + inlay_hints: InlayHintsOptions { + type_hints: config.inlay_hints_type, + parameter_hints: config.inlay_hints_parameter, + max_length: config.inlay_hints_max_length, + }, cargo_watch: CheckOptions { enable: config.cargo_watch_enable, args: config.cargo_watch_args, diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 921990da030..6482f3b771a 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -999,11 +999,7 @@ pub fn handle_inlay_hints( Ok(analysis .inlay_hints(file_id, &world.options.inlay_hints)? .into_iter() - .map(|api_type| InlayHint { - label: api_type.label.to_string(), - range: api_type.range.conv_with(&line_index), - kind: api_type.kind, - }) + .map_conv_with(&line_index) .collect()) } diff --git a/crates/rust-analyzer/src/req.rs b/crates/rust-analyzer/src/req.rs index fa7a42099b5..a3efe3b9feb 100644 --- a/crates/rust-analyzer/src/req.rs +++ b/crates/rust-analyzer/src/req.rs @@ -4,8 +4,6 @@ use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; -use ra_ide::{InlayHintsOptions, InlayKind}; - pub use lsp_types::{ notification::*, request::*, ApplyWorkspaceEditParams, CodeActionParams, CodeLens, CodeLensParams, CompletionParams, CompletionResponse, DiagnosticTag, @@ -198,24 +196,14 @@ pub struct InlayHintsParams { } #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] -#[serde(remote = "InlayKind")] -pub enum InlayKindDef { +pub enum InlayKind { TypeHint, ParameterHint, } -#[derive(Deserialize)] -#[serde(remote = "InlayConfig", rename_all = "camelCase")] -pub struct InlayConfigDef { - pub type_hints: bool, - pub parameter_hints: bool, - pub max_length: Option, -} - #[derive(Debug, Deserialize, Serialize)] pub struct InlayHint { pub range: Range, - #[serde(with = "InlayKindDef")] pub kind: InlayKind, pub label: String, } diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index e9f261c2419..b2c830b309a 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -29,11 +29,16 @@ export async function createClient(config: Config, serverPath: string): Promise< initializationOptions: { publishDecorations: !config.highlightingSemanticTokens, lruCapacity: config.lruCapacity, - inlayHints: config.inlayHints, + + inlayHintsType: config.inlayHints.typeHints, + inlayHintsParameter: config.inlayHints.parameterHints, + inlayHintsMaxLength: config.inlayHints.maxLength, + cargoWatchEnable: cargoWatchOpts.enable, cargoWatchArgs: cargoWatchOpts.arguments, cargoWatchCommand: cargoWatchOpts.command, cargoWatchAllTargets: cargoWatchOpts.allTargets, + excludeGlobs: config.excludeGlobs, useClientWatching: config.useClientWatching, featureFlags: config.featureFlags,