Skip to content

Commit d0c0cda

Browse files
authored
Merge pull request NixOS#233 from piegamesde/compact-lists
Don't expand lists within functions
2 parents 3e8eef7 + d8caa4c commit d0c0cda

File tree

13 files changed

+103
-212
lines changed

13 files changed

+103
-212
lines changed

src/Nixfmt/Pretty.hs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,34 @@ instance Pretty Parameter where
375375
-- then start on a new line instead".
376376
prettyApp :: Bool -> Doc -> Bool -> Expression -> Expression -> Doc
377377
prettyApp indentFunction pre hasPost f a =
378-
let -- This is very hacky, but selections shouldn't be in a priority group,
378+
let -- Walk the function call chain
379+
absorbApp :: Expression -> Doc
380+
-- This is very hacky, but selections shouldn't be in a priority group,
379381
-- because if they get expanded before anything else,
380382
-- only the `.`-and-after part gets to a new line, which looks very odd
381-
absorbApp (Application f' a'@(Term Selection{})) = group' Transparent (absorbApp f') <> line <> nest (group' RegularG a')
382-
absorbApp (Application f' a') = group' Transparent (absorbApp f') <> line <> nest (group' Priority a')
383+
absorbApp (Application f' a'@(Term Selection{})) = group' Transparent (absorbApp f') <> line <> nest (group' RegularG $ absorbInner a')
384+
absorbApp (Application f' a') = group' Transparent (absorbApp f') <> line <> nest (group' Priority $ absorbInner a')
385+
-- First argument
383386
absorbApp expr
384387
| indentFunction && null comment' = nest $ group' RegularG $ line' <> pretty expr
385388
| otherwise = pretty expr
386389

390+
-- Render the inner arguments of a function call
391+
absorbInner :: Expression -> Doc
392+
-- If lists have only simple items, try to render them single-line instead of expanding
393+
-- This is just a copy of the list rendering code, but with `sepBy line` instead of `sepBy hardline`
394+
absorbInner (Term (List paropen@Ann{trailComment = post'} items parclose))
395+
| length (unItems items) <= 4 && all (isSimple . Term) items =
396+
pretty (paropen{trailComment = Nothing})
397+
<> surroundWith sur (nest $ pretty post' <> sepBy line (unItems items))
398+
<> pretty parclose
399+
where
400+
-- If the brackets are on different lines, keep them like that
401+
sur = if sourceLine paropen /= sourceLine parclose then hardline else line
402+
absorbInner expr = pretty expr
403+
404+
-- Render the last argument of a function call
405+
absorbLast :: Expression -> Doc
387406
absorbLast (Term t)
388407
| isAbsorbable t =
389408
group' Priority $ nest $ prettyTerm t
@@ -522,7 +541,11 @@ absorbExpr _ expr = pretty expr
522541
-- Render the RHS value of an assignment or function parameter default value
523542
absorbRHS :: Expression -> Doc
524543
absorbRHS expr = case expr of
525-
-- Absorbable expression. Always start on the same line
544+
-- Exception to the case below: Don't force-expand attrsets if they only contain a single inherit statement
545+
(Term (Set _ _ binders _))
546+
| case unItems binders of [Item (Inherit{})] -> True; _ -> False ->
547+
hardspace <> group (absorbExpr False expr)
548+
-- Absorbable expression. Always start on the same line, and force-expand attrsets
526549
_ | isAbsorbableExpr expr -> hardspace <> group (absorbExpr True expr)
527550
-- Parenthesized expression. Same thing as the special case for parenthesized last argument in function calls.
528551
(Term (Parenthesized open expr' close)) -> hardspace <> absorbParen open expr' close

src/Nixfmt/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ data Item a
122122
Comments Trivia
123123
deriving (Foldable, Show, Functor)
124124

125-
newtype Items a = Items {unItems :: [Item a]} deriving (Functor)
125+
newtype Items a = Items {unItems :: [Item a]} deriving (Functor, Foldable)
126126

127127
instance (Eq a) => Eq (Items a) where
128128
(==) = (==) `on` concatMap Data.Foldable.toList . unItems

test/diff/apply/in.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
6666
}
6767
[
68-
(mapAttrsToStringsSep [force long] "\n" mkSection attrsOfAttrs)
68+
(mapAttrsToStringsSep [force /* meow */ long] "\n" mkSection attrsOfAttrs)
6969
]
7070
(a
7171
b)

test/diff/apply/out-pure.nix

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
}
7272
[
7373
(mapAttrsToStringsSep [
74-
force
74+
force # meow
7575
long
7676
] "\n" mkSection attrsOfAttrs)
7777
]
@@ -160,16 +160,10 @@
160160
''"''
161161
"\${"
162162
];
163-
escapeMultiline =
164-
libStr.replaceStrings
165-
[
166-
"\${"
167-
"''"
168-
]
169-
[
170-
"''\${"
171-
"'''"
172-
];
163+
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [
164+
"''\${"
165+
"'''"
166+
];
173167
test =
174168
foo
175169
[

test/diff/apply/out.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
}
7272
[
7373
(mapAttrsToStringsSep [
74-
force
74+
force # meow
7575
long
7676
] "\n" mkSection attrsOfAttrs)
7777
]

test/diff/idioms_lib_3/out-pure.nix

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,7 @@ rec {
130130
toINI =
131131
{
132132
# apply transformations (e.g. escapes) to section names
133-
mkSectionName ? (
134-
name:
135-
libStr.escape [
136-
"["
137-
"]"
138-
] name
139-
),
133+
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
140134
# format a setting line from key and value
141135
mkKeyValue ? mkKeyValueDefault { } "=",
142136
# allow lists as values for duplicate keys
@@ -191,13 +185,7 @@ rec {
191185
toINIWithGlobalSection =
192186
{
193187
# apply transformations (e.g. escapes) to section names
194-
mkSectionName ? (
195-
name:
196-
libStr.escape [
197-
"["
198-
"]"
199-
] name
200-
),
188+
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
201189
# format a setting line from key and value
202190
mkKeyValue ? mkKeyValueDefault { } "=",
203191
# allow lists as values for duplicate keys
@@ -378,16 +366,10 @@ rec {
378366
''"''
379367
"\${"
380368
];
381-
escapeMultiline =
382-
libStr.replaceStrings
383-
[
384-
"\${"
385-
"''"
386-
]
387-
[
388-
"''\${"
389-
"'''"
390-
];
369+
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [
370+
"''\${"
371+
"'''"
372+
];
391373
singlelineResult =
392374
''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"'';
393375
multilineResult =

test/diff/idioms_lib_3/out.nix

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,7 @@ rec {
133133
toINI =
134134
{
135135
# apply transformations (e.g. escapes) to section names
136-
mkSectionName ? (
137-
name:
138-
libStr.escape [
139-
"["
140-
"]"
141-
] name
142-
),
136+
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
143137
# format a setting line from key and value
144138
mkKeyValue ? mkKeyValueDefault { } "=",
145139
# allow lists as values for duplicate keys
@@ -194,13 +188,7 @@ rec {
194188
toINIWithGlobalSection =
195189
{
196190
# apply transformations (e.g. escapes) to section names
197-
mkSectionName ? (
198-
name:
199-
libStr.escape [
200-
"["
201-
"]"
202-
] name
203-
),
191+
mkSectionName ? (name: libStr.escape [ "[" "]" ] name),
204192
# format a setting line from key and value
205193
mkKeyValue ? mkKeyValueDefault { } "=",
206194
# allow lists as values for duplicate keys
@@ -391,16 +379,10 @@ rec {
391379
''"''
392380
"\${"
393381
];
394-
escapeMultiline =
395-
libStr.replaceStrings
396-
[
397-
"\${"
398-
"''"
399-
]
400-
[
401-
"''\${"
402-
"'''"
403-
];
382+
escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [
383+
"''\${"
384+
"'''"
385+
];
404386
singlelineResult =
405387
''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"'';
406388
multilineResult =

test/diff/idioms_lib_4/out-pure.nix

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -521,33 +521,25 @@ rec {
521521
# the normalized name for macOS.
522522
macos = {
523523
execFormat = macho;
524-
families = {
525-
inherit darwin;
526-
};
524+
families = { inherit darwin; };
527525
name = "darwin";
528526
};
529527
ios = {
530528
execFormat = macho;
531-
families = {
532-
inherit darwin;
533-
};
529+
families = { inherit darwin; };
534530
};
535531
# A tricky thing about FreeBSD is that there is no stable ABI across
536532
# versions. That means that putting in the version as part of the
537533
# config string is paramount.
538534
freebsd12 = {
539535
execFormat = elf;
540-
families = {
541-
inherit bsd;
542-
};
536+
families = { inherit bsd; };
543537
name = "freebsd";
544538
version = 12;
545539
};
546540
freebsd13 = {
547541
execFormat = elf;
548-
families = {
549-
inherit bsd;
550-
};
542+
families = { inherit bsd; };
551543
name = "freebsd";
552544
version = 13;
553545
};
@@ -557,19 +549,15 @@ rec {
557549
};
558550
netbsd = {
559551
execFormat = elf;
560-
families = {
561-
inherit bsd;
562-
};
552+
families = { inherit bsd; };
563553
};
564554
none = {
565555
execFormat = unknown;
566556
families = { };
567557
};
568558
openbsd = {
569559
execFormat = elf;
570-
families = {
571-
inherit bsd;
572-
};
560+
families = { inherit bsd; };
573561
};
574562
solaris = {
575563
execFormat = elf;

test/diff/idioms_lib_4/out.nix

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -521,33 +521,25 @@ rec {
521521
# the normalized name for macOS.
522522
macos = {
523523
execFormat = macho;
524-
families = {
525-
inherit darwin;
526-
};
524+
families = { inherit darwin; };
527525
name = "darwin";
528526
};
529527
ios = {
530528
execFormat = macho;
531-
families = {
532-
inherit darwin;
533-
};
529+
families = { inherit darwin; };
534530
};
535531
# A tricky thing about FreeBSD is that there is no stable ABI across
536532
# versions. That means that putting in the version as part of the
537533
# config string is paramount.
538534
freebsd12 = {
539535
execFormat = elf;
540-
families = {
541-
inherit bsd;
542-
};
536+
families = { inherit bsd; };
543537
name = "freebsd";
544538
version = 12;
545539
};
546540
freebsd13 = {
547541
execFormat = elf;
548-
families = {
549-
inherit bsd;
550-
};
542+
families = { inherit bsd; };
551543
name = "freebsd";
552544
version = 13;
553545
};
@@ -557,19 +549,15 @@ rec {
557549
};
558550
netbsd = {
559551
execFormat = elf;
560-
families = {
561-
inherit bsd;
562-
};
552+
families = { inherit bsd; };
563553
};
564554
none = {
565555
execFormat = unknown;
566556
families = { };
567557
};
568558
openbsd = {
569559
execFormat = elf;
570-
families = {
571-
inherit bsd;
572-
};
560+
families = { inherit bsd; };
573561
};
574562
solaris = {
575563
execFormat = elf;

test/diff/idioms_nixos_1/out-pure.nix

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,7 @@ in
284284
})
285285

286286
(mkIf (!config.boot.isContainer) {
287-
system.build = {
288-
inherit kernel;
289-
};
287+
system.build = { inherit kernel; };
290288

291289
system.modulesTree = [ kernel ] ++ config.boot.extraModulePackages;
292290

0 commit comments

Comments
 (0)