From 58248e24cd45adcbfd7bfd00e1487df196b4a8c6 Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Wed, 11 Mar 2020 20:14:39 -0700 Subject: Switch from Vec to object with props - Instead of a single object type, use several individual nested types to allow toggling from the settings GUI - Remove unused struct definitions - Install and test that the toggles work --- crates/ra_ide/src/inlay_hints.rs | 23 +++++++++--------- crates/ra_project_model/src/lib.rs | 23 ------------------ crates/rust-analyzer/src/config.rs | 4 ++-- crates/rust-analyzer/src/main_loop.rs | 2 +- crates/rust-analyzer/src/main_loop/handlers.rs | 2 +- crates/rust-analyzer/src/req.rs | 21 ++++------------- crates/rust-analyzer/src/world.rs | 2 +- editors/code/package.json | 32 +++++++++----------------- editors/code/src/client.ts | 2 +- editors/code/src/config.ts | 13 ++++++----- editors/code/src/inlay_hints.ts | 2 +- 11 files changed, 41 insertions(+), 85 deletions(-) diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 8454a975b..59922e14c 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -12,13 +12,14 @@ use crate::{FileId, FunctionSignature}; #[derive(Clone, Debug, PartialEq, Eq)] pub struct InlayConfig { - pub display_type: Vec, + pub type_hints: bool, + pub parameter_hints: bool, pub max_length: Option, } impl Default for InlayConfig { fn default() -> Self { - Self { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: None } + Self { type_hints: true, parameter_hints: true, max_length: None } } } @@ -64,7 +65,7 @@ fn get_param_name_hints( inlay_hint_opts: &InlayConfig, expr: ast::Expr, ) -> Option<()> { - if !inlay_hint_opts.display_type.contains(&InlayKind::ParameterHint) { + if !inlay_hint_opts.parameter_hints { return None; } @@ -104,7 +105,7 @@ fn get_bind_pat_hints( inlay_hint_opts: &InlayConfig, pat: ast::BindPat, ) -> Option<()> { - if !inlay_hint_opts.display_type.contains(&InlayKind::TypeHint) { + if !inlay_hint_opts.type_hints { return None; } @@ -223,7 +224,7 @@ fn get_fn_signature(sema: &Semantics, expr: &ast::Expr) -> Option< #[cfg(test)] mod tests { - use crate::inlay_hints::{InlayConfig, InlayKind}; + use crate::inlay_hints::InlayConfig; use insta::assert_debug_snapshot; use crate::mock_analysis::single_file; @@ -237,7 +238,7 @@ mod tests { let _x = foo(4, 4); }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![InlayKind::ParameterHint], max_length: None}).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ parameter_hints: true, type_hints: false, max_length: None}).unwrap(), @r###" [ InlayHint { range: [106; 107), @@ -261,7 +262,7 @@ mod tests { let _x = foo(4, 4); }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![], max_length: None}).unwrap(), @r###"[]"###); + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ type_hints: false, parameter_hints: false, max_length: None}).unwrap(), @r###"[]"###); } #[test] @@ -273,7 +274,7 @@ mod tests { let _x = foo(4, 4); }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![InlayKind::TypeHint], max_length: None}).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ type_hints: true, parameter_hints: false, max_length: None}).unwrap(), @r###" [ InlayHint { range: [97; 99), @@ -810,7 +811,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###" [ InlayHint { range: [74; 75), @@ -1020,7 +1021,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###" [] "### ); @@ -1046,7 +1047,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###" [] "### ); diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 036c8719f..37845ca56 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -16,7 +16,6 @@ use anyhow::{bail, Context, Result}; use ra_cfg::CfgOptions; use ra_db::{CrateGraph, CrateName, Edition, Env, FileId}; use rustc_hash::FxHashMap; -use serde::Deserialize; use serde_json::from_reader; pub use crate::{ @@ -25,28 +24,6 @@ pub use crate::{ sysroot::Sysroot, }; -#[derive(Deserialize, Clone, Debug, PartialEq, Eq)] -#[serde(rename_all = "lowercase")] -pub enum InlayHintDisplayType { - Off, - TypeHints, - ParameterHints, - Full, -} - -#[derive(Deserialize, Clone, Debug, PartialEq, Eq)] -#[serde(rename_all = "camelCase", default)] -pub struct InlayHintOptions { - pub display_type: InlayHintDisplayType, - pub max_length: Option, -} - -impl Default for InlayHintOptions { - fn default() -> Self { - Self { display_type: InlayHintDisplayType::Full, max_length: None } - } -} - #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub struct CargoTomlNotFoundError { pub searched_at: PathBuf, diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 22ff0845c..3a6cfbe7b 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -33,7 +33,7 @@ pub struct ServerConfig { pub lru_capacity: Option, #[serde(with = "InlayConfigDef")] - pub inlay_hint_opts: InlayConfig, + pub inlay_hints: InlayConfig, pub cargo_watch_enable: bool, pub cargo_watch_args: Vec, @@ -60,7 +60,7 @@ impl Default for ServerConfig { exclude_globs: Vec::new(), use_client_watching: false, lru_capacity: None, - inlay_hint_opts: Default::default(), + inlay_hints: Default::default(), cargo_watch_enable: true, cargo_watch_args: Vec::new(), cargo_watch_command: "check".to_string(), diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 729c384ac..b6d1f28d8 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -177,7 +177,7 @@ pub fn main_loop( .and_then(|it| it.folding_range.as_ref()) .and_then(|it| it.line_folding_only) .unwrap_or(false), - inlay_hint_opts: config.inlay_hint_opts, + inlay_hints: config.inlay_hints, 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 23463b28e..921990da0 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -997,7 +997,7 @@ pub fn handle_inlay_hints( let analysis = world.analysis(); let line_index = analysis.file_line_index(file_id)?; Ok(analysis - .inlay_hints(file_id, &world.options.inlay_hint_opts)? + .inlay_hints(file_id, &world.options.inlay_hints)? .into_iter() .map(|api_type| InlayHint { label: api_type.label.to_string(), diff --git a/crates/rust-analyzer/src/req.rs b/crates/rust-analyzer/src/req.rs index dab05405e..1dcab2703 100644 --- a/crates/rust-analyzer/src/req.rs +++ b/crates/rust-analyzer/src/req.rs @@ -2,7 +2,7 @@ use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url}; use rustc_hash::FxHashMap; -use serde::{Deserialize, Deserializer, Serialize}; +use serde::{Deserialize, Serialize}; use ra_ide::{InlayConfig, InlayKind}; @@ -204,24 +204,11 @@ pub enum InlayKindDef { 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")] +#[serde(remote = "InlayConfig", rename_all = "camelCase")] pub struct InlayConfigDef { - #[serde(deserialize_with = "vec_inlay_kind")] - pub display_type: Vec, + pub type_hints: bool, + pub parameter_hints: bool, pub max_length: Option, } diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index 6eb1ea7d2..004803b00 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -33,7 +33,7 @@ pub struct Options { pub publish_decorations: bool, pub supports_location_link: bool, pub line_folding_only: bool, - pub inlay_hint_opts: InlayConfig, + pub inlay_hints: InlayConfig, pub rustfmt_args: Vec, pub cargo_watch: CheckOptions, } diff --git a/editors/code/package.json b/editors/code/package.json index 6f2275062..296d6fe8e 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -307,28 +307,18 @@ "exclusiveMinimum": true, "description": "Number of syntax trees rust-analyzer keeps in memory" }, - "rust-analyzer.inlayHintOpts.displayType": { - "type": "string", - "enum": [ - "off", - "typeHints", - "parameterHints", - "full" - ], - "enumDescriptions": [ - "No type inlay hints", - "Type inlays hints only", - "Parameter inlays hints only", - "All inlay hints types" - ], - "default": "full", - "description": "Display additional type and parameter information in the editor" + "rust-analyzer.inlayHints.typeHints": { + "type": "boolean", + "default": true, + "description": "Whether to show inlay type hints" }, - "rust-analyzer.inlayHintOpts.maxLength": { - "type": [ - "null", - "integer" - ], + "rust-analyzer.inlayHints.parameterHints": { + "type": "boolean", + "default": true, + "description": "Whether to show function parameter name inlay hints at the call site" + }, + "rust-analyzer.inlayHints.maxLength": { + "type": "integer", "default": 20, "minimum": 0, "exclusiveMinimum": true, diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index ac4417c61..3b8ea6f77 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -29,7 +29,7 @@ export async function createClient(config: Config, serverPath: string): Promise< initializationOptions: { publishDecorations: !config.highlightingSemanticTokens, lruCapacity: config.lruCapacity, - inlayHintOpts: config.inlayHintOpts, + inlayHints: config.inlayHints, cargoWatchEnable: cargoWatchOpts.enable, cargoWatchArgs: cargoWatchOpts.arguments, cargoWatchCommand: cargoWatchOpts.command, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index b26bf10da..2668c9640 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -6,7 +6,8 @@ import { log } from "./util"; const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; export interface InlayHintOptions { - displayType: string; + typeHints: boolean; + parameterHints: boolean; maxLength: number; } @@ -28,8 +29,7 @@ export class Config { "cargoFeatures", "cargo-watch", "highlighting.semanticTokens", - "inlayHintOpts.maxLength", - "inlayHintOpts.displayType", + "inlayHints", ] .map(opt => `${Config.rootSection}.${opt}`); @@ -156,10 +156,11 @@ export class Config { get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; } get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; } get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; } - get inlayHintOpts(): InlayHintOptions { + get inlayHints(): InlayHintOptions { return { - displayType: this.cfg.get("inlayHintOpts.displayType") as string, - maxLength: this.cfg.get("inlayHintOpts.maxLength") as number, + typeHints: this.cfg.get("inlayHints.typeHints") as boolean, + parameterHints: this.cfg.get("inlayHints.parameterHints") as boolean, + maxLength: this.cfg.get("inlayHints.maxLength") as number, }; } get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; } diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 8d291406d..b19b09ad5 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -10,7 +10,7 @@ export function activateInlayHints(ctx: Ctx) { const maybeUpdater = { updater: null as null | HintsUpdater, onConfigChange() { - if (ctx.config.inlayHintOpts.displayType === 'off') { + if (!ctx.config.inlayHints.typeHints && !ctx.config.inlayHints.parameterHints) { return this.dispose(); } if (!this.updater) this.updater = new HintsUpdater(ctx); -- cgit v1.2.3