-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Support formatting scripts into multiple lines #4
Comments
I am looking into this issue and I think I made some progress. Let me know if you’d like me to continue looking into this (or if you already are), and if you think the direction I went for is a good one. Thanks! Testing suite In order to check my progress, I added a few tests to the test suite, in def test_script_multiline
# Should not wrap on multiple lines for short lines
assert_format(<<~HAML)
= f.input :roles, as: :check_boxes
HAML
# Reproduction case in the issue
assert_format(<<~HAML)
= f.input :roles,
as: :check_boxes,
collection: Role.global_roles,
label_method: lambda { |role| I18n.t(role.name, scope: "rolify.role.roles") },
value_method: :name,
checked: f.object.roles_name
HAML
# rendering a ViewComponent (a common occurrence in our codebase)
assert_format(<<~HAML)
= render(DesignSystem::FAIcon.new(name: "info",
type: :uploaded,
options: {class: "text-sm text-purple-500"}))
HAML
end And here’s my update to the def visit_script(node)
with_children(node) do
if line = q.literal_lines[node.line]
q.text(line)
else
q.text("&") if node.value[:escape_html]
q.text(node.value[:preserve] ? "~" : "=")
q.text(" ")
# Changed code starts here
formatted = SyntaxTree.format(node.value[:text].strip)
# Valid ruby syntax does not mean valid haml syntax in a script
# Tweak the formatted output to make sure it is valid haml
formatted = formatted
# if a line break happens after anything else than a comma, remove it
.gsub(/([^,])[\r\n]/, '\1')
# when a line break has been removed, make sure the spacing after delimiters
# (commas, parenthesis, curly brackets) is correct. For example:
# `foo:\n bar` => `foo:bar` => `foo: bar`
# `.new(\n args) => `.new(args)` => `.new(args)`)
.gsub(/(\S)[^\S\r\n]{2,}(\S)/, '\1 \2') # replaces multiple whitespace by one
.gsub('( ', '(')
.gsub(' )', ')')
.gsub('} ', '}')
q.text(formatted)
end
end
end The various Thanks! |
I'd be super nervous about going with I think the answer is actually going to be a bit more complicated. Ideally it would parse all of the nodes in the tree that contain Ruby content together, that way we'd be able to handle = if ...
= elsif ... because right now that's going to throw an error any time you only are parsing one of those nodes. We'll also need to add in Anyway, that's the way I want to go, because I feel like that's a more complete solution. Otherwise we're going to be maintaining gsubs that may or may not be correct for all code, it's just not really possible to tell. |
Thank you for the detailed answer! |
By all means! Feel free. If you want to keep a PR open and just iterate there that'd be fine, happy to review it as you go. |
For more information, see prettier/plugin-ruby#1161
The text was updated successfully, but these errors were encountered: