Switch from Vec<InlayKind> 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
This commit is contained in:
Steffen Lyngbaek 2020-03-11 20:14:39 -07:00
parent 974ed7155a
commit 58248e24cd
11 changed files with 41 additions and 85 deletions

View file

@ -12,13 +12,14 @@ use crate::{FileId, FunctionSignature};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InlayConfig {
pub display_type: Vec<InlayKind>,
pub type_hints: bool,
pub parameter_hints: bool,
pub max_length: Option<usize>,
}
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<RootDatabase>, 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###"
[]
"###
);

View file

@ -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<usize>,
}
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,

View file

@ -33,7 +33,7 @@ pub struct ServerConfig {
pub lru_capacity: Option<usize>,
#[serde(with = "InlayConfigDef")]
pub inlay_hint_opts: InlayConfig,
pub inlay_hints: InlayConfig,
pub cargo_watch_enable: bool,
pub cargo_watch_args: Vec<String>,
@ -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(),

View file

@ -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,

View file

@ -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(),

View file

@ -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<Vec<InlayKind>, 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<InlayKind>,
pub type_hints: bool,
pub parameter_hints: bool,
pub max_length: Option<usize>,
}

View file

@ -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<String>,
pub cargo_watch: CheckOptions,
}

View file

@ -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,

View file

@ -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,

View file

@ -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[]; }

View file

@ -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);