Giter Club home page Giter Club logo

php-auto-yasnippets's Introduction

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 Eric James Michael Ritz

php-auto-yasnippets's People

Contributors

nateeag avatar yasuyk avatar bystrano avatar glynnforrest avatar amcorreia avatar

Watchers

James Cloos avatar nverno avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.