From 3ef81633571d36e58204d5030d1543ead2e55518 Mon Sep 17 00:00:00 2001 From: Coenen Benjamin Date: Tue, 4 Nov 2025 07:53:34 +0100 Subject: [PATCH] yaml: Fix indentation with dictionary when editing (#40913) Closes #39570 Release Notes: - Fixed indentation with dictionary when editing YAML file. --------- Signed-off-by: Benjamin <5719034+bnjjj@users.noreply.github.com> --- crates/editor/src/editor_tests.rs | 71 +++++++++++++++++++++++++++ crates/language/src/language.rs | 4 +- crates/languages/src/yaml/config.toml | 9 +++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index 903ca3b9c1d947094f3b79419ae00c499ec9fd0c..3709709c71fd1355014fce3f48681c632df7e18d 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -3136,6 +3136,77 @@ fn test_newline(cx: &mut TestAppContext) { }); } +#[gpui::test] +async fn test_newline_yaml(cx: &mut TestAppContext) { + init_test(cx, |_| {}); + + let mut cx = EditorTestContext::new(cx).await; + let yaml_language = languages::language("yaml", tree_sitter_yaml::LANGUAGE.into()); + cx.update_buffer(|buffer, cx| buffer.set_language(Some(yaml_language), cx)); + + // Object (between 2 fields) + cx.set_state(indoc! {" + test:ˇ + hello: bye"}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + test: + ˇ + hello: bye"}); + + // Object (first and single line) + cx.set_state(indoc! {" + test:ˇ"}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + test: + ˇ"}); + + // Array with objects (after first element) + cx.set_state(indoc! {" + test: + - foo: barˇ"}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + test: + - foo: bar + ˇ"}); + + // Array with objects and comment + cx.set_state(indoc! {" + test: + - foo: bar + - bar: # testˇ"}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + test: + - foo: bar + - bar: # test + ˇ"}); + + // Array with objects (after second element) + cx.set_state(indoc! {" + test: + - foo: bar + - bar: fooˇ"}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + test: + - foo: bar + - bar: foo + ˇ"}); + + // Array with strings (after first element) + cx.set_state(indoc! {" + test: + - fooˇ"}); + cx.update_editor(|e, window, cx| e.newline(&Newline, window, cx)); + cx.assert_editor_state(indoc! {" + test: + - foo + ˇ"}); +} + #[gpui::test] fn test_newline_with_old_selections(cx: &mut TestAppContext) { init_test(cx, |_| {}); diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs index e3fb6733dd5176906f0a9a9d208305d67470ba15..3d777e5ff6f4abcaea211aacdfc4761053b387be 100644 --- a/crates/language/src/language.rs +++ b/crates/language/src/language.rs @@ -680,7 +680,7 @@ pub struct CodeLabelBuilder { filter_range: Range, } -#[derive(Clone, Deserialize, JsonSchema)] +#[derive(Clone, Deserialize, JsonSchema, Debug)] pub struct LanguageConfig { /// Human-readable name of the language. pub name: LanguageName, @@ -823,7 +823,7 @@ pub struct LanguageMatcher { } /// The configuration for JSX tag auto-closing. -#[derive(Clone, Deserialize, JsonSchema)] +#[derive(Clone, Deserialize, JsonSchema, Debug)] pub struct JsxTagAutoCloseConfig { /// The name of the node for a opening tag pub open_tag_node_name: String, diff --git a/crates/languages/src/yaml/config.toml b/crates/languages/src/yaml/config.toml index e54bceda1ae01eff4a5c917a5b8fc74282165bea..e941497bf3f3ea56e7c5188fde3e8708cd36bb8a 100644 --- a/crates/languages/src/yaml/config.toml +++ b/crates/languages/src/yaml/config.toml @@ -12,6 +12,13 @@ brackets = [ auto_indent_on_paste = false auto_indent_using_last_non_empty_line = false -increase_indent_pattern = "^[^#]*:\\s*[|>]?\\s*$" +# 1st block to match basic elements followed by ':' like 'test: ' for example +# 2nd block to match for first element in an array if it's an object +# For example: +# ```yaml +# test: +# - first: one +# ``` +increase_indent_pattern = "(:?^[^#]*:\\s*[|>]?\\s*$)|(:?^\\s*-[^#]*:\\s*(:?#+.*)$)" prettier_parser_name = "yaml" tab_size = 2