Skip to content

fix(format): restore LSP range formatting#10

Open
kilesduli wants to merge 1 commit into
doomemacs:mainfrom
kilesduli:fix-rangeformatting-with-lsp
Open

fix(format): restore LSP range formatting#10
kilesduli wants to merge 1 commit into
doomemacs:mainfrom
kilesduli:fix-rangeformatting-with-lsp

Conversation

@kilesduli

Copy link
Copy Markdown
Contributor

The LSP method textDocument/rangeFormatting returns a list of TextEdits that are applied to the original buffer. However, `+format-region' formats regions in a temporary buffer containing only the selected text, which causes both eglot and lsp-mode range formatting to fail.

Apheleia does not support region formatting directly, so introduce a way for formatters to operate on the original buffer while still receiving the active region boundaries via `+format-region-range'. This also makes it possible for custom Apheleia formatters to implement region-aware formatting.

  • modules/editor/format/autoload/format.el:
  • (+format--region-p): Rename and expose +format--region-p' as +format-region-range' so custom formatters can access region boundaries.
  • (+format-region): Rename +format-region' to +format-region-via-temp-buffer' and keep a compatibility alias.
  • (+format-region-in-original-buffer): Add for formatters that need access to the original buffer and region boundaries.
  • (+format/region): Run formatters in the original buffer when +format-with-lsp-mode' or +format-region-force-use-original-buffer' is enabled.
  • modules/editor/format/autoload/lsp.el (+format--with-lsp): Use explicit capability checks from lsp--server-capabilities' instead of lsp-feature?'. `lsp-feature?' may return non-nil even for unsupported methods.

  • I searched the issue tracker and this hasn't been PRed before.
  • My changes are not on the do-not-PR list for this project.
  • My commits conform to Doom's git conventions.
  • I am blindly checking these off.
  • This PR contains AI-generated work.
  • Any relevant issues or PRs have been linked to.
  • This a draft PR; I need more time to finish it.

The LSP method `textDocument/rangeFormatting` returns a list of
TextEdits that are applied to the original buffer. However,
`+format-region' formats regions in a temporary buffer containing only
the selected text, which causes both eglot and lsp-mode range
formatting to fail.

Apheleia does not support region formatting directly, so introduce a way
for formatters to operate on the original buffer while still receiving
the active region boundaries via `+format-region-range'. This also makes
it possible for custom Apheleia formatters to implement region-aware
formatting.

* modules/editor/format/autoload/format.el:
- (+format--region-p): Rename and expose `+format--region-p' as
`+format-region-range' so custom formatters can access region
boundaries.
- (+format-region): Rename `+format-region' to
`+format-region-via-temp-buffer' and keep a compatibility alias.
- (+format-region-in-original-buffer): Add for formatters that need access
to the original buffer and region boundaries.
- (+format/region): Run formatters in the original buffer when
`+format-with-lsp-mode' or `+format-region-force-use-original-buffer' is
enabled.

* modules/editor/format/autoload/lsp.el (+format--with-lsp):
Use explicit capability checks from `lsp--server-capabilities' instead
of `lsp-feature?'. `lsp-feature?' may return non-nil even for
unsupported methods.
@kilesduli

kilesduli commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

AFAIK, rust-analyzer (nightly) supports range formatting. The code below can be used to test the behavior of +format-region.

(after! rustic-lsp
  (fset 'rustic-setup-eglot (lambda (&rest _)))
  (after! eglot
    (add-to-list 'eglot-server-programs
                 `(rustic-mode . ("rust-analyzer" :initializationOptions
                                ( :procMacro (:enable t)
                                             :cargo ( :buildScripts (:enable t)
                                                                    :features "all")
                                             :rustfmt ( :rangeFormatting ( :enable t))))))))

(setq lsp-rust-analyzer-rustfmt-rangeformatting-enable t)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants