I’m writing a custom syntax for a language that uses the C preprocessor. I ideally would like to get some behavior that I’m used to from emacs. In particular, I would like “expand selection to scope” to select the enclosing #if or #else branch and expanding again to select the entire #if #else #endif conditional.
I have what appears to me to be a correct syntax, but I find that the else branch works and the if branch doesn’t. I’m confused abut what “expand selection to scope” actually does because the syntax looks symmetrical.
In the following example:
#ifdef a
#else
#endif
I have a scope called “meta.ppc-cond” that starts with the # in ifdef and ends at the newline after endif. I have a scope called meta.if-branch that starts at the # in ifdef and ends just before #else (there’s a forward lookahead for #else that pops the if-branch context). There is another scope called meta.else-branch that starts at the # in else and extends to the newline after #endif.
Inside the #ifdef branch, if I do an expand selection to scope, it selects from #ifdef though the newline before #else, as expected. If I do the same inside the #else branch, it selects from the #else through the end of #endif. So far, so good.
If I expand-to-selection twice inside the else clause, the first time selects the else clause (as above), and the second time selects the entire conditional. This is exactly what I want.
If I expand-to-selection twice inside the if clause, however, the first time selects the if clause (as above), but the second time selects the entire file, rather than from #ifdef through #endif. This is what I’m confused about. Everything in the if clause is also in meta.ppc-cond, so why wouldn’t it expand to the meta.ppc-cond scope?
I had some trouble getting the else clause to work. In my original definition, meta.else-clause and meta.ppc-cond ended just after #endif (not including the newline). When I did a first “expand scope to selection” in the else clause, it looks like it selected the entire line containing #endif, but because the selection wasn’t entirely contained in meta.ppc-cond, the second expansion didn’t work. I’d imagine that something similar is going on here (the selection is somehow extending beyond the scope), but it really looks like everything selected by the if-branch is also in meta.ppc-cond. I assume there’s some subtlety here that I’m not aware of.
Any ideas? Thanks.