Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude C++ raw string literals from syntax highlighting in Vim?

Quite honestly, raw string literals are a great addition to the C++ language. But (as expected) editors have a hard time to properly display those literals.

I am using Vim 7.4 and out-of-the-box raw string literals completely break the syntax highlighting. For example in

char const txt[] = R"(printf(")";

the 2nd '(' is highlighted red in vim.

Something like

char const txt2[] = R"(  "{{"  )";

breaks the highlighting of curly braces and the syntax based auto-ident - and so on.

For a start I would be happy to have Vim ignore everything between R"( and )" when doing syntax highlighting.

But note that raw string literals are flexible - arbitrary matching strings are allowed between the first/last double-quote/brace pair, e.g.

R"abcd()")")abcd"

is also a valid raw string literal which encodes

)")"

See also the cppreference link for a general definition of the syntax.

Thus my question how to configure Vim such that C++ raw string literals are properly recognized.

Vim already seems to include some facilities to properly synatx highlight language fragments embedded in a host language (e.g. for compiler-compiler source files). Perhaps they can be used for the raw string literal case as well?

like image 685
maxschlepzig Avatar asked Mar 26 '14 22:03

maxschlepzig


2 Answers

Add this

syntax match cString 'R"\([^(]*\)(\_.*)\1"'

to your custom C++ syntax file (normally ~/.vim/syntax/cpp.vim ; create this file if you don't have one).

like image 129
n. 1.8e9-where's-my-share m. Avatar answered Oct 21 '22 20:10

n. 1.8e9-where's-my-share m.


Just add cpp-vim as a plugin. I have added strict support for newer string literals in pull-request #14.

This is what you get: http://bl.ocks.org/anonymous/raw/9442865

cpp-vim adds support for other C++11 stuff too.

like image 43
pepper_chico Avatar answered Oct 21 '22 20:10

pepper_chico