migrator: Add migration for settings changed prior to migrator-introduction (#27375)
This PR updates two existing settings to use the settings migrator instead of a manually implemented visitor. Both of these settings were changed prior to the introduction of automatic migrations and the visitor ensured that the settings were kept backwards compatible. See https://github.com/zed-industries/zed/pull/22200 and https://github.com/zed-industries/zed/pull/22364 respectively. WIth this change, existing user configurations are updated accordingly and the corresponding settings can derive `Deserialize` again. I also added tests for the replacement of settings values, as there was no test for this behaviour. Additionally, I added a seperate test for the existing migration of `always_show_close_button`, since that migration updated both the key and value. Release Notes: - N/A
This commit is contained in:
parent
46d67a33c7
commit
be83c5e1c5
5 changed files with 128 additions and 95 deletions
|
@ -1,3 +1,9 @@
|
|||
pub(crate) mod m_2025_01_02 {
|
||||
mod settings;
|
||||
|
||||
pub(crate) use settings::SETTINGS_PATTERNS;
|
||||
}
|
||||
|
||||
pub(crate) mod m_2025_01_29 {
|
||||
mod keymap;
|
||||
mod settings;
|
||||
|
|
62
crates/migrator/src/migrations/m_2025_01_02/settings.rs
Normal file
62
crates/migrator/src/migrations/m_2025_01_02/settings.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use collections::HashMap;
|
||||
use std::{ops::Range, sync::LazyLock};
|
||||
use tree_sitter::{Query, QueryMatch};
|
||||
|
||||
use crate::patterns::SETTINGS_NESTED_KEY_VALUE_PATTERN;
|
||||
use crate::MigrationPatterns;
|
||||
|
||||
pub const SETTINGS_PATTERNS: MigrationPatterns = &[(
|
||||
SETTINGS_NESTED_KEY_VALUE_PATTERN,
|
||||
replace_deprecated_settings_values,
|
||||
)];
|
||||
|
||||
fn replace_deprecated_settings_values(
|
||||
contents: &str,
|
||||
mat: &QueryMatch,
|
||||
query: &Query,
|
||||
) -> Option<(Range<usize>, String)> {
|
||||
let parent_object_capture_ix = query.capture_index_for_name("parent_key")?;
|
||||
let parent_object_range = mat
|
||||
.nodes_for_capture_index(parent_object_capture_ix)
|
||||
.next()?
|
||||
.byte_range();
|
||||
let parent_object_name = contents.get(parent_object_range.clone())?;
|
||||
|
||||
let setting_name_ix = query.capture_index_for_name("setting_name")?;
|
||||
let setting_name_range = mat
|
||||
.nodes_for_capture_index(setting_name_ix)
|
||||
.next()?
|
||||
.byte_range();
|
||||
let setting_name = contents.get(setting_name_range.clone())?;
|
||||
|
||||
let setting_value_ix = query.capture_index_for_name("setting_value")?;
|
||||
let setting_value_range = mat
|
||||
.nodes_for_capture_index(setting_value_ix)
|
||||
.next()?
|
||||
.byte_range();
|
||||
let setting_value = contents.get(setting_value_range.clone())?;
|
||||
|
||||
UPDATED_SETTINGS
|
||||
.get(&(parent_object_name, setting_name))
|
||||
.and_then(|new_values| {
|
||||
new_values
|
||||
.iter()
|
||||
.find_map(|(old_value, new_value)| {
|
||||
(*old_value == setting_value).then(|| new_value.to_string())
|
||||
})
|
||||
.map(|new_value| (setting_value_range, new_value))
|
||||
})
|
||||
}
|
||||
|
||||
static UPDATED_SETTINGS: LazyLock<HashMap<(&str, &str), Vec<(&str, &str)>>> = LazyLock::new(|| {
|
||||
HashMap::from_iter([
|
||||
(
|
||||
("chat_panel", "button"),
|
||||
vec![("true", "\"always\""), ("false", "\"never\"")],
|
||||
),
|
||||
(
|
||||
("scrollbar", "diagnostics"),
|
||||
vec![("true", "\"all\""), ("false", "\"none\"")],
|
||||
),
|
||||
])
|
||||
});
|
|
@ -104,6 +104,10 @@ pub fn migrate_keymap(text: &str) -> Result<Option<String>> {
|
|||
|
||||
pub fn migrate_settings(text: &str) -> Result<Option<String>> {
|
||||
let migrations: &[(MigrationPatterns, &Query)] = &[
|
||||
(
|
||||
migrations::m_2025_01_02::SETTINGS_PATTERNS,
|
||||
&SETTINGS_QUERY_2025_01_02,
|
||||
),
|
||||
(
|
||||
migrations::m_2025_01_29::SETTINGS_PATTERNS,
|
||||
&SETTINGS_QUERY_2025_01_29,
|
||||
|
@ -166,6 +170,10 @@ define_query!(
|
|||
);
|
||||
|
||||
// settings
|
||||
define_query!(
|
||||
SETTINGS_QUERY_2025_01_02,
|
||||
migrations::m_2025_01_02::SETTINGS_PATTERNS
|
||||
);
|
||||
define_query!(
|
||||
SETTINGS_QUERY_2025_01_29,
|
||||
migrations::m_2025_01_29::SETTINGS_PATTERNS
|
||||
|
@ -461,4 +469,54 @@ mod tests {
|
|||
),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_settings_value() {
|
||||
assert_migrate_settings(
|
||||
r#"
|
||||
{
|
||||
"scrollbar": {
|
||||
"diagnostics": true
|
||||
},
|
||||
"chat_panel": {
|
||||
"button": true
|
||||
}
|
||||
}
|
||||
"#,
|
||||
Some(
|
||||
r#"
|
||||
{
|
||||
"scrollbar": {
|
||||
"diagnostics": "all"
|
||||
},
|
||||
"chat_panel": {
|
||||
"button": "always"
|
||||
}
|
||||
}
|
||||
"#,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_replace_settings_name_and_value() {
|
||||
assert_migrate_settings(
|
||||
r#"
|
||||
{
|
||||
"tabs": {
|
||||
"always_show_close_button": true
|
||||
}
|
||||
}
|
||||
"#,
|
||||
Some(
|
||||
r#"
|
||||
{
|
||||
"tabs": {
|
||||
"show_close_button": "always"
|
||||
}
|
||||
}
|
||||
"#,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue