Skip to content

Commit 8835d42

Browse files
committed
Add the default snippets back, tweak the serialize output until it's close
1 parent a364384 commit 8835d42

File tree

1 file changed

+105
-32
lines changed

1 file changed

+105
-32
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 105 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,7 @@ config_data! {
383383
/// Enables completions of private items and fields that are defined in the current workspace even if they are not visible at the current position.
384384
completion_privateEditable_enable: bool = false,
385385
/// Custom completion snippets.
386-
// NOTE: Keep this list in sync with the feature docs of user snippets.
387-
completion_snippets_custom: FxHashMap<String, SnippetDef> = FxHashMap::default(),
386+
completion_snippets_custom: FxHashMap<String, SnippetDef> = SnippetDef::default_snippets(),
388387

389388
/// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords.
390389
highlightRelated_breakPoints_enable: bool = true,
@@ -1957,7 +1956,7 @@ mod de_unit_v {
19571956
named_unit_variant!(both);
19581957
}
19591958

1960-
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
1959+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
19611960
#[serde(rename_all = "snake_case")]
19621961
enum SnippetScopeDef {
19631962
Expr,
@@ -1974,47 +1973,121 @@ impl Default for SnippetScopeDef {
19741973
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
19751974
#[serde(default)]
19761975
struct SnippetDef {
1977-
#[serde(deserialize_with = "single_or_array")]
1976+
#[serde(with = "single_or_array")]
1977+
#[serde(skip_serializing_if = "Vec::is_empty")]
19781978
prefix: Vec<String>,
1979-
#[serde(deserialize_with = "single_or_array")]
1979+
1980+
#[serde(with = "single_or_array")]
1981+
#[serde(skip_serializing_if = "Vec::is_empty")]
19801982
postfix: Vec<String>,
1981-
description: Option<String>,
1982-
#[serde(deserialize_with = "single_or_array")]
1983+
1984+
#[serde(with = "single_or_array")]
1985+
#[serde(skip_serializing_if = "Vec::is_empty")]
19831986
body: Vec<String>,
1984-
#[serde(deserialize_with = "single_or_array")]
1987+
1988+
#[serde(with = "single_or_array")]
1989+
#[serde(skip_serializing_if = "Vec::is_empty")]
19851990
requires: Vec<String>,
1991+
1992+
#[serde(skip_serializing_if = "Option::is_none")]
1993+
description: Option<String>,
1994+
19861995
scope: SnippetScopeDef,
19871996
}
19881997

1989-
fn single_or_array<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
1990-
where
1991-
D: serde::Deserializer<'de>,
1992-
{
1993-
struct SingleOrVec;
1998+
impl SnippetDef {
1999+
fn default_snippets() -> FxHashMap<String, SnippetDef> {
2000+
serde_json::from_str(
2001+
r#"{
2002+
"Arc::new": {
2003+
"postfix": "arc",
2004+
"body": "Arc::new(${receiver})",
2005+
"requires": "std::sync::Arc",
2006+
"description": "Put the expression into an `Arc`",
2007+
"scope": "expr"
2008+
},
2009+
"Rc::new": {
2010+
"postfix": "rc",
2011+
"body": "Rc::new(${receiver})",
2012+
"requires": "std::rc::Rc",
2013+
"description": "Put the expression into an `Rc`",
2014+
"scope": "expr"
2015+
},
2016+
"Box::pin": {
2017+
"postfix": "pinbox",
2018+
"body": "Box::pin(${receiver})",
2019+
"requires": "std::boxed::Box",
2020+
"description": "Put the expression into a pinned `Box`",
2021+
"scope": "expr"
2022+
},
2023+
"Ok": {
2024+
"postfix": "ok",
2025+
"body": "Ok(${receiver})",
2026+
"description": "Wrap the expression in a `Result::Ok`",
2027+
"scope": "expr"
2028+
},
2029+
"Err": {
2030+
"postfix": "err",
2031+
"body": "Err(${receiver})",
2032+
"description": "Wrap the expression in a `Result::Err`",
2033+
"scope": "expr"
2034+
},
2035+
"Some": {
2036+
"postfix": "some",
2037+
"body": "Some(${receiver})",
2038+
"description": "Wrap the expression in an `Option::Some`",
2039+
"scope": "expr"
2040+
}
2041+
}"#,
2042+
)
2043+
.unwrap()
2044+
}
2045+
}
2046+
2047+
mod single_or_array {
2048+
use serde::{Deserialize, Serialize};
19942049

1995-
impl<'de> serde::de::Visitor<'de> for SingleOrVec {
1996-
type Value = Vec<String>;
2050+
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
2051+
where
2052+
D: serde::Deserializer<'de>,
2053+
{
2054+
struct SingleOrVec;
19972055

1998-
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1999-
formatter.write_str("string or array of strings")
2000-
}
2056+
impl<'de> serde::de::Visitor<'de> for SingleOrVec {
2057+
type Value = Vec<String>;
20012058

2002-
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
2003-
where
2004-
E: serde::de::Error,
2005-
{
2006-
Ok(vec![value.to_owned()])
2007-
}
2059+
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2060+
formatter.write_str("string or array of strings")
2061+
}
20082062

2009-
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
2010-
where
2011-
A: serde::de::SeqAccess<'de>,
2012-
{
2013-
Deserialize::deserialize(serde::de::value::SeqAccessDeserializer::new(seq))
2063+
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
2064+
where
2065+
E: serde::de::Error,
2066+
{
2067+
Ok(vec![value.to_owned()])
2068+
}
2069+
2070+
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
2071+
where
2072+
A: serde::de::SeqAccess<'de>,
2073+
{
2074+
Deserialize::deserialize(serde::de::value::SeqAccessDeserializer::new(seq))
2075+
}
20142076
}
2077+
2078+
deserializer.deserialize_any(SingleOrVec)
20152079
}
20162080

2017-
deserializer.deserialize_any(SingleOrVec)
2081+
pub fn serialize<S>(vec: &Vec<String>, serializer: S) -> Result<S::Ok, S::Error>
2082+
where
2083+
S: serde::Serializer,
2084+
{
2085+
match &vec[..] {
2086+
// [] case is handled by skip_serializing_if
2087+
[single] => serializer.serialize_str(&single),
2088+
slice => slice.serialize(serializer),
2089+
}
2090+
}
20182091
}
20192092

20202093
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -2064,7 +2137,7 @@ enum InvocationStrategy {
20642137
}
20652138

20662139
#[derive(Serialize, Deserialize, Debug, Clone)]
2067-
struct CheckOnSaveTargets(#[serde(deserialize_with = "single_or_array")] Vec<String>);
2140+
struct CheckOnSaveTargets(#[serde(with = "single_or_array")] Vec<String>);
20682141

20692142
#[derive(Serialize, Deserialize, Debug, Clone)]
20702143
#[serde(rename_all = "snake_case")]
@@ -2249,7 +2322,7 @@ macro_rules! _config_data {
22492322
let field = stringify!($field);
22502323
let ty = stringify!($ty);
22512324
let default =
2252-
serde_json::to_string(&{ let default_: $ty = $default; default_ }).unwrap();
2325+
serde_json::to_string_pretty(&{ let default_: $ty = $default; default_ }).unwrap();
22532326

22542327
(field, ty, &[$($doc),*], default)
22552328
},)*

0 commit comments

Comments
 (0)