Fix Ruby LSP formatting range to comply with LSP specification #2438
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
Hi! While working on the Ruby LSP extension for the IntelliJ platform, I noticed an issue (or not?) with the formatting response from the Ruby Language Server. Thanks!
Implementation
The current implementation of formatting
does not use the proper range for a response.
Consider the following Ruby code:
Let's pretend that our RuboCop configuration specifies omitting whitespaces inside array declarations, which means the code above is not properly formatted.
The code should be formatted like this:
When a client asks Ruby LSP to format the first code, the server sends a response (taken from the Zed editor) where the reported
end
property of theRange
interface hasline
andcharacter
keys that do not match the source document.Both of them equal the character size of the document.
For some reason, Visual Studio Code considers this response valid, and the code is formatted properly.
However, some editors perform a check to ensure that the reported response from Ruby LSP is not out of bounds of the current text document.
According to the LSP specification from the link below: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#range A range in a text document expressed as (zero-based) start and end positions. The correct response should be:
Automated Tests
I am happy to add unit tests for that, but I am not sure where I should start. I didn't find any tests regarding testing requests/responses in accordance with LSP specs.
Manual Tests
Unfortunately, there is no way to test this in Visual Studio Code because formatting, for some reason, works just fine there.