Skip to content

Commit

Permalink
Fix handling of textDocument/diagnostic
Browse files Browse the repository at this point in the history
The `textDocument` parameter for the `textDocument/diagnostic` request
is of type `TextDocumentIdentifier`, which has a single property `uri`
of type `DocumentUri`.

Ref: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_diagnostic

According to the spec the `DocumentUri` type does not hold the contents
of the document, which the handler currently relies on in order to
publish diagnostics to the client.

This breaks the LSP mode in Neovim 0.10, which now supports pull
diagnostics, and possibly other editors too. The same problem exists in
`standardrb` (standardrb/standard#575)

I have made the `textDocument/diagnostic` hander a no-op, since
diagnostics are already being pushed to clients on
`textDocument/didChange`.
  • Loading branch information
muxcmux committed Feb 2, 2024
1 parent 36a6b88 commit 48b8620
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 33 deletions.
1 change: 1 addition & 0 deletions changelog/fix_handling_of_text_document_diagnostic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12664](https://github.com/rubocop/rubocop/pull/12664): Fix handling of `textDocument/diagnostic`. ([@muxcmux][])
4 changes: 1 addition & 3 deletions lib/rubocop/lsp/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def for(name)
end

handle 'textDocument/diagnostic' do |request|
doc = request[:params][:textDocument]
result = diagnostic(doc[:uri], doc[:text])
@server.write(result)
# no-op, diagnostics are handled in textDocument/didChange
end

handle 'textDocument/didChange' do |request|
Expand Down
32 changes: 2 additions & 30 deletions spec/rubocop/lsp/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,43 +103,15 @@
method: 'textDocument/diagnostic',
params: {
textDocument: {
languageId: 'ruby',
text: "def hi#{eol} [1, 2,#{eol} 3 ]#{eol}end#{eol}",
uri: 'file:///path/to/file.rb',
version: 0
uri: 'file:///path/to/file.rb'
}
}
]
end

it 'handles requests' do
expect(stderr).to eq('')
expect(messages.count).to eq(1)
expect(messages.first).to eq(
jsonrpc: '2.0',
method: 'textDocument/publishDiagnostics',
params: {
diagnostics: [
{
code: 'Style/FrozenStringLiteralComment',
message: 'Missing frozen string literal comment.',
range: {
start: { character: 0, line: 0 }, end: { character: 0, line: 0 }
},
severity: 3,
source: 'rubocop'
}, {
code: 'Layout/SpaceInsideArrayLiteralBrackets',
message: 'Do not use space inside array brackets.',
range: {
start: { character: 4, line: 2 }, end: { character: 5, line: 2 }
},
severity: 3,
source: 'rubocop'
}
], uri: 'file:///path/to/file.rb'
}
)
expect(messages.count).to eq(0)
end
end

Expand Down

0 comments on commit 48b8620

Please sign in to comment.