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

Improve formatting of multiline ERB #56

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

pelletencate
Copy link

@pelletencate pelletencate commented Jul 4, 2024

I totally didn't notice the existence of #7 and did my own implementation of better multiline ERB.

This PR does 2 things:

  1. Subtract the indentation level from Syntax Tree line width
    • This could be even further improved: line length should take ERB tag start and end tag overhead in consideration
  2. Change the way multiline ERB is formatted. It will do the following:
    • If formatted ruby yields more than 1 line, then:
      • The ERB tag start and end will be on separate lines
      • The entire Ruby code will be indented with an additional 2 spaces
      • If a mulit-line comment has its own (arbitrary) indentation, properly indent the entire comment underneath the ERB tags, preserving relative indentation

(Due to the fact that I was adding to repetitive logic, I chose to optimize the case statement. If this is undesirable and we prefer even more repetition, I'm OK with having that refactored back)

@pelletencate pelletencate marked this pull request as draft July 4, 2024 15:52
@pelletencate pelletencate marked this pull request as ready for review July 4, 2024 16:45
@pelletencate
Copy link
Author

Oh @elia I completely missed #7 and went my own way with this one.

Comment on lines 273 to 274
SyntaxTree.format(code, @line_width)
width = @line_width - tag_stack.size * 2
SyntaxTree.format(code, width)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi: this fixes part of #55

@@ -39,7 +39,7 @@ class Error < StandardError; end
ATTR = Regexp.union(SINGLE_QUOTE_ATTR, DOUBLE_QUOTE_ATTR, UNQUOTED_ATTR, UNQUOTED_VALUE)
MULTILINE_ATTR_NAMES = %w[class data-action]

ERB_TAG = %r{(<%(?:==|=|-|))\s*(.*?)\s*(-?%>)}m
ERB_TAG = %r{(<%(?:=|-|#)*)(?:(?!\n)\s)*(.*?)\s*(-?%>)}m
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi: (?:(?!\n)\s) matches all whitespace except for newlines. Done this in order to preserve comment indentation on the first line

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.

1 participant