Skip to content

Commit 9bcd62e

Browse files
authored
Merge pull request #55 from kermorgant/rpc-references
support for "replace reference" rpc action
2 parents 77086c9 + a3706b7 commit 9bcd62e

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

phpactor.el

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,58 @@
270270
(view-mode 1))
271271
(pop-to-buffer buffer)))
272272

273+
(defvar phpactor-references nil)
274+
275+
(cl-defun phpactor-action-file-references (&key file_references)
276+
"Receives a list of file references for information purpose."
277+
(setq-local phpactor-references file_references)
278+
(message "Phpactor changed %d references(s), use phpactor-list-references to check them" (length file_references)))
279+
280+
;;; Listing references in this buffer
281+
(defconst phpactor-references-buffer "*Phpactor references*"
282+
"The name of the buffer to list referenced files.")
283+
284+
(defconst phpactor-references-list-col1-width 60)
285+
286+
(defun phpactor-truncate-left (string width)
287+
"Truncate STRING to WIDTH starting from the end, prepending ..."
288+
(if (> (length string) width)
289+
(concat "..." (substring string (- 3 width)))
290+
string))
291+
292+
(defun phpactor-open-file-other-window (path offset)
293+
"Open PATH at OFFSET in a different window."
294+
(save-selected-window
295+
(when (buffer-live-p (get-file-buffer path))
296+
(switch-to-buffer-other-window (get-file-buffer path)))
297+
(phpactor-action-open-file :path path :offset offset)))
298+
299+
(defun phpactor-references-list-make-entry (file-reference index)
300+
"Return an entry for the tabulated list, for FILE-REFERENCE at INDEX."
301+
(let ((path (plist-get file-reference :file))
302+
(in-project-path (string-remove-prefix (phpactor-get-working-dir) (plist-get file-reference :file)))
303+
(references (car (plist-get file-reference :references))))
304+
(list index
305+
(vector (list
306+
(phpactor-truncate-left in-project-path phpactor-references-list-col1-width)
307+
. ('action
308+
(lambda (event) (phpactor-open-file-other-window path (plist-get references :start)))
309+
'help-echo path))
310+
(number-to-string (plist-get references :line_no))))))
311+
312+
;; adapted from flycheck-list-errors
313+
(cl-defun phpactor-list-references ()
314+
(interactive)
315+
(let ((current-references phpactor-references))
316+
(with-current-buffer (get-buffer-create phpactor-references-buffer)
317+
(setq tabulated-list-format (vector `("File" ,phpactor-references-list-col1-width nil) '("Line" 12 nil :right-align t))
318+
tabulated-list-padding 2
319+
tabulated-list-entries (seq-map-indexed #'phpactor-references-list-make-entry current-references))
320+
(tabulated-list-mode)
321+
(tabulated-list-init-header)
322+
(tabulated-list-print t)
323+
(switch-to-buffer-other-window phpactor-references-buffer))))
324+
273325
(cl-defun phpactor-action-collection (&key actions)
274326
"Executes a collection of actions."
275327
(mapc
@@ -506,5 +558,12 @@ function."
506558
(let ((arguments (phpactor--command-argments :source :path)))
507559
(apply #'phpactor-action-dispatch (phpactor--rpc "transform" (append arguments (list :transform "complete_constructor"))))))
508560

561+
;;;###autoload
562+
(defun phpactor-replace-references ()
563+
"Execute Phpactor PRC replace_references command to complete_constructor."
564+
(interactive)
565+
(let ((arguments (phpactor--command-argments :source :path :offset)))
566+
(apply #'phpactor-action-dispatch (phpactor--rpc "references" (append arguments (list :mode "replace"))))))
567+
509568
(provide 'phpactor)
510569
;;; phpactor.el ends here

0 commit comments

Comments
 (0)