diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index c9601a5c..70fce906 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -656,9 +656,26 @@ 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 + -- Code copied over from parentheses. Could be factored out into a common function one day + 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 diff --git a/test/diff/string_interpol/in.nix b/test/diff/string_interpol/in.nix index a861ff8e..01c0d0dd 100644 --- a/test/diff/string_interpol/in.nix +++ b/test/diff/string_interpol/in.nix @@ -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 @@ -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" + ) + }" + ) ] diff --git a/test/diff/string_interpol/out.nix b/test/diff/string_interpol/out.nix index ed65b6bf..643949e4 100644 --- a/test/diff/string_interpol/out.nix +++ b/test/diff/string_interpol/out.nix @@ -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 @@ -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" + )}" + ) ]