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 : xref backend experiment #128

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Cask
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

(package-file "phpactor.el")
(package-file "company-phpactor.el")
(package-file "phpactor-xref.el")

(development
(depends-on "php-mode")
Expand Down
88 changes: 88 additions & 0 deletions phpactor-xref.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
;;; phpactor-xref.el --- phpactor.el xref backend -*- lexical-binding: t; -*-

;; Copyright (C) 2018 Friends of Emacs-PHP development

;; Author: Mikael Kermorgant <[email protected]>
;; Created: 05 Aug 2019
;; Version: 0.1.0
;; Keywords: tools, php
;; Package-Requires: ((phpactor "0.1.0")(seq "2"))
;; URL: https://github.com/emacs-php/phpactor.el
;; License: GPL-3.0-or-later

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:
;; xref backend using Phpactor.
;; Inspiration comes from Nicolas Petton's https://github.com/NicolasPetton/xref-js2

;;; Code:
(require 'xref)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add (require 'seq).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review !

(require 'seq)

;;;###autoload
(defun phpactor-xref-backend ()
"Xref-phpactor backend for Xref."
'phpactor-xref)

(cl-defmethod xref-backend-references ((_backend (eql phpactor-xref)) symbol)
(phpactor-xref--xref-find-references))

(cl-defmethod xref-backend-definitions ((_backend (eql phpactor-xref)) symbol)
(phpactor-xref--find-definitions))

(defun phpactor--xref-find-definitions ()
"Find definitions or jump directly when only one found."
(phpactor-goto-definition))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works but with a mysterious Wrong type argument: listp 2070


(cl-defmethod xref-backend-identifier-completion-table ((_backend (eql phpactor-xref)))
"Return a list of terms for completions taken from the symbols in the current buffer.
The current implementation returns all the words in the buffer,
which is really sub optimal."
(list (symbol-at-point)))

(defun phpactor-xref--xref-find-references ()
"Return a list of reference candidates."
(seq-map (lambda (candidate)
(phpactor-xref--make-xref candidate))
(phpactor-xref--find-candidates)))

(defun phpactor-xref--make-xref (candidate)
"Return a new Xref object built from CANDIDATE."
(xref-make (map-elt candidate 'match)
(xref-make-file-location (map-elt candidate 'file)
(map-elt candidate 'line)
(map-elt candidate 'col))))

(defun phpactor-xref--find-candidates ()
"Fetch references and return a list."
(phpactor--find-references)
(let ((current-references phpactor-references)
matches)
(dolist (file-reference current-references)
(let ((path (plist-get file-reference :file)))
(dolist (reference (plist-get file-reference :references))
(when path
(push (list (cons 'file path)
(cons 'line (plist-get reference :line_no))
(cons 'col (plist-get reference :col_no))
(cons 'match (plist-get reference :line))
;; (cons 'symbol symbol)
;; (cons 'match match)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth trying to fetch content of line at :line_no, which could permit to get a preview while listing candidates in minibuffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that was already given using :line

)
matches)))))
matches))

(provide 'phpactor-xref)
;;; phpactor-xref.el ends here
14 changes: 14 additions & 0 deletions phpactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
"If non-nil, use native json parsing if available."
:group 'phpactor
:type 'boolean)

(defcustom phpactor-use-xref t
"Defines phpactor xref backend."
:group 'phpactor
:type 'boolean)

;; Variables
(defvar phpactor--debug nil)
Expand Down Expand Up @@ -761,6 +766,12 @@ function."
(apply #'phpactor-action-dispatch (phpactor--rpc "references" arguments))
(phpactor-list-references)))

;;;###autoload
(defun phpactor--find-references ()
"Execute Phpactor RPC references action to find references."
(let ((arguments (phpactor--command-argments :source :path :offset)))
(apply #'phpactor-action-dispatch (phpactor--rpc "references" arguments))))

;;;###autoload
(defun phpactor-replace-references ()
"Execute Phpactor RPC references action command to replace references."
Expand Down Expand Up @@ -859,5 +870,8 @@ function."
(let ((arguments (phpactor--command-argments :source :path)))
(apply #'phpactor-action-dispatch (phpactor--rpc "override_method" arguments))))

(autoload 'phpactor-xref-backend "phpactor-xref"
"Phpactor backend for Xref.")

(provide 'phpactor)
;;; phpactor.el ends here