Description
Now that we have cleaned up the config significantly as part of #2 it might be a good time to start thinking about how we go about implementing line length based code formatting.
Both @danielma and I have had a go at implementing this. We both used the printing algorithm described by Philip Wadler in his paper "A prettier printer" which can be found here.
We have taken different approaches for the implementation. You can see the WIP code for @danielma's here and my WIP code here.
This is the same algorithm used by Prettier. They have some descriptions of the commands used to power the formatter here.
The way that Prettier has been implemented can best be described by this flow:
Source -> Doc -> Formatted Source
. Where Doc
is an intermediary representation of the source code. It is this intermediary representation that is then printed with the line length aware formatter.
This approach has the advantage of the Doc
construction phase being the only language aware component. This allows for the same document construction utilities and formatter to be used for different languages. For example:
Ruby Source -> Doc -> Formatted Source
CSS/SCSS Source -> Doc -> Formatted Source
HTML ERB Source -> Doc -> Formatted Source
The downside to this approach is likely performance. I don't have any numbers to back this up but if we are doing more work then we are currently I would hazard to guess that the performance will decrease to some degree.
So I think we have a few decisions to make:
- Should we use the algorithm used by Prettier?
- Should we implement in a decoupled manner such that we can format other languages?
- Are we okay with sacrificing some performance to gain line length aware formatting?
- OO (@danielma's approach) or mostly Functional (my approach)?
- How should we approach implementation?