Skip to content

Commit 7b0c05d

Browse files
committed
Bring the diff down to zero using BTreeMap for snippets, @from_str:
1 parent 48dd942 commit 7b0c05d

File tree

1 file changed

+72
-57
lines changed

1 file changed

+72
-57
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use project_model::{
3030
};
3131
use rustc_hash::{FxHashMap, FxHashSet};
3232
use serde::{de::DeserializeOwned, Deserialize, Serialize};
33+
use std::collections::BTreeMap;
3334
use toml;
3435
use vfs::{AbsPath, AbsPathBuf, FileId};
3536

@@ -141,7 +142,7 @@ config_data! {
141142
// than `checkOnSave_target`
142143
cargo_target: Option<String> = None,
143144
/// Unsets the implicit `#[cfg(test)]` for the specified crates.
144-
cargo_unsetTest: Vec<String> = vec!["core".to_string()],
145+
cargo_unsetTest: Vec<String> = @from_str: r#"["core"]"#,
145146

146147
/// Run the check command for diagnostics on save.
147148
checkOnSave | checkOnSave_enable: bool = true,
@@ -383,7 +384,48 @@ config_data! {
383384
/// Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
384385
completion_privateEditable_enable: bool = false,
385386
/// Custom completion snippets.
386-
completion_snippets_custom: FxHashMap<String, SnippetDef> = SnippetDef::default_snippets(),
387+
// NOTE: we use BTreeMap for deterministic serialization ordering
388+
completion_snippets_custom: BTreeMap<String, SnippetDef> = @from_str: r#"{
389+
"Arc::new": {
390+
"postfix": "arc",
391+
"body": "Arc::new(${receiver})",
392+
"requires": "std::sync::Arc",
393+
"description": "Put the expression into an `Arc`",
394+
"scope": "expr"
395+
},
396+
"Rc::new": {
397+
"postfix": "rc",
398+
"body": "Rc::new(${receiver})",
399+
"requires": "std::rc::Rc",
400+
"description": "Put the expression into an `Rc`",
401+
"scope": "expr"
402+
},
403+
"Box::pin": {
404+
"postfix": "pinbox",
405+
"body": "Box::pin(${receiver})",
406+
"requires": "std::boxed::Box",
407+
"description": "Put the expression into a pinned `Box`",
408+
"scope": "expr"
409+
},
410+
"Ok": {
411+
"postfix": "ok",
412+
"body": "Ok(${receiver})",
413+
"description": "Wrap the expression in a `Result::Ok`",
414+
"scope": "expr"
415+
},
416+
"Err": {
417+
"postfix": "err",
418+
"body": "Err(${receiver})",
419+
"description": "Wrap the expression in a `Result::Err`",
420+
"scope": "expr"
421+
},
422+
"Some": {
423+
"postfix": "some",
424+
"body": "Some(${receiver})",
425+
"description": "Wrap the expression in an `Option::Some`",
426+
"scope": "expr"
427+
}
428+
}"#,
387429

388430
/// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords.
389431
highlightRelated_breakPoints_enable: bool = true,
@@ -2015,55 +2057,6 @@ struct SnippetDef {
20152057
scope: SnippetScopeDef,
20162058
}
20172059

2018-
impl SnippetDef {
2019-
fn default_snippets() -> FxHashMap<String, SnippetDef> {
2020-
serde_json::from_str(
2021-
r#"{
2022-
"Arc::new": {
2023-
"postfix": "arc",
2024-
"body": "Arc::new(${receiver})",
2025-
"requires": "std::sync::Arc",
2026-
"description": "Put the expression into an `Arc`",
2027-
"scope": "expr"
2028-
},
2029-
"Rc::new": {
2030-
"postfix": "rc",
2031-
"body": "Rc::new(${receiver})",
2032-
"requires": "std::rc::Rc",
2033-
"description": "Put the expression into an `Rc`",
2034-
"scope": "expr"
2035-
},
2036-
"Box::pin": {
2037-
"postfix": "pinbox",
2038-
"body": "Box::pin(${receiver})",
2039-
"requires": "std::boxed::Box",
2040-
"description": "Put the expression into a pinned `Box`",
2041-
"scope": "expr"
2042-
},
2043-
"Ok": {
2044-
"postfix": "ok",
2045-
"body": "Ok(${receiver})",
2046-
"description": "Wrap the expression in a `Result::Ok`",
2047-
"scope": "expr"
2048-
},
2049-
"Err": {
2050-
"postfix": "err",
2051-
"body": "Err(${receiver})",
2052-
"description": "Wrap the expression in a `Result::Err`",
2053-
"scope": "expr"
2054-
},
2055-
"Some": {
2056-
"postfix": "some",
2057-
"body": "Some(${receiver})",
2058-
"description": "Wrap the expression in an `Option::Some`",
2059-
"scope": "expr"
2060-
}
2061-
}"#,
2062-
)
2063-
.unwrap()
2064-
}
2065-
}
2066-
20672060
mod single_or_array {
20682061
use serde::{Deserialize, Serialize};
20692062

@@ -2309,12 +2302,33 @@ pub enum TargetDirectory {
23092302
Directory(PathBuf),
23102303
}
23112304

2305+
macro_rules! _default_val {
2306+
(@from_str: $s:literal, $ty:ty) => {{
2307+
let default_: $ty = serde_json::from_str(&$s).unwrap();
2308+
default_
2309+
}};
2310+
($default:expr, $ty:ty) => {{
2311+
let default_: $ty = $default;
2312+
default_
2313+
}};
2314+
}
2315+
2316+
macro_rules! _default_str {
2317+
(@from_str: $s:literal, $_ty:ty) => {
2318+
$s.to_string()
2319+
};
2320+
($default:expr, $ty:ty) => {{
2321+
let val = default_val!($default, $ty);
2322+
serde_json::to_string_pretty(&val).unwrap()
2323+
}};
2324+
}
2325+
23122326
macro_rules! _config_data {
23132327
// modname is for the tests
23142328
($modname:ident: struct $name:ident {
23152329
$(
23162330
$(#[doc=$doc:literal])*
2317-
$field:ident $(| $alias:ident)*: $ty:ty = $default:expr,
2331+
$field:ident $(| $alias:ident)*: $ty:ty = $(@$marker:ident: )? $default:expr,
23182332
)*
23192333
}) => {
23202334
#[allow(non_snake_case)]
@@ -2329,7 +2343,7 @@ macro_rules! _config_data {
23292343
error_sink,
23302344
stringify!($field),
23312345
None$(.or(Some(stringify!($alias))))*,
2332-
{ let default_: $ty = $default; default_ },
2346+
default_val!($(@$marker:)? $default, $ty),
23332347
),
23342348
)*}
23352349
}
@@ -2342,7 +2356,7 @@ macro_rules! _config_data {
23422356
error_sink,
23432357
stringify!($field),
23442358
None$(.or(Some(stringify!($alias))))*,
2345-
{ let default_: $ty = $default; default_ },
2359+
default_val!($(@$marker:)? $default, $ty),
23462360
),
23472361
)*}
23482362
}
@@ -2352,8 +2366,7 @@ macro_rules! _config_data {
23522366
$({
23532367
let field = stringify!($field);
23542368
let ty = stringify!($ty);
2355-
let default =
2356-
serde_json::to_string_pretty(&{ let default_: $ty = $default; default_ }).unwrap();
2369+
let default = default_str!($(@$marker:)? $default, $ty);
23572370

23582371
(field, ty, &[$($doc),*], default)
23592372
},)*
@@ -2371,6 +2384,8 @@ macro_rules! _config_data {
23712384
};
23722385
}
23732386
use _config_data as config_data;
2387+
use _default_str as default_str;
2388+
use _default_val as default_val;
23742389

23752390
#[derive(Debug, Clone, Serialize)]
23762391
struct ConfigData {
@@ -2545,7 +2560,7 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
25452560
"FxHashMap<Box<str>, Box<[Box<str>]>>" => set! {
25462561
"type": "object",
25472562
},
2548-
"FxHashMap<String, SnippetDef>" => set! {
2563+
"BTreeMap<String, SnippetDef>" => set! {
25492564
"type": "object",
25502565
},
25512566
"FxHashMap<String, String>" => set! {

0 commit comments

Comments
 (0)