Skip to content

Line length based code formatting #54

Open
@gingermusketeer

Description

@gingermusketeer

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:

  1. Should we use the algorithm used by Prettier?
  2. Should we implement in a decoupled manner such that we can format other languages?
  3. Are we okay with sacrificing some performance to gain line length aware formatting?
  4. OO (@danielma's approach) or mostly Functional (my approach)?
  5. How should we approach implementation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions