Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct improvement #24

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
ocaml-eglot unreleased
=================

- Improve the behaviour of construct
([#24](https://github.com/tarides/ocaml-eglot/issues/24) fixes
[#23](https://github.com/tarides/ocaml-eglot/issues/23))

ocaml-eglot 1.0.0
=================
Fri Jan 17 04:50:35 PM CET 2025
Expand Down
53 changes: 32 additions & 21 deletions ocaml-eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -430,35 +430,46 @@ of result (LIMIT)."
"local"
"none"))

(defun ocaml-eglot--construct-with-hole (arg hole)
"Construct over the current HOLE.
It use the ARG to use local values or not."
(let* ((with-local-value (ocaml-eglot--construct-local-values arg))
(hole-start (cl-getf hole :start))
(result (ocaml-eglot-req--construct hole-start 1 with-local-value))
(range (cl-getf result :position))
(suggestions (append (cl-getf result :result) nil)))
(when (= (length suggestions) 0)
xvw marked this conversation as resolved.
Show resolved Hide resolved
(eglot--error "No constructors for this hole"))
(cl-labels
((insert-construct-choice (subst)
(let* ((start (cl-getf range :start))
(end (ocaml-eglot-util--position-increase-char
start subst)))
(ocaml-eglot-util--replace-region range subst)
(ocaml-eglot--first-hole-in start end))))
(if (= (length suggestions) 1)
(insert-construct-choice (car suggestions))
(let ((choice (completing-read "Constructor: " suggestions nil t)))
(insert-construct-choice choice))))))

(defun ocaml-eglot-construct (&optional arg)
"Construct over the current hole.
"Construct over the current hole or insert-it.
It use the ARG to use local values or not."
(interactive "P")
(eglot--server-capable-or-lose :experimental :ocamllsp :handleConstruct)
(let* ((current-range (ocaml-eglot-util--current-range))
(start (cl-getf current-range :start))
(end (cl-getf current-range :end))
(hole (ocaml-eglot--get-first-hole-in start end)))
(if (not hole)
(eglot--error "Not a hole")
(let* ((with-local-value (ocaml-eglot--construct-local-values arg))
(hole-start (cl-getf hole :start))
(result (ocaml-eglot-req--construct hole-start 1 with-local-value))
(range (cl-getf result :position))
(suggestions (append (cl-getf result :result) nil)))
(when (= (length suggestions) 0)
(eglot--error "No constructors for this hole"))
(cl-labels
((insert-construct-choice (subst)
(let* ((start (cl-getf range :start))
(end (ocaml-eglot-util--position-increase-char
start subst)))
(ocaml-eglot-util--replace-region range subst)
(ocaml-eglot--first-hole-in start end))))
(if (= (length suggestions) 1)
(insert-construct-choice (car suggestions))
(let ((choice (completing-read "Constructor: " suggestions nil t)))
(insert-construct-choice choice))))))))
(if hole
(ocaml-eglot--construct-with-hole arg hole)
(when (not (equal (symbol-at-point) '_))
(save-excursion (insert "_"))
(let ((hole (ocaml-eglot--get-first-hole-in start end)))
(if (not hole)
(progn (delete-char 1)
(eglot--error "Not a hole"))
(ocaml-eglot--construct-with-hole arg hole)))))))

;; Get Documentation

Expand Down
Loading