lsp-ocaml.el (4950B)
1 ;;; lsp-ocaml.el --- description -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2020 emacs-lsp maintainers 4 5 ;; Author: emacs-lsp maintainers 6 ;; Keywords: lsp, ocaml 7 8 ;; This program is free software; you can redistribute it and/or modify 9 ;; it under the terms of the GNU General Public License as published by 10 ;; the Free Software Foundation, either version 3 of the License, or 11 ;; (at your option) any later version. 12 13 ;; This program is distributed in the hope that it will be useful, 14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ;; GNU General Public License for more details. 17 18 ;; You should have received a copy of the GNU General Public License 19 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 20 21 ;;; Commentary: 22 23 ;; LSP Clients for the Ocaml Programming Language. 24 25 ;;; Code: 26 27 (require 'lsp-mode) 28 29 (defgroup lsp-ocaml nil 30 "LSP support for OCaml, using ocaml-language-server." 31 :group 'lsp-mode 32 :link '(url-link "https://github.com/freebroccolo/ocaml-language-server")) 33 34 (define-obsolete-variable-alias 35 'lsp-ocaml-ocaml-lang-server-command 36 'lsp-ocaml-lang-server-command 37 "lsp-mode 6.1") 38 39 (defcustom lsp-ocaml-lang-server-command 40 '("ocaml-language-server" "--stdio") 41 "Command to start ocaml-language-server." 42 :group 'lsp-ocaml 43 :type '(choice 44 (string :tag "Single string value") 45 (repeat :tag "List of string values" 46 string))) 47 48 (lsp-register-client 49 (make-lsp-client :new-connection (lsp-stdio-connection 50 (lambda () lsp-ocaml-lang-server-command)) 51 :major-modes '(reason-mode caml-mode tuareg-mode) 52 :priority -1 53 :server-id 'ocaml-ls)) 54 55 (defgroup lsp-ocaml-lsp-server nil 56 "LSP support for OCaml, using ocaml-lsp-server." 57 :group 'lsp-mode 58 :link '(url-link "https://github.com/ocaml/ocaml-lsp")) 59 60 (define-obsolete-variable-alias 'lsp-merlin 'lsp-ocaml-lsp-server "lsp-mode 6.1") 61 (define-obsolete-variable-alias 'lsp-merlin-command 'lsp-ocaml-lsp-server-command "lsp-mode 6.1") 62 63 (defcustom lsp-ocaml-lsp-server-command 64 '("ocamllsp") 65 "Command to start ocaml-language-server." 66 :group 'lsp-ocaml 67 :type '(choice 68 (string :tag "Single string value") 69 (repeat :tag "List of string values" 70 string))) 71 72 (lsp-register-client 73 (make-lsp-client 74 :new-connection 75 (lsp-stdio-connection (lambda () lsp-ocaml-lsp-server-command)) 76 :major-modes '(reason-mode caml-mode tuareg-mode) 77 :priority 0 78 :server-id 'ocaml-lsp-server)) 79 80 (defcustom lsp-cut-signature 'space 81 "If non-nil, signatures returned on hover will not be split on newline." 82 :group 'lsp-ocaml 83 :type '(choice (symbol :tag "Default behaviour" 'cut) 84 (symbol :tag "Display all the lines with spaces" 'space))) 85 86 (cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql ocaml-lsp-server)) &optional storable) 87 "Extract a representative line from OCaml's CONTENTS, to show in the echo area. 88 This function splits the content between the signature 89 and the documentation to display the signature 90 and truncate it if it's too wide. 91 The STORABLE argument is used if you want to use this 92 function to get the type and, for example, kill and yank it. 93 94 An example of function using STORABLE is: 95 96 (defun mdrp/lsp-get-type-and-kill () 97 (interactive) 98 (let ((contents (-some->> (lsp--text-document-position-params) 99 (lsp--make-request \"textDocument/hover\") 100 (lsp--send-request) 101 (lsp:hover-contents)))) 102 (let ((contents (and contents 103 (lsp--render-on-hover-content 104 contents 105 t)))) 106 (let ((contents 107 (pcase (lsp-workspaces) 108 (`(,workspace) 109 (lsp-clients-extract-signature-on-hover 110 contents 111 (lsp--workspace-server-id workspace) 112 t)) 113 (lsp-clients-extract-signature-on-hover 114 contents 115 nil) 116 ))) 117 (message \"Copied %s to kill-ring\" contents) 118 (kill-new contents)))))" 119 (let ((type (s-trim (lsp--render-element (lsp-make-marked-string 120 :language "ocaml" 121 :value (car (s-split "---" (lsp--render-element contents)))))))) 122 (if (equal nil storable) 123 (if (eq lsp-cut-signature 'cut) 124 (car (s-lines type)) 125 ;; else lsp-cut-signature is 'space 126 (let ((ntype (s-replace "\n" " " type))) 127 (if (>= (length ntype) (frame-width)) 128 (concat (substring ntype 0 (- (frame-width) 4)) "...") 129 ntype))) 130 type))) 131 132 (lsp-consistency-check lsp-ocaml) 133 134 (provide 'lsp-ocaml) 135 ;;; lsp-ocaml.el ends here