Code Monkey home page Code Monkey logo

Comments (9)

bfrg avatar bfrg commented on May 27, 2024 1

I am not completely against it, but with Vim's currently shipped c.vim syntax file it just won't work most of the time.

What you asked for could be accomplished with something like:

syntax match cppSTLscoped "::" nextgroup=cppSTLtype
syntax keyword cppSTLtype contained hours path vector
highlight default link cppSTLtype Type

Now when hours, path or vector appear after a :: they will be highlighted as Type. The contained (alternatively we could also write containedin=cppSTLscoped) makes sure that these keywords are highlighted only when contained in another syntax group and not on their own.

But the problem is the ALLBUT option in the following syntax declaration in Vim's default c.vim syntax file:
https://github.com/vim/vim/blob/67fbdfefd26a237831c3838f799d3e6198c8a34a/runtime/syntax/c.vim#L161-L162

It basically says that any syntax group contained inside parentheses will be highlighted. See :help syn-contains for more details. This means, cppSTLtype will be highlighted when appearing inside parentheses, even without the preceding ::. For example, path will be highlighted in both foo(path) and foo(std::path).

A workaround is to clear the already declared syntax group cParen and redefine it with cppSTLtype:

syntax clear cParen
syntax region cParen transparent start='(' end=')' 
    \ contains=ALLBUT,@cParenGroup,cCppParen,cErrInBracket,cCppBracket,@cStringGroup,@Spell,cppSTLtype

Now cppSTLtype is highlighted only when appearing after a ::.

When you look at the shipped c.vim syntax file, cParen is declared multiple times, depending on the file type, the C++ standard, and whether or not the user wants curly or bracket errors highlighted. I am very hesitant to copy/paste all those lines just for the sake of cppSTLtype. And I haven't found another way yet.

There is already a request to fix this ALLBUT behavior, see vim/vim#1265.

from vim-cpp-modern.

anntzer avatar anntzer commented on May 27, 2024

Something like

syntax region cppSTLscoped start='\(std\)\@3<=::\<' end='\>' contains=cppSTLtype

syntax keyword cppSTLtype allocator contained
syntax keyword cppSTLtype auto_ptr contained
syntax keyword cppSTLtype basic_filebuf contained
...

(with separate groups for each namespace if we want to actually check the namespace, or with a single container group just checking for a preceding ::) works.

from vim-cpp-modern.

bfrg avatar bfrg commented on May 27, 2024

@anntzer I gave up on such complicated syntax highlighting long time ago. I didn't really need it anyway. Have you tried color_coded?

from vim-cpp-modern.

anntzer avatar anntzer commented on May 27, 2024

The main problem I have with color_coded (and other similar tools) is the need to maintain a .color_coded file. My use case is often python extension modules, where the build flags are generated by setup.py and often requires shelling out to pkg-config anyways. In fact the include paths are likely to be different from one environment to another, so a static .color_coded will just not be enough.

from vim-cpp-modern.

bfrg avatar bfrg commented on May 27, 2024

There's YCM-Generator that also generates .color_coded files.

from vim-cpp-modern.

anntzer avatar anntzer commented on May 27, 2024

It does not support setuptools (https://github.com/rdnetto/YCM-Generator#requirements-and-limitations). Even if I were to write it manually, .color_coded is necessarily a static list of flags, which is not sufficient (pkg-config actually returns different paths on different build systems for me).
If .color_coded was actually able to interpolate subcommands (or run some $scriptlanguage) then I would be able to manually work around it, but right now it's not the case.

from vim-cpp-modern.

bfrg avatar bfrg commented on May 27, 2024

Have you looked at the alternatives clighter8 or chromatica.nvim? Both are written in python.
There is also taghighlight, which highlights keywords based on a ctags file.

from vim-cpp-modern.

anntzer avatar anntzer commented on May 27, 2024

From a quick look, all the compiler-based projects suffer from the same issue (already mentioned above): they require a static list of compile flags (which may or may not be autogenerated via cmake/bear/etc.). This does not handle the (my) case where your compilation options are e.g. $(CXX) $(pkg-config --cflags pkg) ...: good old makefiles are not dead yet.

TagHighlight may or may not work, haven't looked too much into it.

from vim-cpp-modern.

bart9h avatar bart9h commented on May 27, 2024

I also find it annoying to have variables like "hours", and "path" highlighted like they were special.
Maybe a simpler solution would be an option to not highlight these unless there is a leading "::".

from vim-cpp-modern.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.