PHP code completion in TextMate

Alongside Flex Builder 2, TextMate is my editor of choice on OS X.

TextMate is a great all-round editor for simple (and some not-so-simple) tasks. For RoR, it's the dog's bullocks. I was, however, initially somewhat disappointed by its support for PHP.

Out of the box, TextMate doesn't support PHP code completion or provide you with an easy method of accessing the PHP documentation. I found, however, that you can easily configure it to do both of these things by downloading, installing and configuring the PHP Code Completion bundle.

Code completion

To set up code completion in TextMate, you must first download the PHP Code Completion bundle. As you can see, that link leads to an SVN repository.

To download and install the PHP code completion bundle, follow these instructions:

  1. Open Terminal
  2. Create a folder under /Library/Application Support/ called TextMate and a folder under that called /Bundles:
    mkdir -p /Library/Application\ Support/TextMate/Bundles
  3. Switch to that folder:
    cd /Library/Application\ Support/TextMate/Bundles
  4. Check out the bundle you want from the TextMate bundles repository:
    svn co http://macromates.com/svn/Bundles/trunk/Bundles/PHPCodeCompletion.tmbundle
  5. If you had TextMate open during this, select Bundles → Bundle Editor → Reload Bundles.

Create a new PHP file and test out the code completion by typing the first few letters of a function name and pressing ⎋ (esc). If you press ⌥ F1 before code completion, you will see a list of similar functions and if you press it after completion, you will see some brief documentation on the function signature.

Documentation

The PHP docs are probably the best I've seen for any programming language. The online version with comments is especially useful. Although TextMate doesn't have a built-in feature in the default bundles to access this documentation, the PHP code completion bundle you just installed does. It does require some configuration though.

With the PHP code completion bundle installed, you can access the PHP documentation by pressing Shift F1. The first time you do this, you will see a message telling you that you need to configure your settings.

To configure the PHP documentation:

  1. If you don't already have it, download the PHP documentation (.tar.gz version) to your computer. I placed the .tar.gz file in /Users/aral/library/PHP_Manual/ and unzipped it there. The unzipped files appear in an html folder so the path I entered in my preferences (see Step 4, below) was /Users/aral/library/PHP_Manual/html/ (do not leave out the trailing forward slash or else it won't work). Also, wherever you decide to place the documentaiton, make sure that none of the folder names on its path have a space in them.
  2. Click the Edit Preferences link in the PHPCodeCompletion Documentation window to open the PHPCodeCompletion preferences file in TextMate.
  3. Comment out the line that reads $phpcc_configured = FALSE; and uncomment the one that sets the same flag to true.
  4. Set the path that you placed the documentation in to the value of the $phpcc_manual_path_to_local variable.
  5. Save and close the preferences file.

Press Shift F1 on a function name to test out the documentation. If you see the familiar PHP web site appear, it means that you put the wrong path in the preferences file and that you are viewing the online docs as a backup. Of course, you may want to default to the online docs as they contain those invaluable user-submitted comments but it does make things slightly slower too.

That's it! You should now have code completion and documentation working for PHP in TextMate. Enjoy! :)

Comments