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

[WIP] Add blank lines after semicolons #31

Closed
wants to merge 2 commits into from
Closed

[WIP] Add blank lines after semicolons #31

wants to merge 2 commits into from

Conversation

tpwo
Copy link
Contributor

@tpwo tpwo commented Aug 17, 2021

A blank line is added after every line that:

  • Doesn't have within statement on it
  • Isn't inside [] (matrix notation)

Issues:

  • Two blank lines are added if a file ends with a semicolon. The tests are not passing because of that.

    I am not sure how to solve this problem. I thought about checking for end on a given line, but we can have multiple end in one file. And this should only care about the last end.

    One of my ideas is to check for main class/model name (i.e. the one in the 2nd line) and add another if statement that checks if we're in the correct end statement. But I noticed that you have trailing comments at the end of test files (e.g. this one) so this approach would also break here, because a blank line would be missing, but it shouldn't.

    Any better ideas? I am not too familiar with antrl 😄

I added new test .mo files that help to verify:

  • formatting of matrices
  • situation when we're missing within statement from any reason (might be useful for formatting code snippets etc.)

Vertical spacing makes the code more legible. This version adds a single
blank line after almost every semicolon.

There are two exceptions:
- If there`s a `within` on a given line, a blank line won't be added.
- If inside matrix (i.e. `[]`), a blank line won't be added.

  Example output:

    parameter Integer[2,2] A = [
      1,2;
      3,4];

Also:
- Add example with formatting arrays and matrices

Known issues:
- The formatted file will have two blank lines at the end. The solution
  would be to detect if we're on line with the last `end` within the
  file, and then skip adding an additional blank line. There might be
  multiple `end` in a file, so this is not that obvious to solve.
We need to find a way to get rid of duplicated blank line at the end of
files that have semicolon in the last line.
@macintoshpie
Copy link
Contributor

This looks good! I went ahead and addressed the issue you mentioned (extra blank lines at end of file) in #33.
Thanks for adding the tests as well, it's really helpful to isolate the specific ways the formatter works. Hopefully we can formalize it as a style doc at some point 🤞

I'll close this PR now that #33 includes your changes and the fix

@tpwo tpwo deleted the add-vertical-spacing branch August 20, 2021 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants