From 951261e4e60a02bdfe8c0313bb03634bf02507e8 Mon Sep 17 00:00:00 2001 From: piegames Date: Wed, 29 Nov 2023 21:01:02 +0100 Subject: [PATCH] Strings: Fix single-line strings with double single quotes --- src/Nixfmt/Pretty.hs | 6 +++++- test/diff/idioms_nixos_2/out.nix | 5 +++-- test/diff/string_interpol/out.nix | 6 ++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 9150255c..a38a4806 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -702,7 +702,11 @@ prettySimpleString parts = group $ prettyIndentedString :: [[StringPart]] -> Doc prettyIndentedString parts = group $ base $ - text "''" <> line' + text "''" + -- Usually the `''` is followed by a potential line break. + -- However, for single-line strings it should be omitted, because often times a line break will + -- not reduce the indentation at all + <> (case parts of { _:_:_ -> line'; _ -> mempty }) <> nest 2 (sepBy newline (map (prettyLine escape unescapeInterpol) parts)) <> text "''" where escape = replaceMultiple diff --git a/test/diff/idioms_nixos_2/out.nix b/test/diff/idioms_nixos_2/out.nix index d54c2fc9..79aeb15c 100644 --- a/test/diff/idioms_nixos_2/out.nix +++ b/test/diff/idioms_nixos_2/out.nix @@ -998,8 +998,9 @@ in # will be omitted. ${if c.dbname != null then "--database-name" else null} = ''"${c.dbname}"''; ${if c.dbhost != null then "--database-host" else null} = ''"${c.dbhost}"''; - ${if c.dbport != null then "--database-port" else null} = '' - "${toString c.dbport}"''; + ${ + if c.dbport != null then "--database-port" else null + } = ''"${toString c.dbport}"''; ${if c.dbuser != null then "--database-user" else null} = ''"${c.dbuser}"''; "--database-pass" = ''"''$${dbpass.arg}"''; "--admin-user" = ''"${c.adminuser}"''; diff --git a/test/diff/string_interpol/out.nix b/test/diff/string_interpol/out.nix index 50e89762..27605563 100644 --- a/test/diff/string_interpol/out.nix +++ b/test/diff/string_interpol/out.nix @@ -27,8 +27,7 @@ map ( e: - '' - iifname "${cfg.upstreamIface}" tcp dport ${builtins.toString e.sourcePort} dnat to ${e.destination}'' + ''iifname "${cfg.upstreamIface}" tcp dport ${builtins.toString e.sourcePort} dnat to ${e.destination}'' ) tcpPortMap ) @@ -39,8 +38,7 @@ map ( e: - '' - ifname "${cfg.upstreamIface}" udp dport ${builtins.toString e.sourcePort} dnat to ${e.destination}'' + ''ifname "${cfg.upstreamIface}" udp dport ${builtins.toString e.sourcePort} dnat to ${e.destination}'' ) udpPortMap )