Skip to content

Commit

Permalink
Add support for get documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
xvw committed Dec 10, 2024
1 parent 64caa2c commit c038a86
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
17 changes: 17 additions & 0 deletions ocaml-eglot-req.el
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ included and the documentation output can be set using MARKUP-KIND."
`(:with_doc, with-doc)
`(:doc_dormat, markup-kind)))

(defun ocaml-eglot-req--GetDocumentationParam (identifier markup-kind)
"Compute the `GetDocumentationParam'.
A potential IDENTIFIER can be given and MARKUP-KIND can be parametrized."
(let ((params (append (ocaml-eglot-req--TextDocumentPositionParams)
`(:contentFormat, markup-kind))))
(if identifier (append params `(:identifier, identifier))
params)))

;;; Concrete requests

(defun ocaml-eglot-req--jump (target)
Expand Down Expand Up @@ -121,5 +129,14 @@ The markup used to format documentation can be set using MARKUP-KIND."
(let ((params (make-vector 1 uri)))
(ocaml-eglot-req--send :ocamllsp/inferIntf params)))

(defun ocaml-eglot-req--get-documentation (identifier markup-kind)
"Execute the `ocamllsp/getDocumentation'.
If IDENTIFIER is non-nil, it documents it, otherwise, it use the identifier
under the cursor. The MARKUP-KIND can also be configured."
(let ((params (ocaml-eglot-req--GetDocumentationParam
identifier
markup-kind)))
(ocaml-eglot-req--send :ocamllsp/getDocumentation params)))

(provide 'ocaml-eglot-req)
;;; ocaml-eglot-req.el ends here
4 changes: 4 additions & 0 deletions ocaml-eglot-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
(when (not (ocaml-eglot-util--on-interface))
(eglot--error "Function is only available for interfaces")))

(defun ocaml-eglot-util--format-markup (markup)
"Format MARKUP according to LSP's spec."
(eglot--format-markup markup))

;; Jump features

(defun ocaml-eglot-util--extract-jump-position (jump-result)
Expand Down
28 changes: 25 additions & 3 deletions ocaml-eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Otherwise, `merlin-construct' only includes constructors."
:group 'ocaml-eglot
:type 'boolean)

(defcustom ocaml-eglot-preferred-markupkind "plaintext"
(defcustom ocaml-eglot-preferred-markupkind "markdown"
"Preferred markup format."
:group 'ocaml-eglot
:type 'string)
Expand Down Expand Up @@ -274,9 +274,10 @@ of result (LIMIT)."
(let* ((start (eglot--pos-to-lsp-position))
(limit (or(if (> limit 1) limit nil)
ocaml-eglot-type-search-limit 25))
(markup-kind ocaml-eglot-preferred-markupkind)
(with-doc (or ocaml-eglot-type-search-include-doc :json-false))
(entries (ocaml-eglot-req--search query limit with-doc markup-kind))
;; We use plaintext because the result of the documentation may
;; be truncated
(entries (ocaml-eglot-req--search query limit with-doc "plaintext"))
(choices (ocaml-eglot--search-to-completion entries))
(chosen (ocaml-eglot--search-completion
choices
Expand Down Expand Up @@ -323,6 +324,26 @@ It use the ARG to use local values or not."
(insert-construct-choice choice))))))


;; Get Documentation

(defun ocaml-eglot--document-aux (identifier)
"Displays the OCaml documentation for the IDENTIFIER under the cursor."
(when-let* ((result (ocaml-eglot-req--get-documentation
identifier ocaml-eglot-preferred-markupkind))
(doc-value (cl-getf result :doc))
(formated-value (ocaml-eglot-util--format-markup doc-value)))
(message "%s" formated-value)))

(defun ocaml-eglot-document ()
"Displays the OCaml documentation for the identifier under the cursor."
(interactive)
(ocaml-eglot--document-aux nil))

(defun ocaml-eglot-document-identifier (identifier)
"Displays the OCaml documentation for a given IDENTIFIER."
(interactive "sIdentifier: ")
(ocaml-eglot--document-aux identifier))

;;; Mode

(defvar ocaml-eglot-map
Expand All @@ -331,6 +352,7 @@ It use the ARG to use local values or not."
(define-key ocaml-eglot-keymap (kbd "C-c C-c") #'ocaml-eglot-error-prev)
(define-key ocaml-eglot-keymap (kbd "C-c C-l") #'ocaml-eglot-locate)
(define-key ocaml-eglot-keymap (kbd "C-c C-a") #'ocaml-eglot-alternate-file)
(define-key ocaml-eglot-keymap (kbd "C-c C-d") #'ocaml-eglot-document)
ocaml-eglot-keymap)
"Keymap for OCaml-eglot minor mode.")

Expand Down

0 comments on commit c038a86

Please sign in to comment.