Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC 101/166 style #118

Merged
merged 128 commits into from
Mar 14, 2024
Merged
Changes from 1 commit
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
d2cc629
Apply hlint suggestions
Lucus16 Apr 6, 2023
d83a00c
Generalize parsing utilities
Lucus16 Apr 6, 2023
c8ebd02
Attach leading trivia to next token
Lucus16 Apr 6, 2023
0c9d9a4
Add test for if-with-comments
Lucus16 Apr 6, 2023
f702d73
Fix argument order in Ann
Lucus16 Apr 6, 2023
5ae8e62
Allow standalone comments in lists and sets
Lucus16 Apr 29, 2023
4a05494
Add direnv
piegamesde May 5, 2023
13b7904
Update CLI flags description
piegamesde May 5, 2023
f938cfc
Add simple test runner
piegamesde May 5, 2023
9ce4ce3
Rework function declarations
piegamesde Apr 18, 2023
e05d6a2
Force-expand lists with more than one item
piegamesde Apr 18, 2023
305ed77
Expand let statements more
piegamesde Apr 18, 2023
73e7ec4
Rework if statements
piegamesde Apr 18, 2023
cd0acd3
Don't indent `in` body anymore
piegamesde May 5, 2023
8315ca3
Rework `inherit` statements
piegamesde May 5, 2023
28a54b3
fixup! Rework if statements
piegamesde May 5, 2023
7fe6a99
Add some code documentation
piegamesde May 5, 2023
e9cdac6
Rework bindings
piegamesde May 5, 2023
2e2797a
Tests: replace diff/idioms_pkgs_3
piegamesde May 7, 2023
dafec15
fixup! Rework bindings
piegamesde May 7, 2023
de7bd6d
Expand singleton lists with a multiline item
piegamesde May 7, 2023
d1ba3cd
Test: add idioms_nixos_2
piegamesde May 7, 2023
e6bed2a
Don't absort `in` body anymore
piegamesde May 17, 2023
a226b6d
Improve group handling for bindings and inherit
piegamesde May 18, 2023
0528d78
Special case selection operator
piegamesde May 18, 2023
8a6cb8c
Rework operators
piegamesde May 19, 2023
487d313
Rework parentheses
piegamesde May 19, 2023
6ce18ad
Rework function calls
piegamesde May 19, 2023
3802eaa
Add direnv
piegamesde May 5, 2023
7c18e90
Merge branch 'leading-comments' into rfc101-style
piegamesde May 19, 2023
10745f3
Fix function commas
piegamesde May 19, 2023
20d35b3
Move comments around a bit
piegamesde May 20, 2023
2a9ec6b
Tweak operations some more
piegamesde May 20, 2023
cb309e4
wip
piegamesde May 21, 2023
6f4791d
pretty parentheses
piegamesde May 21, 2023
8d07dc9
stupid silly bug
piegamesde May 21, 2023
b1b9313
Absorb parenthesized abstractions with multiple arguments
piegamesde Jun 6, 2023
0b26acb
Tweak operations some more
piegamesde Jun 6, 2023
3e16d20
Don't force-expand (simple) if statements anymore
piegamesde Jun 6, 2023
db2c938
Special case binary operators
piegamesde Jun 6, 2023
66ac4b4
Improve error message on verify
piegamesde Jun 8, 2023
5a8eb61
Improve layouting algorithm
piegamesde Jun 8, 2023
eeb2534
Factor out function application code
piegamesde Jun 8, 2023
792c405
Infinisil style function application
piegamesde Jun 9, 2023
b7daac9
Improve comment handling
piegamesde Jun 10, 2023
b86d8ea
Unindent semicolons again
piegamesde Jun 14, 2023
9a6cc7f
Don't absorb lambda body
piegamesde Jun 14, 2023
3da135d
Absorb parenthesized function calls again
piegamesde Jun 14, 2023
66b6712
Binder with with: absorb less
piegamesde Jun 22, 2023
5bb0639
Rework sets and lists
piegamesde Jun 23, 2023
f7cf76f
Force-spread inherit with more than three items
piegamesde Jun 23, 2023
a7dc8bb
Binder: Always absorb strings and paths
piegamesde Jun 23, 2023
4e1ff44
Tests: add lib/systems/parse.nix
piegamesde Jun 23, 2023
c926692
Better trailing comment parsing
piegamesde Jun 22, 2023
c2702df
Function application: fix edge case
piegamesde Jun 24, 2023
538663c
Function application: don't always absorb last argument
piegamesde Jun 24, 2023
325305e
Binders: force-expand nested attribute sets
piegamesde Jun 25, 2023
a95bd3a
Abstraction: don't absorb body when there are more than two parameters
piegamesde Jun 25, 2023
32609c8
Ignore comments in line length calculation
piegamesde Jun 25, 2023
c568bfa
Ignore indentation in line length calculation
piegamesde Jun 25, 2023
1e42aa2
Merge remote-tracking branch 'upstream/master' into rfc101-style
piegamesde Jun 26, 2023
e6693ca
Absorb abstraction in binder
piegamesde Jul 4, 2023
3b26c7d
Improve mapFirstToken code style
piegamesde Jul 8, 2023
4c0007e
Tests: Add check-meta.nix
piegamesde Jul 9, 2023
ef310bb
Improved helper functions
piegamesde Jul 17, 2023
a54a01a
Improve priority group handling
piegamesde Jul 12, 2023
c4bfa2c
Rework `//` operator
piegamesde Jul 9, 2023
81d3cf8
Copy special cases over to `++` operator
piegamesde Jul 13, 2023
cc5b426
Introduce support for optional trailing commas
piegamesde Jul 17, 2023
4468e9b
Expand attrset function parameters less
piegamesde Jul 16, 2023
9ac66a9
Rework renderer again
piegamesde Jul 18, 2023
a93dcf7
Binders: be more selective about semicolon placement
piegamesde Jul 17, 2023
43dd01d
Fix comment handling
piegamesde Sep 28, 2023
549541f
Expand singleton lists again
piegamesde Oct 10, 2023
7649a1b
Put semicolons on same line again
piegamesde Oct 11, 2023
8645a22
Update the default line length to 100
piegamesde Jun 23, 2023
d2d4a54
Tests: add make-derivation.nix
piegamesde Nov 17, 2023
8a5109b
Types: introduce mapLastToken
piegamesde Nov 17, 2023
55aae75
Function declarations: fixup commas and comments more
piegamesde Nov 17, 2023
91acfa0
Tests: add interpolation test
piegamesde Nov 29, 2023
064bf4d
String interpolations: Ignore line length limits
piegamesde Nov 17, 2023
9d7317f
String interpolations: compact function applications
piegamesde Nov 18, 2023
46d12b2
Function application: Compact simple functions
piegamesde Nov 18, 2023
62fe12f
String interpolations: Fix indentation
piegamesde Nov 18, 2023
951261e
Strings: Fix single-line strings with double single quotes
piegamesde Nov 29, 2023
136edf4
Inherit: Indent trailing semicolon again
piegamesde Nov 29, 2023
206653b
List, Attrset: Remove surrounding spaces
piegamesde Nov 29, 2023
27ce996
Fix idempotency, enable --verify in tests
piegamesde Dec 1, 2023
21ef16e
Move some more comments up
piegamesde Dec 1, 2023
fda8afa
Function application: Fix indentation with multiline function arguments
piegamesde Dec 10, 2023
0a8c246
Revert "List, Attrset: Remove surrounding spaces"
piegamesde Dec 13, 2023
35da232
Strings: Fix multi-line strings that end with a single quote
piegamesde Jan 3, 2024
a273e5a
Lists: Fix absorbtion rules
piegamesde Jan 8, 2024
070063e
Assert: always force-expand
piegamesde Jan 15, 2024
53c7361
Rework: with, paramAttr, bindings, parentheses
piegamesde Jan 15, 2024
067c281
Force-expand attrsets in attrset functions
piegamesde Jan 15, 2024
d6930fd
Refactoring
piegamesde Jan 23, 2024
08fe739
Fix false positive in --verify checks
piegamesde Feb 3, 2024
bb45962
Treat some parenthesized expressions as "simple"
piegamesde Feb 2, 2024
a75658d
Strings: Don't normalize anymore
piegamesde Feb 4, 2024
543b65f
WIP: Automatically minimize verification failures
piegamesde Feb 4, 2024
5ab9430
Relax dependency version bounds
piegamesde Sep 23, 2023
887a40d
Update Nix dependencies
piegamesde Oct 2, 2023
6cfbc2c
Fix TypeOperators warning
infinisil Oct 31, 2023
bbe0ba5
Improve multiline asserts
piegamesde Feb 6, 2024
eb732b1
Rework indentation handling
piegamesde Feb 5, 2024
62b0c39
Small cleanup
piegamesde Feb 6, 2024
d1067e4
Predoc: Support multiple priority groups
piegamesde Feb 6, 2024
7b1fcf5
Function application: Expand non-last arguments when rest fits onto a…
piegamesde Feb 6, 2024
a273a47
Add some tests
piegamesde Feb 7, 2024
9ec2ac0
If: Move trailing comments around
piegamesde Feb 24, 2024
aa85d38
Predoc: Prevent trailing whitespace on lines
piegamesde Feb 26, 2024
d531cef
With: Don't absorb when there is a trailing comment
piegamesde Feb 25, 2024
8f4bf7e
Abstraction: Don't absorb when there is a trailing comment
piegamesde Feb 26, 2024
3b95476
Binding: Small refactoring & add test
piegamesde Feb 26, 2024
bcbf8c6
Binding: Don't absorb // and ++ when there is a trailing comment
piegamesde Feb 26, 2024
d6902b0
Fix indentation
piegamesde Feb 25, 2024
6413a12
List, Attrset: Preserve empty line when there are no items
piegamesde Feb 24, 2024
6bd07b5
Fix parenthesized function application
piegamesde Feb 26, 2024
22fa00e
Parentheses: Move trailing comments up and in
piegamesde Feb 26, 2024
f7d9cfe
Application: add some special cases
piegamesde Feb 26, 2024
25792d4
Comments: use RFC style
piegamesde Mar 1, 2024
2b5ee82
String interpolation: Indentation fixes
piegamesde Mar 1, 2024
7d99e0b
Merge branch 'master' into rfc101-style
infinisil Mar 13, 2024
4eb99c1
Use /usr/bin/env shebang in tests
infinisil Mar 13, 2024
08b34c0
Comply with REUSE
infinisil Mar 13, 2024
f43ec86
Remove accidentally committed file
infinisil Mar 13, 2024
8d13b59
Parser: Fix operator parsing
piegamesde Mar 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Factor out function application code
This is a no-op, but will make tracking changes of coming refactorings easier
  • Loading branch information
piegamesde committed Jun 15, 2023
commit eeb25346eded82857ef87b42cfdb2c9ea0714f04
61 changes: 33 additions & 28 deletions src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
@@ -268,6 +268,38 @@ instance Pretty Parameter where
pretty (ContextParameter param1 at param2)
= pretty param1 <> pretty at <> pretty param2


-- Function application
-- Some example mapping of Nix code to Doc (using brackets as groups, but omitting the outermost group
-- and groups around the expressions for conciseness):
-- `f a` -> [f line*] a
-- `f g a` -> [f line g line*] a
-- `f g h a` -> [[f line g] line h line*] a
-- `f g h i a` -> [[[f line g] line h] line i line*] a
-- As you can see, it separates the elements by `line` whitespace. However, there are two tricks to make it look good:
-- Firstly, for each function call (imagine the fully parenthesised Nix code), we group it. Due to the greedy expansion
-- of groups this means that it will place as many function calls on the first line as possible, but then all the remaining
-- ones on a separate line each.
-- Secondly, the `line` between the second-to-last and last argument (marked with asterisk above) is moved into its preceding
-- group. This allows the last argument to be multi-line without forcing the preceding arguments to be multiline.
prettyApp :: Expression -> Expression -> Doc
prettyApp f a
= let
absorbApp (Application f' a') = (group $ absorbApp f') <> line <> (group a')
absorbApp expr = pretty expr

absorbLast (Term t) | isAbsorbable t
= prettyTerm t
absorbLast (Term (Parenthesized open expr close))
= base $ group $ pretty open <> line' <> nest 2 (group expr) <> line' <> pretty close
absorbLast arg = group arg

-- Extract comment before the first function and move it out, to prevent functions being force-expanded
(fWithoutComment, comment) = mapFirstToken' (\(Ann leading token trailing) -> (Ann [] token trailing, leading)) f
in
pretty comment <> (group $
(group' False $ absorbApp fWithoutComment <> line) <> absorbLast a)

isAbsorbable :: Term -> Bool
isAbsorbable (String (Ann _ parts@(_:_:_) _))
= not $ isSimpleString parts
@@ -363,35 +395,8 @@ instance Pretty Expression where
pretty (Abstraction param colon body)
= pretty param <> pretty colon <> absorbSet body

-- Function application
-- Some example mapping of Nix code to Doc (using brackets as groups, but omitting the outermost group
-- and groups around the expressions for conciseness):
-- `f a` -> [f line*] a
-- `f g a` -> [f line g line*] a
-- `f g h a` -> [[f line g] line h line*] a
-- `f g h i a` -> [[[f line g] line h] line i line*] a
-- As you can see, it separates the elements by `line` whitespace. However, there are two tricks to make it look good:
-- Firstly, for each function call (imagine the fully parenthesised Nix code), we group it. Due to the greedy expansion
-- of groups this means that it will place as many function calls on the first line as possible, but then all the remaining
-- ones on a separate line each.
-- Secondly, the `line` between the second-to-last and last argument (marked with asterisk above) is moved into its preceding
-- group. This allows the last argument to be multi-line without forcing the preceding arguments to be multiline.
pretty (Application f a)
= let
absorbApp (Application f' a') = (group $ absorbApp f') <> line <> (group a')
absorbApp expr = pretty expr

absorbLast (Term t) | isAbsorbable t
= prettyTerm t
absorbLast (Term (Parenthesized open expr close))
= base $ group $ pretty open <> line' <> nest 2 (group expr) <> line' <> pretty close
absorbLast arg = group arg

-- Extract comment before the first function and move it out, to prevent functions being force-expanded
(fWithoutComment, comment) = mapFirstToken' (\(Ann leading token trailing) -> (Ann [] token trailing, leading)) f
in
pretty comment <> (group $
(group' False $ absorbApp fWithoutComment <> line) <> absorbLast a)
= prettyApp f a

-- '//' operator
pretty (Operation a op@(Ann _ TUpdate _) b)