-
Notifications
You must be signed in to change notification settings - Fork 175
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
feat: implement remove newlines in brackets before toggle block to on… #3005
base: main
Are you sure you want to change the base?
feat: implement remove newlines in brackets before toggle block to on… #3005
Conversation
@@ -272,6 +272,33 @@ def create_text_edit(range, new_text) | |||
) | |||
end | |||
|
|||
sig { params(str: String).returns(String) } | |||
def remove_newlines_in_brackets(str) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach is essentially re-parsing the string that was already parsed by Prism to remove the newlines. I'm concerned that this will be quite error prone, since parsing Ruby is challenging.
For example, will the code work properly in a case like this?
[].each do |something|
# Brackets that don't represent an array mixed with arrays
a[:hello] = [
something[1],
]
end
If we could find a way to use the already parsed nodes from Prism to rebuild the block's body, I think that would be more robust. Maybe we need to go through the nodes inside the body and transform them into single line one by one, with different rules for hash and array nodes so that we can produce valid code.
Motivation
Close #2968
The current block conversion from
do..end
to brackets fails when handling complex data structures like Hashes or Arrays. Simply converting line breaks to semicolons doesn't maintain code validity in these cases.Implementation
remove_newlines_in_brackets
method to properly handle newlines within Hash/Array literalsAutomated Tests
test_toggle_block_do_end_to_brackets
: Verifies basic block conversion functionalitytest_toggle_block_do_end_with_hash_to_brackets
: Ensures proper handling of Hash literals within blocks