|
25 | 25 | ;;; Code:
|
26 | 26 |
|
27 | 27 | (require 'lsp-mode)
|
| 28 | +(require 'find-file) |
28 | 29 |
|
29 | 30 | (defgroup lsp-ocaml nil
|
30 | 31 | "LSP support for OCaml, using ocaml-language-server."
|
|
60 | 61 | (define-obsolete-variable-alias 'lsp-merlin 'lsp-ocaml-lsp-server "lsp-mode 6.1")
|
61 | 62 | (define-obsolete-variable-alias 'lsp-merlin-command 'lsp-ocaml-lsp-server-command "lsp-mode 6.1")
|
62 | 63 |
|
| 64 | +;;; ------------------- |
| 65 | +;;; OCaml-lsp custom variables |
| 66 | +;;; ------------------- |
| 67 | + |
63 | 68 | (defcustom lsp-ocaml-lsp-server-command
|
64 | 69 | '("ocamllsp")
|
65 |
| - "Command to start ocaml-language-server." |
66 |
| - :group 'lsp-ocaml |
| 70 | + "Command to start ocaml-lsp-server." |
| 71 | + :group 'lsp-ocaml-lsp-server |
67 | 72 | :type '(choice
|
68 | 73 | (string :tag "Single string value")
|
69 | 74 | (repeat :tag "List of string values"
|
|
79 | 84 |
|
80 | 85 | (defcustom lsp-cut-signature 'space
|
81 | 86 | "If non-nil, signatures returned on hover will not be split on newline."
|
82 |
| - :group 'lsp-ocaml |
| 87 | + :group 'lsp-ocaml-lsp-server |
83 | 88 | :type '(choice (symbol :tag "Default behaviour" 'cut)
|
84 | 89 | (symbol :tag "Display all the lines with spaces" 'space)))
|
85 | 90 |
|
@@ -129,6 +134,78 @@ An example of function using STORABLE is:
|
129 | 134 | ntype)))
|
130 | 135 | type)))
|
131 | 136 |
|
| 137 | +;;; ------------------- |
| 138 | +;;; OCaml-lsp extensions interface |
| 139 | +;;; ------------------- |
| 140 | + |
| 141 | +;;; The following functions are used to create an interface between custom OCaml-lsp requests and lsp-mode |
| 142 | + |
| 143 | +(defun lsp-ocaml--switch-impl-intf () |
| 144 | + "Switch to the file(s) that the current file can switch to. |
| 145 | +
|
| 146 | +OCaml-lsp custom protocol documented here |
| 147 | +https://github.com/ocaml/ocaml-lsp/blob/master/ocaml-lsp-server/docs/ocamllsp/switchImplIntf-spec.md" |
| 148 | + (-if-let* ((params (lsp-make-ocaml-lsp-switch-impl-intf-params |
| 149 | + :uri (lsp--buffer-uri))) |
| 150 | + (uris (lsp-request "ocamllsp/switchImplIntf" params))) |
| 151 | + uris |
| 152 | + (lsp--warn "Your version of ocaml-lsp doesn't support the switchImplIntf extension"))) |
| 153 | + |
| 154 | +;;; ------------------- |
| 155 | +;;; OCaml-lsp general utilities |
| 156 | +;;; ------------------- |
| 157 | + |
| 158 | +(defun lsp-ocaml--has-one-element-p (lst) |
| 159 | + "Returns t if LST contains only one element." |
| 160 | + (and lst (= (length lst) 1))) |
| 161 | + |
| 162 | +;;; ------------------- |
| 163 | +;;; OCaml-lsp URI utilities |
| 164 | +;;; ------------------- |
| 165 | + |
| 166 | +(defun lsp-ocaml--load-uri (uri &optional other-window) |
| 167 | + "Check if URI exists and open its buffer or create a new one. |
| 168 | +
|
| 169 | +If OTHER-WINDOW is not nil, open the buffer in an other window." |
| 170 | + (let ((path (lsp--uri-to-path uri))) |
| 171 | + (cond |
| 172 | + |
| 173 | + ;; A buffer already exists with PATH |
| 174 | + ((bufferp (get-file-buffer path)) |
| 175 | + (ff-switch-to-buffer (get-file-buffer path) other-window) |
| 176 | + path) |
| 177 | + |
| 178 | + ;; PATH is an existing file |
| 179 | + ((file-exists-p path) |
| 180 | + (ff-find-file path other-window nil) |
| 181 | + path) |
| 182 | + |
| 183 | + ;; PATH is not an existing file |
| 184 | + (t |
| 185 | + nil)))) |
| 186 | + |
| 187 | +(defun lsp-ocaml--find-alternate-uri () |
| 188 | + "Return the URI corresponding to the alternate file if there's only one or prompt for a choice." |
| 189 | + (let ((uris (lsp-ocaml--switch-impl-intf))) |
| 190 | + (if (lsp-ocaml--has-one-element-p uris) |
| 191 | + (car uris) |
| 192 | + (let* ((filenames (mapcar #'f-filename uris)) |
| 193 | + (selected-file (completing-read "Choose an alternate file " filenames))) |
| 194 | + (nth (cl-position selected-file filenames :test #'string=) uris))))) |
| 195 | + |
| 196 | +;;; ------------------- |
| 197 | +;;; OCaml-lsp extensions |
| 198 | +;;; ------------------- |
| 199 | + |
| 200 | +;;; The following functions are interactive implementations of the OCaml-lsp requests |
| 201 | + |
| 202 | +(defun lsp-ocaml-find-alternate-file () |
| 203 | + "Return the URI corresponding to the alternate file if there's only one or prompt for a choice." |
| 204 | + (interactive) |
| 205 | + (let ((uri (lsp-ocaml--find-alternate-uri))) |
| 206 | + (unless (lsp-ocaml--load-uri uri nil) |
| 207 | + (message "No alternate file %s could be found for %s" (f-filename uri) (buffer-name))))) |
| 208 | + |
132 | 209 | (lsp-consistency-check lsp-ocaml)
|
133 | 210 |
|
134 | 211 | (provide 'lsp-ocaml)
|
|
0 commit comments