Subject: [PATCH] Eglot: introduce eglot-show-diagnostics-source
From: Nicolás Ojeda Bär <[email-masked]>
To: [email-masked]
Date: Tue, 01 Apr 2025 22:58:35 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

Dear developers (Cc: João Távora),

This small patch introduces a new variable eglot-show-diagnostics-source
which controls whether Eglot prefixes every diagnostic with the name of
its source. When only one source is active, this information is not
useful and its repetition for every diagnostic wastes screen real
estate.

This addition has already been discussed (and accepted in principle) in
GitHub:

https://github.com/joaotavora/eglot/issues/1501

I tried to follow the existing conventions for the patch and its
documentation, but this is my first contribution to Emacs, so please do
not hesitate to point out if I missed anything.

I am looking forward to any feedback you may have on this patch. Thanks.

Cheers,
Nicolas

From 14b3e3397d831c319ea865f4bfea81fe1d0dfb75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicol=C3=A1s=20Ojeda=20B=C3=A4r?= <[email-masked]>
Date: Tue, 1 Apr 2025 22:24:21 +0200
Subject: [PATCH] Eglot: introduce eglot-show-diagnostics-source

Setting this variable to nil causes Eglot to stop prefixing every
diagnostic with the name of the source of the diagnostic.  This can be
useful when only one source is active, to reduce the repetition of the
same information in every diagnostic.

* lisp/progmodes/eglot.el (eglot-handle-notification): Take new variable
  into account.

* doc/misc/eglot.texi (Customizing Eglot): Mention new variable.

* etc/EGLOT-NEWS: Mention new variable.
---
 doc/misc/eglot.texi     |  7 +++++++
 etc/EGLOT-NEWS          |  7 +++++++
 lisp/progmodes/eglot.el | 12 +++++++++++-
 3 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/doc/misc/eglot.texi b/doc/misc/eglot.texi
index fac90562f6b..f483611541a 100644
--- a/doc/misc/eglot.texi
+++ b/doc/misc/eglot.texi
@@ -1050,6 +1050,13 @@ Other Variables
 Note that you can still configure the excluded Emacs features manually
 to use Eglot in your @code{eglot-managed-mode-hook} or via some other
 mechanism.
+
+@item eglot-show-diagnostics-source
+Typically Eglot prefixes each diagnostic with the name of the source of
+the diagnostic. However, when only one source is active, the same
+information is repeated in all diagnostics which may not be useful.  In
+that case, this variable can be set to nil to supress the addition of
+this information to each diagnostic.
 @end table
 
 @node More Customization
diff --git a/etc/EGLOT-NEWS b/etc/EGLOT-NEWS
index 7b53d5943ba..125935c001e 100644
--- a/etc/EGLOT-NEWS
+++ b/etc/EGLOT-NEWS
@@ -20,6 +20,13 @@ https://github.com/joaotavora/eglot/issues/1234.
 
 * Changes in upcoming Eglot
 
+** New 'eglot-show-diagnostics-source' variable
+
+Eglot typically includes the name of the source of each diagnostic
+reported by the LSP server. Setting this variable to nil inhibits this
+behaviour and can be useful when only one source is active to avoid
+repeating the same information in every diagnostic.
+
 ** Support for call and type hierarchies
 
 The new commands 'eglot-show-type-hierarchy' and
diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index c937283122e..5a367787a8d 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -581,6 +581,12 @@ eglot-advertise-cancellation
 servers."
   :type 'boolean)
 
+(defcustom eglot-show-diagnostics-source t
+  "If non-nil, Eglot advertises the source of each diagnostics by
+prefixing the diagnostic with the source name. Set this to nil to
+inhibit this behaviour."
+  :type 'boolean)
+
 (defface eglot-code-action-indicator-face
   '((t (:inherit font-lock-escape-face :weight bold)))
   "Face used for code action suggestions.")
@@ -2667,7 +2673,11 @@ eglot-handle-notification
                     ((= sev 2)  'eglot-warning)
                     (t          'eglot-note)))
             (mess (source code message)
-              (concat source (and code (format " [%s]" code)) ": " message))
+              (let ((source (and eglot-show-diagnostics-source source)))
+                (cond ((and (null code) (null source)) message)
+                      ((null code) (format "%s: %s" source message))
+                      ((null source) (format "[%s]: %s" code message))
+                      (t (format "%s [%s]: %s" source code message)))))
             (find-it (abspath)
               ;; `find-buffer-visiting' would be natural, but calls the
               ;; potentially slow `file-truename' (bug#70036).
-- 
2.48.1