Sunday, March 4, 2007

TextMate: Spell Checking Comments (and String Literals)

Whenever I can, I use a spellchecker. Mainly because there are a few dozen words I consistently get wrong (ironically, this includes the word 'consistantly' 'consistently'). There's also a half dozen words I can spell just the beginning of, and about 3 words that I can never spell at all and I end up using a synonym.

So I use a spellchecker. Except, of course, in source code, because spell checking source code is an excerise in false positives.

Until today, when I figured out how to make <%html.getlink("TextMate", "http://macromates.com/")%> spell check comments (and string values, which it already does). As an extra bonus, the settings outlined below make these changes for every language bundle that inherits from the Source bundle: meaning that your C++ comments/strings will get spell checked right along with your Python comments/strings, along with your JavaScript comments/strings, along with your... you get the idea.

  1. make sure Edit->Spelling->Check Spelling As You Type is on. If you check a source file now, and click on a spelling error in a string literal, the spelling system should underline any errors for you.
  2. Now we're going to make TextMate find errors in comments. Open your Bundle Editor and select the Source bundle. Notice the "Spell Checking: Enable For Strings" (P) item. (P means preference). Copy the value from that item.
  3. From the + menu, select "New Preference". Name it "Spell Checking: Enable For Comments" (or whatever you want - its not the name that matters)
  4. Paste the { spellChecking... } that you copied from the "Enable For Strings" preference item into the value for this new preference
  5. Set your Scope Selector for the new preference item to source comment.block, comment.line
  6. Close the Bundle Editor and Reload Bundles

Your comments (as well as string values) should now be spell checked. You will of course get some false positives, since sometimes variable names are mentioned in comments - but it saves you from the general embarrassment of spelling errors in comments/string values.