How to make brittany format data declarations? #47
Labels
No Label
blocked: dependency
blocked: info-needed
bug
duplicate
enhancement
fixed in HEAD
help wanted
hs:arrows
hs:brackets
hs:classes
hs:comments
hs:do-notation
hs:guards
hs:lists
hs:operators
hs:patterns
hs:records
hs:types
invalid
language extension support
layouting
needs confirmation
priority: high
priority: low
question
revisit before next release
wontfix
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: hexagoxel/brittany#47
Loading…
Reference in New Issue
There is no content yet.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may exist for a short time before cleaning up, in most cases it CANNOT be undone. Continue?
I'm thinking of what it would take to make brittany format data declarations as well. I've looked at the high-level descriptions and have skimmed the source code.
The source code is a bit difficult to navigate since I don't know which files or directories have what function. There also don't seem to be comments indicating which file / function / case branch relates to what kind of language elements.
My first attempt at extending brittany would be to copy-past some existing code, re-using e.g.
let
orcase
expressions to formatdata
statements, and making this work in simple cases. The errors and misfortunes I'd encounter would help me learn more about brittany, in the best traditions of cargo-cult programming. Would this stand some chance of success?You probably have not noticed the branch "datadecl" yet; it already contains some (unfinished) initial work in this direction. Although I notice I have a commit that is not even pushed on that branch.
Let me rebase and push, one sec.
Ok, pushed.
It is good news that you want to contribute and I should be able to give you some more specific hints at how to approach the task.
Firstly, my workflow in general when enhancing brittany is:
brittany --dump-ast-full
TyClD
- don't let the weird naming confuse you, data decls are a form of Ty(pe)Cl(ass), apparently.. - and thenDataDecl
docLit (Text.pack "TODO node XY")
as a first sub-stepbrittany
and find out exactly how broken the output still is. Repeat steps 6/7.minimal100% coverageThis is a quick iterative approach; A more thorough one would be test-driven: First, think of all the different relevant cases (here: newtype simple, newtype record, data with one constructor, data with one record, data with multiple constructors, data with multiple record constructors, GADT, (and i still have not investigated if there are GADT records..)). Write (a/ simple example/s) for each case, and consider how they should be layouted. Only then start looking at the relevant GHC API nodes.
Some other random thoughts:
regarding reading the GHC API: In this context, all "PostRn" stuff can be ignored.
master now works on both ghc-8.0 and 8.2, but of course that is not yet necessary for the additions; The difference in the API seems relatively small, so it should be no problem for me to adapt the new code to the other version later. I cannot think of arguments to start with one of them over the other, either.
Regarding handling of comments: Feel free to ignore for now. How to make them work is not documented, and looking at existing comment-handling-code will probably not give a clear view either. (My approach usually is to just pepper some
docWrapNode
applications, but that does not cover all cases and .. nevermind for now.)I really recommend automating parts of that workflow. For example I usually have one simple
Sample.hs
that contains nothing but the example I am working on, and I have a bash script that looks something likeSo I can quickly get the full new results. (For the compilation I recommend ghcid for quick feedback from ghc. Just mentioning; I don't know how much dev you do in haskell.)
Oh, and to answer the initial question: It should not be necessary to touch any code other than
Language.Haskell.Brittany.Internal.Layouters.*
when adding support for other parts of ghc syntax. The structure of these modules roughly matched the structure of the GHC API (Layouters.Expr ~ ghc:HsExpr
,Layouters.Pattern ~ ghc:HsPat
etc.) although the new branch deviates as DataDecl is inHsDecls
- but I really prefer to split up stuff a bit, the modules are big enough as they are.Thinking some more, perhaps it is not even good that the initial work is already done - to get familiar with the interface it might be better to start by deleting the current body of
layoutDataDecl
, and just play around with filling that hole for a bit. I am mildly optimistic that just creative copy-pasting from other parts will indeed be possible, and even if you move a few steps back, getting familiar with the brittany and GHC API should be the first goal.@lspitzner is
datadecl
still a viable branch?@eborden yes. i just rebased it to current
dev
. it only is 8.0 but doesn't seem to have bitrotten yet.It seems like the
datadecl
branch has drifted away frommaster
a little. Would it be worthwhile to bring it up to date?Progress is being made in this direction. See #161.
Work on sum types has begun over here: https://github.com/lspitzner/brittany/pull/294