-
Notifications
You must be signed in to change notification settings - Fork 172
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
Auto indent after end keyword #1014
Conversation
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 is looking great!
We need to add Also, can we change the implementation to use node locating? I think it's going to be more robust. Something like this # ...
when "d"
auto_indent_after_end_keyword
# ...
def auto_indent_after_end_keyword
# We need to look for the end in the current line, not the previous one
current_line = @lines[@position[:line]]
return unless current_line&.strip == "end"
# Instead of relying on regexes, we can try to find a Statements node on top
# of the triggered position
target, _parent, _nesting = T.cast(
@document.locate_node(@position, node_types: [YARP::StatementsNode]),
[T.nilable(YARP::StatementsNode), T.nilable(YARP::Node), T::Array[String]],
)
return unless target
# If there is a statements node, then we want to push every node inside body
# two characters forward
target.body.each do |node|
add_edit_with_text(...)
end
move_cursor_to(@position[:line], @position[:character])
end |
Ok, I'll change it! |
Should I close this pull request ? |
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.
I double checked the implementation and made some fixes. With the updated code, the auto-indent works as intended.
Can you please apply the suggestions and rebase the PR? Then we can ship it
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.
Thank you very much for the contribution!
Motivation
Closes #272
Implementation
I wrote something based on actual code and not too complex. Basically, I just verified the exact position and back to the place where the indentation was wrong, adjusted it, and then back to the final position.
Automated Tests
Added test case for formatting indentation.