Adding Diff syntax highlighting to Sublime Text

26th April 2013

My chosen colour scheme for Sublime Text doesn't include support for diff/patch files, so I added my own.

To the bottom of my .tmTheme file, I added this just above the closing </array>:

        <dict>
            <key>name</key>
            <string>diff.header</string>
            <key>scope</key>
            <string>meta.diff, meta.diff.header</string>
            <key>settings</key>
            </dict><dict>
                <key>foreground</key>
                <string>#009933</string>
            </dict>
 
        <dict>
            <key>name</key>
            <string>diff.deleted</string>
            <key>scope</key>
            <string>markup.deleted</string>
            <key>settings</key>
            </dict><dict>
                <key>foreground</key>
                <string>#DD5555</string>
            </dict>
 
        <dict>
            <key>name</key>
            <string>diff.inserted</string>
            <key>scope</key>
            <string>markup.inserted</string>
            <key>settings</key>
            </dict><dict>
                <key>foreground</key>
                <string>#3333FF</string>
            </dict>
 
        <dict>
            <key>name</key>
            <string>diff.changed</string>
            <key>scope</key>
            <string>markup.changed</string>
            <key>settings</key>
            </dict><dict>
                <key>foreground</key>
                <string>#E6DB74</string>
            </dict>

This sets up a green colour for the meta information, blue for added lines, red for deleted lines and a yellowish colour for changed.

Notes on embedding fonts in rst2pdf

18th April 2013

I wanted to use Helvetica Light in a PDF that I'm creating using rst2pdf and this proved a little tricker than I expected.

In this case, I discovered that setting stdFont: Helvetica-Light or stdFont: HelveticaLight in the style file resulted in Verdana being used in the PDF file!

Fortunately, the -v switch to rst2pdf is your friend when you want to know what's happening:

[INFO] findfonts.py:270 Trying to embed Helvetica-Light
[INFO] findfonts.py:307 fname for findTTFont: Helvetica-Light
[INFO] findfonts.py:308 Variants via findTTFont: ['/Library/Fonts/Microsoft/Verdana.ttf', '/Library/Fonts/Microsoft/Verdana Bold.ttf', '/Library/Fonts/Microsoft/Verdana Italic.ttf', '/Library/Fonts/Microsoft/Verdana Bold Italic.ttf']
[INFO] findfonts.py:317 Registering font: Verdana from /Library/Fonts/Microsoft/Verdana.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold from /Library/Fonts/Microsoft/Verdana Bold.ttf
[INFO] findfonts.py:317 Registering font: Verdana Italic from /Library/Fonts/Microsoft/Verdana Italic.ttf
[INFO] findfonts.py:317 Registering font: Verdana Bold Italic from /Library/Fonts/Microsoft/Verdana Bold Italic.ttf
[INFO] findfonts.py:329 Embedding via findTTFont as ['Verdana', 'Verdana Bold', 'Verdana Italic', 'Verdana Bold Italic']

As can be seen by the output, rst2pdf can't find Helvetica-Light and so has substituted Verdana instead. This would seem to be because it couldn't find a TTF file for the font on my Mac as Helvetica Light is stored in a dfont file.

To solve this, I used DFontSplitter to extract Helvetica Light as a separate TTF file and added this as an embedded font to the style file:

embeddedFonts: []
    [fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf, fonts/HelveticaLight.ttf]

and re-ran with -v which gave me this information:

[INFO] styles.py:276 Registering font: fonts/HelveticaLight from ./fonts/HelveticaLight.ttf

I now know that the name to use when referencing this font is "fonts/HelveticaLight" and so I can use it within the style file like this:

stdFont: fonts/HelveticaLight

and the font is embedded as I expected.

Improved Sublime Text 2 PHP getter and setter generation

14th January 2013

As I've been using Sublime Text 2 for all coding for the last year, I've noticed a significant problem with my simple 'getset' snippet that I created last year: it doesn't handle underscores in property names correctly.

I've finally got around to fix it!

As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. This snippet creates a getXxx() and setXxx() method from a property name.

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

<snippet>
    <content><![CDATA[/**
 * Getter for ${1:$SELECTION}
 *
 * @return mixed
 */
public function get${1/(?:^|_)(\w)/\U$1$2/g$1}()
{
    return \$this->$1;
}

/**
 * Setter for $1
 *
 * @param mixed \$${1/_(\w)/\U$1/g$1} Value to set

 * @return self
 */
public function set${1/(?:^|_)(\w)/\U$1$2/g$1}(\$${1/_(\w)/\U$1/g$1})
{
    \$this->$1 = \$${1/_(\w)/\U$1/g$1};
    return \$this;
}

]]></content>
    <!-- Optional: Tab trigger to activate the snippet -->
    <tabTrigger>gs</tabTrigger>
    <!-- Optional: Scope the tab trigger will be active in -->
    <scope>source.php</scope>
    <!-- Optional: Description 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 gs followed by tab and it will automatically expand and you can then type your underscore_separated property name and the correct method names will be created.

Alternatively, select some text and press shift+cmd+p and then type gs to automatically replace the selected text with the get and set methods completed for the text that was selected.

As an example, typing gs {tab} date_last_updated will produce:

    /**
     * Getter for date_last_updated
     *
     * @return mixed
     */
    public function getDateLastUpdated()
    {
        return $this-&gt;date_last_updated;
    }
 
    /**
     * Setter for date_last_updated
     *
     * @param mixed $dateLastUpdated Value to set
 
     * @return self
     */
    public function setDateLastUpdated($dateLastUpdated)
    {
        $this-&gt;date_last_updated = $dateLastUpdated;
        return $this;
    }

I've also updated the snippet in my sublime-akrabat package on github.

Gittyup - Easily keep master in sync with upstream

21st June 2012

If, like me, you use git and have an upstream remote which is not your origin, then I highly recommend that you use Evan Coury's gittyup script.

This is a very simple script that does the following:

1. Verify that you are in a valid Git repo.
2. Remember which branch you are on.
3. Stash any uncommitted changes you have.
4. Checkout master.
5. Fetch all remotes. (nice to track other remotes)
6. Merge upstream/master into local master (FF ONLY!)
7. Push your updated local master branch to origin.
8. Check out the branch you were previously on.
9. Applies any stashed changes and index status.

This is a very quick an easy way to update your local master with upstream and push to origin without losing any changes you may currently be working on.

You should set it up now!

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.