From 037caec62c881e11325f93627d9bfd44fb374d51 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 7 Jan 2021 14:37:38 +0300 Subject: Use american spelling for config As per https://github.com/rust-analyzer/rust-analyzer/blob/171c3c08fe245938fb25321394233de5fe2abc7c/docs/dev/style.md#variable-naming Also implement config aliasing, for pain-free settings migrations in the future --- crates/rust-analyzer/src/config.rs | 31 ++++++++++++++++++++++--------- docs/user/generated_config.adoc | 2 +- editors/code/package.json | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index e3ba81ac6..2d3e25cbf 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -7,7 +7,7 @@ //! configure the server itself, feature flags are passed into analysis, and //! tweak things like automatic insertion of `()` in completions. -use std::{convert::TryFrom, ffi::OsString, path::PathBuf}; +use std::{convert::TryFrom, ffi::OsString, iter, path::PathBuf}; use flycheck::FlycheckConfig; use hir::PrefixKind; @@ -28,7 +28,8 @@ use crate::{caps::completion_item_edit_resolve, diagnostics::DiagnosticsMapConfi config_data! { struct ConfigData { /// The strategy to use when inserting new imports or merging imports. - assist_importMergeBehaviour: MergeBehaviorDef = "\"full\"", + assist_importMergeBehavior | + assist_importMergeBehaviour: MergeBehaviorDef = "\"full\"", /// The path structure for newly inserted paths to use. assist_importPrefix: ImportPrefixDef = "\"plain\"", @@ -530,7 +531,7 @@ impl Config { } } fn merge_behavior(&self) -> Option { - match self.data.assist_importMergeBehaviour { + match self.data.assist_importMergeBehavior { MergeBehaviorDef::None => None, MergeBehaviorDef::Full => Some(MergeBehavior::Full), MergeBehaviorDef::Last => Some(MergeBehavior::Last), @@ -639,7 +640,7 @@ macro_rules! _config_data { (struct $name:ident { $( $(#[doc=$doc:literal])* - $field:ident: $ty:ty = $default:expr, + $field:ident $(| $alias:ident)?: $ty:ty = $default:expr, )* }) => { #[allow(non_snake_case)] @@ -648,7 +649,12 @@ macro_rules! _config_data { impl $name { fn from_json(mut json: serde_json::Value) -> $name { $name {$( - $field: get_field(&mut json, stringify!($field), $default), + $field: get_field( + &mut json, + stringify!($field), + None$(.or(Some(stringify!($alias))))?, + $default, + ), )*} } @@ -680,14 +686,21 @@ use _config_data as config_data; fn get_field( json: &mut serde_json::Value, field: &'static str, + alias: Option<&'static str>, default: &str, ) -> T { let default = serde_json::from_str(default).unwrap(); - let mut pointer = field.replace('_', "/"); - pointer.insert(0, '/'); - json.pointer_mut(&pointer) - .and_then(|it| serde_json::from_value(it.take()).ok()) + // XXX: check alias first, to work-around the VS Code where it pre-fills the + // defaults instead of sending an empty object. + alias + .into_iter() + .chain(iter::once(field)) + .find_map(move |field| { + let mut pointer = field.replace('_', "/"); + pointer.insert(0, '/'); + json.pointer_mut(&pointer).and_then(|it| serde_json::from_value(it.take()).ok()) + }) .unwrap_or(default) } diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index e45ea5c35..a76c99d1e 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -1,4 +1,4 @@ -[[rust-analyzer.assist.importMergeBehaviour]]rust-analyzer.assist.importMergeBehaviour (default: `"full"`):: +[[rust-analyzer.assist.importMergeBehavior]]rust-analyzer.assist.importMergeBehavior (default: `"full"`):: The strategy to use when inserting new imports or merging imports. [[rust-analyzer.assist.importPrefix]]rust-analyzer.assist.importPrefix (default: `"plain"`):: The path structure for newly inserted paths to use. diff --git a/editors/code/package.json b/editors/code/package.json index ea7f0990c..3e6ebd7ed 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -349,7 +349,7 @@ "default": {}, "markdownDescription": "Optional settings passed to the debug engine. Example: `{ \"lldb\": { \"terminal\":\"external\"} }`" }, - "rust-analyzer.assist.importMergeBehaviour": { + "rust-analyzer.assist.importMergeBehavior": { "markdownDescription": "The strategy to use when inserting new imports or merging imports.", "default": "full", "type": "string", -- cgit v1.2.3