Subject: Re: bug#71353: [PATCH] eglot--format-markup doesn't support MarkedString code-blocks

From: João Távora <[email-masked]>
Date: Tue, 18 Jun 2024 14:39:20 +0100

On Sat, Jun 15, 2024 at 1:36 PM Eli Zaretskii <[email-masked]> wrote:
>
> > From: João Távora <[email-masked]>
> > Date: Sat, 15 Jun 2024 10:37:48 +0100
> > Cc: Felician Nemeth <[email-masked]>, Troy Brown <[email-masked]>,
> >       [email-masked]
> >
> > Can you please resend the patch for inspection?
>
> Attached.

The gist of the patch is correct, but please consider replacing by
this alternative, I don't like if-let inside pcase-let, it's too
complex: the following patch should be "flatter".

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 6896baf30ce..eabe01a1676 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -1863,14 +1863,22 @@ eglot--snippet-expansion-fn
            (apply #'yas-expand-snippet args)))))

 (defun eglot--format-markup (markup)
-  "Format MARKUP according to LSP's spec."
-  (pcase-let ((`(,string ,mode)
-               (if (stringp markup) (list markup 'gfm-view-mode)
-                 (list (plist-get markup :value)
-                       (pcase (plist-get markup :kind)
-                         ("markdown" 'gfm-view-mode)
-                         ("plaintext" 'text-mode)
-                         (_ major-mode))))))
+  "Format MARKUP according to LSP's spec.
+MARKUP is either an LSP MarkedString or MarkupContent object."
+  (let (string mode language)
+    (cond ((stringp markup)
+           (setq string markup
+                 mode 'gfm-view-mode))
+          ((setq language (plist-get markup :language))
+           (setq string (concat "```" language "\n"
+                                (plist-get markup :value) "\n```")
+                 mode 'gfm-view-mode))
+          (t
+           (setq string (plist-get markup :value)
+                 mode (pcase (plist-get markup :kind)
+                        ("markdown" 'gfm-view-mode)
+                        ("plaintext" 'text-mode)
+                        (_ major-mode)))))
     (with-temp-buffer
       (setq-local markdown-fontify-code-blocks-natively t)
       (insert string)