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

[WIP] Refactor the definitions of RPC functions #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
89 changes: 42 additions & 47 deletions phpactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -491,47 +491,55 @@ function."
;;
;; See https://phpactor.github.io/phpactor/rpc.html#phpactor-commands

;;;###autoload
(defun phpactor-copy-class ()
"Execute Phpactor RPC copy_class command."
(interactive)
(let ((arguments (phpactor--command-argments :source_path)))
(apply #'phpactor-action-dispatch (phpactor--rpc "copy_class" arguments))))
(defmacro phpactor-defrpc (name command-arguments)
"Define a function which calls a Phpactor RPC command.

;;;###autoload
(defun phpactor-move-class ()
"Execute Phpactor RPC move_class command."
(interactive)
(let ((arguments (phpactor--command-argments :source_path)))
(apply #'phpactor-action-dispatch (phpactor--rpc "move_class" arguments))))
The NAME must be the name of the RPC command as a string,
e.g. 'classs_search', with underscores if needed. The macro will
convert those underscores to dashes for the function name,
e.g. it would define the function 'phpactor-class-search' given
the example NAME above.

;;;###autoload
(defun phpactor-offset-info ()
"Execute Phpactor RPC offset_info command."
(interactive)
(let ((arguments (phpactor--command-argments :source :offset)))
(apply #'phpactor-action-dispatch (phpactor--rpc "offset_info" arguments))))
The COMMAND-ARGUMENTS must be a list of symbols acceptable to the
`phpactor--command-argments' function. Even if there is only one
argument, it must be in a list nonetheless.

;;;###autoload
(defun phpactor-transform ()
"Execute Phpactor RPC transform command."
(interactive)
(let ((arguments (phpactor--command-argments :source :path)))
(apply #'phpactor-action-dispatch (phpactor--rpc "transform" arguments))))
For example:

(phpactor-defrpc \"class_search\" '(:source_path))

This will create a function, `phpactor-class-search', which will
invoke the 'class_search' Phpactor RPC command. The resulting
function will accept no arguments. And it will have a generic
docstring.

See `phpactor--rpc-command-data' for a list of the data we pass along
to this macro."
(let ((function-name (intern (concat "phpactor-" (replace-regexp-in-string "_" "-" name)))))
`(defun ,function-name ()
,(format "Execute Phpactor RPC %s command." name)
(interactive)
(let ((arguments ((phpactor--command-argments ,@command-arguments))))
(apply #'phpactor-action-dispatch (phpactor--rpc ,name arguments))))))

;;;###autoload
(defun phpactor-context-menu ()
"Execute Phpactor PRC context_menu command."
(interactive)
(let ((arguments (phpactor--command-argments :source :offset :current_path)))
(apply #'phpactor-action-dispatch (phpactor--rpc "context_menu" arguments))))
(defconst phpactor--rpc-command-data
'(("complete" . ())
("class_search" . ())
("copy_class" . (:source_path))
("move_class" . (:source_path))
("offset_info" . (:source :offset))
("transform" . (:source :path))
("context_menu" . (:source :offset :current_path))
("navigate" . (:source_path))
("status" . ())
("goto_definition" . (:source :offset :path)))
"A list of all Phpactor RPC commands and the respective data
necessary for `phpactor-defrpc' to create functions for each.")

;;;###autoload
(defun phpactor-navigate ()
"Execute Phpactor RPC navigate command."
(interactive)
(let ((arguments (phpactor--command-argments :source_path)))
(apply #'phpactor-action-dispatch (phpactor--rpc "navigate" arguments))))
(dolist (rpc-data phpactor--rpc-command-data)
(phpactor-defrpc (car rpc-data) (cdr rpc-data)))

;;;###autoload
(defun phpactor-echo (message)
Expand All @@ -540,19 +548,6 @@ function."
(let ((phpactor-action--message-format "Message from Phpactor: %s"))
(apply #'phpactor-action-dispatch (phpactor--rpc "echo" (list :message message)))))

;;;###autoload
(defun phpactor-status ()
"Execute Phpactor RPC status command, and pop to buffer."
(interactive)
(apply #'phpactor-action-dispatch (phpactor--rpc "status" [])))

;;;###autoload
(defun phpactor-goto-definition ()
"Execute Phpactor RPC goto_definition command."
(interactive)
(let ((arguments (phpactor--command-argments :source :offset :path)))
(apply #'phpactor-action-dispatch (phpactor--rpc "goto_definition" arguments))))

;;;###autoload
(defun phpactor-import-class (name)
"Execute Phpactor PRC import_class command for class NAME."
Expand Down