Sublime Text 2 Plugin: Function Name Display

27th February 2012

As I'm using Sublime Text 2 more and more, I thought it would be useful to display the current method name in the status bar. I poked around on the forums and created a plugin from ideas I found in a couple of different threads.

This plugin is imaginatively titled Sublime Function Name Display!

As I've hooked into the on_selection_modified event handler, I was keen to avoid slowing down the editor too much when you move your carat quickly. As a result, I have used a simple waiting system from forum member 'facelessuser', to ensure that the plugin only updates the status bar 100ms after you've stopped moving the carat.

As I don't know Python, I copied the code from other people but I'm happy with the way my first plugin has turned out.

Two new Sublime Text 2 packages for PHP

6th February 2012

Stuart Herbert has written two new Sublime Text packages for PHP:

The best way to install these is to install Package Control first and then use shift+cmd+P -> install package.

Even better, Stuart has rolled my getter/setter creation snippet into Additional PHP Snippets, so you can now have it without any hassle!

Update: Also, Ben Selby has created a package for DocBlox!

Sublime Text 2 Snippet for PHP getter and setter generation

3rd January 2012

I've been playing with Sublime Text 2 recently and have quite enjoyed how quiet my ageing laptop is when the fans aren't running due to a Java-based IDE.

As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. I needed to create a few getXxx() and setXxx() methods for some properties of a class and decided that the easiest way to do this would be with a snippet.

To create a snippet, go to Tools->New Snippet... and replace the code example provided with this:


<snippet>
    <content><![CDATA[public function get${1/(.*)/\u$1/}()
{
    return \$this->${1:$SELECTION};
}

public function set${1/(.*)/\u$1/}(\$$1)
{
    \$this->$= \$$1;
    return \$this;
}
]]></content>
    <!-- OptionalTab trigger to activate the snippet -->
    <tabTrigger>getset</tabTrigger>
    <!-- OptionalScope the tab trigger will be active in -->
    <scope>source.php</scope>
    <!-- OptionalDescription to show in the menu -->
    <description>Create getter and setter methods</description>
</snippet>

Save the file as getset.sublime-snippet and you're done.

To use, simply type getset followed by tab (in the latest dev builds, at least) and it will automatically expand. Alternatively, select some text and use shift+cmd+p -> getset to automatically replace the selected text with the get and set methods completed for the text that was selected.

Using PDT with Zend Framework Projects

1st April 2010

I original wrote this as a comment on Victor Nicollet's blog, but I thought I should document it here too so that I can refer other people to the information.

These are some tips and tricks when using PDT with Zend Framework that make my life easier:

Autocompletion for dynamic properties

Zend_Db_Table_Row_Abstract uses __get() and __set() magic in order to map to the underlying database table row in question. This means that you can use property autocompletion on an instance of it. You can however tell your your table class which class to use for the row objects:

class Application_Model_Users extends Zend_Db_Table_Abstract
{
    protected $_name 'users'// database table name
    protected $_rowClass 'Application_Model_User'// row class to use
}

With your row class, you can take advantage of the @property docblock element to document the fields in the class:

/**
 * @property string $user_id
 * @property string $username
 * @property string $password
 */
class Application_Model_User extends Zend_Db_Table_Row_Abstact
{
}

Autocomplete on instances of Application_Model_User will now work.

Zend_View scripts are more complicated as they use $this to access magic properties. You can do however use the @var trick at the top of the .phtml file:

$user $this->user/* @var $user Application_Model_User */
$page $this->page/* @var $user Application_Model_Page */
etc...

You can now autocomplete on $user and $page. This also has the side-effect of documenting which view properties are used in this view script.

Opening an arbitrary method

View helpers are also magic methods on Zend_View's $this within a script file. This means that you can't cmd+click( or press F3) on the view helper's method name to jump directly to the code for that method. You can however take advantage of PDT's Navigation->Open Method feature. Simply select the method name and then press shift+cmd+m. The "Open Method" dialog appears and usually the view helper is selected as the first item. Just press return to go directly to the code for that view helper.

Opening an arbitrary file

For opening up an arbitrary file that's in my project, I find that PDT's Navigation->Open Resource (shift+cmd+r) is handy. For TextMate users, this is similar to the cmd+t feature.

Opening an arbitrary class

Using Navigation->Open Type (shift+cmd+t), you can also jump to classes easily. e.g. I regularly hit shift+cmd+t followed by "*Model_" and I get a list of all my models and can jump to the one I want using the down arrow key and then return.

Finding the current open file in the project tree

Just press cmd+option+w. You have to be careful here as it's very similar keyboard shortcut to close and close all! However if you have an entity file open and you name your files like I do, then the mapper is next to it in the project list, so it's only a couple of keystrokes to open it up.

Conclusion

These tips obviously work with all frameworks and any PHP coding :) Netbeans also has similar features, so I would guess all IDEs do. If you are wedded to vim or such like, then ctags is helpful for jumping around your code.

If you have a favourite tip that I haven't mentioned, let me know in the comments!

The watch Linux command line tool

24th July 2009

I'm sure everyone else already knows about watch, but it's new to me. This little utility executes a program repeatedly at a set interval and displays its output.

I've been using it with mysqladmin's processlist command like this:


watch -n 1 /usr/bin/mysqladmin -uroot -pMYPASSWORD  processlist

Note that this does put your password on display at the top of the command window whilst watch is running. If you don't want that, you could write a little bash script instead like this one from a friend of mine:


#!/bin/sh

while :
do
sleep 1
clear
mysqladmin -uroot -pMYPASSWORD processlist 
done

Either way, we get a display of the MySQL process list every second in a Terminal window and it becomes very easy to see which processes are causing trouble.