Skip to content

Commit a463903

Browse files
authored
Merge pull request #269 from piegamesde/final-polish
Final polish
2 parents 91939f4 + 622ea8c commit a463903

File tree

13 files changed

+161
-122
lines changed

13 files changed

+161
-122
lines changed

src/Nixfmt/Parser.hs

+1
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ stripIndentation parts = case commonIndentation $ mapMaybe lineHead parts of
292292
Nothing -> map (const []) parts
293293
Just indentation -> map (stripParts indentation) parts
294294

295+
-- Merge adjacent string parts which resulted as parsing artifacts
295296
normalizeLine :: [StringPart] -> [StringPart]
296297
normalizeLine [] = []
297298
normalizeLine (TextPart "" : xs) = normalizeLine xs

src/Nixfmt/Pretty.hs

+24-5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import Nixfmt.Types (
6262
mapLastToken',
6363
tokenText,
6464
)
65+
import Nixfmt.Util (isSpaces)
6566
import Prelude hiding (String)
6667

6768
toLineComment :: TrailingComment -> Trivium
@@ -412,7 +413,10 @@ prettyApp indentFunction pre hasPost f a =
412413
<> pretty parclose
413414
where
414415
-- If the brackets are on different lines, keep them like that
415-
sur = if sourceLine paropen /= sourceLine parclose then hardline else line
416+
sur
417+
| sourceLine paropen /= sourceLine parclose = hardline
418+
| null $ unItems items = hardspace
419+
| otherwise = line
416420
absorbInner expr = pretty expr
417421

418422
-- Render the last argument of a function call
@@ -536,6 +540,12 @@ isAbsorbable (Path _) = True
536540
-- Non-empty sets and lists
537541
isAbsorbable (Set _ _ (Items (_ : _)) _) = True
538542
isAbsorbable (List _ (Items (_ : _)) _) = True
543+
-- Empty sets and lists if they have a line break
544+
-- https://github.com/NixOS/nixfmt/issues/253
545+
isAbsorbable (Set _ (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2}))
546+
| line1 /= line2 = True
547+
isAbsorbable (List (Ann{sourceLine = line1}) (Items []) (Ann{sourceLine = line2}))
548+
| line1 /= line2 = True
539549
isAbsorbable (Parenthesized (LoneAnn _) (Term t) _) = isAbsorbable t
540550
isAbsorbable _ = False
541551

@@ -599,7 +609,11 @@ absorbRHS expr = case expr of
599609
-- Special case `//` and `++` operations to be more compact in some cases
600610
-- Case 1: two arguments, LHS is absorbable term, RHS fits onto the last line
601611
(Operation (Term t) (LoneAnn op) b)
602-
| isAbsorbable t && isUpdateOrConcat op ->
612+
| isAbsorbable t
613+
&& isUpdateOrConcat op
614+
-- Exclude further operations on the RHS
615+
-- Hotfix for https://github.com/NixOS/nixfmt/issues/198
616+
&& case b of (Operation{}) -> False; _ -> True ->
603617
group' RegularG $ line <> group' Priority (prettyTermWide t) <> line <> pretty op <> hardspace <> pretty b
604618
-- Case 2a: LHS fits onto first line, RHS is an absorbable term
605619
(Operation l (LoneAnn op) (Term t))
@@ -768,6 +782,9 @@ isSimple (Term (SimpleString (LoneAnn _))) = True
768782
isSimple (Term (IndentedString (LoneAnn _))) = True
769783
isSimple (Term (Path (LoneAnn _))) = True
770784
isSimple (Term (Token (LoneAnn (Identifier _)))) = True
785+
isSimple (Term (Token (LoneAnn (Integer _)))) = True
786+
isSimple (Term (Token (LoneAnn (Float _)))) = True
787+
isSimple (Term (Token (LoneAnn (EnvPath _)))) = True
771788
isSimple (Term (Selection t selectors def)) =
772789
isSimple (Term t) && all isSimpleSelector selectors && isNothing def
773790
isSimple (Term (Parenthesized (LoneAnn _) e (LoneAnn _))) = isSimple e
@@ -805,11 +822,13 @@ instance Pretty StringPart where
805822
(unexpandSpacing' (Just 30) whole')
806823

807824
instance Pretty [StringPart] where
808-
-- When the interpolation is the only thing on the string line,
825+
-- When the interpolation is the only thing on the string line (ignoring leading whitespace),
809826
-- then absorb the content (i.e. don't surround with line').
810827
-- Only do this when there are no comments
811-
pretty [Interpolation (Whole expr [])] =
812-
group $ text "${" <> nest inner <> text "}"
828+
pretty [Interpolation (Whole expr [])] = pretty [TextPart "", Interpolation (Whole expr [])]
829+
pretty [TextPart pre, Interpolation (Whole expr [])]
830+
| isSpaces pre =
831+
text pre <> offset (textWidth pre) (group $ text "${" <> nest inner <> text "}")
813832
where
814833
-- Code copied over from parentheses. Could be factored out into a common function one day
815834
inner = case expr of

src/Nixfmt/Util.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Nixfmt.Util (
1212
)
1313
where
1414

15-
import Data.Char (isAlpha, isDigit, isSpace)
15+
import Data.Char (isAlpha, isDigit)
1616
import Data.Text as Text (
1717
Text,
1818
all,
@@ -73,7 +73,7 @@ commonPrefix a b =
7373
-- prefix of zero texts is infinite, represented as Nothing.
7474
commonIndentation :: [Text] -> Maybe Text
7575
commonIndentation [] = Nothing
76-
commonIndentation [x] = Just $ Text.takeWhile isSpace x
76+
commonIndentation [x] = Just $ Text.takeWhile (== ' ') x
7777
commonIndentation (x : y : xs) = commonIndentation (commonPrefix x y : xs)
7878

7979
isSpaces :: Text -> Bool

test/diff/apply_with_lists/in.nix

+3
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,7 @@
7575
out = "20170512";
7676
}
7777
] null)
78+
# https://github.com/NixOS/nixfmt/issues/268 regression test. [] and {} should be treated the same
79+
(defaultNullOpts.mkNullable (with types; either str (listOf str)) [] "Some example long text that causes the line to be too long.")
80+
(defaultNullOpts.mkNullable (with types; either str (listOf str)) {} "Some example long text that causes the line to be too long.")
7881
]

test/diff/apply_with_lists/out-pure.nix

+9-11
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,9 @@
1919
name
2020
)
2121
(replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ])
22-
(lists.removePrefix
23-
[
24-
1
25-
2
26-
]
27-
[ ]
28-
)
22+
(lists.removePrefix [ 1 2 ] [ ])
2923
(lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
30-
[
31-
1
32-
2
33-
]
24+
[ 1 2 ]
3425
[ ]
3526
)
3627
(builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ]
@@ -107,4 +98,11 @@
10798
]
10899
null
109100
)
101+
# https://github.com/NixOS/nixfmt/issues/268 regression test. [] and {} should be treated the same
102+
(defaultNullOpts.mkNullable (
103+
with types; either str (listOf str)
104+
) [ ] "Some example long text that causes the line to be too long.")
105+
(defaultNullOpts.mkNullable (
106+
with types; either str (listOf str)
107+
) { } "Some example long text that causes the line to be too long.")
110108
]

test/diff/apply_with_lists/out.nix

+9-11
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,9 @@
1919
name
2020
)
2121
(replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ])
22-
(lists.removePrefix
23-
[
24-
1
25-
2
26-
]
27-
[ ]
28-
)
22+
(lists.removePrefix [ 1 2 ] [ ])
2923
(lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
30-
[
31-
1
32-
2
33-
]
24+
[ 1 2 ]
3425
[ ]
3526
)
3627
(builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ]
@@ -117,4 +108,11 @@
117108
]
118109
null
119110
)
111+
# https://github.com/NixOS/nixfmt/issues/268 regression test. [] and {} should be treated the same
112+
(defaultNullOpts.mkNullable (
113+
with types; either str (listOf str)
114+
) [ ] "Some example long text that causes the line to be too long.")
115+
(defaultNullOpts.mkNullable (
116+
with types; either str (listOf str)
117+
) { } "Some example long text that causes the line to be too long.")
120118
]

test/diff/attr_set/in.nix

+17
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,21 @@
274274
pkgs.xorg.fontadobe75dpi
275275
];
276276
}
277+
# Regression https://github.com/NixOS/nixfmt/issues/253
278+
{
279+
foo1 = {
280+
};
281+
foo2 = bar {
282+
};
283+
foo3 = bar {
284+
} {
285+
};
286+
foo4 = [
287+
];
288+
foo5 = bar [
289+
];
290+
foo6 = bar [
291+
] [
292+
];
293+
}
277294
]

test/diff/attr_set/out-pure.nix

+9
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,13 @@
378378
pkgs.xorg.fontadobe75dpi
379379
];
380380
}
381+
# Regression https://github.com/NixOS/nixfmt/issues/253
382+
{
383+
foo1 = { };
384+
foo2 = bar { };
385+
foo3 = bar { } { };
386+
foo4 = [ ];
387+
foo5 = bar [ ];
388+
foo6 = bar [ ] [ ];
389+
}
381390
]

test/diff/attr_set/out.nix

+23
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,27 @@
381381
pkgs.xorg.fontadobe75dpi
382382
];
383383
}
384+
# Regression https://github.com/NixOS/nixfmt/issues/253
385+
{
386+
foo1 = {
387+
};
388+
foo2 = bar {
389+
};
390+
foo3 =
391+
bar
392+
{
393+
}
394+
{
395+
};
396+
foo4 = [
397+
];
398+
foo5 = bar [
399+
];
400+
foo6 =
401+
bar
402+
[
403+
]
404+
[
405+
];
406+
}
384407
]

test/diff/idioms_nixos_2/out-pure.nix

+21-31
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ let
5151
post_max_size = cfg.maxUploadSize;
5252
memory_limit = cfg.maxUploadSize;
5353
}
54-
// cfg.phpOptions // optionalAttrs cfg.caching.apcu { "apc.enable_cli" = "1"; };
54+
// cfg.phpOptions
55+
// optionalAttrs cfg.caching.apcu { "apc.enable_cli" = "1"; };
5556

5657
occ = pkgs.writeScriptBin "nextcloud-occ" ''
5758
#! ${pkgs.runtimeShell}
@@ -839,11 +840,9 @@ in
839840
'use_ssl' => ${boolToString s3.useSsl},
840841
${optionalString (s3.region != null) "'region' => '${s3.region}',"}
841842
'use_path_style' => ${boolToString s3.usePathStyle},
842-
${
843-
optionalString (
844-
s3.sseCKeyFile != null
845-
) "'sse_c_key' => nix_read_secret('${s3.sseCKeyFile}'),"
846-
}
843+
${optionalString (
844+
s3.sseCKeyFile != null
845+
) "'sse_c_key' => nix_read_secret('${s3.sseCKeyFile}'),"}
847846
],
848847
]
849848
'';
@@ -885,9 +884,8 @@ in
885884
}
886885
$CONFIG = [
887886
'apps_paths' => [
888-
${
889-
optionalString (cfg.extraApps != { })
890-
"[ 'path' => '${cfg.home}/nix-apps', 'url' => '/nix-apps', 'writable' => false ],"
887+
${optionalString (cfg.extraApps != { })
888+
"[ 'path' => '${cfg.home}/nix-apps', 'url' => '/nix-apps', 'writable' => false ],"
891889
}
892890
[ 'path' => '${cfg.home}/apps', 'url' => '/apps', 'writable' => false ],
893891
[ 'path' => '${cfg.home}/store-apps', 'url' => '/store-apps', 'writable' => true ],
@@ -898,37 +896,29 @@ in
898896
${optionalString cfg.caching.apcu "'memcache.local' => '\\OC\\Memcache\\APCu',"}
899897
'log_type' => '${cfg.logType}',
900898
'loglevel' => '${builtins.toString cfg.logLevel}',
901-
${
902-
optionalString (
903-
c.overwriteProtocol != null
904-
) "'overwriteprotocol' => '${c.overwriteProtocol}',"
905-
}
899+
${optionalString (
900+
c.overwriteProtocol != null
901+
) "'overwriteprotocol' => '${c.overwriteProtocol}',"}
906902
${optionalString (c.dbname != null) "'dbname' => '${c.dbname}',"}
907903
${optionalString (c.dbhost != null) "'dbhost' => '${c.dbhost}',"}
908904
${optionalString (c.dbport != null) "'dbport' => '${toString c.dbport}',"}
909905
${optionalString (c.dbuser != null) "'dbuser' => '${c.dbuser}',"}
910-
${
911-
optionalString (
912-
c.dbtableprefix != null
913-
) "'dbtableprefix' => '${toString c.dbtableprefix}',"
914-
}
915-
${
916-
optionalString (c.dbpassFile != null) ''
917-
'dbpassword' => nix_read_secret(
918-
"${c.dbpassFile}"
919-
),
920-
''
921-
}
906+
${optionalString (
907+
c.dbtableprefix != null
908+
) "'dbtableprefix' => '${toString c.dbtableprefix}',"}
909+
${optionalString (c.dbpassFile != null) ''
910+
'dbpassword' => nix_read_secret(
911+
"${c.dbpassFile}"
912+
),
913+
''}
922914
'dbtype' => '${c.dbtype}',
923915
'trusted_domains' => ${
924916
writePhpArray ([ cfg.hostName ] ++ c.extraTrustedDomains)
925917
},
926918
'trusted_proxies' => ${writePhpArray (c.trustedProxies)},
927-
${
928-
optionalString (
929-
c.defaultPhoneRegion != null
930-
) "'default_phone_region' => '${c.defaultPhoneRegion}',"
931-
}
919+
${optionalString (
920+
c.defaultPhoneRegion != null
921+
) "'default_phone_region' => '${c.defaultPhoneRegion}',"}
932922
${optionalString (nextcloudGreaterOrEqualThan "23") "'profile.enabled' => ${boolToString cfg.globalProfiles},"}
933923
${objectstoreConfig}
934924
];

0 commit comments

Comments
 (0)