A GNU Emacs auto completion source for the PHP.
This repository contains:
- ac-php-core, the core library of the ac-php package
- ac-php, auto completion source for PHP to use with
auto-complete
- company-php, a
company-mode
back-end for PHP - helm-ac-php-apropros, an apropos functionality using the ac-php index and
helm
as interface
The PHP code completion and the jump to definition/declaration/inclusion-file provided by phpctags.
-
Support of
auto-complete
,company-mode
-
use phpstorm stubs for completion.
-
Auto Completion for user defined classes
-
Support of annotations and type hints
-
Currently ships with Spacemacs
For more see screenshots page.
ac-php supports PHPDoc annotations. Thus auto completion should work for:
Property annotations
/**
* @var \Acme\Services\HelloService
*/
public $hello;
Invisible (magic) members annotations
/**
* @property \Acme\Services\HelloService $hello
* @property-read \Acme\Services\HelloService $hello_r
* @property-write \Acme\Services\HelloService $hello_w
*/
class Test
{
// ...
}
Return type annotations
/**
* @return \Acme\Services\HelloService
*/
public function get_v1()
Invisible (magic) methods annotations
/**
* @method \Acme\Services\HelloService hello()
*/
class Test
{
// ...
}
Variable annotations
/** @var \Acme\Services\HelloService $hello */
$hello = hello();
Note: if a function or a class member has no defined return value then you need to define it using annotation.
In addition, ac-php supports return / parameter type hints. Thus you'll get additional auto completion support for the following cases:
Parameter type hints
use Acme\Services\HelloService;
public function hello(HelloService $service)
{
// ...
}
PHP 7 return type hints
public function hello(): \Acme\Services\HelloService
{
// ...
}
PHP 8 type
class Test
{
public \Acme\Services\HelloService $hello;
}
Known to work with GNU Emacs 24.4 and later. ac-php may work with older versions of GNU Emacs, or with other flavors of GNU Emacs (e.g. XEmacs) but this is not guaranteed. Bug reports for problems related to using ac-php with older versions of Emacs will most like not be addressed.
Prerequisite packages are:
- Common
- GNU Emacs >= 24.4
- PHP CLI
- php-mode
ac-php-core.el
ac-php.el
helm-ac-php-apropros.el
company-php.el
The best way of installing ac-php, at least for GNU Emacs 24, is to use the packaging system. Add MELPA or MELPA Stable to the list of repositories to access this mode. For those who want only formal, tagged releases use MELPA Stable:
(require 'package)
(add-to-list 'package-archives
'("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize)
For those who want rolling releases as they happen use MELPA:
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
and then use M-x package-refresh-contents
and M-x package-list-packages
to get to
the package listing and install ac-php
from there. MELPA tracks this Git repository
and updates relatively soon after each commit or formal release. For more detail on
setting up see MELPA Getting Started.
You can install ac-php
manually by adding following to your init file:
(unless (package-installed-p 'ac-php)
(package-refresh-contents)
(package-install 'ac-php))
Command | Description |
---|---|
ac-php-remake-tags |
Run this command to update tags if source has been changed |
ac-php-remake-tags-all |
Run this command if you find an error |
ac-php-find-symbol-at-point |
Jump to definition |
ac-php-location-stack-back |
Return back |
ac-php-show-tip |
Show definition at point |
ac-php-toggle-debug |
Toggle debug mode |
helm-ac-php-apropos |
Search through all the definitions of a project with helm |
- Add hook as follows:
(require 'php-mode)
(add-hook 'php-mode-hook
'(lambda ()
;; Enable auto-complete-mode
(auto-complete-mode t)
(require 'ac-php)
(setq ac-sources '(ac-source-php))
;; As an example (optional)
(yas-global-mode 1)
;; Enable ElDoc support (optional)
(ac-php-core-eldoc-setup)
;; Jump to definition (optional)
(define-key php-mode-map (kbd "M-]")
'ac-php-find-symbol-at-point)
;; Return back (optional)
(define-key php-mode-map (kbd "M-[")
'ac-php-location-stack-back)))
- Create the configuration file
.ac-php-conf.json
in the project root:
$ cd /project/to/poject/root
$ touch .ac-php-conf.json
You can add the standard php library with phpstorm stubs.
composer require jetbrains/phpstorm-stubs
And use M-x company-complete
to complete.
- Add hook as follows:
(require 'php-mode)
(add-hook 'php-mode-hook
'(lambda ()
;; Enable company-mode
(company-mode t)
(require 'company-php)
;; Enable ElDoc support (optional)
(ac-php-core-eldoc-setup)
(set (make-local-variable 'company-backends)
'((company-ac-php-backend company-dabbrev-code)
company-capf company-files))
;; Jump to definition (optional)
(define-key php-mode-map (kbd "M-]")
'ac-php-find-symbol-at-point)
;; Return back (optional)
(define-key php-mode-map (kbd "M-[")
'ac-php-location-stack-back)))
- Create the configuration file
.ac-php-conf.json
in the project root:
$ cd /project/to/poject/root
$ touch .ac-php-conf.json
And use M-x company-complete
to complete.
To use ac-php with Spacemacs please refer to: https://github.com/syl20bnr/spacemacs/tree/develop/layers/%2Blang/php
ac-php uses its own tags format. By default all tags located at ~/.cache/ac-php/tags-<project-directory>
.
For example, if the real path of the project is /home/jim/ac-php/phptest
, then tags will be placed at
~/.cache/ac-php/tags-home-jim-ac-php-phptest/
.
And you can redefine the base path (~/.cache/ac-php
) using ac-php-tags-path
variable.
Because of the way that the ac-php package works, there are a couple of simple initialization tasks which must occur to get the completion working as it should. On any new project make sure to perform the following initialization tasks:
-
Run the following
cd /root/of/project touch .ac-php-conf.json
-
Inside of Emacs run:
M-x
ac-php-remake-tags
The .ac-php-conf.json
file is required to enable auto-completion. When you run ac-php-remake-tags
and your .ac-php-conf.json
file is empty the default configuration will be used and inserted in the file.
If your project contains the following files at the root folder:
.projectile
vendor/autoload.php
the necessary configuration file (.ac-php-conf.json
) will be created automatically if it does not exist. Its contents will be similar to:
{
"filter": {
"php-file-ext-list": [
"php"
],
"php-path-list": [
"."
],
"ignore-ruleset": [
"# like .gitignore file ",
"/vendor/**/[tT]ests/**/*.php"
]
}
}
php-file-ext-list
: file extern name listphp-path-list
: base path for recursive file search to collect tagsignore-ruleset
: exclude files config, like '.gitignore' in git as lines
If source has been changed, you may need run ac-php-remake-tags
to remake tags.
ac-php internally uses phpctags, so its operability depends on the correct
syntax you use in the project. If something goes wrong with one of your files, you can find
all errors in the *Messages*
buffer. For example:
phpctags[/home/jim/phptest/testa.php] ERROR:PHPParser: Unexpected token '}' on line 11 -
In this case you'll need to fix an error in testa.php
and re run ac-php-remake-tags
.
Note: You shouldn't download phpctags, ac-php already ships with its own modified version.
If you see something like this:
ac-php: Unable to resolve project root
This means you have to create the .ac-php-conf.json
file in the project root:
$ touch /path/to/the/project/.ac-php-conf.json
- Q: Something went wrong. It seems I followed all the instructions, but auto completion stopped working
- A: Run
M-x ac-php-remake-tags-all
- Q: How I can create a reproducible test to create an issue?
- A: Please use this issue as an example
ac-php is open source software licensed under the GNU General Public Licence version 3 .