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

3

8

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?

Coplanar answered 26/3, 2014 at 22:28 Comment(0)
S
5

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.

Shantelleshantha answered 27/3, 2014 at 7:14 Comment(1)
Works great, they use the syn region syntax highlighting command to recognize raw string literals - such that also multi-line raw string literals are highlighted corrected. Directly cloning the git repository into a pathogen bundle directory works, too.Coplanar
L
7

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).

Lomasi answered 26/3, 2014 at 23:9 Comment(3)
for sure, it's minimal addition just to support c++11, it's used to add stuff to the official built in vim syntax files.Shantelleshantha
sometimes, for any obscure reason, things don't get added to the official vim files...Shantelleshantha
@Coplanar I have somewhat fixed multiline strings (added \_.*) but it's really not that robust and will not match wchar_t strings, unicode strings, ... cpp-vim has much better support for c++11 features, use it.Lomasi
S
5

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.

Shantelleshantha answered 27/3, 2014 at 7:14 Comment(1)
Works great, they use the syn region syntax highlighting command to recognize raw string literals - such that also multi-line raw string literals are highlighted corrected. Directly cloning the git repository into a pathogen bundle directory works, too.Coplanar
U
0

A tiny tweak on the above syntax rule:

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

The original attempts to greedily select the longest match; so if you have multiple raw strings in a file (using the same open/close pattern) is would break. This one is non-greedy, and should match correctly. Thank you so much for the original though, it was a huge help to me!

Unprepared answered 28/4, 2019 at 2:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.