Skip to content

Commit 82956cb

Browse files
committed
Merge branch 'release/2.2.0'
2 parents 72a963d + 1e7c4e9 commit 82956cb

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Currently includes:
88
* `php-extras-eldoc-documentation-function`
99
* Auto complete source for PHP functions based on
1010
`php-extras-eldoc-documentation-function`
11+
* Company completion back-end for PHP functions based on
12+
`php-extras-eldoc-documentation-function`
1113

1214

1315
## `php-extras-insert-previous-variable`
@@ -67,6 +69,12 @@ The source we provide with `php-extras` will hopefully be more up to
6769
date.
6870

6971

72+
## Company completion back-end for PHP functions based
73+
74+
Users of [company-mode](http://company-mode.github.io/) will also get
75+
in-buffer completion based on the extracted PHP functions.
76+
77+
7078
## Installation
7179

7280
The easiest way to install `php-extras` is probably to install it via
@@ -93,7 +101,7 @@ If you insist on installing it manually try to follow this recipe:
93101
* Add this to your `.emacs` / `.emacs.d/init.el`:
94102

95103
```lisp
96-
(add-to-list 'load-path "/somewhere/on/your/disk/php-xtras")
104+
(add-to-list 'load-path "/somewhere/on/your/disk/php-extras")
97105
(eval-after-load 'php-mode
98106
(require 'php-extras))
99107
```

php-extras.el

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
;; Author: Arne Jørgensen <arne@arnested.dk>
66
;; URL: https://github.com/arnested/php-extras
77
;; Created: June 28, 2012
8-
;; Version: 2.1.1
8+
;; Version: 2.2.0
99
;; Package-Requires: ((php-mode "1.5.0"))
1010
;; Keywords: programming, php
1111

@@ -36,6 +36,14 @@
3636

3737
(require 'eldoc)
3838
(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)
3947

4048
(defvar php-extras-php-variable-regexp
4149
(format "\\(\\$[a-zA-Z_%s-%s][a-zA-Z0-9_%s-%s]*\\(\\[.*\\]\\)*\\)"
@@ -204,6 +212,69 @@ The candidates are generated from the
204212
;;;###autoload
205213
(add-hook 'php-mode-hook #'php-extras-completion-setup)
206214

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+
207278

208279

209280
;;;###autoload

0 commit comments

Comments
 (0)