Skip to content

Commit

Permalink
Strings: Fix single-line strings with double single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed Nov 29, 2023
1 parent 62fe12f commit 951261e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/diff/idioms_nixos_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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}"'';
Expand Down
6 changes: 2 additions & 4 deletions test/diff/string_interpol/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
)
Expand Down

0 comments on commit 951261e

Please sign in to comment.