Repository navigation
Gate comment continuation on clients that place the caret after the edit - #4285
Open
caiotheodoro wants to merge 1 commit into
Open
caiotheodoro wants to merge 1 commit into
caiotheodoro wants to merge 1 commit into
Conversation
Comment continuation inserts a `#` at the caret on newline, which only works if the client moves the caret to the end of the inserted text. Clients that leave the caret at the insertion point (e.g. Emacs) end up with a stray `#` ahead of the cursor. Only send the continuation edit for clients known to place the caret correctly (VS Code and forks, and Zed). Other clients get a plain newline instead of a misplaced comment marker. Fixes Shopify#3749
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Closes #3749
Comment continuation inserts
#at the caret on"\n" without checking whether theclient can place the caret after the inserted text. On clients that leave the caret at
the insertion point (e.g. Emacs lsp-mode), the result is a stray
#ahead of thecursor — the continuation is unusable and worse than sending nothing, as noted by the
maintainer direction on the issue.
Implementation
handle_comment_linenow returns early unless the client is known to move the caret tothe end of text inserted at the caret position. That property is not part of the LSP
specification; it is known to hold for VS Code, its forks (
supports_snippet_anchor?)and Zed (per the edit semantics established in #4133), so a dedicated
supports_comment_continuation?predicate covers exactly those. Other clients receiveno continuation edit instead of a misplaced one — matching #4133's approach of skipping
what can't be done correctly rather than doing it approximately.
VS Code, its forks and Zed are unaffected; Emacs and other clients get a plain newline.
Automated Tests
Added two cases to
test/requests/on_type_formatting_test.rb:test_comment_continuation_is_not_sent_for_clients_that_cannot_place_the_caret—"Emacs"receives no edits (fails before the fix).test_comment_continuation_for_snippetless_clients_that_place_the_caret_after_inserts—"Zed"still receives the#edit, guarding the existing Zed behavior.bundle exec ruby -Itest test/requests/on_type_formatting_test.rb— 46 runs, green.bundle exec rake test— 3567 runs; only pre-existing environment failure(
test_backtrace_is_printed_to_stderr_on_exceptions, expects the checkout dir to benamed
ruby-lsp; fails on a clean main checkout too). RuboCop clean on touched files.Manual Tests
In a client that does not interpret
$0and does not move the caret after at-caretedits (e.g. Emacs + lsp-mode):
#on a line.#with the caret before it. After: next line is empty.