Skip to content

Commit 26ec6ac

Browse files
kermorgantMikael Kermorgant
authored and
Mikael Kermorgant
committed
xref backend using phpactor
Thanks Nicolas Petton for inspiring this with your work on xref-js2.
1 parent 16561e8 commit 26ec6ac

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

Cask

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
(package-file "phpactor.el")
66
(package-file "company-phpactor.el")
7+
(package-file "phpactor-xref.el")
78

89
(development
910
(depends-on "php-mode")

phpactor-xref.el

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
;;; phpactor-xref.el --- phpactor.el xref backend -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2018 Friends of Emacs-PHP development
4+
5+
;; Author: Mikael Kermorgant <[email protected]>
6+
;; Created: 05 Aug 2019
7+
;; Version: 0.1.0
8+
;; Keywords: tools, php
9+
;; Package-Requires: ((phpactor "0.1.0")(seq "2"))
10+
;; URL: https://github.com/emacs-php/phpactor.el
11+
;; License: GPL-3.0-or-later
12+
13+
;; This program is free software; you can redistribute it and/or modify
14+
;; it under the terms of the GNU General Public License as published by
15+
;; the Free Software Foundation, either version 3 of the License, or
16+
;; (at your option) any later version.
17+
18+
;; This program is distributed in the hope that it will be useful,
19+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
;; GNU General Public License for more details.
22+
23+
;; You should have received a copy of the GNU General Public License
24+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
25+
26+
;;; Commentary:
27+
;; xref backend using Phpactor.
28+
;; Inspiration comes from Nicolas Petton's https://github.com/NicolasPetton/xref-js2
29+
30+
;;; Code:
31+
(require 'xref)
32+
(require 'seq)
33+
34+
;;;###autoload
35+
(defun phpactor-xref-backend ()
36+
"Xref-phpactor backend for Xref."
37+
'phpactor-xref)
38+
39+
(cl-defmethod xref-backend-references ((_backend (eql phpactor-xref)) symbol)
40+
(phpactor-xref--xref-find-references))
41+
42+
(cl-defmethod xref-backend-definitions ((_backend (eql phpactor-xref)) symbol)
43+
(phpactor-xref--find-definitions))
44+
45+
(cl-defmethod xref-backend-identifier-completion-table ((_backend (eql phpactor-xref)))
46+
"Return a list of terms for completions taken from the symbols in the current buffer.
47+
The current implementation returns all the words in the buffer,
48+
which is really sub optimal."
49+
(list (symbol-at-point)))
50+
51+
(defun phpactor-xref--xref-find-references ()
52+
"Return a list of reference candidates."
53+
(seq-map (lambda (candidate)
54+
(phpactor-xref--make-xref candidate))
55+
(phpactor-xref--find-candidates)))
56+
57+
(defun phpactor-xref--find-definitions()
58+
"Return a list of candidates matching SYMBOL."
59+
(seq-map (lambda (candidate)
60+
(phpactor-xref--make-xref candidate))
61+
(phpactor-xref--find-candidates)))
62+
63+
(defun phpactor-xref--make-xref (candidate)
64+
"Return a new Xref object built from CANDIDATE."
65+
(xref-make (map-elt candidate 'match)
66+
(xref-make-file-location (map-elt candidate 'file)
67+
(map-elt candidate 'line)
68+
(map-elt candidate 'col))))
69+
70+
(defun phpactor-xref--find-candidates ()
71+
"Fetch references and return a list."
72+
(phpactor--find-references)
73+
(let ((current-references phpactor-references)
74+
matches)
75+
(dolist (file-reference current-references)
76+
(let ((path (plist-get file-reference :file)))
77+
(dolist (reference (plist-get file-reference :references))
78+
(when path
79+
(push (list (cons 'file path)
80+
(cons 'line (plist-get reference :line_no))
81+
(cons 'col (plist-get reference :col_no))
82+
(cons 'match (plist-get reference :line))
83+
;; (cons 'symbol symbol)
84+
;; (cons 'match match)
85+
)
86+
matches)))))
87+
matches))
88+
89+
(provide 'phpactor-xref)
90+
;;; phpactor-xref.el ends here

phpactor.el

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
"If non-nil, use native json parsing if available."
7373
:group 'phpactor
7474
:type 'boolean)
75+
76+
(defcustom phpactor-use-xref t
77+
"Defines phpactor xref backend."
78+
:group 'phpactor
79+
:type 'boolean)
7580

7681
;; Variables
7782
(defvar phpactor--debug nil)
@@ -730,6 +735,12 @@ function."
730735
(apply #'phpactor-action-dispatch (phpactor--rpc "references" arguments))
731736
(phpactor-list-references)))
732737

738+
;;;###autoload
739+
(defun phpactor--find-references ()
740+
"Execute Phpactor RPC references action to find references."
741+
(let ((arguments (phpactor--command-argments :source :path :offset)))
742+
(apply #'phpactor-action-dispatch (phpactor--rpc "references" arguments))))
743+
733744
;;;###autoload
734745
(defun phpactor-replace-references ()
735746
"Execute Phpactor RPC references action command to replace references."
@@ -821,5 +832,8 @@ function."
821832
(let ((arguments (phpactor--command-argments :source :path :offset)))
822833
(apply #'phpactor-action-dispatch (phpactor--rpc "change_visibility" arguments))))
823834

835+
(autoload 'phpactor-xref-backend "phpactor-xref"
836+
"Phpactor backend for Xref.")
837+
824838
(provide 'phpactor)
825839
;;; phpactor.el ends here

0 commit comments

Comments
 (0)