Skip to content

Commit

Permalink
Merge pull request #11 from tarides/jump-no-target-to-suggest-availab…
Browse files Browse the repository at this point in the history
…le-jump

Rewrite `ocaml-eglot-jump` to infer available target
  • Loading branch information
xvw authored Dec 16, 2024
2 parents 6e95199 + 6611594 commit 3f88949
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
7 changes: 3 additions & 4 deletions ocaml-eglot-req.el
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ A potential IDENTIFIER can be given and MARKUP-KIND can be parametrized."

;;; Concrete requests

(defun ocaml-eglot-req--jump (target)
"Execute the `ocamllsp/jump' request with a given TARGET."
(let ((params (append (ocaml-eglot-req--TextDocumentPositionParams)
`(:target, target))))
(defun ocaml-eglot-req--jump ()
"Execute the `ocamllsp/jump' request."
(let ((params (ocaml-eglot-req--TextDocumentPositionParams)))
(ocaml-eglot-req--send :ocamllsp/jump params)))

(defun ocaml-eglot-req--construct (depth with-local-value)
Expand Down
8 changes: 0 additions & 8 deletions ocaml-eglot-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@
"Format MARKUP according to LSP's spec."
(eglot--format-markup markup))

;; Jump features

(defun ocaml-eglot-util--extract-jump-position (jump-result)
"Extracts the position of a JUMP-RESULT of the LSP server."
(let ((target-vec (cl-getf jump-result :jumps)))
(when (> (length target-vec) 0)
(let ((real-target (aref target-vec 0)))
(cl-getf real-target :position)))))

(provide 'ocaml-eglot-util)
;;; ocaml-eglot-util.el ends here
31 changes: 18 additions & 13 deletions ocaml-eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,25 @@ If there is no available holes, it returns the first one of HOLES."

;; Jump to source elements

(defun ocaml-eglot-jump (target)
"Jumps to the begining of the closest fun/let/match/module/module-type.
The target is specified giving TARGET."
(interactive
(list (completing-read
"target: "
'("fun" "let" "match" "module"
"module-type" "match-next-case"
"match-prev-case"))))
(defun ocaml-eglot-jump ()
"Jumps to the the closest fun/let/match/module/module-type/match-case."
(interactive)
(eglot--server-capable-or-lose :experimental :ocamllsp :handleJump)
(let* ((jumps (ocaml-eglot-req--jump target))
(position (ocaml-eglot-util--extract-jump-position jumps)))
(unless position (eglot--error "No matching target"))
(ocaml-eglot-util--jump-to position)))
(let ((jumps-result (cl-getf (ocaml-eglot-req--jump) :jumps)))
(when (<= (length jumps-result) 0)
(eglot--error "No matching target"))
(let* ((jumps
(mapcar
(lambda (jump)
(let ((key (cl-getf jump :target))
(value (cl-getf jump :position)))
(cons key value)))
jumps-result))
(selected (completing-read "Target: " jumps))
(position (alist-get selected jumps nil nil #'string-equal)))
(if position
(ocaml-eglot-util--jump-to position)
(eglot--error "Target not found")))))

;; Search by type or polarity

Expand Down

0 comments on commit 3f88949

Please sign in to comment.