Skip to content

Support "on type formatting" in Zed - #4133

Open
janko wants to merge 2 commits into
Shopify:mainfrom
janko:zed-on-type-formatting
Open

Support "on type formatting" in Zed#4133
janko wants to merge 2 commits into
Shopify:mainfrom
janko:zed-on-type-formatting

Conversation

@janko

@janko janko commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Motivation

Zed doesn't support snippet syntax ($0) in document/onTypeFormatting responses, nor is it possible for its Ruby extension to implement it. Because the cursor position isn't right after edits, Zed's Ruby extension currently has onTypeFormatting disabled by default for Ruby LSP.

Fixes #2971

Implementation

For "end" and "heredoc end", we can work around this by inserting them in the line below the cursor. This prevents the cursor from moving after the inserted text, avoiding the need to rewind it. We can't support this at the EOF, as there is no next line, but that's the rarer scenario.

This won't work for formatting on the same line, because Zed cannot rewind the cursor. Zed already supports auto-closing brackets, so we skip closing curly braces. We also skip pipe auto-close, because it's better not to add anything than to end up with incorrect cursor placement.

Automated Tests

I've added unit tests.

Manual Tests

To test this in Zed, you first need to enable on type formatting in Zed settings:

{
  "lsp": {
    "ruby-lsp": {
      "initialization_options": {
        "enabledFeatures": {
          "onTypeFormatting": true
        }
      }
    }
  }
}

Zed doesn't support snippet syntax ($0) in `document/onTypeFormatting`
responses, nor is it possible for its Ruby extension to implement it.

For "end" and "heredoc end", we can work around this by inserting them
in the line *below* the cursor. This prevents the cursor from moving
after the inserted text, avoiding the need to rewind it. We can't
support this at the EOF, as there is no next line, but that's the rarer
scenario.

This won't work for formatting on the same line, because Zed cannot
rewind the cursor. Zed supports auto-closing brackets, so we skip
closing curly braces. We do the same for pipes, because it's better not
to add anything than to do it incorrectly.
@JacobNWolf

Copy link
Copy Markdown

Very eager to see this merged!

@vinistock

Copy link
Copy Markdown
Member

Thank you for the PR. Can't we completely do without the cursor movement if we switch to the strategy you proposed for Zed? Even for other editors?

Also, I do want to point out that the curly brace formatting is for occurrences inside interpolation "#{" and not for bare {} which VS Code also auto-closes.

@janko

janko commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

Can't we completely do without the cursor movement if we switch to the strategy you proposed for Zed? Even for other editors?

Well, considering that I haven't found a way to do inline formatting (e.g. |), or make end auto-insertion work at the end of the file, at this stage we cannot drop the cursor movement implementation.

Also, I do want to point out that the curly brace formatting is for occurrences inside interpolation "#{" and not for bare {} which VS Code also auto-closes.

Oh, didn't come to my mind, that makes sense 👍🏻

@janko

janko commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

The #{ can probably be autocompleted by Zed by modifying autoclose_before setting to include double quotes and backticks, and similar for VSCode. The | symbol can't seem to reliably be autoclosed by the editor, so we're back to the language server with cursor movement.

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.

On type formatting for blocks is unhelpful due to cursor placement

3 participants