From 0e6a7dd3588cd398ba1260c52be7c74280b37733 Mon Sep 17 00:00:00 2001 From: xvw Date: Mon, 20 Jan 2025 12:27:52 +0100 Subject: [PATCH 1/3] Improve the behaviour of construct (fix #23) Co-authored-by: Shon Feder --- ocaml-eglot.el | 53 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/ocaml-eglot.el b/ocaml-eglot.el index 839e9ed..cfbb6b0 100644 --- a/ocaml-eglot.el +++ b/ocaml-eglot.el @@ -430,8 +430,30 @@ 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) + (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) @@ -439,26 +461,15 @@ It use the ARG to use local values or not." (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) + (if (not (equal (symbol-at-point) '_)) + (progn (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 From c03b417200885237c95bf879d9ff4324f0fea344 Mon Sep 17 00:00:00 2001 From: xvw Date: Mon, 20 Jan 2025 12:33:20 +0100 Subject: [PATCH 2/3] Add CHANGES entry --- CHANGES.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 6175c02..92f9b2d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 From 26726a5964a59480fb7288986fc065354f1e4771 Mon Sep 17 00:00:00 2001 From: xvw Date: Mon, 20 Jan 2025 12:37:30 +0100 Subject: [PATCH 3/3] Use `when` instead of `if` --- ocaml-eglot.el | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ocaml-eglot.el b/ocaml-eglot.el index cfbb6b0..8d9e315 100644 --- a/ocaml-eglot.el +++ b/ocaml-eglot.el @@ -463,13 +463,13 @@ It use the ARG to use local values or not." (hole (ocaml-eglot--get-first-hole-in start end))) (if hole (ocaml-eglot--construct-with-hole arg hole) - (if (not (equal (symbol-at-point) '_)) - (progn (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)))))))) + (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