From e98aff109a1c4bda6a05f16981898425c302aa0c Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Tue, 10 Mar 2020 00:55:46 -0700 Subject: [PATCH 1/5] Parameter inlay hint separate from variable type inlay? #2876 Add setting to allow enabling either type inlay hints or parameter inlay hints or both. Group the the max inlay hint length option into the object. - Add a new type for the inlayHint options. - Add tests to ensure the inlays don't happen on the server side --- Cargo.lock | 1 + crates/ra_ide/Cargo.toml | 1 + crates/ra_ide/src/inlay_hints.rs | 101 +++++++++++++++++++++----- crates/ra_ide/src/lib.rs | 5 +- crates/ra_project_model/src/lib.rs | 29 ++++++++ crates/rust-analyzer/src/config.rs | 5 +- crates/rust-analyzer/src/main_loop.rs | 2 +- crates/rust-analyzer/src/world.rs | 4 +- docs/user/features.md | 4 +- editors/code/package.json | 20 ++++- editors/code/src/client.ts | 2 +- editors/code/src/config.ts | 13 +++- editors/code/src/inlay_hints.ts | 2 +- 13 files changed, 155 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 330bdd1cba..6173741b1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1028,6 +1028,7 @@ dependencies = [ "ra_hir", "ra_ide_db", "ra_prof", + "ra_project_model", "ra_syntax", "ra_text_edit", "rand", diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index 7235c944c9..486832529c 100644 --- a/crates/ra_ide/Cargo.toml +++ b/crates/ra_ide/Cargo.toml @@ -21,6 +21,7 @@ rustc-hash = "1.1.0" rand = { version = "0.7.3", features = ["small_rng"] } ra_syntax = { path = "../ra_syntax" } +ra_project_model = { path = "../ra_project_model" } ra_text_edit = { path = "../ra_text_edit" } ra_db = { path = "../ra_db" } ra_ide_db = { path = "../ra_ide_db" } diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index cf0cbdbd02..0f1c13c149 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -3,6 +3,7 @@ use hir::{Adt, HirDisplay, Semantics, Type}; use ra_ide_db::RootDatabase; use ra_prof::profile; +use ra_project_model::{InlayHintDisplayType, InlayHintOptions}; use ra_syntax::{ ast::{self, ArgListOwner, AstNode, TypeAscriptionOwner}, match_ast, SmolStr, TextRange, @@ -26,7 +27,7 @@ pub struct InlayHint { pub(crate) fn inlay_hints( db: &RootDatabase, file_id: FileId, - max_inlay_hint_length: Option, + inlay_hint_opts: &InlayHintOptions, ) -> Vec { let _p = profile("inlay_hints"); let sema = Semantics::new(db); @@ -36,9 +37,9 @@ pub(crate) fn inlay_hints( for node in file.syntax().descendants() { match_ast! { match node { - ast::CallExpr(it) => { get_param_name_hints(&mut res, &sema, ast::Expr::from(it)); }, - ast::MethodCallExpr(it) => { get_param_name_hints(&mut res, &sema, ast::Expr::from(it)); }, - ast::BindPat(it) => { get_bind_pat_hints(&mut res, &sema, max_inlay_hint_length, it); }, + ast::CallExpr(it) => { get_param_name_hints(&mut res, &sema, inlay_hint_opts, ast::Expr::from(it)); }, + ast::MethodCallExpr(it) => { get_param_name_hints(&mut res, &sema, inlay_hint_opts, ast::Expr::from(it)); }, + ast::BindPat(it) => { get_bind_pat_hints(&mut res, &sema, inlay_hint_opts, it); }, _ => (), } } @@ -49,8 +50,14 @@ pub(crate) fn inlay_hints( fn get_param_name_hints( acc: &mut Vec, sema: &Semantics, + inlay_hint_opts: &InlayHintOptions, expr: ast::Expr, ) -> Option<()> { + match inlay_hint_opts.display_type { + InlayHintDisplayType::Off | InlayHintDisplayType::TypeHints => return None, + _ => {} + } + let args = match &expr { ast::Expr::CallExpr(expr) => expr.arg_list()?.args(), ast::Expr::MethodCallExpr(expr) => expr.arg_list()?.args(), @@ -84,9 +91,14 @@ fn get_param_name_hints( fn get_bind_pat_hints( acc: &mut Vec, sema: &Semantics, - max_inlay_hint_length: Option, + inlay_hint_opts: &InlayHintOptions, pat: ast::BindPat, ) -> Option<()> { + match inlay_hint_opts.display_type { + InlayHintDisplayType::Off | InlayHintDisplayType::ParameterHints => return None, + _ => {} + } + let ty = sema.type_of_pat(&pat.clone().into())?; if should_not_display_type_hint(sema.db, &pat, &ty) { @@ -96,7 +108,7 @@ fn get_bind_pat_hints( acc.push(InlayHint { range: pat.syntax().text_range(), kind: InlayKind::TypeHint, - label: ty.display_truncated(sema.db, max_inlay_hint_length).to_string().into(), + label: ty.display_truncated(sema.db, inlay_hint_opts.max_length).to_string().into(), }); Some(()) } @@ -205,7 +217,62 @@ mod tests { use insta::assert_debug_snapshot; use crate::mock_analysis::single_file; + use ra_project_model::{InlayHintDisplayType, InlayHintOptions}; + #[test] + fn param_hints_only() { + let (analysis, file_id) = single_file( + r#" + fn foo(a: i32, b: i32) -> i32 { a + b } + fn main() { + let _x = foo(4, 4); + }"#, + ); + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::ParameterHints, max_length: None}).unwrap(), @r###" + [ + InlayHint { + range: [106; 107), + kind: ParameterHint, + label: "a", + }, + InlayHint { + range: [109; 110), + kind: ParameterHint, + label: "b", + }, + ]"###); + } + + #[test] + fn hints_disabled() { + let (analysis, file_id) = single_file( + r#" + fn foo(a: i32, b: i32) -> i32 { a + b } + fn main() { + let _x = foo(4, 4); + }"#, + ); + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::Off, max_length: None}).unwrap(), @r###"[]"###); + } + + #[test] + fn type_hints_only() { + let (analysis, file_id) = single_file( + r#" + fn foo(a: i32, b: i32) -> i32 { a + b } + fn main() { + let _x = foo(4, 4); + }"#, + ); + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::TypeHints, max_length: None}).unwrap(), @r###" + [ + InlayHint { + range: [97; 99), + kind: TypeHint, + label: "i32", + }, + ]"###); + } #[test] fn default_generic_types_should_not_be_displayed() { let (analysis, file_id) = single_file( @@ -221,7 +288,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [69; 71), @@ -278,7 +345,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [193; 197), @@ -358,7 +425,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -422,7 +489,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -472,7 +539,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [188; 192), @@ -567,7 +634,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [188; 192), @@ -662,7 +729,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [252; 256), @@ -734,7 +801,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###" [ InlayHint { range: [74; 75), @@ -822,7 +889,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, None).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" [ InlayHint { range: [798; 809), @@ -944,7 +1011,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###" [] "### ); @@ -970,7 +1037,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, Some(8)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###" [] "### ); diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 9f45003d33..8b1292a41e 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -44,6 +44,7 @@ mod marks; #[cfg(test)] mod test_utils; +use ra_project_model::InlayHintOptions; use std::sync::Arc; use ra_cfg::CfgOptions; @@ -318,9 +319,9 @@ impl Analysis { pub fn inlay_hints( &self, file_id: FileId, - max_inlay_hint_length: Option, + inlay_hint_opts: &InlayHintOptions, ) -> Cancelable> { - self.with_db(|db| inlay_hints::inlay_hints(db, file_id, max_inlay_hint_length)) + self.with_db(|db| inlay_hints::inlay_hints(db, file_id, inlay_hint_opts)) } /// Returns the set of folding ranges. diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 37845ca56f..a5012c0efd 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -16,6 +16,7 @@ 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::{ @@ -24,6 +25,34 @@ 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 InlayHintOptions { + pub fn new(max_length: Option) -> Self { + Self { display_type: InlayHintDisplayType::Full, max_length } + } +} + +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 3314269ece..1617eab0b9 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -7,6 +7,7 @@ //! configure the server itself, feature flags are passed into analysis, and //! tweak things like automatic insertion of `()` in completions. +use ra_project_model::InlayHintOptions; use rustc_hash::FxHashMap; use ra_project_model::CargoFeatures; @@ -30,7 +31,7 @@ pub struct ServerConfig { pub lru_capacity: Option, - pub max_inlay_hint_length: Option, + pub inlay_hint_opts: InlayHintOptions, pub cargo_watch_enable: bool, pub cargo_watch_args: Vec, @@ -57,7 +58,7 @@ impl Default for ServerConfig { exclude_globs: Vec::new(), use_client_watching: false, lru_capacity: None, - max_inlay_hint_length: None, + inlay_hint_opts: 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 f9de712a00..91fb66abbe 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), - max_inlay_hint_length: config.max_inlay_hint_length, + inlay_hint_opts: config.inlay_hint_opts.clone(), cargo_watch: CheckOptions { enable: config.cargo_watch_enable, args: config.cargo_watch_args, diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index 1ddc3c1a5e..8043a6cd3b 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -15,7 +15,7 @@ use ra_cargo_watch::{url_from_path_with_drive_lowercasing, CheckOptions, CheckWa use ra_ide::{ Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, }; -use ra_project_model::{get_rustc_cfg_options, ProjectWorkspace}; +use ra_project_model::{get_rustc_cfg_options, InlayHintOptions, ProjectWorkspace}; use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; use relative_path::RelativePathBuf; @@ -32,7 +32,7 @@ pub struct Options { pub publish_decorations: bool, pub supports_location_link: bool, pub line_folding_only: bool, - pub max_inlay_hint_length: Option, + pub inlay_hint_opts: InlayHintOptions, pub rustfmt_args: Vec, pub cargo_watch: CheckOptions, } diff --git a/docs/user/features.md b/docs/user/features.md index ba4d50fa80..06bc7ded55 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -191,8 +191,8 @@ Two types of inlay hints are displayed currently: In VS Code, the following settings can be used to configure the inlay hints: -* `rust-analyzer.displayInlayHints` — toggles inlay hints display on or off -* `rust-analyzer.maxInlayHintLength` — shortens the hints if their length exceeds the value specified. If no value is specified (`null`), no shortening is applied. +* `rust-analyzer.inlayHintOpts.displayType` configure which types of inlay hints are shown. +* `rust-analyzer.inlayHintOpts.maxLength` — shortens the hints if their length exceeds the value specified. If no value is specified (`null`), no shortening is applied. **Note:** VS Code does not have native support for inlay hints [yet](https://github.com/microsoft/vscode/issues/16221) and the hints are implemented using decorations. This approach has limitations, the caret movement and bracket highlighting near the edges of the hint may be weird: diff --git a/editors/code/package.json b/editors/code/package.json index 5128854545..6f2275062c 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -307,12 +307,24 @@ "exclusiveMinimum": true, "description": "Number of syntax trees rust-analyzer keeps in memory" }, - "rust-analyzer.displayInlayHints": { - "type": "boolean", - "default": true, + "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.maxInlayHintLength": { + "rust-analyzer.inlayHintOpts.maxLength": { "type": [ "null", "integer" diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index 540f7c9ea7..ac4417c618 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, - maxInlayHintLength: config.maxInlayHintLength, + inlayHintOpts: config.inlayHintOpts, cargoWatchEnable: cargoWatchOpts.enable, cargoWatchArgs: cargoWatchOpts.arguments, cargoWatchCommand: cargoWatchOpts.command, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index b72206d3cb..5acce0752e 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -5,6 +5,11 @@ import { log } from "./util"; const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; +export interface InlayHintOptions { + displayType: string; + maxLength: number; +} + export interface CargoWatchOptions { enable: boolean; arguments: string[]; @@ -149,8 +154,12 @@ 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 displayInlayHints() { return this.cfg.get("displayInlayHints") as boolean; } - get maxInlayHintLength() { return this.cfg.get("maxInlayHintLength") as number; } + get inlayHintOpts(): InlayHintOptions { + return { + displayType: this.cfg.get("inlayHintOpts.displayType") as string, + maxLength: this.cfg.get("inlayHintOpts.maxLength") as number, + }; + } get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; } get useClientWatching() { return this.cfg.get("useClientWatching") as boolean; } get featureFlags() { return this.cfg.get("featureFlags") as Record; } diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index e1a82e03e8..8d291406d4 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.displayInlayHints) { + if (ctx.config.inlayHintOpts.displayType === 'off') { return this.dispose(); } if (!this.updater) this.updater = new HintsUpdater(ctx); From cfb48df149bfa8a15b113b1a252598457a4ea392 Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Tue, 10 Mar 2020 11:21:56 -0700 Subject: [PATCH 2/5] Address Issues from Github - Updated naming of config - Define struct in ra_ide and use remote derive in rust-analyzer/config - Make inlayConfig type more flexible to support more future types - Remove constructor only used in tests --- Cargo.lock | 1 - crates/ra_ide/Cargo.toml | 1 - crates/ra_ide/src/inlay_hints.rs | 61 +++++++++++-------- crates/ra_ide/src/lib.rs | 5 +- crates/ra_project_model/src/lib.rs | 6 -- crates/rust-analyzer/src/config.rs | 33 +++++++++- crates/rust-analyzer/src/main_loop.rs | 2 +- .../rust-analyzer/src/main_loop/handlers.rs | 2 +- crates/rust-analyzer/src/world.rs | 7 ++- editors/code/src/config.ts | 4 +- 10 files changed, 77 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6173741b1a..330bdd1cba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1028,7 +1028,6 @@ dependencies = [ "ra_hir", "ra_ide_db", "ra_prof", - "ra_project_model", "ra_syntax", "ra_text_edit", "rand", diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index 486832529c..7235c944c9 100644 --- a/crates/ra_ide/Cargo.toml +++ b/crates/ra_ide/Cargo.toml @@ -21,7 +21,6 @@ rustc-hash = "1.1.0" rand = { version = "0.7.3", features = ["small_rng"] } ra_syntax = { path = "../ra_syntax" } -ra_project_model = { path = "../ra_project_model" } ra_text_edit = { path = "../ra_text_edit" } ra_db = { path = "../ra_db" } ra_ide_db = { path = "../ra_ide_db" } diff --git a/crates/ra_ide/src/inlay_hints.rs b/crates/ra_ide/src/inlay_hints.rs index 0f1c13c149..8454a975b6 100644 --- a/crates/ra_ide/src/inlay_hints.rs +++ b/crates/ra_ide/src/inlay_hints.rs @@ -3,7 +3,6 @@ use hir::{Adt, HirDisplay, Semantics, Type}; use ra_ide_db::RootDatabase; use ra_prof::profile; -use ra_project_model::{InlayHintDisplayType, InlayHintOptions}; use ra_syntax::{ ast::{self, ArgListOwner, AstNode, TypeAscriptionOwner}, match_ast, SmolStr, TextRange, @@ -11,7 +10,19 @@ use ra_syntax::{ use crate::{FileId, FunctionSignature}; -#[derive(Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct InlayConfig { + pub display_type: Vec, + pub max_length: Option, +} + +impl Default for InlayConfig { + fn default() -> Self { + Self { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: None } + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] pub enum InlayKind { TypeHint, ParameterHint, @@ -27,7 +38,7 @@ pub struct InlayHint { pub(crate) fn inlay_hints( db: &RootDatabase, file_id: FileId, - inlay_hint_opts: &InlayHintOptions, + inlay_hint_opts: &InlayConfig, ) -> Vec { let _p = profile("inlay_hints"); let sema = Semantics::new(db); @@ -50,12 +61,11 @@ pub(crate) fn inlay_hints( fn get_param_name_hints( acc: &mut Vec, sema: &Semantics, - inlay_hint_opts: &InlayHintOptions, + inlay_hint_opts: &InlayConfig, expr: ast::Expr, ) -> Option<()> { - match inlay_hint_opts.display_type { - InlayHintDisplayType::Off | InlayHintDisplayType::TypeHints => return None, - _ => {} + if !inlay_hint_opts.display_type.contains(&InlayKind::ParameterHint) { + return None; } let args = match &expr { @@ -91,12 +101,11 @@ fn get_param_name_hints( fn get_bind_pat_hints( acc: &mut Vec, sema: &Semantics, - inlay_hint_opts: &InlayHintOptions, + inlay_hint_opts: &InlayConfig, pat: ast::BindPat, ) -> Option<()> { - match inlay_hint_opts.display_type { - InlayHintDisplayType::Off | InlayHintDisplayType::ParameterHints => return None, - _ => {} + if !inlay_hint_opts.display_type.contains(&InlayKind::TypeHint) { + return None; } let ty = sema.type_of_pat(&pat.clone().into())?; @@ -214,10 +223,10 @@ fn get_fn_signature(sema: &Semantics, expr: &ast::Expr) -> Option< #[cfg(test)] mod tests { + use crate::inlay_hints::{InlayConfig, InlayKind}; use insta::assert_debug_snapshot; use crate::mock_analysis::single_file; - use ra_project_model::{InlayHintDisplayType, InlayHintOptions}; #[test] fn param_hints_only() { @@ -228,7 +237,7 @@ mod tests { let _x = foo(4, 4); }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::ParameterHints, max_length: None}).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![InlayKind::ParameterHint], max_length: None}).unwrap(), @r###" [ InlayHint { range: [106; 107), @@ -252,7 +261,7 @@ mod tests { let _x = foo(4, 4); }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::Off, max_length: None}).unwrap(), @r###"[]"###); + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![], max_length: None}).unwrap(), @r###"[]"###); } #[test] @@ -264,7 +273,7 @@ mod tests { let _x = foo(4, 4); }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions{ display_type: InlayHintDisplayType::TypeHints, max_length: None}).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![InlayKind::TypeHint], max_length: None}).unwrap(), @r###" [ InlayHint { range: [97; 99), @@ -288,7 +297,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [69; 71), @@ -345,7 +354,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [193; 197), @@ -425,7 +434,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -489,7 +498,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [21; 30), @@ -539,7 +548,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [188; 192), @@ -634,7 +643,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [188; 192), @@ -729,7 +738,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [252; 256), @@ -801,7 +810,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###" [ InlayHint { range: [74; 75), @@ -889,7 +898,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(None)).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig::default()).unwrap(), @r###" [ InlayHint { range: [798; 809), @@ -1011,7 +1020,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###" [] "### ); @@ -1037,7 +1046,7 @@ fn main() { }"#, ); - assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayHintOptions::new(Some(8))).unwrap(), @r###" + assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###" [] "### ); diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 8b1292a41e..a3acfd2253 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -44,7 +44,6 @@ mod marks; #[cfg(test)] mod test_utils; -use ra_project_model::InlayHintOptions; use std::sync::Arc; use ra_cfg::CfgOptions; @@ -69,7 +68,7 @@ pub use crate::{ expand_macro::ExpandedMacro, folding_ranges::{Fold, FoldKind}, hover::HoverResult, - inlay_hints::{InlayHint, InlayKind}, + inlay_hints::{InlayConfig, InlayHint, InlayKind}, references::{Declaration, Reference, ReferenceAccess, ReferenceKind, ReferenceSearchResult}, runnables::{Runnable, RunnableKind, TestId}, source_change::{FileSystemEdit, SourceChange, SourceFileEdit}, @@ -319,7 +318,7 @@ impl Analysis { pub fn inlay_hints( &self, file_id: FileId, - inlay_hint_opts: &InlayHintOptions, + inlay_hint_opts: &InlayConfig, ) -> Cancelable> { self.with_db(|db| inlay_hints::inlay_hints(db, file_id, inlay_hint_opts)) } diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index a5012c0efd..036c8719fc 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -41,12 +41,6 @@ pub struct InlayHintOptions { pub max_length: Option, } -impl InlayHintOptions { - pub fn new(max_length: Option) -> Self { - Self { display_type: InlayHintDisplayType::Full, max_length } - } -} - impl Default for InlayHintOptions { fn default() -> Self { Self { display_type: InlayHintDisplayType::Full, max_length: None } diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 1617eab0b9..2f407723b9 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -7,12 +7,40 @@ //! configure the server itself, feature flags are passed into analysis, and //! tweak things like automatic insertion of `()` in completions. -use ra_project_model::InlayHintOptions; +use ra_ide::{InlayConfig, InlayKind}; 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)] @@ -31,7 +59,8 @@ pub struct ServerConfig { pub lru_capacity: Option, - pub inlay_hint_opts: InlayHintOptions, + #[serde(with = "InlayConfigDef")] + pub inlay_hint_opts: InlayConfig, pub cargo_watch_enable: bool, pub cargo_watch_args: Vec, diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 91fb66abbe..729c384ace 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.clone(), + inlay_hint_opts: config.inlay_hint_opts, 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 fcb40432d4..4f58bcb0a1 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.max_inlay_hint_length)? + .inlay_hints(file_id, &world.options.inlay_hint_opts)? .into_iter() .map(|api_type| InlayHint { label: api_type.label.to_string(), diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs index 8043a6cd3b..6eb1ea7d27 100644 --- a/crates/rust-analyzer/src/world.rs +++ b/crates/rust-analyzer/src/world.rs @@ -13,9 +13,10 @@ use lsp_types::Url; use parking_lot::RwLock; use ra_cargo_watch::{url_from_path_with_drive_lowercasing, CheckOptions, CheckWatcher}; use ra_ide::{ - Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, + Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, InlayConfig, LibraryData, + SourceRootId, }; -use ra_project_model::{get_rustc_cfg_options, InlayHintOptions, ProjectWorkspace}; +use ra_project_model::{get_rustc_cfg_options, ProjectWorkspace}; use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; use relative_path::RelativePathBuf; @@ -32,7 +33,7 @@ pub struct Options { pub publish_decorations: bool, pub supports_location_link: bool, pub line_folding_only: bool, - pub inlay_hint_opts: InlayHintOptions, + pub inlay_hint_opts: InlayConfig, pub rustfmt_args: Vec, pub cargo_watch: CheckOptions, } diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 5acce0752e..b26bf10dae 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -27,7 +27,9 @@ export class Config { private static readonly requiresReloadOpts = [ "cargoFeatures", "cargo-watch", - "highlighting.semanticTokens" + "highlighting.semanticTokens", + "inlayHintOpts.maxLength", + "inlayHintOpts.displayType", ] .map(opt => `${Config.rootSection}.${opt}`); From 974ed7155acccb5da0c2aeac09d7052c4f75902d Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Tue, 10 Mar 2020 18:59:49 -0700 Subject: [PATCH 3/5] Deduplicate some Inlay definitions - Remove match conversion for InlayKind since we're using remote --- crates/rust-analyzer/src/config.rs | 31 ++----------------- .../rust-analyzer/src/main_loop/handlers.rs | 7 ++--- crates/rust-analyzer/src/req.rs | 29 +++++++++++++++-- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 2f407723b9..22ff0845ca 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 4f58bcb0a1..23463b28e6 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 a3efe3b9fe..dab05405e0 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, } From 58248e24cd45adcbfd7bfd00e1487df196b4a8c6 Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Wed, 11 Mar 2020 20:14:39 -0700 Subject: [PATCH 4/5] 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 +- .../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 8454a975b6..59922e14c4 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 036c8719fc..37845ca56f 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 22ff0845ca..3a6cfbe7b8 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 729c384ace..b6d1f28d8d 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 23463b28e6..921990da03 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 dab05405e0..1dcab2703e 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 6eb1ea7d27..004803b00e 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 6f2275062c..296d6fe8e5 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 ac4417c618..3b8ea6f77e 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 b26bf10dae..2668c9640b 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 8d291406d4..b19b09ad56 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); From a153b9087520012b5f815b4df6c3657d490b30c8 Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Thu, 12 Mar 2020 08:43:07 -0700 Subject: [PATCH 5/5] Make maxLength nullable again --- editors/code/package.json | 5 ++++- editors/code/src/config.ts | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/editors/code/package.json b/editors/code/package.json index 296d6fe8e5..78f3539e98 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -318,7 +318,10 @@ "description": "Whether to show function parameter name inlay hints at the call site" }, "rust-analyzer.inlayHints.maxLength": { - "type": "integer", + "type": [ + "null", + "integer" + ], "default": 20, "minimum": 0, "exclusiveMinimum": true, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 2668c9640b..c3b3ecabfb 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -8,7 +8,7 @@ const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; export interface InlayHintOptions { typeHints: boolean; parameterHints: boolean; - maxLength: number; + maxLength: number | null; } export interface CargoWatchOptions { @@ -160,7 +160,7 @@ export class Config { return { typeHints: this.cfg.get("inlayHints.typeHints") as boolean, parameterHints: this.cfg.get("inlayHints.parameterHints") as boolean, - maxLength: this.cfg.get("inlayHints.maxLength") as number, + maxLength: this.cfg.get("inlayHints.maxLength") as null | number, }; } get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }