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

Markdown headers aren't highlighted in the context window

Open ALVAROPING1 opened this issue 3 weeks ago • 0 comments

Description

Recently, the markdown queries bundled with nvim-treesitter were updated to capture the entire line of headers rather than the individual prefix and title text components (https://github.com/nvim-treesitter/nvim-treesitter/pull/6766). These new queries capture everything until the end of the line, including the new line character, and end at the first column of the next line.

image

With the new queries, when the renderer tries to reuse them to get the highlights for the context window (https://github.com/nvim-treesitter/nvim-treesitter-context/blob/master/lua/treesitter-context/render.lua#L138-L148), it finds that the captured text's range ends at the beginning of the next line while the context's range ends at the end of the current line (necol = 0, nerow = end_row+1, and end_col = end of <end_row> line). Since the end line of the capture is found to be after the end line of the context (nerow > end_row), the capture is skipped and the corresponding text is not highlighted.

Furthermore, since this also causes the iteration to stop, a user can't extend the nvim-treesitter's queries to add back the previous captures, because the iteration will stop before the user's captures are processed.

I'm not familiar enough with nvim-treesitter-context's codebase to know why the iteration is stopped instead of skipping the current capture, nor if changing the stop iteration condition to ignore the case when the capture ends at the beginning of the next line of the contex's end line would have any negative consequences.

Neovim version

NVIM v0.10.0 Build type: Release LuaJIT 2.1.1713484068

Expected behavior

The context window in markdown files highlights the titles with the @markup.heading.N.markdown highlight groups

Actual behavior

The context window in markdown files doesn't highlight titles:

https://github.com/nvim-treesitter/nvim-treesitter-context/assets/43814863/780db0bb-9b0c-40a0-ad19-09cd9c1d0d03

Minimal config

local plugins = {
  ts         = 'https://github.com/nvim-treesitter/nvim-treesitter',
  ts_context = 'https://github.com/nvim-treesitter/nvim-treesitter-context',
}

for name, url in pairs(plugins) do
  local install_path = '/tmp/nvim/site/'..name
  if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system { 'git', 'clone', '--depth=1', url, install_path }
  end
  vim.o.runtimepath = install_path..','..vim.o.runtimepath
end

-- Set a high contrast color as the markdown heading's highlight color to make the issue easy to spot
vim.api.nvim_set_hl(0, "@markup.heading", { fg = "#ff00ff" })
-- Activate treesitter highlight
require("nvim-treesitter.configs").setup({ highlight = { enable = true } })

Steps to reproduce

  1. nvim --clean -u minimal.lua
  2. Open a markdown file and add some headings
  3. Scroll the window until the headings go outside the screen
  4. See that the highlight color of the headings in the context's window differ from those in the buffer

ALVAROPING1 avatar Jun 22 '24 22:06 ALVAROPING1