handle end when there's content remaining on the line correctly #2819
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The current behavior of the automatic
end
on type is unpredictable and strange. It appends theend
in weird spots and leaves the cursor in a strange location.Some examples:
If there's content on the line, the end is inserted and the remaining content is appended after the new
end
:If there's a set of parens and you expand to a newline, it puts autocompletes the
end
inside the paren:I encounter this most often when breaking sigs up onto multiple lines
Or when attempting to break up the list of parameters:
Or when breaking up lines with long conditions:
Implementation
The current logic is a little bit mystical to me. I'm not sure why the conditions are what they are, and I couldn't find a reasonable way to tweak it to be more reasonable.
My expectation as a user is that if I hit enter, anything after my cursor on the line should always be appended within the autocorrected
end
block, and my cursor should be left at the beginning of the new line. There's no other natural behavior here.The complexity is that there is some odd behavior in the parser or in the other auto-correcting behavior from the LSP itself which causes the lines provided to the
handle_statement_end
to behave differently.)
, then the current line is what you would expect.)
, then the current line terminates at the last position, and the next line is whatever content was remaining on the line before the user entered the newline character.In either case, the user wants the same behavior - all of the remaining content on the line should occur before the autocorrected
end
, and the cursor should be at the beginning of the new line they just created.Automated Tests
I couldn't quickly figure out how to get the existing unit tests to run, but I'm thinking they should all pass first try if they're well written. I'd be happy to add additional unit tests here to test more advanced cases.
Manual Tests
Started ruby-lsp in debug mode. Here are some results from the new code.
Breaking up content:
If there's a set of parens and you expand to a newline:
breaking sigs up onto multiple lines
Or when attempting to break up the list of parameters:
Or when breaking up lines with long conditions: