Skip to content

feat: append LSP diagnostic codes to flymake messages#5036

Open
alberti42 wants to merge 1 commit intoemacs-lsp:masterfrom
alberti42:show-diagnostic-codes
Open

feat: append LSP diagnostic codes to flymake messages#5036
alberti42 wants to merge 1 commit intoemacs-lsp:masterfrom
alberti42:show-diagnostic-codes

Conversation

@alberti42
Copy link
Copy Markdown

Currently, lsp-diagnostics--flymake-update-diagnostics extracts :message and :severity? from each LSP diagnostic but silently drops :code?. As a result, flymake displays messages like:

"variable_XYZ" is possibly unbound

without the diagnostic rule name that the language server also provides.

This patch binds :code? and appends it to the message text when present:

"variable_XYZ" is possibly unbound [reportPossiblyUnbound]

This is a small but valuable change. The diagnostic code is the primary handle users need to:

  • search for the error online
  • look it up in the language server's documentation
  • write a precise inline suppression comment (e.g. # pyright: ignore[reportPossiblyUnbound])

Without the code, users must guess the rule name. The fix is a two-line change: bind :code? in the existing -let* destructuring and conditionally append [code] to the message.

The code field is optional in the LSP spec, so the format falls back gracefully to the plain message when absent.

@jcs090218
Copy link
Copy Markdown
Member

Can you do this to flycheck too? Thanks! :D

@alberti42
Copy link
Copy Markdown
Author

Hi @jcs090218, thanks for the quick reply already.

I checked flycheck. The good news is that we don't need to change anything in the flycheck branch. The code is already correct and exactly displays the error code as [CODE].

You can see that code? was already deconstructed and passed to :id. Flycheck then takes care of rendering it correctly.

(-map (-lambda ((&Diagnostic :message :severity? :tags? :code? :source?
                                    :range (&Range :start (start &as &Position
                                                                 :line      start-line
                                                                 :character start-character)
                                                   :end   (end   &as &Position
                                                                 :line      end-line
                                                                 :character end-character))))
               (flycheck-error-new
                :buffer (current-buffer)
                :checker checker
                :filename buffer-file-name
                :message message
                :level (lsp-diagnostics--flycheck-calculate-level severity? tags?)
                :id code?
                :group source?
                :line (lsp-translate-line (1+ start-line))
                :column (1+ (lsp-translate-column start-character))
                :end-line (lsp-translate-line (1+ end-line))
                :end-column (unless (lsp--position-equal start end)
                              (1+ (lsp-translate-column end-character))))))

What was missing was precisely the flymake branch where the code was not displayed. If this PR makes it to upstream, then both branches display the same message and error code.

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