Skip to content

Commit 1076328

Browse files
author
Mikael Kermorgant
committed
put phpactor-xref.el in separate file
1 parent 27e5b97 commit 1076328

File tree

3 files changed

+102
-51
lines changed

3 files changed

+102
-51
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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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"))
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+
28+
;; xref backend using Phpactor.
29+
30+
;;; Code:
31+
(require 'xref)
32+
33+
;;;###autoload
34+
(defun phpactor-xref-backend ()
35+
"Xref-phpactor backend for Xref."
36+
'phpactor-xref)
37+
38+
(cl-defmethod xref-backend-references ((_backend (eql phpactor-xref)) symbol)
39+
(phpactor-xref--xref-find-references))
40+
41+
(cl-defmethod xref-backend-definitions ((_backend (eql phpactor-xref)) symbol)
42+
(phpactor-xref--find-definitions))
43+
44+
(cl-defmethod xref-backend-identifier-completion-table ((_backend (eql phpactor-xref)))
45+
"Return a list of terms for completions taken from the symbols in the current buffer.
46+
The current implementation returns all the words in the buffer,
47+
which is really sub optimal."
48+
(list (symbol-at-point)))
49+
50+
(defun phpactor-xref--xref-find-references ()
51+
"Return a list of reference candidates."
52+
(seq-map (lambda (candidate)
53+
(phpactor-xref--make-xref candidate))
54+
(phpactor-xref--find-candidates)))
55+
56+
(defun phpactor-xref--find-definitions()
57+
"Return a list of candidates matching SYMBOL."
58+
(seq-map (lambda (candidate)
59+
(phpactor-xref--make-xref candidate))
60+
(phpactor-xref--find-candidates)))
61+
62+
(defun phpactor-xref--make-xref (candidate)
63+
"Return a new Xref object built from CANDIDATE."
64+
(xref-make (map-elt candidate 'file)
65+
(xref-make-file-location (map-elt candidate 'file)
66+
(map-elt candidate 'line)
67+
0)))
68+
69+
(defun phpactor-xref--find-candidates ()
70+
"Fetch references and return a list."
71+
(phpactor--find-references)
72+
(let ((current-references phpactor-references)
73+
matches)
74+
(dolist (file-reference current-references)
75+
(let ((path (plist-get file-reference :file)))
76+
(dolist (reference (plist-get file-reference :references))
77+
(when path
78+
(push (list (cons 'file path)
79+
(cons 'line (plist-get reference :line_no))
80+
;; (cons 'symbol symbol)
81+
;; (cons 'match match)
82+
)
83+
matches)))))
84+
matches))
85+
86+
(provide 'phpactor-xref)
87+
;;; phpactor-xref ends here

phpactor.el

Lines changed: 14 additions & 51 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)
@@ -722,57 +727,6 @@ function."
722727
(let ((arguments (phpactor--command-argments :source :path)))
723728
(apply #'phpactor-action-dispatch (phpactor--rpc "transform" (append arguments (list :transform "implement_contracts"))))))
724729

725-
(require 'xref)
726-
727-
(defun xref-phpactor-xref-backend ()
728-
"Xref-phpactor backend for Xref."
729-
'xref-phpactor)
730-
731-
(cl-defmethod xref-backend-definitions ((_backend (eql xref-phpactor)) symbol)
732-
(xref-phpactor--xref-find-definitions symbol))
733-
734-
(defun xref-phpactor--xref-find-definitions (symbol)
735-
"Return a list of candidates matching SYMBOL."
736-
(seq-map (lambda (candidate)
737-
(xref-phpactor--make-xref candidate))
738-
(xref-phpactor--find-definitions symbol)))
739-
740-
(defun xref-phpactor--make-xref (candidate)
741-
"Return a new Xref object built from CANDIDATE."
742-
(xref-make (map-elt candidate 'match)
743-
(xref-make-file-location (map-elt candidate 'file)
744-
(map-elt candidate 'line)
745-
0)))
746-
747-
(defun xref-phpactor--find-definitions (symbol)
748-
"Return a list of definitions for SYMBOL from an ag search."
749-
(xref-phpactor--find-candidates symbol))
750-
751-
(defun xref-phpactor--find-candidates (symbol)
752-
(let ((current-references phpactor-references)
753-
matches)
754-
(dolist (file-reference current-references)
755-
(let ((path (plist-get file-reference :file)))
756-
(dolist (reference (plist-get file-reference :references))
757-
(when path
758-
(push 'matches (list (cons 'file path)
759-
(cons 'line (plist-get reference :line_no))
760-
;; (cons 'symbol symbol)
761-
;; (cons 'match match)
762-
))))))
763-
matches))
764-
765-
(defun xref-phpactor--candidate (symbol file-reference)
766-
"Return a candidate alist built from SYMBOL and a raw MATCH result.
767-
The MATCH is one output result from the ag search."
768-
(let* ()
769-
;; Some minified JS files might match a search. To avoid cluttering the
770-
;; search result, we trim the output.
771-
(list (cons 'file (expand-file-name (car attrs) (xref-js2--root-dir)))
772-
(cons 'line (string-to-number (cadr attrs)))
773-
(cons 'symbol symbol)
774-
(cons 'match match))))
775-
776730
;;;###autoload
777731
(defun phpactor-find-references ()
778732
"Execute Phpactor RPC references action to find references."
@@ -781,6 +735,12 @@ The MATCH is one output result from the ag search."
781735
(apply #'phpactor-action-dispatch (phpactor--rpc "references" arguments))
782736
(phpactor-list-references)))
783737

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+
784744
;;;###autoload
785745
(defun phpactor-replace-references ()
786746
"Execute Phpactor RPC references action command to replace references."
@@ -872,5 +832,8 @@ The MATCH is one output result from the ag search."
872832
(let ((arguments (phpactor--command-argments :source :path :offset)))
873833
(apply #'phpactor-action-dispatch (phpactor--rpc "change_visibility" arguments))))
874834

835+
(when phpactor-use-xref
836+
(require 'phpactor-xref))
837+
875838
(provide 'phpactor)
876839
;;; phpactor.el ends here

0 commit comments

Comments
 (0)