• Stars
    star
    252
  • Rank 156,091 (Top 4 %)
  • Language
    Vim Script
  • Created about 13 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP namespace support for VIM. Types "use" statements for you

Goal

vim-php-namespace is a vim plugin for inserting "use" statements automatically.

Features

Import classes, functions, traits, or enums (add use statements)

Imports the symbol under the cursor by adding the corresponding use statement.

To use this feature, add the following mappings in ~/.vimrc:

function! IPhpInsertUse()
    call PhpInsertUse()
    call feedkeys('a',  'n')
endfunction
autocmd FileType php inoremap <Leader>u <Esc>:call IPhpInsertUse()<CR>
autocmd FileType php noremap <Leader>u :call PhpInsertUse()<CR>

Then, typing \u in normal or insert mode will import the symbol under the cursor.

<?php
new Response<-- cursor here or on the name; hit \u now to insert the use statement

Make symbol fully qualified

Expands the symbol under the cursor to its fully qualified name.

To use this feature, add the following mappings in ~/.vimrc:

function! IPhpExpandClass()
    call PhpExpandClass()
    call feedkeys('a', 'n')
endfunction
autocmd FileType php inoremap <Leader>e <Esc>:call IPhpExpandClass()<CR>
autocmd FileType php noremap <Leader>e :call PhpExpandClass()<CR>

Then, typing \e in normal or insert mode will expand the symbol to its fully qualified name.

<?php
$this->getMock('RouterInterface<-- cursor here or on the name; type \e now to expand the class name'

Sort existing use statements alphabetically

To use this feature, add the following mappings in ~/.vimrc:

autocmd FileType php inoremap <Leader>s <Esc>:call PhpSortUse()<CR>
autocmd FileType php noremap <Leader>s :call PhpSortUse()<CR>

Then, hitting \s in normal or insert mode will sort use statements.

It is also possible to sort statements automatically after a PhpInsertUse() by defining the following variable:

let g:php_namespace_sort_after_insert = 1

Installation:

Using pathogen

git clone git://github.com/arnaud-lb/vim-php-namespace.git ~/.vim/bundle/vim-php-namespace

Using vim-plug

Add to vimrc:

Plug 'arnaud-lb/vim-php-namespace'

Run command in vim:

:PlugInstall

Using vundle

Add to vimrc:

Bundle 'arnaud-lb/vim-php-namespace'

Run command in vim:

:BundleInstall

Manual installation

Download and copy plugin/phpns.vim to ~/.vim/plugin/

Post installation

Generate a tag file

The plugin makes use of tag files. If you don't already use a tag file you may create one with the following command; after having installed the ctags package:

ctags -R --PHP-kinds=cfi

Traits

[universal-ctags] supports traits natively (with --php-kinds=cfit).

If you can't use universal-ctags, the --regex-php argument allows to extract traits:

ctags -R --PHP-kinds=cfi --regex-php="/^[ \t]*trait[ \t]+([a-z0_9_]+)/\1/t,traits/i"

You can also create a ~/.ctags file with the following contents:

--regex-php=/^[ \t]*trait[ \t]+([a-z0_9_]+)/\1/t,traits/i

Note that using --regex-php= is 10x slower than using universal-ctags.

Enums

The --regex-php argument can be used to extract enums:

ctags -R --PHP-kinds=cfi --regex-php="/^[ \t]*enum[ \t]+([a-z0_9_]+)/\1/e,enum/i"

You can also create a ~/.ctags file with the following contents:

--regex-php=/^[ \t]*enum[ \t]+([a-z0_9_]+)/\1/e,enum/i

Automatically updating tags

The AutoTags plugin can update the tags file every time a file is created or modified under vim.

To keep updates fast, AutoTags won't operate if the tags file exceeds 7MB. To avoid exceeding this limit on projects with many dependencies, use a separate tags file for dependencies:

# dependencies tags file (index only the vendor directory, and save tags in ./tags.vendors)
ctags -R --PHP-kinds=cfi -f tags.vendors vendor

# project tags file (index only src, and save tags in ./tags; AutoTags will update this one)
ctags -R --PHP-kinds=cfi src

Do not forget to load both files in vim:

" ~/.vimrc
set tags+=tags,tags.vendors

Key mappings

See Features section for adding key mappings.

The <Leader> key usually is \.

Credits:

This was originally based on a similar script for java packages found at http://vim.wikia.com/wiki/Add_Java_import_statements_automatically (in comments).

More Repositories

1

php-rdkafka

Production-ready, stable Kafka client for PHP
C
1,968
star
2

php-memory-profiler

Memory profiler for PHP. Helps finding memory leaks in PHP scripts.
C
773
star
3

MtHaml

Multi target HAML (HAML for PHP, Twig, <your language here>)
PHP
363
star
4

php-go

php-go allows to call Go code from PHP, with minimal code boilerplate
C
174
star
5

php-inotify

Inotify bindings for PHP 5, 7, and next
C
39
star
6

MtHamlBundle

Symfony2 HAML bundle
PHP
38
star
7

imagesize.js

Get the size of an image without reading or downloading it entirely
JavaScript
30
star
8

php-sema

A library for semantic analysis of PHP code
PHP
19
star
9

Zwig

Twig / Zend Framework adapter
PHP
16
star
10

alb-oembed

Simple PHP oEmbed consumer library with discovery support
PHP
16
star
11

TwigReflectionBundle

Displays what's in Twig
PHP
15
star
12

goresize

image resizing proxy written in golang
Go
12
star
13

xtrabackup-manager

xtrabackup-manager fork. Original code at https://code.google.com/p/xtrabackup-manager/
PHP
9
star
14

TwigShellBundle

Simple Twig Shell
PHP
8
star
15

Silex-MtHaml

HAML templating for Silex
PHP
8
star
16

fselectmenu

JavaScript
7
star
17

phpketama

Pure-PHP implementation of libketama, a consistent hashing library
PHP
6
star
18

php-rdkafka-doc

PHP
6
star
19

php-inotify-ffi

Pure-PHP inotify binding (FFI-based)
PHP
5
star
20

binsort

Binsort is a tool to sort files of fixed-length binary records
Go
5
star
21

AlbOpenIDServerBundle

OpenID Provider bundle
PHP
5
star
22

php-throttle

php module for throttling file upload speed
C
4
star
23

fdinfo

progress bar for quiet cli tools
C
4
star
24

OpenISETL

Scala/JVM implementation of the ISETL language
Scala
4
star
25

run

`nohup something >/dev/null 2>&1 &`; shortened
C
3
star
26

sfbootstrap

PHP
3
star
27

mediawiki-php-FastStringSearch

Github mirror of PHP extension FastStringSearch - our actual code is hosted with Gerrit (please see https://www.mediawiki.org/wiki/Developer_access for contributing)
C
2
star
28

kafka-python

Python client for Apache Kafka
Python
1
star
29

zf

zf mirror
PHP
1
star
30

iterm-to-neovim

Converts Iterm2 color schemes to neovim g:terminal_color_x
Go
1
star
31

jQuery.event.queueHandler

Queues handlers to be ran after the current event's handlers have been ran
JavaScript
1
star
32

graphql-demo

A Symfony application demoing a simple GraphQL API
PHP
1
star