Skip to content

Commit

Permalink
Don't absort in body anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed May 17, 2023
1 parent d1ba3cd commit e6bed2a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 30 deletions.
12 changes: 1 addition & 11 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,6 @@ absorb left right (Just level) x
absorbSet :: Expression -> Doc
absorbSet = absorb line mempty Nothing

-- What is allowed to come on the same line as `in`?
-- Absorbable terms like sets
-- if, with, let
absorbIn :: Expression -> Doc
absorbIn (Term t) | isAbsorbable t = hardspace <> prettyTerm t <> hardspace
absorbIn x@(If _ _ _ _ _ _) = group x
absorbIn x@(With _ _ _ _) = group x
absorbIn x@(Let _ _ _ _) = group x
absorbIn x = line <> group x <> line

-- Only absorb "else if"
absorbElse :: Expression -> Doc
absorbElse (If if_ cond then_ expr0 else_ expr1)
Expand Down Expand Up @@ -245,7 +235,7 @@ instance Pretty Expression where
(Ann in_ inTrailing inLeading) expr)
= base $ group letPart <> line <> inPart
where letPart = pretty let_ <> pretty letTrailing <> hardline <> letBody
inPart = pretty in_ <> hardspace <> absorbIn expr
inPart = pretty in_ <> line <> group expr <> line
letBody = nest 2 $
pretty letLeading
<> sepBy hardline binders
Expand Down
6 changes: 4 additions & 2 deletions test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ rec {
versionSuffix =
let
suffixFile = ../.version-suffix;
in if pathExists suffixFile then
in
if pathExists suffixFile then
lib.strings.fileContents suffixFile
else
"pre-git"
Expand All @@ -243,7 +244,8 @@ rec {
let
revisionFile = "${toString ./..}/.git-revision";
gitRepo = "${toString ./..}/.git";
in if lib.pathIsGitRepo gitRepo then
in
if lib.pathIsGitRepo gitRepo then
lib.commitIdFromGitRepo gitRepo
else if lib.pathExists revisionFile then
lib.fileContents revisionFile
Expand Down
30 changes: 20 additions & 10 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ let

inherit (lib) isFunction;

in rec {
in
rec {

## -- HELPER FUNCTIONS & DEFAULTS --

Expand All @@ -39,7 +40,8 @@ in rec {
abort ("generators.mkValueStringDefault: "
+ "${t} not supported: ${toPretty { } v}")
;
in if isInt v then
in
if isInt v then
toString v
# convert derivations to store paths
else if lib.isDerivation v then
Expand Down Expand Up @@ -252,7 +254,8 @@ in rec {
section = head sections;
subsections = tail sections;
subsection = concatStringsSep "." subsections;
in if containsQuote || subsections == [ ] then
in
if containsQuote || subsections == [ ] then
name
else
''${section} "${subsection}"''
Expand Down Expand Up @@ -346,12 +349,14 @@ in rec {
depth: v:
let
evalNext = x: mapAny (depth + 1) (transform (depth + 1) x);
in if isAttrs v then
in
if isAttrs v then
mapAttrs (stepIntoAttr evalNext) v
else if isList v then
map evalNext v
else
transform (depth + 1) v;
transform (depth + 1) v
;
in
mapAny 0
;
Expand Down Expand Up @@ -395,7 +400,8 @@ in rec {
else
" "
;
in if isInt v then
in
if isInt v then
toString v
# toString loses precision on floats, so we use toJSON instead. This isn't perfect
# as the resulting string may not parse back as a float (e.g. 42, 1e-06), but for
Expand Down Expand Up @@ -433,7 +439,8 @@ in rec {
else
introSpace + lastLine) + "''"
;
in if multiline && length lines > 1 then
in
if multiline && length lines > 1 then
multilineResult
else
singlelineResult
Expand Down Expand Up @@ -461,7 +468,8 @@ in rec {
name + "?"
else
name) fna);
in if fna == { } then
in
if fna == { } then
"<function>"
else
"<function, args: {${showFnas}}>"
Expand Down Expand Up @@ -566,7 +574,8 @@ in rec {
]) x))
;

in ''
in
''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
Expand All @@ -583,7 +592,8 @@ in rec {
with builtins;
let
concatItems = lib.strings.concatStringsSep ", ";
in if isAttrs v then
in
if isAttrs v then
"{ ${
concatItems (lib.attrsets.mapAttrsToList
(key: value: "${key} = ${toDhall args value}") v)
Expand Down
3 changes: 2 additions & 1 deletion test/diff/idioms_nixos_1/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ let
${concatStringsSep "\n" config.boot.kernelModules}
'';

in {
in
{

###### interface

Expand Down
12 changes: 8 additions & 4 deletions test/diff/idioms_nixos_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ let

inherit (config.system) stateVersion;

in {
in
{

imports = [
(mkRemovedOptionModule [
Expand Down Expand Up @@ -878,7 +879,8 @@ in {
renderedAppStoreSetting =
let
x = cfg.appstoreEnable;
in if x == null then
in
if x == null then
"false"
else
boolToString x
Expand Down Expand Up @@ -1052,7 +1054,8 @@ in {
"--admin-pass" = ''"''$${adminpass.arg}"'';
"--data-dir" = ''"${datadir}/data"'';
});
in ''
in
''
${mkExport dbpass}
${mkExport adminpass}
${occ}/bin/nextcloud-occ maintenance:install \
Expand All @@ -1064,7 +1067,8 @@ in {
${toString i} --value="${toString v}"
'') ([ cfg.hostName ] ++ cfg.config.extraTrustedDomains));

in {
in
{
wantedBy = [ "multi-user.target" ];
before = [ "phpfpm-nextcloud.service" ];
path = [ occ ];
Expand Down
3 changes: 2 additions & 1 deletion test/diff/let_in/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ let

a =
let
in [
in
[
1
2
]
Expand Down
3 changes: 2 additions & 1 deletion test/diff/monsters_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ let
${concatStringsSep "\n" config.boot.kernelModules}
'';

in {
in
{

###### interface

Expand Down

0 comments on commit e6bed2a

Please sign in to comment.