Skip to content

Commit

Permalink
Put semicolons on same line again
Browse files Browse the repository at this point in the history
  • Loading branch information
piegamesde committed Oct 18, 2023
1 parent 549541f commit 7649a1b
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 158 deletions.
11 changes: 3 additions & 8 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ instance Pretty Binder where
group' False $ line <> pretty l <> line <> group' True (pretty TUpdate <> hardspace <> prettyTermWide t)
-- Case 2b: LHS fits onto first line, RHS is a function application
(Operation l (Ann [] TUpdate Nothing) (Application f a)) ->
line <> (group l) <> line <> prettyApp hardline (pretty TUpdate <> hardspace) mempty hardline f a
line <> (group l) <> line <> prettyApp hardline (pretty TUpdate <> hardspace) mempty mempty f a
-- Special case `++` operations to be more compact in some cases
-- Case 1: two arguments, LHS is absorbable term, RHS fits onto the last line
(Operation (Term t) (Ann [] TConcat Nothing) b) | isAbsorbable t ->
Expand All @@ -155,7 +155,7 @@ instance Pretty Binder where
group' False $ line <> pretty l <> line <> group' True (pretty TConcat <> hardspace <> prettyTermWide t)
-- Case 2b: LHS fits onto first line, RHS is a function application
(Operation l (Ann [] TConcat Nothing) (Application f a)) ->
line <> (group l) <> line <> prettyApp hardline (pretty TConcat <> hardspace) mempty hardline f a
line <> (group l) <> line <> prettyApp hardline (pretty TConcat <> hardspace) mempty mempty f a
-- Everything else:
-- If it fits on one line, it fits
-- If it fits on one line but with a newline after the `=`, it fits (including semicolon)
Expand Down Expand Up @@ -451,11 +451,6 @@ instance Pretty Expression where
group (pretty if_ <> line <> nest 2 (pretty cond) <> line <> pretty then_)
<> (surroundWith line $ nest 2 $ group expr0)
<> pretty else_ <> absorbElse expr1
-- This trailing line' is a bit of a hack. It makes sure that the semicolon in binders gets placed onto
-- a new line if the items ends with a (multiline) if.
-- Normally this should only be the case when in binders as this might interfere with other syntax constructs,
-- but because our style always puts a new line after multiline Ifs it turns out to work just fine ^^
<> line'

pretty (Abstraction (IDParameter param) colon body)
= pretty param <> pretty colon <> absorbAbs 1 body
Expand Down Expand Up @@ -503,7 +498,7 @@ instance Pretty Expression where
line <> pretty (moveTrailingCommentUp op') <> nest 2 (absorbOperation expr)
in
group' False $
((concat . map prettyOperation . (flatten Nothing)) operation) <> line'
(concat . map prettyOperation . (flatten Nothing)) operation

pretty (MemberCheck expr qmark sel)
= pretty expr <> softline
Expand Down
9 changes: 3 additions & 6 deletions test/diff/attr_set/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@
++ (optionals condition [
more
items
])
;
]);
b = with pkgs; [
a
lot
Expand Down Expand Up @@ -195,8 +194,7 @@
secret-config.ssh-hosts
// {
foo = "bar";
}
;
};
programs.ssh.knownHosts3 =
lib.mapAttrs (
host_name: publicKey: {
Expand Down Expand Up @@ -225,8 +223,7 @@
)
// {
foo = "bar";
}
;
};
programs.ssh.knownHosts5 =
someStuff
// lib.mapAttrs (
Expand Down
24 changes: 8 additions & 16 deletions test/diff/idioms_lib_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ rec {
if pathExists suffixFile then
lib.strings.fileContents suffixFile
else
"pre-git"
;
"pre-git";

/* Attempts to return the the current revision of nixpkgs and
returns the supplied default value otherwise.
Expand All @@ -216,8 +215,7 @@ rec {
else if lib.pathExists revisionFile then
lib.fileContents revisionFile
else
default
;
default;

nixpkgsVersion =
builtins.trace "`lib.nixpkgsVersion` is deprecated, use `lib.version` instead!"
Expand Down Expand Up @@ -263,8 +261,7 @@ rec {
else if a > b then
1
else
0
;
0;

/* Split type into two subtypes by predicate `p`, take all elements
of the first subtype to be less than all the elements of the
Expand Down Expand Up @@ -300,8 +297,7 @@ rec {
else if p b then
1
else
no a b
;
no a b;

/* Reads a JSON file.
Expand Down Expand Up @@ -352,8 +348,7 @@ rec {
abort "NIX_ABORT_ON_WARN=true; warnings are treated as unrecoverable errors."
)
else
msg: builtins.trace "warning: ${msg}"
;
msg: builtins.trace "warning: ${msg}";

/* Like warn, but only warn when the first argument is `true`.
Expand Down Expand Up @@ -436,8 +431,7 @@ rec {
if f ? __functor then
f.__functionArgs or (lib.functionArgs (f.__functor f))
else
builtins.functionArgs f
;
builtins.functionArgs f;

/* Check whether something is a function or something
annotated with function args.
Expand Down Expand Up @@ -470,8 +464,7 @@ rec {
"14" = "E";
"15" = "F";
}
.${toString d}
;
.${toString d};
in
lib.concatMapStrings toHexDigit (toBaseDigits 16 i);

Expand All @@ -496,8 +489,7 @@ rec {
r = i - ((i / base) * base);
q = (i - r) / base;
in
[ r ] ++ go q
;
[ r ] ++ go q;
in
assert (base >= 2);
assert (i >= 0);
Expand Down
42 changes: 14 additions & 28 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ rec {
else if isFloat v then
libStr.floatToString v
else
err "this value is" (toString v)
;
err "this value is" (toString v);

# Generate a line of key k and value v, separated by
# character sep. If sep appears in k, it is escaped.
Expand Down Expand Up @@ -103,8 +102,7 @@ rec {
if listsAsDuplicateKeys then
k: v: map (mkLine k) (if lib.isList v then v else [ v ])
else
k: v: [ (mkLine k v) ]
;
k: v: [ (mkLine k v) ];
in
attrs:
libStr.concatStrings (lib.concatLists (libAttr.mapAttrsToList mkLines attrs));
Expand Down Expand Up @@ -156,8 +154,7 @@ rec {
''
[${mkSectionName sectName}]
''
+ toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues
;
+ toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
in
# map input to ini sections
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
Expand Down Expand Up @@ -216,8 +213,7 @@ rec {
else
(toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } globalSection) + "\n"
)
+ (toINI { inherit mkSectionName mkKeyValue listsAsDuplicateKeys; } sections)
;
+ (toINI { inherit mkSectionName mkKeyValue listsAsDuplicateKeys; } sections);

# Generate a git-config file from an attrset.
#
Expand Down Expand Up @@ -252,8 +248,7 @@ rec {
if containsQuote || subsections == [ ] then
name
else
''${section} "${subsection}"''
;
''${section} "${subsection}"'';

# generation for multiple ini values
mkKeyValue =
Expand All @@ -273,8 +268,7 @@ rec {
else if length path > 1 then
{ ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value; }
else
{ ${head path} = value; }
;
{ ${head path} = value; };
in
attrs:
lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs));
Expand Down Expand Up @@ -321,8 +315,7 @@ rec {
else
const "<unevaluated>"
else
id
;
id;
mapAny =
with builtins;
depth: v:
Expand All @@ -334,8 +327,7 @@ rec {
else if isList v then
map evalNext v
else
transform (depth + 1) v
;
transform (depth + 1) v;
in
mapAny 0;

Expand Down Expand Up @@ -368,16 +360,14 @@ rec {
${indent} ''
else
" "
;
" ";
outroSpace =
if multiline then
''
${indent}''
else
" "
;
" ";
in
if isInt v then
toString v
Expand Down Expand Up @@ -417,8 +407,7 @@ rec {
+ introSpace
+ concatStringsSep introSpace (lib.init escapedLines)
+ (if lastLine == "" then outroSpace else introSpace + lastLine)
+ "''"
;
+ "''";
in
if multiline && length lines > 1 then multilineResult else singlelineResult
else if true == v then
Expand Down Expand Up @@ -473,8 +462,7 @@ rec {
+ outroSpace
+ "}"
else
abort "generators.toPretty: should never happen (v = ${v})"
;
abort "generators.toPretty: should never happen (v = ${v})";
in
go indent;

Expand Down Expand Up @@ -502,8 +490,7 @@ rec {
else if isFloat x then
float ind x
else
abort "generators.toPlist: should never happen (v = ${v})"
;
abort "generators.toPlist: should never happen (v = ${v})";

literal = ind: x: ind + x;

Expand Down Expand Up @@ -586,6 +573,5 @@ rec {
else if v == null then
abort "generators.toDhall: cannot convert a null to Dhall"
else
builtins.toJSON v
;
builtins.toJSON v;
}
Loading

0 comments on commit 7649a1b

Please sign in to comment.