From 48b8620610f681a718872b58aafcda7aa56bb7e9 Mon Sep 17 00:00:00 2001 From: muxcmux Date: Wed, 31 Jan 2024 21:20:48 +0000 Subject: [PATCH] Fix handling of textDocument/diagnostic 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` (https://github.com/standardrb/standard/issues/575) I have made the `textDocument/diagnostic` hander a no-op, since diagnostics are already being pushed to clients on `textDocument/didChange`. --- ...ix_handling_of_text_document_diagnostic.md | 1 + lib/rubocop/lsp/routes.rb | 4 +-- spec/rubocop/lsp/server_spec.rb | 32 ++----------------- 3 files changed, 4 insertions(+), 33 deletions(-) create mode 100644 changelog/fix_handling_of_text_document_diagnostic.md diff --git a/changelog/fix_handling_of_text_document_diagnostic.md b/changelog/fix_handling_of_text_document_diagnostic.md new file mode 100644 index 000000000000..02a0e77cd60b --- /dev/null +++ b/changelog/fix_handling_of_text_document_diagnostic.md @@ -0,0 +1 @@ +* [#12664](https://github.com/rubocop/rubocop/pull/12664): Fix handling of `textDocument/diagnostic`. ([@muxcmux][]) diff --git a/lib/rubocop/lsp/routes.rb b/lib/rubocop/lsp/routes.rb index 8d7c40d08fda..27ba1fc4a328 100644 --- a/lib/rubocop/lsp/routes.rb +++ b/lib/rubocop/lsp/routes.rb @@ -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| diff --git a/spec/rubocop/lsp/server_spec.rb b/spec/rubocop/lsp/server_spec.rb index 678d57738bf7..595a9ab2f1b7 100644 --- a/spec/rubocop/lsp/server_spec.rb +++ b/spec/rubocop/lsp/server_spec.rb @@ -103,10 +103,7 @@ 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' } } ] @@ -114,32 +111,7 @@ 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