fix(format): restore LSP range formatting#10
Open
kilesduli wants to merge 1 commit into
Open
Conversation
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.
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) |
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.
The LSP method
textDocument/rangeFormattingreturns 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.
+format--region-p' as+format-region-range' so custom formatters can access region boundaries.+format-region' to+format-region-via-temp-buffer' and keep a compatibility alias.+format-with-lsp-mode' or+format-region-force-use-original-buffer' is enabled.lsp--server-capabilities' instead oflsp-feature?'. `lsp-feature?' may return non-nil even for unsupported methods.