Skip to content

Gate comment continuation on clients that place the caret after the edit - #4285

Open
caiotheodoro wants to merge 1 commit into
Shopify:mainfrom
caiotheodoro:fix/comment-continuation-caret
Open

caiotheodoro wants to merge 1 commit into
Shopify:mainfrom
caiotheodoro:fix/comment-continuation-caret

Conversation

@caiotheodoro

Copy link
Copy Markdown

Motivation

Closes #3749

Comment continuation inserts # at the caret on "\n" without checking whether the
client 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 the
cursor — the continuation is unusable and worse than sending nothing, as noted by the
maintainer direction on the issue.

Implementation

handle_comment_line now returns early unless the client is known to move the caret to
the 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 receive
no 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 be
named ruby-lsp; fails on a clean main checkout too). RuboCop clean on touched files.

Manual Tests

In a client that does not interpret $0 and does not move the caret after at-caret
edits (e.g. Emacs + lsp-mode):

  1. Type # on a line.
  2. Press RET.
  3. Before: next line contains # with the caret before it. After: next line is empty.

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
@caiotheodoro
caiotheodoro requested a review from a team as a code owner November 24, 2026 00:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Poor comment continuation character placement

1 participant