One of the things on my Dojo completion hitlist was handling multiline statements, especially dojo.query
chains, since I tend to break them into multiple lines when chaining more than 1 or 2 NodeList
function calls (and I’m betting most folks do that, too). While thinking about how to handle this, I came to the conclusion that there were basically three ways I could approach it:
- Implement a real Javascript parser that is liberal enough to handle incomplete statements (e.g. it has to handle the statement you’re trying to complete!), or
- Integrate an existing parser that is liberal enough to handle incomplete statements (I have no idea if a liberal open source JS parser exists), or
- Implement something simpler and easier that covers most or all of what I think of as common cases.
I decided to go with #3. In fact, this is my general rule of thumb for the completion bundle in the short and medium term. I want to make something that is both useful, and something I can actually release since I’m only working on it a few hours each week. An unreleased, vaporware completion bundle is far less cool than a released one that works 80% of the time.
Here are a few shots of the multiline statement parser in action, completing a dojo.query
chain. Notice also, in the last shot, it guesses correctly that I am starting a new statement, even though I didn’t end the previous one with a semicolon. However, it’s not perfect, and will almost certainly break under less common conditions. I’ve tried, though, to abstract the parser from the completion code in a way that allows the parser to be improved when I find cases where it breaks.