Skip to content

Adding autoformatting #296

Closed
Closed
@KarelPeeters

Description

@KarelPeeters

Intro

I'm investigating how hard it would be to add automating as a feature, as part of #156. This project already has a parser, now we "only" need to add the other direction 🙂.

Inspiration can be taken from rustfmt, notably Design.md and the more up-to-date Contributing.md.

Current state

I noticed the file display.rs has implementations for some, but not all, AST nodes. As far as I can tell statements and anything that would require them (up to and including AnyDesignUnit::Secondary) does not yet have a Display implementation.
Is the intention that this gets extended to cover the entire AST?

Some limitations of the current design:

  • Indentation is hard-coded per AST node type, which does not extend to arbitrarily nested expressions.
  • Parenthesis insertion is decided based on precedence levels, potentially overriding user intent.
  • Comments are dropped, these need to be preserved.
  • There is no line wrapping, and it's not obvious how to add this.

Future

Some decisions need to be made:

  • Should the formatter be whitespace only or more all-encompassing (including line formatting and blank lines) like rustfmt?
    • I think white-space only is much easier to implement, since we don't need to make any complicated line-breaking decisions.
  • How much information do we want to add to the AST to enable formatting? For example, comments can be handled in two ways: either add them to the AST and then emit them again, or keep them in a separate data structure and re-insert them during stringifying. The same is true for line breaks.

I'll do some more research and maybe start on a prototype, I just wanted to start the discussion as early as possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions