Sublime Forum

How to deal with partially overlapping context?

#1

Today I get an interesting issue with my shellscript (mainly for Bash) highlighting package here.

As shown in the screenshot above, it highlights correctly but the scope ends wrongly.

It’s because after meeting <<EOF;, we go into a heredoc context hence } is not paired to { which is in the previous context. After meeting EOF, the heredoc ends, we pop current context and go back to a function context but there is already no } to close the function context. Finally, the scope ends with a source.shell meta... meta... but not a simple source.shell.

Any thought to fix this? Two contexts are partially overlapping.

0 Likes

#2

I just dealt with something similar in the c-rewrite branch because of the C preprocessor.

Effectively what you’ll need to do is:

  1. When seeing a heredoc in a { } block, parse the rest of the line as a “normal” expression, but in a custom context.
  2. If you find the end of a function in the rest of the line, set to a new context which is named something like function-finish-heredoc.
  3. By using set, you’ve popped out of the { } block context, and entered a new one with a meta_content_scope for the heredoc.

In C, C++, Objective-C and Objective-C++ this gets complicated fast because the preprocessor can slice up if/else branches in funky ways, and the only reliable way to detect functions is to ensure you are in the global scope. There isn’t any way to handle every situation due to the preprocessor making a full pass before the compiler, but I’ve used some real-world codebases to catch some of the more common situations. Anyway, here is a chunk of the logic, if you are so inclined: https://github.com/sublimehq/Packages/blob/c-rewrite/C%2B%2B/C.sublime-syntax#L507-L579.

2 Likes

#3

Thanks. I will check that when I am available :grinning:

0 Likes