From f4a043df8840e44a62dc0a08d741aec7b84bb8b6 Mon Sep 17 00:00:00 2001 From: Skyler Hawthorne Date: Fri, 14 Apr 2023 11:00:15 -0400 Subject: [PATCH] Fix YAML auto indent This makes use of the new `extend` capture to correct YAML indentation. Fixes #6661 --- helix-term/tests/integration.rs | 1 + helix-term/tests/test/languages/mod.rs | 3 + helix-term/tests/test/languages/yaml.rs | 598 ++++++++++++++++++++++++ runtime/queries/yaml/indents.scm | 8 +- 4 files changed, 609 insertions(+), 1 deletion(-) create mode 100644 helix-term/tests/test/languages/mod.rs create mode 100644 helix-term/tests/test/languages/yaml.rs diff --git a/helix-term/tests/integration.rs b/helix-term/tests/integration.rs index d77eefed0..35214bcb8 100644 --- a/helix-term/tests/integration.rs +++ b/helix-term/tests/integration.rs @@ -18,6 +18,7 @@ mod test { mod auto_indent; mod auto_pairs; mod commands; + mod languages; mod movement; mod prompt; mod splits; diff --git a/helix-term/tests/test/languages/mod.rs b/helix-term/tests/test/languages/mod.rs new file mode 100644 index 000000000..40ebd796f --- /dev/null +++ b/helix-term/tests/test/languages/mod.rs @@ -0,0 +1,3 @@ +use super::*; + +mod yaml; diff --git a/helix-term/tests/test/languages/yaml.rs b/helix-term/tests/test/languages/yaml.rs new file mode 100644 index 000000000..9b7b7b10f --- /dev/null +++ b/helix-term/tests/test/languages/yaml.rs @@ -0,0 +1,598 @@ +use super::*; + +#[tokio::test(flavor = "multi_thread")] +async fn auto_indent() -> anyhow::Result<()> { + let app = || AppBuilder::new().with_file("foo.yaml", None); + + let below_tests = [ + ( + helpers::platform_line(indoc! {r##" + #[t|]#op: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "o", + helpers::platform_line(indoc! {"\ + top: + #[\n|]# + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + b#[a|]#z: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + #[\n|]# + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + baz: foo + bazi#[:|]# + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + #[\n|]# + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + baz: foo + bazi: + more: #[yes|]# + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + #[\n|]# + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + baz: foo + bazi: + more: yes + why: becaus#[e|]# + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + #[\n|]# + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux:#[\n|]# + - 1 + - 2 + bax: foox + fook: + "}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + #[\n|]# + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1#[\n|]# + - 2 + bax: foox + fook: + "}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + #[\n|]# + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook:#[\n|]# + "}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + #[\n|]# + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: | + some + multi + line + string#[\n|]# + fook: + "}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: | + some + multi + line + string + #[\n|]# + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: > + some + multi + line#[\n|]# + string + fook: + "}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: > + some + multi + line + #[\n|]# + string + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: >#[\n|]# + fook: + "}), + "o", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: > + #[\n|]# + fook: + "}), + ), + ]; + + for test in below_tests { + test_with_config(app(), test).await?; + } + + let above_tests = [ + ( + helpers::platform_line(indoc! {r##" + #[t|]#op: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "O", + helpers::platform_line(indoc! {"\ + #[\n|]# + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + b#[a|]#z: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "O", + helpers::platform_line(indoc! {"\ + top: + #[\n|]# + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + baz: foo + bazi#[:|]# + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + #[\n|]# + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + baz: foo + bazi: + more: #[yes|]# + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + #[\n|]# + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {r##" + top: + baz: foo + bazi: + more: yes + why: becaus#[e|]# + quux: + - 1 + - 2 + bax: foox + fook: + "##}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + #[\n|]# + why: because + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux:#[\n|]# + - 1 + - 2 + bax: foox + fook: + "}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + #[\n|]# + quux: + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1#[\n|]# + - 2 + bax: foox + fook: + "}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + #[\n|]# + - 1 + - 2 + bax: foox + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + fook:#[\n|]# + "}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bazi: + more: yes + why: because + quux: + - 1 + - 2 + bax: foox + #[\n|]# + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: | + some + multi + line + string#[\n|]# + fook: + "}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: | + some + multi + line + #[\n|]# + string + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: > + some#[\n|]# + multi + line + string + fook: + "}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: > + #[\n|]# + some + multi + line + string + fook: + "}), + ), + ( + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: > + fook:#[\n|]# + "}), + "O", + helpers::platform_line(indoc! {"\ + top: + baz: foo + bax: > + #[\n|]# + fook: + "}), + ), + ]; + + for test in above_tests { + test_with_config(app(), test).await?; + } + + Ok(()) +} diff --git a/runtime/queries/yaml/indents.scm b/runtime/queries/yaml/indents.scm index 70a00b695..1ca487bf0 100644 --- a/runtime/queries/yaml/indents.scm +++ b/runtime/queries/yaml/indents.scm @@ -1,2 +1,8 @@ -(block_mapping_pair) @indent +(block_scalar) @indent @extend +((block_mapping_pair + key: (_) @key + value: (_)? @val + (#not-same-line? @key @val) + ) @indent @extend +)