Skip to content

Commit 42e43d9

Browse files
authored
Merge pull request #355 from dyegoaurelio/lang-annotation-in-list-argument
Fix language annotations inside lists on function arguments
2 parents b672593 + f4bbd8c commit 42e43d9

File tree

4 files changed

+69
-25
lines changed

4 files changed

+69
-25
lines changed

src/Nixfmt/Pretty.hs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ instance (Pretty a) => Pretty (Item a) where
109109
pretty (Comments trivia) = pretty trivia
110110
pretty (Item x) = group x
111111

112-
-- For lists, attribute sets and let bindings
113-
prettyItems :: (Pretty a) => Items a -> Doc
114-
prettyItems (Items items) = go items
112+
-- Render items (for lists, attribute sets and let bindings) separated by the given separator
113+
renderItems :: (Pretty a) => Doc -> Items a -> Doc
114+
renderItems separator (Items items) = go items
115115
where
116116
go [] = mempty
117117
go [item] = pretty item
@@ -120,9 +120,22 @@ prettyItems (Items items) = go items
120120
pretty (LanguageAnnotation lang)
121121
<> hardspace
122122
<> group stringItem
123-
<> if null rest then mempty else hardline <> go rest
123+
<> if null rest then mempty else separator <> go rest
124124
go (item : rest) =
125-
pretty item <> if null rest then mempty else hardline <> go rest
125+
pretty item <> if null rest then mempty else separator <> go rest
126+
127+
-- Render a list given the separator between items
128+
renderList :: Doc -> Ann Token -> Items Term -> Ann Token -> Doc
129+
renderList itemSep paropen@Ann{trailComment = post} items parclose =
130+
pretty (paropen{trailComment = Nothing})
131+
<> surroundWith sur (nest $ pretty post <> renderItems itemSep items)
132+
<> pretty parclose
133+
where
134+
-- If the brackets are on different lines, keep them like that
135+
sur
136+
| sourceLine paropen /= sourceLine parclose = hardline
137+
| null $ unItems items = hardspace
138+
| otherwise = line
126139

127140
instance Pretty Trivia where
128141
pretty [] = mempty
@@ -197,7 +210,7 @@ prettySet _ (krec, paropen@(LoneAnn _), Items [], parclose@Ann{preTrivia = []})
197210
-- Singleton sets are allowed to fit onto one line,
198211
-- but apart from that always expand.
199212
prettySet wide (krec, paropen@Ann{trailComment = post}, binders, parclose) =
200-
let !surrounded = surroundWith sep (nest $ pretty post <> prettyItems binders)
213+
let !surrounded = surroundWith sep (nest $ pretty post <> renderItems hardline binders)
201214
in pretty (fmap (,hardspace) krec)
202215
<> pretty (paropen{trailComment = Nothing})
203216
<> surrounded
@@ -243,13 +256,8 @@ prettyTerm (List paropen@Ann{trailComment = Nothing} (Items []) parclose@Ann{pre
243256
sep = if sourceLine paropen /= sourceLine parclose then hardline else hardspace
244257
-- General list
245258
-- Always expand if len > 1
246-
prettyTerm (List paropen@Ann{trailComment = post} items parclose) =
247-
pretty (paropen{trailComment = Nothing})
248-
<> surroundWith sur (nest $ pretty post <> prettyItems items)
249-
<> pretty parclose
250-
where
251-
-- If the brackets are on different lines, keep them like that
252-
sur = if sourceLine paropen /= sourceLine parclose then hardline else line
259+
prettyTerm (List paropen items parclose) =
260+
renderList hardline paropen items parclose
253261
prettyTerm (Set krec paropen items parclose) = prettySet False (krec, paropen, items, parclose)
254262
-- Parentheses
255263
prettyTerm (Parenthesized paropen expr parclose@Ann{preTrivia = closePre}) =
@@ -428,18 +436,10 @@ prettyApp indentFunction pre hasPost f a =
428436
-- Render the inner arguments of a function call
429437
absorbInner :: Expression -> Doc
430438
-- If lists have only simple items, try to render them single-line instead of expanding
431-
-- This is just a copy of the list rendering code, but with `sepBy line` instead of `sepBy hardline`
432-
absorbInner (Term (List paropen@Ann{trailComment = post'} items parclose))
439+
-- Uses renderList with `line` separator instead of `hardline`
440+
absorbInner (Term (List paropen items parclose))
433441
| length (unItems items) <= 6 && all (isSimple . Term) items =
434-
pretty (paropen{trailComment = Nothing})
435-
<> surroundWith sur (nest $ pretty post' <> sepBy line (unItems items))
436-
<> pretty parclose
437-
where
438-
-- If the brackets are on different lines, keep them like that
439-
sur
440-
| sourceLine paropen /= sourceLine parclose = hardline
441-
| null $ unItems items = hardspace
442-
| otherwise = line
442+
renderList line paropen items parclose
443443
absorbInner expr = pretty expr
444444

445445
-- Render the last argument of a function call
@@ -696,7 +696,7 @@ instance Pretty Expression where
696696
convertTrailing (Just (TrailingComment t)) = [LineComment (" " <> t)]
697697

698698
letPart = group $ pretty let_ <> hardline <> letBody
699-
letBody = nest $ prettyItems binders
699+
letBody = nest $ renderItems hardline binders
700700
inPart =
701701
group $
702702
pretty in_

test/diff/language-annotation/in.nix

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@
7070
"console.log('Script 4');"
7171
];
7272

73+
# Language annotation in list on function argument
74+
runScripts = (lib.mkSomething [
75+
/* bash */ ''
76+
echo "Script A"
77+
''
78+
][
79+
/* python */ ''
80+
print("Script B")
81+
''
82+
/* ruby */ "puts 'Script C'"
83+
]);
84+
7385
aboveString =
7486
/* bash */
7587
"echo 'Above string'";

test/diff/language-annotation/out-pure.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@
7474
/* js */ "console.log('Script 4');"
7575
];
7676

77+
# Language annotation in list on function argument
78+
runScripts = (
79+
lib.mkSomething
80+
[
81+
/* bash */ ''
82+
echo "Script A"
83+
''
84+
]
85+
[
86+
/* python */ ''
87+
print("Script B")
88+
''
89+
/* ruby */ "puts 'Script C'"
90+
]
91+
);
92+
7793
aboveString = /* bash */ "echo 'Above string'";
7894

7995
# Language annotation in attribute set

test/diff/language-annotation/out.nix

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@
7474
/* js */ "console.log('Script 4');"
7575
];
7676

77+
# Language annotation in list on function argument
78+
runScripts = (
79+
lib.mkSomething
80+
[
81+
/* bash */ ''
82+
echo "Script A"
83+
''
84+
]
85+
[
86+
/* python */ ''
87+
print("Script B")
88+
''
89+
/* ruby */ "puts 'Script C'"
90+
]
91+
);
92+
7793
aboveString = /* bash */ "echo 'Above string'";
7894

7995
# Language annotation in attribute set

0 commit comments

Comments
 (0)