The way TextMate “parses” the current “context” – in a Javascript statement – is flawed. I love the editor… but I HATE having to be so manually-vigilant in my day-to-day bracket-matching.
See below – a scenario in which Textmate incorrectly “sees” the brackets in commented lines, and “matches them” with uncommented code!

Here is the code, if you have images turned off, or use Lynx, or something crazy, like that..
$('.parishilton').live( 'fartOn', function() {
sexy = $('#thatshot').fadeOut('fast');
/** $('shouldnt').matchThis(function(){ */
// $('or').thisOneEither(function(){
}); // Textmate thinks I should be here!
}); // Me too!
}); // ONLY this one should "match".
This is made evident when using the built-in ⬆+⌘+B to highlight the current scope, as well as when passing through them with the insertion point – which “pops” the incorrectly matched (commented) brackets. (Impossible to screenshot, sorry)
Xcode does a better job of matching the context, and recognizing the syntax.. but I can tell – it too struggles with this. (Made evident by flaky folding, etc with such structures.) At least Xcode knows this is WRONG.. where as ™ will let you go on and on… Thinking all is well.. Even when your brackets are seriously f*%#@*d.
Is Javascript just hard for syntax analyzers to “know for sure” what is going on? Is there a “better” syntax for comments that I could be using? In the above example I’ve tried both C-style /** comment */, as well as // comment notations… but both seem to suffer the same shortcomings?
Or is this just a case of a poor implementation in TextMate? If so, is there a plist or something I can muck with to try and improve the state of affairs?
JavaScript can be hard to parse, indeed, specially when you use error-ridden callback-spaghetti code like in your sample. I seem to remember that TextMate’s parsing is regexp-based. If I’m correct that kind of system is not very smart or flexible. FYI Vim exhibits the same behaviour.
TextMate doesn’t do syntax-checking at all. It doesn’t run your code through a build process or some static analysis tool at each key press. That’s why it’s bad at spotting and showing mistakes. TextMate is not an IDE, it’s a text editor.
The corollary to the previous points is that you should – at least – run your code through
jslintorjshintto spot mistakes. There’s a bundle for that called “JavaScript Tools” IIRC. Once you know where the problems are – according to your SA tool of choice – you can fix them without relying on TextMate’s limited syntax-checking abilities. This step (syntax checking) is a crucial part of any serious coding – however high or low your level of expertise is -, you may be able to automate it.That said, if you write a lot of code like that,
jslintmay not be enough for your needs. Here is a screenshot of your code in Vim after runningjslintthrough syntastic:Errors are spotted one at a time, here. Fixing your code is sure to be a long and boring process. It’s not always like that, of course, but it can – and does – happen.
Syntax checking as never been and is not part of TextMate’s overall design.
I don’t know what’s planned for TM2 (lost patience, switched to Vim). Maybe live syntax checking is in the pipe, maybe it isn’t. I don’t know and I don’t care.
An option, if you don’t like that part of TextMate’s design, would be to drop it and use an IDE like Eclipse or Komodo or NetBeans or WebStorm or Aptana… That range of programs typically run all kinds of background processes as you type specifically to prevent that kind of problem. Here is a screenshot of your code in Eclipse:
As you can see, Eclipse doesn’t match the bracket at the cursor with anything and it shows useful warnings where you have orphaned brackets and semicolons.
$ideNameis obviously not as sexy as TextMate but it’s definitely more powerful in that kind of scenario.Think about it.
A second option is to use TextMate’s own “Bundle Editor” to alter JavaScript’s syntax definition. And propose your changes to the ones in charge at Macromates.