nvim-treesitter / nvim-treesitter

Nvim Treesitter configurations and abstraction layer
Apache License 2.0
10.93k stars 910 forks source link

Language injection highlighting keeps colors from the parent language #4157

Open lucasrafael98 opened 1 year ago

lucasrafael98 commented 1 year ago

Describe the highlighting problem

When injecting highlights into strings in a Go file, some identifiers keep the color used for strings in Go, instead of the color they should have if they were in a plain file of their own language. The below example shows this: HTML tags are green (string color) where in a plain HTML file they'd be white, and the same with the select_expression terms in the SQL string.

Color scheme used is https://github.com/ellisonleao/gruvbox.nvim, but it seems to apply to the default as well.

Example snippet that causes the problem

package main

import "fmt"

func main() {
    fmt.Println(`
        <html>
        </html>
    `)

    fmt.Println(`
        SELECT id, name FROM table
    `)
}

Tree-sitter parsing result

package_clause [0, 0] - [0, 12]
  package_identifier [0, 8] - [0, 12]
import_declaration [2, 0] - [2, 12]
  import_spec [2, 7] - [2, 12]
    path: interpreted_string_literal [2, 7] - [2, 12]
function_declaration [4, 0] - [13, 1]
  name: identifier [4, 5] - [4, 9]
  parameters: parameter_list [4, 9] - [4, 11]
  body: block [4, 12] - [13, 1]
    call_expression [5, 1] - [8, 3]
      function: selector_expression [5, 1] - [5, 12]
        operand: identifier [5, 1] - [5, 4]
        field: field_identifier [5, 5] - [5, 12]
      arguments: argument_list [5, 12] - [8, 3]
        raw_string_literal [5, 13] - [8, 2]
          element [6, 2] - [7, 9]
            start_tag [6, 2] - [6, 8]
              tag_name [6, 3] - [6, 7]
            end_tag [7, 2] - [7, 9]
              tag_name [7, 4] - [7, 8]
    call_expression [10, 1] - [12, 3]
      function: selector_expression [10, 1] - [10, 12]
        operand: identifier [10, 1] - [10, 4]
        field: field_identifier [10, 5] - [10, 12]
      arguments: argument_list [10, 12] - [12, 3]
        raw_string_literal [10, 13] - [12, 2]
          statement [11, 2] - [11, 28]
            select [11, 2] - [11, 17]
              keyword_select [11, 2] - [11, 8]
              select_expression [11, 9] - [11, 17]
                term [11, 9] - [11, 11]
                  value: field [11, 9] - [11, 11]
                    name: identifier [11, 9] - [11, 11]
                term [11, 13] - [11, 17]
                  value: field [11, 13] - [11, 17]
                    name: identifier [11, 13] - [11, 17]
            from [11, 18] - [11, 28]
              keyword_from [11, 18] - [11, 22]
              relation [11, 23] - [11, 28]
                table_reference [11, 23] - [11, 28]
                  name: identifier [11, 23] - [11, 28]

Example screenshot

image

Expected behavior

Colors I'd expect would be the same as the colors in a plain file: image image

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check()
========================================================================
## Installation
  - OK: `tree-sitter` found 0.20.7 (parser generator, only needed for :TSInstallFromGrammar)
  - OK: `node` found v18.7.0 (only needed for :TSInstallFromGrammar)
  - OK: `git` executable found.
  - OK: `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" }
    Version: cc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
  - OK: Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI.

## OS Info:
{
  machine = "x86_64",
  release = "5.19.0-29-generic",
  sysname = "Linux",
  version = "#30-Ubuntu SMP PREEMPT_DYNAMIC Wed Jan 4 12:14:09 UTC 2023"
}

## Parser/Features         H L F I J
  - sql                 ✓ . . . ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - norg                ✓ . ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - css                 ✓ . ✓ ✓ ✓
  - bash                ✓ ✓ ✓ . ✓
  - json                ✓ ✓ ✓ ✓ .
  - vim                 ✓ ✓ ✓ . ✓
  - help                ✓ . . . ✓
  - markdown            ✓ . ✓ . ✓
  - norg_meta           ✓ . . ✓ .
  - nix                 ✓ ✓ ✓ . ✓
  - terraform           ✓ . ✓ ✓ ✓
  - go                  ✓ ✓ ✓ ✓ ✓
  - html                ✓ ✓ ✓ ✓ ✓
  - hcl                 ✓ . ✓ ✓ ✓
  - yaml                ✓ ✓ ✓ ✓ ✓
  - scheme              ✓ . ✓ . ✓
  - embedded_template   ✓ . . . ✓
  - dockerfile          ✓ . . . ✓
  - query               ✓ ✓ ✓ ✓ ✓

Output of nvim --version

NVIM v0.8.1
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by runner@fv-az178-366

Features: +acl +iconv +tui

Also tested with the latest nightly build, same result:

NVIM v0.9.0-dev-661+ga37c686d2
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/gcc-10  -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -g  -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fno-common -fdiagnostics-color=always -fstack-protector-strong -DNVIM_UNIBI_HAS_VAR_FROM -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DMIN_LOG_LEVEL=3 -I/home/runner/work/neovim/neovim/.deps/usr/include -I/usr/include -I/home/runner/work/neovim/neovim/.deps/usr/include/luajit-2.1 -I/home/runner/work/neovim/neovim/build/src/nvim/auto -I/home/runner/work/neovim/neovim/build/include -I/home/runner/work/neovim/neovim/build/cmake.config -I/home/runner/work/neovim/neovim/src
Compiled by runner@fv-az207-335

Features: +acl +iconv +tui
See ":help feature-compile"

Additional context

No response

bartels commented 1 year ago

This may be a related issue to #3335

Graphql injected into a javascript template string, some identifiers are highlighted with the @string capture from javascript instead of the capture from graphql.

In both cases, the parent language highlight is overriding the injected language