Skip to content

Commit e4285b2

Browse files
probably-nebmikayla-makicole-miller
committed
restore code_actions_on_format
Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com> Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com> Co-authored-by: Cole <cole@zed.dev>
1 parent 5ff0b78 commit e4285b2

File tree

4 files changed

+175
-64
lines changed

4 files changed

+175
-64
lines changed

crates/migrator/src/migrations.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,9 @@ pub(crate) mod m_2025_10_03 {
117117

118118
pub(crate) use settings::SETTINGS_PATTERNS;
119119
}
120+
121+
pub(crate) mod m_2025_10_16 {
122+
mod settings;
123+
124+
pub(crate) use settings::restore_code_actions_on_format;
125+
}

crates/migrator/src/migrations/m_2025_10_10/settings.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use anyhow::Result;
2+
use serde_json::Value;
3+
4+
use crate::patterns::migrate_language_setting;
5+
6+
pub fn restore_code_actions_on_format(value: &mut Value) -> Result<()> {
7+
migrate_language_setting(value, restore_code_actions_on_format_inner)
8+
}
9+
10+
fn restore_code_actions_on_format_inner(value: &mut Value, path: &[&str]) -> Result<()> {
11+
let Some(obj) = value.as_object_mut() else {
12+
return Ok(());
13+
};
14+
let code_actions_on_format = obj
15+
.get("code_actions_on_format")
16+
.cloned()
17+
.unwrap_or_else(|| Value::Object(Default::default()));
18+
19+
fn fmt_path(path: &[&str], key: &str) -> String {
20+
let mut path = path.to_vec();
21+
path.push(key);
22+
path.join(".")
23+
}
24+
25+
let Some(mut code_actions_map) = code_actions_on_format.as_object().cloned() else {
26+
anyhow::bail!(
27+
r#"The `code_actions_on_format` is in an invalid state and cannot be migrated at {}. Please ensure the code_actions_on_format setting is a Map<String, bool>"#,
28+
fmt_path(path, "code_actions_on_format"),
29+
);
30+
};
31+
32+
let Some(formatter) = obj.get("formatter") else {
33+
return Ok(());
34+
};
35+
let formatter_array = if let Some(array) = formatter.as_array() {
36+
array.clone()
37+
} else {
38+
vec![formatter.clone()]
39+
};
40+
let mut code_action_formatters = Vec::new();
41+
for formatter in formatter_array {
42+
let Some(code_action) = formatter.get("code_action") else {
43+
return Ok(());
44+
};
45+
let Some(code_action_name) = code_action.as_str() else {
46+
anyhow::bail!(
47+
r#"The `code_action` is in an invalid state and cannot be migrated at {}. Please ensure the code_action setting is a String"#,
48+
fmt_path(path, "formatter"),
49+
);
50+
};
51+
code_action_formatters.push(code_action_name.to_string());
52+
}
53+
54+
code_actions_map.extend(code_action_formatters.into_iter().rev().map(|code_action| (code_action, Value::Bool(true))));
55+
56+
obj.remove("formatter");
57+
obj.insert(
58+
"code_actions_on_format".into(),
59+
Value::Object(code_actions_map),
60+
);
61+
62+
Ok(())
63+
}

crates/migrator/src/migrator.rs

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ pub fn migrate_settings(text: &str) -> Result<Option<String>> {
211211
migrations::m_2025_10_03::SETTINGS_PATTERNS,
212212
&SETTINGS_QUERY_2025_10_03,
213213
),
214+
MigrationType::Json(migrations::m_2025_10_16::restore_code_actions_on_format),
214215
];
215216
run_migrations(text, migrations)
216217
}
@@ -361,6 +362,7 @@ mod tests {
361362
pretty_assertions::assert_eq!(migrated.as_deref(), output);
362363
}
363364

365+
#[track_caller]
364366
fn assert_migrate_settings(input: &str, output: Option<&str>) {
365367
let migrated = migrate_settings(input).unwrap();
366368
assert_migrated_correctly(migrated, output);
@@ -1336,7 +1338,10 @@ mod tests {
13361338

13371339
#[test]
13381340
fn test_flatten_code_action_formatters_basic_array() {
1339-
assert_migrate_settings(
1341+
assert_migrate_settings_with_migrations(
1342+
&[MigrationType::Json(
1343+
migrations::m_2025_10_01::flatten_code_actions_formatters,
1344+
)],
13401345
&r#"{
13411346
"formatter": [
13421347
{
@@ -1367,7 +1372,10 @@ mod tests {
13671372

13681373
#[test]
13691374
fn test_flatten_code_action_formatters_basic_object() {
1370-
assert_migrate_settings(
1375+
assert_migrate_settings_with_migrations(
1376+
&[MigrationType::Json(
1377+
migrations::m_2025_10_01::flatten_code_actions_formatters,
1378+
)],
13711379
&r#"{
13721380
"formatter": {
13731381
"code_actions": {
@@ -1521,7 +1529,10 @@ mod tests {
15211529
#[test]
15221530
fn test_flatten_code_action_formatters_array_with_multiple_action_blocks_in_defaults_and_multiple_languages()
15231531
{
1524-
assert_migrate_settings(
1532+
assert_migrate_settings_with_migrations(
1533+
&[MigrationType::Json(
1534+
migrations::m_2025_10_01::flatten_code_actions_formatters,
1535+
)],
15251536
&r#"{
15261537
"formatter": {
15271538
"code_actions": {
@@ -1987,4 +1998,96 @@ mod tests {
19871998
None,
19881999
);
19892000
}
2001+
2002+
#[test]
2003+
fn test_restore_code_actions_on_format() {
2004+
assert_migrate_settings_with_migrations(
2005+
&[MigrationType::Json(
2006+
migrations::m_2025_10_16::restore_code_actions_on_format,
2007+
)],
2008+
&r#"{
2009+
"formatter": {
2010+
"code_action": "foo"
2011+
}
2012+
}"#
2013+
.unindent(),
2014+
Some(
2015+
&r#"{
2016+
"code_actions_on_format": {
2017+
"foo": true
2018+
}
2019+
}
2020+
"#
2021+
.unindent(),
2022+
),
2023+
);
2024+
2025+
assert_migrate_settings_with_migrations(
2026+
&[MigrationType::Json(
2027+
migrations::m_2025_10_16::restore_code_actions_on_format,
2028+
)],
2029+
&r#"{
2030+
"formatter": [
2031+
{ "code_action": "foo" },
2032+
"auto"
2033+
]
2034+
}"#
2035+
.unindent(),
2036+
None,
2037+
);
2038+
2039+
assert_migrate_settings_with_migrations(
2040+
&[MigrationType::Json(
2041+
migrations::m_2025_10_16::restore_code_actions_on_format,
2042+
)],
2043+
&r#"{
2044+
"formatter": {
2045+
"code_action": "foo"
2046+
},
2047+
"code_actions_on_format": {
2048+
"bar": true,
2049+
"baz": false
2050+
}
2051+
}"#
2052+
.unindent(),
2053+
Some(
2054+
&r#"{
2055+
"code_actions_on_format": {
2056+
"foo": true,
2057+
"bar": true,
2058+
"baz": false
2059+
}
2060+
}"#
2061+
.unindent(),
2062+
),
2063+
);
2064+
2065+
assert_migrate_settings_with_migrations(
2066+
&[MigrationType::Json(
2067+
migrations::m_2025_10_16::restore_code_actions_on_format,
2068+
)],
2069+
&r#"{
2070+
"formatter": [
2071+
{ "code_action": "foo" },
2072+
{ "code_action": "qux" },
2073+
],
2074+
"code_actions_on_format": {
2075+
"bar": true,
2076+
"baz": false
2077+
}
2078+
}"#
2079+
.unindent(),
2080+
Some(
2081+
&r#"{
2082+
"code_actions_on_format": {
2083+
"foo": true,
2084+
"qux": true,
2085+
"bar": true,
2086+
"baz": false
2087+
}
2088+
}"#
2089+
.unindent(),
2090+
),
2091+
);
2092+
}
19902093
}

0 commit comments

Comments
 (0)