Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Dynamically Generated YASnippets for PHP Code in GNU Emacs

License

Notifications You must be signed in to change notification settings

emacs-php/php-auto-yasnippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Automatic YASnippets for PHP in Emacs

The php-auto-yasnippets package for GNU Emacs will automatically create ‘snippets’ for standard PHP functions. It defines these snippets using the YASnippet package, with optional support for auto-complete. For example, if you type the PHP function

implode

and press C-c C-y then php-auto-yasnippets will expand that into

implode($glue, $pieces)

with the cursor ready to overwrite $glue with the string you want to use. Pressing Tab will skip over to $pieces to do the same. This way you can be sure you not only have the correct number of arguments, but also that you have them in the correct order. PHP comes with a large standard library and functions that sound similar sometimes require arguments in contrasting orders. This package will help you avoid having to remember those corner cases.

If a function has any optional parameters then php-auto-yasnippets will wrap them in square braces. This is the same convention the PHP manual uses to indicate optional parameters. For example, php-auto-yasnippets will expand filter_input into this:

filter_input($type, $variable_name, [$filter], [$options])

If you do not need the optional parameters you can delete them by pressing C-d when you Tab over to them.

You can use the prefix command to expand method names. When you use the prefix, u.g. C-u C-c C-y, the package will ask you for the name of the class which implements that method. This information is necessary in order to generate the correct snippet.

Requirements

Installation

To use php-auto-yasnippets you need to do three things. First, place the package in your load-path (C-h v load-path for help) and load it from your Emacs configuration file by adding:

(require 'php-auto-yasnippets)

Second, make sure the variable php-auto-yasnippet-php-program points to the program Create-PHP-YASnippet.php. That PHP program should have come with this package; if you do not have it then you can get it from the project website. By default php-auto-yasnippets will search for the PHP program in the same directory as the Elisp code, i.e. the php-auto-yasnippets.el. If you want to put the PHP program in another place then use setq in your configuration file to set the variable to the proper path, e.g:

(require 'php-auto-yasnippets)
(setq php-auto-yasnippet-php-program "~/path/to/Create-PHP-YASnippet.php")

Finally, bind the function yas/create-php-snippet to a key of your choice. You must do this because php-auto-yasnippets defines no key-bindings. And since the package requires php-mode, and is most useful when writing PHP code, you may want to use a key-binding that only works when using php-mode. For example:

(define-key php-mode-map (kbd "C-c C-y") 'yas/create-php-snippet)

Now if you type the name of a PHP function and press C-c C-y it will expand into a snippet containing all of the parameters, their names, any default values, et cetera.

auto-complete

With auto-complete support activated, it's even simpler:

imp

followed by Tab, then Return (to choose the first completion) expands the snippet to

implode($glue, $pieces)

First, install and configure auto-complete.

Add

(payas/ac-setup)

to your php-auto-yasnippets setup, and you should be good to go.

Note that auto-completion does not support user-defined functions or classes (it relies on auto-complete's php-mode dictionary of functions).

Loading User Code

This package can also generate snippets for user-defined functions and methods.

You can tell a buffer what files to load for function and class definitions by setting php-auto-yasnippet-required-files to a list of required paths.

If you use Composer, you might put something like this in ~/project/.dir-locals.el:

(php-mode . ((php-auto-yasnippet-required-files (list "~/project/vendor/autoload.php"))))

Now you can generate snippets for any classes Composer autoloads, in any PHP file in the project.

Contributors

Miscellaneous

This package uses Semantic Versioning.

License

GNU General Public License

Copyright 2013, 2014, 2015, 2016, 2017 Eric James Michael Ritz

About

Dynamically Generated YASnippets for PHP Code in GNU Emacs

Resources

License

Stars

Watchers

Forks

Packages

No packages published