From de80b8ee31a1b8e7141e6d171bca31ea4ea62f46 Mon Sep 17 00:00:00 2001 From: Bobbe <34186858+30350n@users.noreply.github.com> Date: Sat, 8 Mar 2025 18:15:18 +0100 Subject: [PATCH] feat: string: handle indentation modes --- src/alejandra/src/rules/string.rs | 14 ++++++++++++-- .../cases/indentation-fourspaces/string/in.nix | 6 ++++++ .../cases/indentation-fourspaces/string/out.nix | 6 ++++++ .../tests/cases/indentation-tabs/string/in.nix | 6 ++++++ .../tests/cases/indentation-tabs/string/out.nix | 6 ++++++ src/alejandra/tests/fmt.rs | 1 + 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/alejandra/tests/cases/indentation-fourspaces/string/in.nix create mode 100644 src/alejandra/tests/cases/indentation-fourspaces/string/out.nix create mode 100644 src/alejandra/tests/cases/indentation-tabs/string/in.nix create mode 100644 src/alejandra/tests/cases/indentation-tabs/string/out.nix diff --git a/src/alejandra/src/rules/string.rs b/src/alejandra/src/rules/string.rs index 35064e3..9ce5928 100644 --- a/src/alejandra/src/rules/string.rs +++ b/src/alejandra/src/rules/string.rs @@ -1,3 +1,5 @@ +use crate::config::Indentation; + const PLACEHOLDER: &str = "\ 4d13159079d76c1398db5f3ab0c62325\ f884b545e63226f7ec8aad96c52e13e8\ @@ -86,7 +88,7 @@ pub(crate) fn rule( }) .collect(); - // Indent everything 2 spaces + // Indent everything if lines.len() > 1 && lines.iter().filter(|line| !line.trim().is_empty()).count() >= 1 { @@ -94,7 +96,15 @@ pub(crate) fn rule( .iter() .map(|line| { if !line.trim().is_empty() { - format!(" {}", line) + format!( + "{}{}", + match build_ctx.config.indentation { + Indentation::FourSpaces => " ", + Indentation::Tabs => "\t", + Indentation::TwoSpaces => " ", + }, + line + ) } else { line.to_string() } diff --git a/src/alejandra/tests/cases/indentation-fourspaces/string/in.nix b/src/alejandra/tests/cases/indentation-fourspaces/string/in.nix new file mode 100644 index 0000000..5a8e97e --- /dev/null +++ b/src/alejandra/tests/cases/indentation-fourspaces/string/in.nix @@ -0,0 +1,6 @@ +[ + '' + foo + bar + '' +] diff --git a/src/alejandra/tests/cases/indentation-fourspaces/string/out.nix b/src/alejandra/tests/cases/indentation-fourspaces/string/out.nix new file mode 100644 index 0000000..7cb3d1b --- /dev/null +++ b/src/alejandra/tests/cases/indentation-fourspaces/string/out.nix @@ -0,0 +1,6 @@ +[ + '' + foo + bar + '' +] diff --git a/src/alejandra/tests/cases/indentation-tabs/string/in.nix b/src/alejandra/tests/cases/indentation-tabs/string/in.nix new file mode 100644 index 0000000..5a8e97e --- /dev/null +++ b/src/alejandra/tests/cases/indentation-tabs/string/in.nix @@ -0,0 +1,6 @@ +[ + '' + foo + bar + '' +] diff --git a/src/alejandra/tests/cases/indentation-tabs/string/out.nix b/src/alejandra/tests/cases/indentation-tabs/string/out.nix new file mode 100644 index 0000000..ea4686b --- /dev/null +++ b/src/alejandra/tests/cases/indentation-tabs/string/out.nix @@ -0,0 +1,6 @@ +[ + '' + foo + bar + '' +] diff --git a/src/alejandra/tests/fmt.rs b/src/alejandra/tests/fmt.rs index f628f71..cd3d1ae 100644 --- a/src/alejandra/tests/fmt.rs +++ b/src/alejandra/tests/fmt.rs @@ -13,6 +13,7 @@ fn cases() { let configs = HashMap::from([ ("default", Config::default()), ("indentation-tabs", Config { indentation: Indentation::Tabs }), + ("indentation-fourspaces", Config { indentation: Indentation::FourSpaces }), ]); let cases_path = PathBuf::new().join("tests").join("cases");