|
| 1 | +;;; php-ide-phpactor.el --- PHP-IDE feature using Phpactor RPC -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2022 Friends of Emacs-PHP development |
| 4 | + |
| 5 | +;; Author: USAMI Kenta <[email protected]> |
| 6 | +;; Keywords: tools, files |
| 7 | +;; URL: https://github.com/emacs-php/php-mode |
| 8 | +;; Version: 1.24.0 |
| 9 | +;; License: GPL-3.0-or-later |
| 10 | + |
| 11 | +;; This program is free software; you can redistribute it and/or modify |
| 12 | +;; it under the terms of the GNU General Public License as published by |
| 13 | +;; the Free Software Foundation, either version 3 of the License, or |
| 14 | +;; (at your option) any later version. |
| 15 | + |
| 16 | +;; This program is distributed in the hope that it will be useful, |
| 17 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +;; GNU General Public License for more details. |
| 20 | + |
| 21 | +;; You should have received a copy of the GNU General Public License |
| 22 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 23 | + |
| 24 | +;;; Commentary: |
| 25 | + |
| 26 | +;; PHP-IDE implementation to integrate Phpactor (phpactor.el). |
| 27 | +;; This feature depends on <https://github.com/emacs-php/phpactor.el>. |
| 28 | + |
| 29 | +;;; Code: |
| 30 | +(require 'phpactor nil t) |
| 31 | +(require 'popup nil t) |
| 32 | +(require 'smart-jump nil t) |
| 33 | +(eval-when-compile |
| 34 | + (require 'cl-lib)) |
| 35 | + |
| 36 | +(defvar-local php-ide-phpactor-buffer nil) |
| 37 | +(defvar-local php-ide-phpactor-hover-last-pos nil) |
| 38 | +(defvar-local php-ide-phpactor-hover-last-msg nil) |
| 39 | + |
| 40 | +(declare-function phpactor--command-argments "ext:phpactor" (&rest arg-keys)) |
| 41 | +(declare-function phpactor--parse-json "ext:phpactor" (buffer)) |
| 42 | +(declare-function phpactor--rpc-async "ext:phpactor" (action arguments callback)) |
| 43 | +(declare-function phpactor-goto-definition "ext:phpactor" ()) |
| 44 | +(declare-function popup-tip "ext:popup" (string)) |
| 45 | +(declare-function smart-jump-back "ext:smart-jump" ()) |
| 46 | +(declare-function smart-jump-go "ext:smart-jump" (&optional smart-list continue)) |
| 47 | +(declare-function smart-jump-references "ext:smart-jump" (&optional smart-list continue)) |
| 48 | + |
| 49 | +(defgroup php-ide-phpactor nil |
| 50 | + "UI support for PHP developing." |
| 51 | + :tag "PHP-IDE Phpactor" |
| 52 | + :prefix "php-ide-phpactor-" |
| 53 | + :group 'php-ide) |
| 54 | + |
| 55 | +(defcustom php-ide-phpactor-activate-features '(all) |
| 56 | + "A set of Phpactor features you want to enable." |
| 57 | + :tag "PHP-IDE Phpactor Activate Features" |
| 58 | + :type '(set (const all :tag "All") |
| 59 | + (const hover) |
| 60 | + (const navigation)) |
| 61 | + :safe (lambda (v) (and (listp v))) |
| 62 | + :group 'php-ide-phpactor) |
| 63 | + |
| 64 | +(defvar php-ide-phpactor-timer nil |
| 65 | + "Timer object for execute Phpactor and display hover message.") |
| 66 | + |
| 67 | +(defvar php-ide-phpactor-disable-hover-at-point-functions |
| 68 | + '(php-in-string-or-comment-p)) |
| 69 | + |
| 70 | +(defun php-ide-phpactor--disable-hover-at-point-p () |
| 71 | + "Return non-NIL if any function return non-NIL for disable to hover at point." |
| 72 | + (cl-loop for f in php-ide-phpactor-disable-hover-at-point-functions |
| 73 | + never (not (funcall f)))) |
| 74 | + |
| 75 | +(defun php-ide-phpactor-hover () |
| 76 | + "Show brief information about the symbol underneath the cursor." |
| 77 | + (interactive) |
| 78 | + (when (and php-ide-phpactor-buffer (not (php-ide-phpactor--disable-hover-at-point-p))) |
| 79 | + (if (eq (point) php-ide-phpactor-hover-last-pos) |
| 80 | + (when php-ide-phpactor-hover-last-msg |
| 81 | + (let ((msg php-ide-phpactor-hover-last-msg)) |
| 82 | + (setq php-ide-phpactor-hover-last-msg nil) |
| 83 | + (popup-tip msg))) |
| 84 | + (setq php-ide-phpactor-hover-last-pos (point)) |
| 85 | + (let ((main-buffer (current-buffer))) |
| 86 | + (phpactor--rpc-async "hover" (phpactor--command-argments :source :offset) |
| 87 | + (lambda (proc) |
| 88 | + (let* ((response (phpactor--parse-json (process-buffer proc))) |
| 89 | + (msg (plist-get (plist-get response :parameters) :message))) |
| 90 | + (with-current-buffer main-buffer |
| 91 | + (setq php-ide-phpactor-hover-last-msg msg))))))))) |
| 92 | + |
| 93 | +(defsubst php-ide-phpactor--feature-activated-p (feature) |
| 94 | + "Is FEATURE activated in `php-ide-phpactor-activate-features'." |
| 95 | + (or (memq 'all php-ide-phpactor-activate-features) |
| 96 | + (memq feature php-ide-phpactor-activate-features))) |
| 97 | + |
| 98 | +;;;###autoload |
| 99 | +(defun php-ide-phpactor-activate () |
| 100 | + "Activate PHP-IDE using phpactor.el." |
| 101 | + (interactive) |
| 102 | + (when (php-ide-phpactor--feature-activated-p 'navigation) |
| 103 | + (if (not (bound-and-true-p phpactor-smart-jump-initialized)) |
| 104 | + (local-set-key [remap xref-find-definitions] #'phpactor-goto-definition) |
| 105 | + (local-set-key [remap xref-find-definitions] #'smart-jump-go) |
| 106 | + (local-set-key [remap xref-pop-marker-stack] #'smart-jump-back) |
| 107 | + (local-set-key [remap xref-find-references] #'smart-jump-references))) |
| 108 | + (when (php-ide-phpactor--feature-activated-p 'hover) |
| 109 | + (unless php-ide-phpactor-timer |
| 110 | + (setq php-ide-phpactor-timer (run-with-timer 0.8 0.8 #'php-ide-phpactor-hover)))) |
| 111 | + (setq php-ide-phpactor-buffer t)) |
| 112 | + |
| 113 | +;;;###autoload |
| 114 | +(defun php-ide-phpactor-deactivate () |
| 115 | + "Dectivate PHP-IDE using phpactor.el." |
| 116 | + (interactive) |
| 117 | + (local-unset-key [remap xref-find-definitions]) |
| 118 | + (local-unset-key [remap xref-pop-marker-stack]) |
| 119 | + (local-unset-key [remap xref-find-references]) |
| 120 | + |
| 121 | + (when php-ide-phpactor-timer |
| 122 | + (cancel-timer php-ide-phpactor-timer) |
| 123 | + (setq php-ide-phpactor-timer nil)) |
| 124 | + (setq php-ide-phpactor-buffer nil)) |
| 125 | + |
| 126 | +(provide 'php-ide-phpactor) |
| 127 | +;;; php-ide-phpactor.el ends here |
0 commit comments