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

Rewrite ocaml-eglot-jump to infer available target #11

Merged
merged 1 commit into from
Dec 16, 2024
Merged
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: 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
Loading