Function: eglot--format-markup

eglot--format-markup is a byte-compiled function defined in eglot.el.gz.

Signature

(eglot--format-markup MARKUP &optional MODE)

Documentation

Format MARKUP according to LSP's spec.

MARKUP is either an LSP MarkedString or MarkupContent object. If MODE, force MODE to be used for fontifying MARKUP.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/eglot.el.gz
(cl-defun eglot--format-markup
    (markup &optional mode
            &aux string lang render extract)
  "Format MARKUP according to LSP's spec.
MARKUP is either an LSP MarkedString or MarkupContent object.
If MODE, force MODE to be used for fontifying MARKUP."
  (cl-labels
      ((gfm-extract ()
         ;; For `gfm-view-mode', the `invisible' regions are set to
         ;; `markdown-markup'.  Set them to 't' on extraction, since
         ;; this has actual meaning in the "*eldoc*" buffer where we're
         ;; taking this string (#bug79552).
         (cl-loop with inhibit-read-only = t
                  for from = (point-min) then to
                  while (< from (point-max))
                  for inv = (get-text-property from 'invisible)
                  for to = (or (next-single-property-change from 'invisible)
                               (point-max))
                  when inv
                  do (put-text-property from to 'invisible t)
                  finally return (buffer-string)))
       (calc2 (forced-mode)
         (cond
          (forced-mode               forced-mode)
          ((fboundp eglot-documentation-renderer) eglot-documentation-renderer)
          ((fboundp 'gfm-view-mode) #'gfm-view-mode)
          (t                        #'text-mode)))
       (calc (s &optional (forced-mode mode) &aux (x (calc2 forced-mode)))
         (setq string s render x
               extract (if (eq x 'gfm-view-mode) #'gfm-extract #'buffer-string))))
    (cond ((stringp markup) (calc markup))            ; plain string
          ((setq lang (plist-get markup :language))   ; deprecated MarkedString
           (calc (format "```%s\n%s\n```" lang (plist-get markup :value))))
          (t (calc (plist-get markup :value)          ; Assume MarkupContent
                   (or mode (pcase (plist-get markup :kind)
                              ("markdown" nil)
                              ("plaintext" 'text-mode)
                              (_ major-mode))))))
    (with-temp-buffer
      (setq-local markdown-fontify-code-blocks-natively t)
      (insert string)
      (let ((inhibit-message t) (message-log-max nil))
        (ignore-errors (delay-mode-hooks (funcall render)))
        (font-lock-ensure)
        (string-trim (funcall extract))))))