nvim-treesitter-context icon indicating copy to clipboard operation
nvim-treesitter-context copied to clipboard

Syntax highlight colors are not always respected

Open rwblokzijl opened this issue 3 years ago • 2 comments

When using this plugin, the syntax highlight colors are not always respected (at least within terraform).

Normal highlighting(using treesitter): 2022-09-02-140333_334x34_scrot Highlighting with nvim-treesitter-context: 2022-09-02-140359_345x33_scrot

It seems to use the basic vim highlights instead of the treesitter ones. Python does seem to work correctly with treesitter hightlights.

rwblokzijl avatar Sep 02 '22 12:09 rwblokzijl

It seems to be using the same highlighting as when you visual select something:

  • Normal: image
  • treesitter-context: image
  • Visual Selection: image

hisbaan avatar Sep 02 '22 16:09 hisbaan

I don't have the same issue:

  • Visual Selection: 2022-09-06-142148_317x35_scrot

rwblokzijl avatar Sep 06 '22 12:09 rwblokzijl

Vim highlights are not used. The treesitter highlights are applied by reading the AST at the position.

Please provide precise instructions on how to reproduce this.

lewis6991 avatar Nov 22 '22 16:11 lewis6991

Hi, I'm having a similar issue with a plugin of mine, reported here. Is there any way for other highlights to also get added to the context buffer? I'm using nvim_buf_set_extmark to add my plugin's highlights

m-demare avatar Dec 14 '22 20:12 m-demare

Is there any way for other highlights to also get added to the context buffer? I'm using nvim_buf_set_extmark to add my plugin's highlights

Not currently. ts-context has its own mini treesitter highlighter to apply highlights directly from the AST. Other highlights in the buffer aren't looked at.

lewis6991 avatar Dec 14 '22 21:12 lewis6991

Interesting. Maybe it would be nice to allow other plugins to register a callback to add highlights on top of that, or something like that. I could maybe try my hand at it this weekend if you think it makes sense

m-demare avatar Dec 16 '22 01:12 m-demare

Here's a reproducible example. It seems to occur with some colour schemes, so I've included one that has the issue. @rwblokzijl seems to have one too which looks like some sort of gruvbox variant.

Minimal config

Save this into a file called minimal.lua (or whatever you want to name it) then run nvim -u minimal.lua example.lua where example.lua is the other code block below.

-- minimal.lua
vim.cmd([[packadd packer.nvim]])

local packer = require("packer")
packer.init({
	enable = true,
	threshold = 0,
})

local use = packer.use
packer.reset()

packer.startup({
	function()
		use({
			"rose-pine/neovim",
			as = "rose-pine",
			config = function()
				vim.cmd("colorscheme rose-pine")
                vim.opt.background = 'dark'
			end,
		})
        use { 'nvim-treesitter/nvim-treesitter-context' }
	end,
})

require('treesitter-context').setup{
    enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
    max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
    trim_scope = 'outer', -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
    patterns = { -- Match patterns for TS nodes. These get wrapped to match at word boundaries.
        -- For all filetypes
        -- Note that setting an entry here replaces all other patterns for this entry.
        -- By setting the 'default' entry below, you can control which nodes you want to
        -- appear in the context window.
        default = {
            'class',
            'function',
            'method',
            'for',
            'while',
            'if',
            'switch',
            'case',
        },
        -- Patterns for specific filetypes
        -- If a pattern is missing, *open a PR* so everyone can benefit.
        tex = {
            'chapter',
            'section',
            'subsection',
            'subsubsection',
        },
        rust = {
            'impl_item',
            'struct',
            'enum',
        },
        scala = {
            'object_definition',
        },
        vhdl = {
            'process_statement',
            'architecture_body',
            'entity_declaration',
        },
        markdown = {
            'section',
        },
        lua = {
            'local function',
        }
    },
    exact_patterns = {
        -- Example for a specific filetype with Lua patterns
        -- Treat patterns.rust as a Lua pattern (i.e "^impl_item$" will
        -- exactly match "impl_item" only)
        -- rust = true,
    },

    -- [!] The options below are exposed but shouldn't require your attention,
    --     you can safely ignore them.

    zindex = 20, -- The Z-index of the context window
    mode = 'cursor',  -- Line used to calculate context. Choices: 'cursor', 'topline'
    separator = nil, -- Separator between context and content. Should be a single character string, like '-'.
}

Example file

-- example.lua
require('test').setup({
    function()
        -- this is a test

        -- scroll so require leaves the screen
        -- and shows up in treesitter context

        -- You can use C-e to scroll down
        -- without moving the cursor
    end
})

hisbaan avatar Dec 26 '22 03:12 hisbaan

Yes i use ellisonleao/gruvbox.nvim with a red i liked better: https://github.com/rwblokzijl/neovim/blob/85326669b4db8a5de346a0af8a5b9a69378c0e99/lua/util/colors.lua#L40

rwblokzijl avatar Dec 28 '22 14:12 rwblokzijl

Is this issue still present with #198 merged?

lewis6991 avatar Mar 08 '23 13:03 lewis6991

Seems fixed! Thanks a lot! (tested with https://github.com/nvim-treesitter/nvim-treesitter-context/pull/210)

rwblokzijl avatar Mar 08 '23 14:03 rwblokzijl

I see 1 exception: the first line of a template literal image

image

rwblokzijl avatar Mar 08 '23 15:03 rwblokzijl

It's still there for me. Using the above minimal.lua/example.lua

regular: image

treesitter-context: image

Another theme where this occurs is catppuccin (tested on all flavours)

hisbaan avatar Mar 08 '23 16:03 hisbaan

Hi all! In the past I've noticed the same issue (I mainly use Sonokai as colorscheme), and it shows up with the example.lua provided by @hisbaan. In mine only the color of the word setup is wrong though. From what I've seen poking around, the highlights are applied in the correct order using the vim.api.nvim_buf_set_extmark() but when you query the extmark for that specific area the order is not respected:

From row 0 to 0 and from col 0 to 7 with Extmark ID 1 --> Name capture: 'variable' || Node type: 'identifier' || Highlight group: '@variable.lua' From row 0 to 0 and from col 0 to 7 with Extmark ID 2 --> Name capture: 'function.call' || Node type: 'identifier' || Highlight group: '@function.call.lua' From row 0 to 0 and from col 0 to 7 with Extmark ID 3 --> Name capture: 'function.builtin' || Node type: 'identifier' || Highlight group: '@function.builtin.lua' From row 0 to 0 and from col 7 to 8 with Extmark ID 4 --> Name capture: 'punctuation.bracket' || Node type: '(' || Highlight group: '@punctuation.bracket.lua' From row 0 to 0 and from col 8 to 14 with Extmark ID 5 --> Name capture: 'string' || Node type: 'string' || Highlight group: '@string.lua' From row 0 to 0 and from col 8 to 14 with Extmark ID 6 --> Name capture: 'spell' || Node type: 'string' || Highlight group: '@spell.lua' From row 0 to 0 and from col 14 to 15 with Extmark ID 7 --> Name capture: 'punctuation.bracket' || Node type: ')' || Highlight group: '@punctuation.bracket.lua' From row 0 to 0 and from col 15 to 16 with Extmark ID 8 --> Name capture: 'punctuation.delimiter' || Node type: '.' || Highlight group: '@punctuation.delimiter.lua' From row 0 to 0 and from col 16 to 21 with Extmark ID 9 --> Name capture: 'variable' || Node type: 'identifier' || Highlight group: '@variable.lua' From row 0 to 0 and from col 16 to 21 with Extmark ID 10 --> Name capture: 'field' || Node type: 'identifier' || Highlight group: '@field.lua' From row 0 to 0 and from col 16 to 21 with Extmark ID 11 --> Name capture: 'function.call' || Node type: 'identifier' || Highlight group: '@function.call.lua' From row 0 to 0 and from col 21 to 22 with Extmark ID 12 --> Name capture: 'punctuation.bracket' || Node type: '(' || Highlight group: '@punctuation.bracket.lua' From row 0 to 0 and from col 22 to 23 with Extmark ID 13 --> Name capture: 'punctuation.bracket' || Node type: '{' || Highlight group: '@punctuation.bracket.lua' From row 0 to 0 and from col 22 to 23 with Extmark ID 14 --> Name capture: 'constructor' || Node type: '{' || Highlight group: '@constructor.lua' From row 1 to 1 and from col 4 to 12 with Extmark ID 15 --> Name capture: 'keyword.function' || Node type: 'function' || Highlight group: '@keyword.function.lua' From row 1 to 1 and from col 12 to 13 with Extmark ID 16 --> Name capture: 'punctuation.bracket' || Node type: '(' || Highlight group: '@punctuation.bracket.lua' From row 1 to 1 and from col 13 to 14 with Extmark ID 17 --> Name capture: 'punctuation.bracket' || Node type: ')' || Highlight group: '@punctuation.bracket.lua' { { 9, 0, 16 }, { 11, 0, 16 }, { 10, 0, 16 } }

The last line is obtained with vim.api.nvim_buf_get_extmarks() specifying the row and columns where setup is in example.lua. I think that the correct order should be: { { 9, 0, 16 }, { 10, 0, 16 }, { 11, 0, 16 } } Now, even if this assumption is correct, I don't know how I would fix it.

mrsflv avatar Mar 12 '23 21:03 mrsflv