Skip to content

lsp-ui-doc--extract renders content kind=markdown as plaintext #193

@cpbotha

Description

@cpbotha

lsp-ui-doc--extract returns kind=markdown contents as plaintext, while there is great support for showing markdown.

See for example the screenshot below:

lsp-mode-displays-markdown-hover-as-plain

This hover text is returned by the Microsoft Python Language Server (same one as for visual studio code). Two issues: 1. All of those   entities (I have also logged microsoft/python-language-server#422 for this) and 2. Emacs is not displaying the markdown.

I was able to fix both issues by modifying lsp-ui-doc--extract as follows:

(defun lsp-ui-doc--extract (contents)
  "Extract the documentation from CONTENTS.
CONTENTS can be differents type of values:
MarkedString | MarkedString[] | MarkupContent (as defined in the LSP).
We don't extract the string that `lps-line' is already displaying."
  (when contents
    (cond
     ((stringp contents) contents)
     ((sequencep contents) ;; MarkedString[]
      (mapconcat 'lsp-ui-doc--extract-marked-string
                 (lsp-ui-doc--filter-marked-string contents)
                 "\n\n"
                 ;; (propertize "\n\n" 'face '(:height 0.4))
                 ))

     ;; cpbotha: with numpy functions, e.g. np.array for example, kind=markdown
     ;; and docs are in markdown, but in default lsp-ui-20181031 this is rendered as plaintext
     ;; see https://microsoft.github.io/language-server-protocol/specificatoin#markupcontent
     ;; not only that, MS PyLS turns all spaces into   instances, which we remove here
     ;; this single additional cond clause fixes all of this for hover
     ;; as if that was not enough: import pandas as pd - pandas is returned with kind plaintext
     ;; but contents markdown, whereas pd is returned with kind markdown. fortunately,
     ;; handling plaintext with the markdown viewer still looks good, so here we are.
     ((member (gethash "kind" contents) '("markdown" "plaintext"))
      (replace-regexp-in-string " " " " (lsp-ui-doc--extract-marked-string contents)))
     
     ((gethash "kind" contents) (gethash "value" contents)) ;; MarkupContent
     ((gethash "language" contents) ;; MarkedString
      (lsp-ui-doc--extract-marked-string contents)))))

You'll see that I'm sending both markdown and plaintext to the markdown handler. One could decide to only do this for markdown. Anyways, after applying this change, hover looks like:

emacs-ms-lsp-docbox-and-describe-term

Making this change would probably fix issue #172 also.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions