aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteffen Lyngbaek <[email protected]>2020-03-12 03:14:39 +0000
committerSteffen Lyngbaek <[email protected]>2020-03-12 03:14:39 +0000
commit58248e24cd45adcbfd7bfd00e1487df196b4a8c6 (patch)
tree7221dea8daa9dcede6ffaede88c890de17586c51
parent974ed7155acccb5da0c2aeac09d7052c4f75902d (diff)
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
-rw-r--r--crates/ra_ide/src/inlay_hints.rs23
-rw-r--r--crates/ra_project_model/src/lib.rs23
-rw-r--r--crates/rust-analyzer/src/config.rs4
-rw-r--r--crates/rust-analyzer/src/main_loop.rs2
-rw-r--r--crates/rust-analyzer/src/main_loop/handlers.rs2
-rw-r--r--crates/rust-analyzer/src/req.rs21
-rw-r--r--crates/rust-analyzer/src/world.rs2
-rw-r--r--editors/code/package.json32
-rw-r--r--editors/code/src/client.ts2
-rw-r--r--editors/code/src/config.ts13
-rw-r--r--editors/code/src/inlay_hints.ts2
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};
12 12
13#[derive(Clone, Debug, PartialEq, Eq)] 13#[derive(Clone, Debug, PartialEq, Eq)]
14pub struct InlayConfig { 14pub struct InlayConfig {
15 pub display_type: Vec<InlayKind>, 15 pub type_hints: bool,
16 pub parameter_hints: bool,
16 pub max_length: Option<usize>, 17 pub max_length: Option<usize>,
17} 18}
18 19
19impl Default for InlayConfig { 20impl Default for InlayConfig {
20 fn default() -> Self { 21 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 }
22 } 23 }
23} 24}
24 25
@@ -64,7 +65,7 @@ fn get_param_name_hints(
64 inlay_hint_opts: &InlayConfig, 65 inlay_hint_opts: &InlayConfig,
65 expr: ast::Expr, 66 expr: ast::Expr,
66) -> Option<()> { 67) -> Option<()> {
67 if !inlay_hint_opts.display_type.contains(&InlayKind::ParameterHint) { 68 if !inlay_hint_opts.parameter_hints {
68 return None; 69 return None;
69 } 70 }
70 71
@@ -104,7 +105,7 @@ fn get_bind_pat_hints(
104 inlay_hint_opts: &InlayConfig, 105 inlay_hint_opts: &InlayConfig,
105 pat: ast::BindPat, 106 pat: ast::BindPat,
106) -> Option<()> { 107) -> Option<()> {
107 if !inlay_hint_opts.display_type.contains(&InlayKind::TypeHint) { 108 if !inlay_hint_opts.type_hints {
108 return None; 109 return None;
109 } 110 }
110 111
@@ -223,7 +224,7 @@ fn get_fn_signature(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<
223 224
224#[cfg(test)] 225#[cfg(test)]
225mod tests { 226mod tests {
226 use crate::inlay_hints::{InlayConfig, InlayKind}; 227 use crate::inlay_hints::InlayConfig;
227 use insta::assert_debug_snapshot; 228 use insta::assert_debug_snapshot;
228 229
229 use crate::mock_analysis::single_file; 230 use crate::mock_analysis::single_file;
@@ -237,7 +238,7 @@ mod tests {
237 let _x = foo(4, 4); 238 let _x = foo(4, 4);
238 }"#, 239 }"#,
239 ); 240 );
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###"
241 [ 242 [
242 InlayHint { 243 InlayHint {
243 range: [106; 107), 244 range: [106; 107),
@@ -261,7 +262,7 @@ mod tests {
261 let _x = foo(4, 4); 262 let _x = foo(4, 4);
262 }"#, 263 }"#,
263 ); 264 );
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###"[]"###);
265 } 266 }
266 267
267 #[test] 268 #[test]
@@ -273,7 +274,7 @@ mod tests {
273 let _x = foo(4, 4); 274 let _x = foo(4, 4);
274 }"#, 275 }"#,
275 ); 276 );
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###"
277 [ 278 [
278 InlayHint { 279 InlayHint {
279 range: [97; 99), 280 range: [97; 99),
@@ -810,7 +811,7 @@ fn main() {
810}"#, 811}"#,
811 ); 812 );
812 813
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###"
814 [ 815 [
815 InlayHint { 816 InlayHint {
816 range: [74; 75), 817 range: [74; 75),
@@ -1020,7 +1021,7 @@ fn main() {
1020}"#, 1021}"#,
1021 ); 1022 );
1022 1023
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###"
1024 [] 1025 []
1025 "### 1026 "###
1026 ); 1027 );
@@ -1046,7 +1047,7 @@ fn main() {
1046}"#, 1047}"#,
1047 ); 1048 );
1048 1049
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###"
1050 [] 1051 []
1051 "### 1052 "###
1052 ); 1053 );
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};
16use ra_cfg::CfgOptions; 16use ra_cfg::CfgOptions;
17use ra_db::{CrateGraph, CrateName, Edition, Env, FileId}; 17use ra_db::{CrateGraph, CrateName, Edition, Env, FileId};
18use rustc_hash::FxHashMap; 18use rustc_hash::FxHashMap;
19use serde::Deserialize;
20use serde_json::from_reader; 19use serde_json::from_reader;
21 20
22pub use crate::{ 21pub use crate::{
@@ -25,28 +24,6 @@ pub use crate::{
25 sysroot::Sysroot, 24 sysroot::Sysroot,
26}; 25};
27 26
28#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
29#[serde(rename_all = "lowercase")]
30pub 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)]
39pub struct InlayHintOptions {
40 pub display_type: InlayHintDisplayType,
41 pub max_length: Option<usize>,
42}
43
44impl Default for InlayHintOptions {
45 fn default() -> Self {
46 Self { display_type: InlayHintDisplayType::Full, max_length: None }
47 }
48}
49
50#[derive(Clone, PartialEq, Eq, Hash, Debug)] 27#[derive(Clone, PartialEq, Eq, Hash, Debug)]
51pub struct CargoTomlNotFoundError { 28pub struct CargoTomlNotFoundError {
52 pub searched_at: PathBuf, 29 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 {
33 pub lru_capacity: Option<usize>, 33 pub lru_capacity: Option<usize>,
34 34
35 #[serde(with = "InlayConfigDef")] 35 #[serde(with = "InlayConfigDef")]
36 pub inlay_hint_opts: InlayConfig, 36 pub inlay_hints: InlayConfig,
37 37
38 pub cargo_watch_enable: bool, 38 pub cargo_watch_enable: bool,
39 pub cargo_watch_args: Vec<String>, 39 pub cargo_watch_args: Vec<String>,
@@ -60,7 +60,7 @@ impl Default for ServerConfig {
60 exclude_globs: Vec::new(), 60 exclude_globs: Vec::new(),
61 use_client_watching: false, 61 use_client_watching: false,
62 lru_capacity: None, 62 lru_capacity: None,
63 inlay_hint_opts: Default::default(), 63 inlay_hints: Default::default(),
64 cargo_watch_enable: true, 64 cargo_watch_enable: true,
65 cargo_watch_args: Vec::new(), 65 cargo_watch_args: Vec::new(),
66 cargo_watch_command: "check".to_string(), 66 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(
177 .and_then(|it| it.folding_range.as_ref()) 177 .and_then(|it| it.folding_range.as_ref())
178 .and_then(|it| it.line_folding_only) 178 .and_then(|it| it.line_folding_only)
179 .unwrap_or(false), 179 .unwrap_or(false),
180 inlay_hint_opts: config.inlay_hint_opts, 180 inlay_hints: config.inlay_hints,
181 cargo_watch: CheckOptions { 181 cargo_watch: CheckOptions {
182 enable: config.cargo_watch_enable, 182 enable: config.cargo_watch_enable,
183 args: config.cargo_watch_args, 183 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(
997 let analysis = world.analysis(); 997 let analysis = world.analysis();
998 let line_index = analysis.file_line_index(file_id)?; 998 let line_index = analysis.file_line_index(file_id)?;
999 Ok(analysis 999 Ok(analysis
1000 .inlay_hints(file_id, &world.options.inlay_hint_opts)? 1000 .inlay_hints(file_id, &world.options.inlay_hints)?
1001 .into_iter() 1001 .into_iter()
1002 .map(|api_type| InlayHint { 1002 .map(|api_type| InlayHint {
1003 label: api_type.label.to_string(), 1003 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 @@
2 2
3use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url}; 3use lsp_types::{Location, Position, Range, TextDocumentIdentifier, Url};
4use rustc_hash::FxHashMap; 4use rustc_hash::FxHashMap;
5use serde::{Deserialize, Deserializer, Serialize}; 5use serde::{Deserialize, Serialize};
6 6
7use ra_ide::{InlayConfig, InlayKind}; 7use ra_ide::{InlayConfig, InlayKind};
8 8
@@ -204,24 +204,11 @@ pub enum InlayKindDef {
204 ParameterHint, 204 ParameterHint,
205} 205}
206 206
207// Work-around until better serde support is added
208// https://github.com/serde-rs/serde/issues/723#issuecomment-382501277
209fn vec_inlay_kind<'de, D>(deserializer: D) -> Result<Vec<InlayKind>, D::Error>
210where
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
220#[derive(Deserialize)] 207#[derive(Deserialize)]
221#[serde(remote = "InlayConfig")] 208#[serde(remote = "InlayConfig", rename_all = "camelCase")]
222pub struct InlayConfigDef { 209pub struct InlayConfigDef {
223 #[serde(deserialize_with = "vec_inlay_kind")] 210 pub type_hints: bool,
224 pub display_type: Vec<InlayKind>, 211 pub parameter_hints: bool,
225 pub max_length: Option<usize>, 212 pub max_length: Option<usize>,
226} 213}
227 214
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 {
33 pub publish_decorations: bool, 33 pub publish_decorations: bool,
34 pub supports_location_link: bool, 34 pub supports_location_link: bool,
35 pub line_folding_only: bool, 35 pub line_folding_only: bool,
36 pub inlay_hint_opts: InlayConfig, 36 pub inlay_hints: InlayConfig,
37 pub rustfmt_args: Vec<String>, 37 pub rustfmt_args: Vec<String>,
38 pub cargo_watch: CheckOptions, 38 pub cargo_watch: CheckOptions,
39} 39}
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 @@
307 "exclusiveMinimum": true, 307 "exclusiveMinimum": true,
308 "description": "Number of syntax trees rust-analyzer keeps in memory" 308 "description": "Number of syntax trees rust-analyzer keeps in memory"
309 }, 309 },
310 "rust-analyzer.inlayHintOpts.displayType": { 310 "rust-analyzer.inlayHints.typeHints": {
311 "type": "string", 311 "type": "boolean",
312 "enum": [ 312 "default": true,
313 "off", 313 "description": "Whether to show inlay type hints"
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"
326 }, 314 },
327 "rust-analyzer.inlayHintOpts.maxLength": { 315 "rust-analyzer.inlayHints.parameterHints": {
328 "type": [ 316 "type": "boolean",
329 "null", 317 "default": true,
330 "integer" 318 "description": "Whether to show function parameter name inlay hints at the call site"
331 ], 319 },
320 "rust-analyzer.inlayHints.maxLength": {
321 "type": "integer",
332 "default": 20, 322 "default": 20,
333 "minimum": 0, 323 "minimum": 0,
334 "exclusiveMinimum": true, 324 "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<
29 initializationOptions: { 29 initializationOptions: {
30 publishDecorations: !config.highlightingSemanticTokens, 30 publishDecorations: !config.highlightingSemanticTokens,
31 lruCapacity: config.lruCapacity, 31 lruCapacity: config.lruCapacity,
32 inlayHintOpts: config.inlayHintOpts, 32 inlayHints: config.inlayHints,
33 cargoWatchEnable: cargoWatchOpts.enable, 33 cargoWatchEnable: cargoWatchOpts.enable,
34 cargoWatchArgs: cargoWatchOpts.arguments, 34 cargoWatchArgs: cargoWatchOpts.arguments,
35 cargoWatchCommand: cargoWatchOpts.command, 35 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";
6const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; 6const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
7 7
8export interface InlayHintOptions { 8export interface InlayHintOptions {
9 displayType: string; 9 typeHints: boolean;
10 parameterHints: boolean;
10 maxLength: number; 11 maxLength: number;
11} 12}
12 13
@@ -28,8 +29,7 @@ export class Config {
28 "cargoFeatures", 29 "cargoFeatures",
29 "cargo-watch", 30 "cargo-watch",
30 "highlighting.semanticTokens", 31 "highlighting.semanticTokens",
31 "inlayHintOpts.maxLength", 32 "inlayHints",
32 "inlayHintOpts.displayType",
33 ] 33 ]
34 .map(opt => `${Config.rootSection}.${opt}`); 34 .map(opt => `${Config.rootSection}.${opt}`);
35 35
@@ -156,10 +156,11 @@ export class Config {
156 get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; } 156 get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; }
157 get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; } 157 get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; }
158 get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; } 158 get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; }
159 get inlayHintOpts(): InlayHintOptions { 159 get inlayHints(): InlayHintOptions {
160 return { 160 return {
161 displayType: this.cfg.get("inlayHintOpts.displayType") as string, 161 typeHints: this.cfg.get("inlayHints.typeHints") as boolean,
162 maxLength: this.cfg.get("inlayHintOpts.maxLength") as number, 162 parameterHints: this.cfg.get("inlayHints.parameterHints") as boolean,
163 maxLength: this.cfg.get("inlayHints.maxLength") as number,
163 }; 164 };
164 } 165 }
165 get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; } 166 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) {
10 const maybeUpdater = { 10 const maybeUpdater = {
11 updater: null as null | HintsUpdater, 11 updater: null as null | HintsUpdater,
12 onConfigChange() { 12 onConfigChange() {
13 if (ctx.config.inlayHintOpts.displayType === 'off') { 13 if (!ctx.config.inlayHints.typeHints && !ctx.config.inlayHints.parameterHints) {
14 return this.dispose(); 14 return this.dispose();
15 } 15 }
16 if (!this.updater) this.updater = new HintsUpdater(ctx); 16 if (!this.updater) this.updater = new HintsUpdater(ctx);