Skip to content

Commit 58248e2

Browse files
committed
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
1 parent 974ed71 commit 58248e2

File tree

11 files changed

+41
-85
lines changed

11 files changed

+41
-85
lines changed

crates/ra_ide/src/inlay_hints.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ use crate::{FileId, FunctionSignature};
1212

1313
#[derive(Clone, Debug, PartialEq, Eq)]
1414
pub struct InlayConfig {
15-
pub display_type: Vec<InlayKind>,
15+
pub type_hints: bool,
16+
pub parameter_hints: bool,
1617
pub max_length: Option<usize>,
1718
}
1819

1920
impl Default for InlayConfig {
2021
fn default() -> Self {
21-
Self { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: None }
22+
Self { type_hints: true, parameter_hints: true, max_length: None }
2223
}
2324
}
2425

@@ -64,7 +65,7 @@ fn get_param_name_hints(
6465
inlay_hint_opts: &InlayConfig,
6566
expr: ast::Expr,
6667
) -> Option<()> {
67-
if !inlay_hint_opts.display_type.contains(&InlayKind::ParameterHint) {
68+
if !inlay_hint_opts.parameter_hints {
6869
return None;
6970
}
7071

@@ -104,7 +105,7 @@ fn get_bind_pat_hints(
104105
inlay_hint_opts: &InlayConfig,
105106
pat: ast::BindPat,
106107
) -> Option<()> {
107-
if !inlay_hint_opts.display_type.contains(&InlayKind::TypeHint) {
108+
if !inlay_hint_opts.type_hints {
108109
return None;
109110
}
110111

@@ -223,7 +224,7 @@ fn get_fn_signature(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<
223224

224225
#[cfg(test)]
225226
mod tests {
226-
use crate::inlay_hints::{InlayConfig, InlayKind};
227+
use crate::inlay_hints::InlayConfig;
227228
use insta::assert_debug_snapshot;
228229

229230
use crate::mock_analysis::single_file;
@@ -237,7 +238,7 @@ mod tests {
237238
let _x = foo(4, 4);
238239
}"#,
239240
);
240-
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![InlayKind::ParameterHint], max_length: None}).unwrap(), @r###"
241+
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ parameter_hints: true, type_hints: false, max_length: None}).unwrap(), @r###"
241242
[
242243
InlayHint {
243244
range: [106; 107),
@@ -261,7 +262,7 @@ mod tests {
261262
let _x = foo(4, 4);
262263
}"#,
263264
);
264-
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![], max_length: None}).unwrap(), @r###"[]"###);
265+
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ type_hints: false, parameter_hints: false, max_length: None}).unwrap(), @r###"[]"###);
265266
}
266267

267268
#[test]
@@ -273,7 +274,7 @@ mod tests {
273274
let _x = foo(4, 4);
274275
}"#,
275276
);
276-
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ display_type: vec![InlayKind::TypeHint], max_length: None}).unwrap(), @r###"
277+
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig{ type_hints: true, parameter_hints: false, max_length: None}).unwrap(), @r###"
277278
[
278279
InlayHint {
279280
range: [97; 99),
@@ -810,7 +811,7 @@ fn main() {
810811
}"#,
811812
);
812813

813-
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###"
814+
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
814815
[
815816
InlayHint {
816817
range: [74; 75),
@@ -1020,7 +1021,7 @@ fn main() {
10201021
}"#,
10211022
);
10221023

1023-
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###"
1024+
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
10241025
[]
10251026
"###
10261027
);
@@ -1046,7 +1047,7 @@ fn main() {
10461047
}"#,
10471048
);
10481049

1049-
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { display_type: vec![InlayKind::TypeHint, InlayKind::ParameterHint], max_length: Some(8) }).unwrap(), @r###"
1050+
assert_debug_snapshot!(analysis.inlay_hints(file_id, &InlayConfig { max_length: Some(8), ..Default::default() }).unwrap(), @r###"
10501051
[]
10511052
"###
10521053
);

crates/ra_project_model/src/lib.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use anyhow::{bail, Context, Result};
1616
use ra_cfg::CfgOptions;
1717
use ra_db::{CrateGraph, CrateName, Edition, Env, FileId};
1818
use rustc_hash::FxHashMap;
19-
use serde::Deserialize;
2019
use serde_json::from_reader;
2120

2221
pub use crate::{
@@ -25,28 +24,6 @@ pub use crate::{
2524
sysroot::Sysroot,
2625
};
2726

28-
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
29-
#[serde(rename_all = "lowercase")]
30-
pub enum InlayHintDisplayType {
31-
Off,
32-
TypeHints,
33-
ParameterHints,
34-
Full,
35-
}
36-
37-
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
38-
#[serde(rename_all = "camelCase", default)]
39-
pub struct InlayHintOptions {
40-
pub display_type: InlayHintDisplayType,
41-
pub max_length: Option<usize>,
42-
}
43-
44-
impl Default for InlayHintOptions {
45-
fn default() -> Self {
46-
Self { display_type: InlayHintDisplayType::Full, max_length: None }
47-
}
48-
}
49-
5027
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
5128
pub struct CargoTomlNotFoundError {
5229
pub searched_at: PathBuf,

crates/rust-analyzer/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct ServerConfig {
3333
pub lru_capacity: Option<usize>,
3434

3535
#[serde(with = "InlayConfigDef")]
36-
pub inlay_hint_opts: InlayConfig,
36+
pub inlay_hints: InlayConfig,
3737

3838
pub cargo_watch_enable: bool,
3939
pub cargo_watch_args: Vec<String>,
@@ -60,7 +60,7 @@ impl Default for ServerConfig {
6060
exclude_globs: Vec::new(),
6161
use_client_watching: false,
6262
lru_capacity: None,
63-
inlay_hint_opts: Default::default(),
63+
inlay_hints: Default::default(),
6464
cargo_watch_enable: true,
6565
cargo_watch_args: Vec::new(),
6666
cargo_watch_command: "check".to_string(),

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub fn main_loop(
177177
.and_then(|it| it.folding_range.as_ref())
178178
.and_then(|it| it.line_folding_only)
179179
.unwrap_or(false),
180-
inlay_hint_opts: config.inlay_hint_opts,
180+
inlay_hints: config.inlay_hints,
181181
cargo_watch: CheckOptions {
182182
enable: config.cargo_watch_enable,
183183
args: config.cargo_watch_args,

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ pub fn handle_inlay_hints(
997997
let analysis = world.analysis();
998998
let line_index = analysis.file_line_index(file_id)?;
999999
Ok(analysis
1000-
.inlay_hints(file_id, &world.options.inlay_hint_opts)?
1000+
.inlay_hints(file_id, &world.options.inlay_hints)?
10011001
.into_iter()
10021002
.map(|api_type| InlayHint {
10031003
label: api_type.label.to_string(),

crates/rust-analyzer/src/req.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url};
44
use rustc_hash::FxHashMap;
5-
use serde::{Deserialize, Deserializer, Serialize};
5+
use serde::{Deserialize, Serialize};
66

77
use ra_ide::{InlayConfig, InlayKind};
88

@@ -204,24 +204,11 @@ pub enum InlayKindDef {
204204
ParameterHint,
205205
}
206206

207-
// Work-around until better serde support is added
208-
// https://github.com/serde-rs/serde/issues/723#issuecomment-382501277
209-
fn vec_inlay_kind<'de, D>(deserializer: D) -> Result<Vec<InlayKind>, D::Error>
210-
where
211-
D: Deserializer<'de>,
212-
{
213-
#[derive(Deserialize)]
214-
struct Wrapper(#[serde(with = "InlayKindDef")] InlayKind);
215-
216-
let v = Vec::deserialize(deserializer)?;
217-
Ok(v.into_iter().map(|Wrapper(a)| a).collect())
218-
}
219-
220207
#[derive(Deserialize)]
221-
#[serde(remote = "InlayConfig")]
208+
#[serde(remote = "InlayConfig", rename_all = "camelCase")]
222209
pub struct InlayConfigDef {
223-
#[serde(deserialize_with = "vec_inlay_kind")]
224-
pub display_type: Vec<InlayKind>,
210+
pub type_hints: bool,
211+
pub parameter_hints: bool,
225212
pub max_length: Option<usize>,
226213
}
227214

crates/rust-analyzer/src/world.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct Options {
3333
pub publish_decorations: bool,
3434
pub supports_location_link: bool,
3535
pub line_folding_only: bool,
36-
pub inlay_hint_opts: InlayConfig,
36+
pub inlay_hints: InlayConfig,
3737
pub rustfmt_args: Vec<String>,
3838
pub cargo_watch: CheckOptions,
3939
}

editors/code/package.json

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -307,28 +307,18 @@
307307
"exclusiveMinimum": true,
308308
"description": "Number of syntax trees rust-analyzer keeps in memory"
309309
},
310-
"rust-analyzer.inlayHintOpts.displayType": {
311-
"type": "string",
312-
"enum": [
313-
"off",
314-
"typeHints",
315-
"parameterHints",
316-
"full"
317-
],
318-
"enumDescriptions": [
319-
"No type inlay hints",
320-
"Type inlays hints only",
321-
"Parameter inlays hints only",
322-
"All inlay hints types"
323-
],
324-
"default": "full",
325-
"description": "Display additional type and parameter information in the editor"
310+
"rust-analyzer.inlayHints.typeHints": {
311+
"type": "boolean",
312+
"default": true,
313+
"description": "Whether to show inlay type hints"
326314
},
327-
"rust-analyzer.inlayHintOpts.maxLength": {
328-
"type": [
329-
"null",
330-
"integer"
331-
],
315+
"rust-analyzer.inlayHints.parameterHints": {
316+
"type": "boolean",
317+
"default": true,
318+
"description": "Whether to show function parameter name inlay hints at the call site"
319+
},
320+
"rust-analyzer.inlayHints.maxLength": {
321+
"type": "integer",
332322
"default": 20,
333323
"minimum": 0,
334324
"exclusiveMinimum": true,

editors/code/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function createClient(config: Config, serverPath: string): Promise<
2929
initializationOptions: {
3030
publishDecorations: !config.highlightingSemanticTokens,
3131
lruCapacity: config.lruCapacity,
32-
inlayHintOpts: config.inlayHintOpts,
32+
inlayHints: config.inlayHints,
3333
cargoWatchEnable: cargoWatchOpts.enable,
3434
cargoWatchArgs: cargoWatchOpts.arguments,
3535
cargoWatchCommand: cargoWatchOpts.command,

editors/code/src/config.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { log } from "./util";
66
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
77

88
export interface InlayHintOptions {
9-
displayType: string;
9+
typeHints: boolean;
10+
parameterHints: boolean;
1011
maxLength: number;
1112
}
1213

@@ -28,8 +29,7 @@ export class Config {
2829
"cargoFeatures",
2930
"cargo-watch",
3031
"highlighting.semanticTokens",
31-
"inlayHintOpts.maxLength",
32-
"inlayHintOpts.displayType",
32+
"inlayHints",
3333
]
3434
.map(opt => `${Config.rootSection}.${opt}`);
3535

@@ -156,10 +156,11 @@ export class Config {
156156
get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; }
157157
get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; }
158158
get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; }
159-
get inlayHintOpts(): InlayHintOptions {
159+
get inlayHints(): InlayHintOptions {
160160
return {
161-
displayType: this.cfg.get("inlayHintOpts.displayType") as string,
162-
maxLength: this.cfg.get("inlayHintOpts.maxLength") as number,
161+
typeHints: this.cfg.get("inlayHints.typeHints") as boolean,
162+
parameterHints: this.cfg.get("inlayHints.parameterHints") as boolean,
163+
maxLength: this.cfg.get("inlayHints.maxLength") as number,
163164
};
164165
}
165166
get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }

0 commit comments

Comments
 (0)