Sublime Forum

SQL syntax highlight within C/C++ strings?

#1

In ST3, if a python string starts with an uppercase SQL keyword (SELECT/DELETE/INSERT/…), the entire string gets colored appropriately for an SQL statement. Unfortunately, the same is not true for C++. I have never developed ST syntax files, but I see no reason why adding similar feature for C++ syntax would be much harder - at least for C++11 raw strings. Maybe there already exists a package with improved C++ syntax that has such feature?

0 Likes

#2

Today I had time to fiddle around with syntax definitions, taking the Python.sublime-syntax as example. I managed to make C/C++ string literals to be highlighted with SQL syntax if they begin with SQL keywords. But.

Now I guess I know why this feature is absent from the original ST C/C++ syntax :slight_smile: It just doesn’t make as much sense there as it does, e.g., in Python.

The point is that more often than not your SQL expressions are long enough and are formatted inside the code into multiple lines. Python has no difficulty in dealing with that, using its triple-quoted strings. But there is no direct analog in C++. Yes, there are raw string literals (since C++11). But since they might use custom delimiters, it is impossible (or at least not that trivial) to create syntax definition that will work universally. That’s probably even a bigger downside to R"delimiter(raw_string)delimiter" than those mentioned in this complaint at StackExchange.

0 Likes

#3

In case if someone else uses heavily regexps and SQL within C++ source code, here I’ve tweaked ST3’s C++ syntax definition so that one might use raw string literals with predefined delimiters to highlight nested syntax.

0 Likes