Skip to content

Commit

Permalink
String interpolation: Indentation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed Mar 1, 2024
1 parent 25792d4 commit 19a377b
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,25 @@ instance Pretty StringPart where

instance Pretty [StringPart] where
-- When the interpolation is the only thing on the string line,
-- then absorb the content (i.e. don't surround with line')
-- then absorb the content (i.e. don't surround with line').
-- Only do this when there are no comments
pretty [Interpolation (Whole expr [])]
= group $ text "${" <> nest inner <> text "}"
where
inner = case expr of
-- Start on the same line for these
_ | isAbsorbableExpr expr -> group $ absorbExpr False expr
-- Parenthesized application
(Application f a) -> prettyApp True mempty True f a
-- Same thing for selections
(Term (Selection t _)) | isAbsorbable t -> line' <> group expr <> line'
(Term (Selection _ _)) -> group expr <> line'
-- Start on a new line for the others
_ -> line' <> group expr <> line'

-- Fallback case: there are some comments around it. Always surround with line' then
pretty [Interpolation expr]
= group $ text "${" <> pretty expr <> text "}"
= group $ text "${" <> surroundWith line' (nest expr) <> text "}"

-- If we split a string line over multiple code lines due to large
-- interpolations, make sure to indent based on the indentation of the line
Expand Down
53 changes: 53 additions & 0 deletions test/diff/string_interpol/in.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
"${/*a*/"${/*b*/"${c}"}"/*d*/}"
''${/*a*/''${/*b*/''${c}''}''/*d*/}''
{
ExecStart = "${pkgs.openarena}/bin/oa_ded +set fs_basepath ${pkgs.openarena}/openarena-0.8.8 +set fs_homepath /var/lib/openarena ${
concatMapStringsSep (x: x) " " cfg.extraFlags
Expand All @@ -26,4 +27,56 @@
}
'';
}
{
system.nixos.versionSuffix1 = ".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified
or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}.${self.shortRev or "dirty"}";

system.nixos.versionSuffix2 = ".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified
or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}";

system.nixos.versionSuffix3 = "${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified
or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}";
}
(
system nixos versionSuffix1 ".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified
or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}.${self.shortRev or "dirty"}"
)
(
system nixos versionSuffix2 ".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified
or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}"
)
(
system nixos versionSuffix3 "${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified
or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}"
)
]
58 changes: 54 additions & 4 deletions test/diff/string_interpol/out.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
[
"${
# a
"${
# b
"${c}"}" # d
# a
"${
# b
"${c}"
}" # d
}"
''${
# a
''${
# b
''${c}''
}'' # d
}''
{
ExecStart = "${pkgs.openarena}/bin/oa_ded +set fs_basepath ${pkgs.openarena}/openarena-0.8.8 +set fs_homepath /var/lib/openarena ${
concatMapStringsSep (x: x) " " cfg.extraFlags
Expand Down Expand Up @@ -42,4 +50,46 @@
}
'';
}
{
system.nixos.versionSuffix1 = ".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}.${self.shortRev or "dirty"}";

system.nixos.versionSuffix2 = ".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}";

system.nixos.versionSuffix3 = "${final.substring 0 8 (
self.lastModifiedDate or self.lastModified or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)}";
}
(system nixos versionSuffix1
".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}.${self.shortRev or "dirty"}"
)
(system nixos versionSuffix2
".${
final.substring 0 8 (
self.lastModifiedDate or self.lastModified or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)
}"
)
(system nixos versionSuffix3
"${final.substring 0 8 (
self.lastModifiedDate or self.lastModified or "19700101"
self.lastModifiedDate or self.lastModified or "19700101"
)}"
)
]

0 comments on commit 19a377b

Please sign in to comment.