nvim-treesitter / nvim-treesitter

Nvim Treesitter configurations and abstraction layer
Apache License 2.0
10.91k stars 908 forks source link

cpp highlight in markdown incorrect #4887

Closed williamhCode closed 1 year ago

williamhCode commented 1 year ago

Describe the highlighting problem

In a markdown file, if I write some cpp code, the @text.literal.markdown highlight overrides @variable.cpp, so the highlighting is off.

image

bob should be red :Inspect -

image

Example snippet that causes the problem

```cpp
Person bob = {};
bob.name = "bob";

### Tree-sitter parsing result

section [0, 0] - [4, 0] markdown fenced_code_block [0, 0] - [4, 0] markdown fenced_code_block_delimiter [0, 0] - [0, 3] markdown info_string [0, 3] - [0, 6] markdown language [0, 3] - [0, 6] markdown block_continuation [1, 0] - [1, 0] markdown code_fence_content [1, 0] - [3, 0] markdown declaration [1, 0] - [1, 16] cpp type: type_identifier [1, 0] - [1, 6] cpp declarator: init_declarator [1, 7] - [1, 15] cpp declarator: identifier [1, 7] - [1, 10] cpp "=" [1, 11] - [1, 12] cpp value: initializer_list [1, 13] - [1, 15] cpp "{" [1, 13] - [1, 14] cpp "}" [1, 14] - [1, 15] cpp ";" [1, 15] - [1, 16] cpp expression_statement [2, 0] - [2, 17] cpp assignment_expression [2, 0] - [2, 16] cpp left: field_expression [2, 0] - [2, 8] cpp argument: identifier [2, 0] - [2, 3] cpp operator: "." [2, 3] - [2, 4] cpp field: field_identifier [2, 4] - [2, 8] cpp operator: "=" [2, 9] - [2, 10] cpp right: string_literal [2, 11] - [2, 16] cpp """ [2, 11] - [2, 12] cpp """ [2, 15] - [2, 16] cpp ";" [2, 16] - [2, 17] cpp "=" [1, 11] - [1, 12] markdown "{" [1, 13] - [1, 14] markdown "}" [1, 14] - [1, 15] markdown ";" [1, 15] - [1, 16] markdown block_continuation [2, 0] - [2, 0] markdown "." [2, 3] - [2, 4] markdown "=" [2, 9] - [2, 10] markdown """ [2, 11] - [2, 12] markdown """ [2, 15] - [2, 16] markdown ";" [2, 16] - [2, 17] markdown block_continuation [3, 0] - [3, 0] markdown fenced_code_block_delimiter [3, 0] - [3, 3] markdown


### Example screenshot

<img width="376" alt="image" src="https://github.com/nvim-treesitter/nvim-treesitter/assets/83525937/1dee91c8-4af9-4558-b658-b2580d4434be">

colorscheme: https://github.com/olimorris/onedarkpro.nvim

### Expected behavior

`bob` should be highlighted red, not grayed out

### Output of `:checkhealth nvim-treesitter`

```markdown
==============================================================================
nvim-treesitter: require("nvim-treesitter.health").check()

Installation ~
- OK `tree-sitter` found 0.20.8 (parser generator, only needed for :TSInstallFromGrammar)
- OK `node` found v20.2.0 (only needed for :TSInstallFromGrammar)
- OK `git` executable found.
- OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
  Version: Apple clang version 14.0.0 (clang-1400.0.29.202)
- OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

OS Info:
{
  machine = "arm64",
  release = "21.6.0",
  sysname = "Darwin",
  version = "Darwin Kernel Version 21.6.0: Thu Mar  9 20:10:19 PST 2023; root:xnu-8020.240.18.700.8~1/RELEASE_ARM64_T8101"
} ~

Parser/Features         H L F I J
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - c_sharp             ✓ ✓ ✓ . ✓
  - cpp                 ✓ ✓ ✓ ✓ ✓
  - glsl                ✓ ✓ ✓ ✓ ✓
  - java                ✓ ✓ ✓ ✓ ✓
  - latex               ✓ . ✓ . ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - markdown            ✓ . ✓ . ✓
  - markdown_inline     ✓ . . . ✓
  - python              ✓ ✓ ✓ ✓ ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - rust                ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓
  - wgsl                ✓ . ✓ ✓ .

  Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections
         +) multiple parsers found, only one will be used
         x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version

NVIM v0.10.0-dev-415+g55f6a1cab-Homebrew
Build type: Release
LuaJIT 2.1.0-beta3

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/opt/homebrew/Cellar/neovim/HEAD-55f6a1c/share/nvim"

Run :checkhealth for more info

Additional context

Only happens with cpp as far as I've experienced.

theHamsta commented 1 year ago

Not much that can be done here, unless tree-sitter-cpp returns a node for whatever bob is.

The ask is to give injections a (unconditionally?) higher priority than the parent language (when both would have normally the same priority). This could be implemented or at least have discussion on how users could influence.

Is the problem that variable has smaller priority? Because it should actually be visible if the range is smaller than the literal.

clason commented 1 year ago

Yes, that is indeed the case (in the C highlights, which C++ inherits):

; Lower priority to prefer @parameter when identifier appears in parameter_declaration.
((identifier) @variable (#set! "priority" 95))

May want to remove that (and rely on query ordering for precedence).