|
| 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 |
0 commit comments