From 19a377b74da57e201a4509cb0492fe537915969b Mon Sep 17 00:00:00 2001 From: piegames Date: Fri, 1 Mar 2024 18:07:15 +0100 Subject: [PATCH] String interpolation: Indentation fixes --- src/Nixfmt/Pretty.hs | 20 +++++++++-- test/diff/string_interpol/in.nix | 53 ++++++++++++++++++++++++++++ test/diff/string_interpol/out.nix | 58 ++++++++++++++++++++++++++++--- 3 files changed, 125 insertions(+), 6 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index c9601a5c..09da42e9 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -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 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" + )}" + ) ]