Skip to content

Commit

Permalink
Merge pull request #16 from tarides/chore-before-release-yay
Browse files Browse the repository at this point in the history
Fix #15
  • Loading branch information
xvw authored Jan 14, 2025
2 parents 1578210 + cfefbb1 commit 904707e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
11 changes: 11 additions & 0 deletions ocaml-eglot-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
(when (> (length vec) 0)
(aref vec 0)))

(defun ocaml-eglot-util--load-uri (uri)
"Check and load if URI is available for typechecking."
(let ((path (eglot--uri-to-path uri)))
(when (file-exists-p path)
(if (member path (mapcar #'buffer-file-name (buffer-list)))
t
(let ((buf (current-buffer)))
(find-file path)
(switch-to-buffer buf)
t)))))

(defun ocaml-eglot-util--replace-region (range content)
"Replace a LSP region (RANGE) by a given CONTENT."
(pcase-let ((`(,beg . ,end) (eglot--range-region range)))
Expand Down
28 changes: 16 additions & 12 deletions ocaml-eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,24 @@ Show it in the current window."
(let ((uris (ocaml-eglot-req--switch-file uri)))
(ocaml-eglot-util--vec-first-or-nil uris)))

(defun ocaml-eglot-infer-interface ()
"Infer the interface for the current file."
(defun ocaml-eglot-infer-interface (&optional need-confirmation)
"Infer the interface for the current file.
If NEED-CONFIRMATION is set to non-nil, it will prompt a confirmation."
(interactive)
(eglot--server-capable-or-lose :experimental :ocamllsp :handleInferIntf)
(ocaml-eglot-util--ensure-interface)
(let* ((current-uri (ocaml-eglot-util--current-uri))
(impl-uri (ocaml-eglot--find-alternate-file current-uri))
(result (ocaml-eglot-req--infer-intf impl-uri)))
(when (or
(= (buffer-size) 0)
(yes-or-no-p "The buffer is not empty, overwrite it? "))
(erase-buffer)
(insert result))))
(impl-uri (ocaml-eglot--find-alternate-file current-uri)))
(if (ocaml-eglot-util--load-uri impl-uri)
(when (or (not need-confirmation)
(y-or-n-p "Try to generate interface? ") )
(let ((result (ocaml-eglot-req--infer-intf impl-uri)))
(when (or (= (buffer-size) 0)
(y-or-n-p "The buffer is not empty, overwrite it? "))
(erase-buffer)
(insert result))))
(when (not need-confirmation)
(eglot--error "%s is not loaded" impl-uri)))))

;; Find alternate file `ml<->mli'

Expand All @@ -234,9 +239,8 @@ Show it in the current window."
"Hook to try to generate interface on visiting new files.."
(when (and
(ocaml-eglot-util--on-interface)
(= (buffer-size) 0)
(yes-or-no-p "Try to generate interface? "))
(call-interactively #'ocaml-eglot-infer-interface)))
(= (buffer-size) 0))
(ocaml-eglot-infer-interface t )))

;; Holes

Expand Down

0 comments on commit 904707e

Please sign in to comment.