[vim/vim] Incorrect highlighting for C++ special identifiers (#8745)

333 views
Skip to first unread message

Anis Ladram

unread,
Aug 10, 2021, 8:41:36 AM8/10/21
to vim/vim, Subscribed

Describe the bug
C++ identifiers with special meaning (final, override, import and module). are incorrectly highlighted out-of-context. These keywords should be considered as regular identifiers when used out of their special meaning context (c.f. C++20 standard, section 5.10, paragraph 2).

To Reproduce

  1. Run vim --clean
  2. Edit a.cc
  3. Type 'int final, override, import, module;'
  4. Incorrect highlighting for these variables name

Expected behavior
Highlighting for these variables names should be the same as the one for int x;

Environment (please complete the following information):
Vim version 8.2.3323


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications on the go with GitHub Mobile for iOS or Android.

Christian Brabandt

unread,
Aug 30, 2021, 5:47:27 AM8/30/21
to vim/vim, Subscribed

That is a CPP syntax issue. So I'll leave that for https://github.com/vim-jp/vim-cpp

Christian Brabandt

unread,
Aug 30, 2021, 5:47:29 AM8/30/21
to vim/vim, Subscribed

Closed #8745.

Dominique Pellé

unread,
Aug 30, 2021, 6:47:01 AM8/30/21
to vim/vim, Subscribed

This is probably difficult to do properly as C++ syntax is complex and Vim does not use a C++ parser, but uses heuristics based on regexp for syntax highlighting. We can maybe improve heuristics, but we can't expect to get syntax highlighting it 100% right with heuristics. In practice, it's already doing a fairly good job for C++. Calling variables override,final or module seems quite a rare and is asking for trouble. I understand that C++ allowed that for to avoid breaking existing software. Backward compatibility is important for C++. For Vim, the cost / benefit of fixing this ticket seems low IMO.

The only way to syntax highlight C++ correctly (and other languages) 100% correctly, is to have a real C++ parser using e.g. Tree-sitter. Maybe something for Vim-10? :-)

Christian Brabandt

unread,
Aug 30, 2021, 8:45:10 AM8/30/21
to vim/vim, Subscribed

yes, related: #8658

Note, Vim cannot do semantic highlighting (yet), just regex based highlighting.

Bram Moolenaar

unread,
Aug 30, 2021, 3:54:44 PM8/30/21
to vim/vim, Subscribed


> This is probably difficult to do properly as C++ syntax is complex and
> Vim does not use a C++ parser, but uses heuristics based on regexp for
> syntax highlighting. We can maybe improve heuristics, but we can't
> expect to get syntax highlighting it 100% right with heuristics. In
> practice, it's already doing a fairly good job for C++. Calling
> variables `override`,`final` or `module` seems quite a rare and is

> asking for trouble. I understand that C++ allowed that for to avoid
> breaking existing software. Backward compatibility is important for
> C++. For Vim, the cost / benefit of fixing this ticket seems low IMO.
>
> The only way to syntax highlight C++ correctly (and other languages)
> 100% correctly, is to have a real C++ parser using e.g. Tree-sitter.
> Maybe something for Vim-10? :-)

I know someone was working on Tree-sitter, and it was not easy. Is it
working now? Evaluating the advantages and disadvantages would be a
good idea. I would expect it to behave very different from the
pattern-based syntax highlighting. E.g., how well does it handle syntax
errors? What happens while typing? Is there a noticeable delay when
opening a large file? Does it solve the slowness of long lines (e.g.
with XML)?

--
Hacker: Someone skilled in computer programming (good guy).
Cracker: A hacker that uses his skills to crack software (bad guy).

/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Christian Brabandt

unread,
Aug 30, 2021, 4:27:43 PM8/30/21
to vim/vim, Subscribed

apparently it works quite nicely for Neovim (for languages that have treesitter parsers).

Yegappan Lakshmanan

unread,
Aug 31, 2021, 11:02:00 AM8/31/21
to vim_dev, vim/vim, Subscribed
Hi,

On Mon, Aug 30, 2021 at 1:27 PM Christian Brabandt <vim-dev...@256bit.org> wrote:

apparently it works quite nicely for Neovim (for languages that have treesitter parsers).


For Neovim, it looks like you need the nvim-treesitter plugin to use treesitter:


Based on the documentation, it looks like this needs a C compiler and libstdc++
in the path.

- Yegappan

Bram Moolenaar

unread,
Aug 31, 2021, 2:47:27 PM8/31/21
to vim...@googlegroups.com, Yegappan Lakshmanan
And it seems that what they call "modules" requires Lua.
That limits the audience to early adopters, it seems.
But perhaps it can be made to build and installed like Vim.
With the disadvantage of requiring a binary install to update
the parsers. Not a trivial thing to add, requires a design discussion.
The number of parsers seems quite big, but what's the quality like?


--
ALL: A witch! A witch!
WITCH: It's a fair cop.
ALL: Burn her! Burn her! Let's make her into a ladder.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
Reply all
Reply to author
Forward
0 new messages