|
5 | 5 | ;; Author: Arne Jørgensen <arne@arnested.dk> |
6 | 6 | ;; URL: https://github.com/arnested/php-extras |
7 | 7 | ;; Created: June 28, 2012 |
8 | | -;; Version: 2.1.1 |
| 8 | +;; Version: 2.2.0 |
9 | 9 | ;; Package-Requires: ((php-mode "1.5.0")) |
10 | 10 | ;; Keywords: programming, php |
11 | 11 |
|
|
36 | 36 |
|
37 | 37 | (require 'eldoc) |
38 | 38 | (require 'thingatpt) |
| 39 | +(eval-when-compile (require 'cl-lib)) |
| 40 | + |
| 41 | +;; Silence byte-compiler warnings |
| 42 | +(declare-function php-get-pattern "php-mode" ()) |
| 43 | +(declare-function company-doc-buffer "company" (&optional string)) |
| 44 | +(declare-function company-grab-symbol "company" ()) |
| 45 | +(declare-function company-begin-backend "company" (backend &optional callback)) |
| 46 | +(defvar company-backends) |
39 | 47 |
|
40 | 48 | (defvar php-extras-php-variable-regexp |
41 | 49 | (format "\\(\\$[a-zA-Z_%s-%s][a-zA-Z0-9_%s-%s]*\\(\\[.*\\]\\)*\\)" |
@@ -204,6 +212,69 @@ The candidates are generated from the |
204 | 212 | ;;;###autoload |
205 | 213 | (add-hook 'php-mode-hook #'php-extras-completion-setup) |
206 | 214 |
|
| 215 | + |
| 216 | +;;; Backend for `company-mode' |
| 217 | + |
| 218 | +;;;###autoload |
| 219 | +(defun php-extras-company (command &optional candidate &rest ignore) |
| 220 | + "`company-mode' back-end using `php-extras-function-arguments'." |
| 221 | + (interactive (list 'interactive)) |
| 222 | + (when (derived-mode-p 'php-mode) |
| 223 | + (cl-case command |
| 224 | + (interactive |
| 225 | + (company-begin-backend 'php-extras-company)) |
| 226 | + |
| 227 | + (init |
| 228 | + (php-extras-load-eldoc) |
| 229 | + (unless (hash-table-p php-extras-function-arguments) |
| 230 | + ;; Signaling an error here will cause company-mode to remove |
| 231 | + ;; this backend from the list, so we need not check that the |
| 232 | + ;; hash table exists later |
| 233 | + (error "No PHP function information loaded"))) |
| 234 | + |
| 235 | + (prefix |
| 236 | + (company-grab-symbol)) |
| 237 | + |
| 238 | + (candidates |
| 239 | + (all-completions candidate php-extras-function-arguments)) |
| 240 | + |
| 241 | + (annotation |
| 242 | + (let ((prototype (php-extras-get-function-property candidate 'prototype))) |
| 243 | + (and prototype |
| 244 | + (replace-regexp-in-string "\\`[^(]*" "" prototype)))) |
| 245 | + |
| 246 | + (meta |
| 247 | + (php-extras-get-function-property candidate 'purpose)) |
| 248 | + |
| 249 | + (doc-buffer |
| 250 | + (let ((docs (php-extras-get-function-property candidate 'documentation))) |
| 251 | + (when docs |
| 252 | + (company-doc-buffer docs)))) |
| 253 | + |
| 254 | + (post-completion |
| 255 | + (php-extras-ac-insert-action))))) |
| 256 | + |
| 257 | +;;;###autoload |
| 258 | +(defun php-extras-company-setup () |
| 259 | + ;; Add `php-extras-company' to `company-backends' only if it's not |
| 260 | + ;; already present in the list, to avoid overwriting any user |
| 261 | + ;; preference for the order and merging of backends (as configured |
| 262 | + ;; via customize, e.g.). Elements of `company-backends' may be |
| 263 | + ;; lists of backends to merge together, so this is more complicated |
| 264 | + ;; than just (memq ..) |
| 265 | + (when (boundp 'company-backends) |
| 266 | + (unless |
| 267 | + (cl-loop |
| 268 | + for backend in company-backends |
| 269 | + thereis (or (eq backend 'php-extras-company) |
| 270 | + (and (listp backend) |
| 271 | + (memq 'php-extras-company backend)))) |
| 272 | + (add-to-list 'company-backends 'php-extras-company)))) |
| 273 | + |
| 274 | +;;;###autoload |
| 275 | +(eval-after-load 'company |
| 276 | + '(php-extras-company-setup)) |
| 277 | + |
207 | 278 |
|
208 | 279 |
|
209 | 280 | ;;;###autoload |
|
0 commit comments