emacs auto-complete for php
support auto-complete
and company-mode
and spacemacs layer
use phpctags gen tags
and use ac-php
work with tags
- support system function
- support system class
core: SPL SplFileInfo DOMDocument SimpleXMLElement ...
externed: PDO Http mysqli Imagick SQLite3 Memcache ...
externed: Redis ,Swoole
- support user self class system
- example:
- Install
- Test
- Usage
- Usage Company
- Usage spacemacs
- php extern for complete
- tags
- large php project config
- disable cscope config
- FQA
- install
php-cli
command for phpctags
localhost:~/$ sudo apt-get install php-cli
- install
cscope
command forac-php-cscope-find-egrep-pattern
localhost:~/$ sudo apt-get install cscope
brew install homebrew/php/php56
brew install cscope
localhost:~$ php --version
PHP 5.5.20 (cli) (built: Feb 25 2015 23:30:53)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
localhost:~$ cscope --version
cscope: version 15.8a
#backup old .emacs
cp ~/.emacs ~/.emacs.bak
save it as ~/.emacs
(setq package-archives
'(("melpa" . "https://melpa.org/packages/")) )
(package-initialize)
(unless (package-installed-p 'ac-php )
(package-refresh-contents)
(package-install 'ac-php )
)
(require 'cl)
(require 'php-mode)
(add-hook 'php-mode-hook
'(lambda ()
(auto-complete-mode t)
(require 'ac-php)
(setq ac-sources '(ac-source-php ) )
(yas-global-mode 1)
(define-key php-mode-map (kbd "C-]") 'ac-php-find-symbol-at-point) ;goto define
(define-key php-mode-map (kbd "C-t") 'ac-php-location-stack-back ) ;go back
))
cd ~/
git clone https://github.com/xcwen/ac-php/
#test php files in ~/ac-php/phptest
#open file for test
emacs ~/ac-php/phptest/testb.php
- install
ac-php
from melpa
(setq package-archives
'(("melpa" . "https://melpa.org/packages/")) )
"M-x" :package-list-packages
find ac-php
install
- emacs php-mode function define
work for auto-complete-mode , Company-mode config at here
(add-hook 'php-mode-hook
'(lambda ()
(auto-complete-mode t)
(require 'ac-php)
(setq ac-sources '(ac-source-php ) )
(yas-global-mode 1)
(define-key php-mode-map (kbd "C-]") 'ac-php-find-symbol-at-point) ;goto define
(define-key php-mode-map (kbd "C-t") 'ac-php-location-stack-back ) ;go back
))
- create file ".ac-php-conf.json" in root of project
cd /project/to/path # root dir of project
touch .ac-php-conf.json
-
DONE
-
command
cmd | info |
---|---|
ac-php-remake-tags | if source is changed ,re run this commond for update tags |
ac-php-remake-tags-all | **if you find a error, run it an retest** |
ac-php-find-symbol-at-point | goto define |
ac-php-location-stack-back | go back |
ac-php-show-tip | show define at point |
ac-php-cscope-find-egrep-pattern | find current-word in project
|
"M-x" :package-list-packages
find company-php
install
(add-hook 'php-mode-hook
'(lambda ()
(require 'company-php)
(company-mode t)
(add-to-list 'company-backends 'company-ac-php-backend )))
use M-x: company-complete
for complete
use spacemacs layer phpplus
from https://github.com/xcwen/spacemacs-config
cd ~
git clone https://github.com/xcwen/spacemacs-config
#copy `phpplus` to yours config
cp -rf ~/spacemacs-config/layers/phpplus ~/.spacemacs.d/layers/
# rm layer `php` from `dotspacemacs-configuration-layers`
# add layer `phpplus` to `dotspacemacs-configuration-layers`
# restart your emacs
define class memeber type :
public $v1;
=>
/**
* @var classtype
*/
public $v1;
if you won't define public $v1
you can define in class comment ,like this =>
/**
* @property \Test\Testa $v1
* @method int add($a,$b)
* @use \Test\TestC
*/
class Testb extends Ta {
...
}
define class function return type:
public function get_v1()
=>
/**
* @return classtype
*/
public function get_v1()
or define use php7 :
public function get_v1() :classtype {
}
or define in doc:
/**
* @method classtype get_v1()
*/
class Testb extends Ta {
...
}
define variable: (**if function or member no define reutrn value you need define it **)
$value=ff();
=>
/** @var $value Testa */
$value=ff();
like this
/**
class define ..
@property \Test\Testa $v8
*/
class Testa {
/**
* @var int;
*/
const CON=1;
/**
* @var Testb;
*/
public $v1;
/**
* @var \Test\TestC;
*/
public $v2;
public function set_v1($v){
//for complete system function
$v=trim($v);
$c=new Testb();
//can complete
$c->get_v2();
//for complete memeber
$this->v1=$v;
//for complete function return type
$this-> get_v2()->testC();
//for complete field from comment
$this->v8->testA();
//for complete system class
$q=new SplQueue ();
$q->push(11);
//jump for class function point
$f=array($this->v8, "test_A" );
$f();
}
/**
*
* @return \Test\TestC;
*/
public function get_v2(){
$this->v2;
}
}
tags file location dir is in ~/.ac-php/project_dir
for example: ~/.ac-php/tags-home-jim-ac-php-phptest/
localhost:~/.ac-php$ tree tags-home-jim-ac-php-phptest/
tags-home-jim-ac-php-phptest/
├── cache-files.json
├── tags-data-cache2.el
├── tags-data.el
└── tags_dir_jim
├── a.el
├── dir1-sss.el
├── testa.el
├── testb.el
└── testc.el
1 directory, 8 files
config file name is .ac-php-conf.json
when run ac-php-remake-tags
will .ac-php-conf.json
set default json config when it's empty
like this
{
"use-cscope" : false,
"filter": {
"php-file-ext-list": [
"php"
],
"php-path-list": [
"."
],
"php-path-list-without-subdir": []
}
}
php-file-ext-list
: file extern name list;
php-path-list
: find php files recursion ;
php-path-list-without-subdir
: find php files no recursion no find php from subdir ;
for exmaple:
├── dir1
│ ├── 1.php
│ └── dir11
│ └── 11.php
├── dir2
│ └── 2.php
└── dir3
├── 3.php
├── dir31
│ └── 31.php
├── dir32
│ └── 32.php
└── dir33
└── 33.php
{
"filter": {
"php-file-ext-list": [
"php"
],
"php-path-list": [
"./dir1",
"./dir2",
"./dir3/dir32/"
],
"php-path-list-without-subdir": [
"./dir3"
]
}
}
filter result is:
├── dir1
│ ├── 1.php
│ └── dir11
│ └── 11.php
├── dir2
│ └── 2.php
└── dir3
├── 3.php
├── dir32
│ └── 32.php
31.php
33.php
will not gen tags;
for laravel example
{
"use-cscope": false,
"filter": {
"php-file-ext-list": [
"php"
],
"php-path-list": [
"./app",
"./database",
"./vendor/laravel/framework/src/Illuminate"
],
"php-path-list-without-subdir": []
}
}
set use-cscope: false
in .ac-php-conf.json
if source is changed ,re run this commond for update tags: ac-php-remake-tags
if php file cannot pass from phpctags
.
you can find any error from Messages
buffer fix it and next
like this
phpctags[/home/jim/phptest/testa.php] ERROR:PHPParser: Unexpected token '}' on line 11 -
you need fix testa.php error and re run ac-php-remake-tags
if show:
no find file .ac-php-conf.json dir in path list :/home/jim/phptest/
you need create file ".ac-php-conf.json" in root of project
like this:
touch /home/jim/phptest/.ac-php-conf.json
or
touch /home/jim/.ac-php-conf.json
you find a question
exec : M-x
: ac-php-remake-tags-all
and retest