Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `blueprint`
- [ ] `bp`
- [ ] `brightscript`
- [ ] `c3`
- [ ] `caddy`
- [ ] `cairo`
- [ ] `chatito`
Expand Down Expand Up @@ -190,6 +191,7 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `gowork`
- [ ] `gpg`
- [ ] `gren`
- [ ] `groq`
- [ ] `gstlaunch`
- [ ] `hack`
- [ ] `hare`
Expand Down Expand Up @@ -222,6 +224,7 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `just`
- [ ] `kcl`
- [ ] `kconfig`
- [ ] `kitty`
- [ ] `koto`
- [ ] `kusto`
- [ ] `lalrpop`
Expand Down Expand Up @@ -254,6 +257,7 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `perl`
- [ ] `phpdoc`
- [ ] `pioasm`
- [ ] `pkl`
- [ ] `po`
- [ ] `pod`
- [ ] `poe_filter`
Expand Down Expand Up @@ -285,6 +289,7 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `rego`
- [ ] `requirements`
- [ ] `rescript`
- [ ] `rifleconf`
- [ ] `rnoweb`
- [ ] `robot`
- [ ] `robots`
Expand All @@ -300,10 +305,12 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `slint`
- [ ] `smithy`
- [ ] `snakemake`
- [ ] `snl`
- [ ] `soql`
- [ ] `sosl`
- [ ] `sourcepawn`
- [ ] `sparql`
- [ ] `sproto`
- [ ] `sql`
- [ ] `squirrel`
- [ ] `ssh_config`
Expand Down Expand Up @@ -342,12 +349,14 @@ Note: support for specific languages is strictly community maintained and can br
- [ ] `wgsl_bevy`
- [ ] `wing`
- [ ] `wit`
- [ ] `wxml`
- [ ] `xcompose`
- [ ] `xresources`
- [ ] `yuck`
- [ ] `zathurarc`
- [ ] `ziggy`
- [ ] `ziggy_schema`
- [ ] `zsh`

</details>

Expand Down Expand Up @@ -384,6 +393,11 @@ require'treesitter-context'.setup{
Use the highlight group `TreesitterContext` to change the colors of the
context. Per default it links to `NormalFloat`.

Use `TreesitterContextLevel1` ... `TreesitterContextLevel8` to apply
depth-specific highlights for nested context rows. By default each level links
to `TreesitterContext`. Nesting depths greater than 8 fall back to
`TreesitterContext`.

Use the highlight group `TreesitterContextLineNumber` to change the colors of the
context line numbers if `line_numbers` is set. Per default it links to `LineNr`.

Expand Down
13 changes: 13 additions & 0 deletions doc/nvim-treesitter-context.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ HIGHLIGHTS *nvim-treesitter-context-highlights*

Colors of the context.

`TreesitterContextLevel1` *hl-TreesitterContextLevel1*
`TreesitterContextLevel2` *hl-TreesitterContextLevel2*
`TreesitterContextLevel3` *hl-TreesitterContextLevel3*
`TreesitterContextLevel4` *hl-TreesitterContextLevel4*
`TreesitterContextLevel5` *hl-TreesitterContextLevel5*
`TreesitterContextLevel6` *hl-TreesitterContextLevel6*
`TreesitterContextLevel7` *hl-TreesitterContextLevel7*
`TreesitterContextLevel8` *hl-TreesitterContextLevel8*
(default: links to `TreesitterContext`)

Depth-specific colors for nested context rows.
Depths greater than 8 fall back to `TreesitterContext`.

`TreesitterContextLineNumber` *hl-TreesitterContextLineNumber*
(default: links to `LineNr`)

Expand Down
32 changes: 32 additions & 0 deletions lua/treesitter-context/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local util = require('treesitter-context.util')
local config = require('treesitter-context.config')

local ns = api.nvim_create_namespace('nvim-treesitter-context')
local MAX_DEPTH_HIGHLIGHT_GROUPS = 8

--- List of free buffers that can be reused.
--- @type integer[]
Expand Down Expand Up @@ -211,6 +212,36 @@ local function highlight_contexts(bufnr, ctx_bufnr, contexts)
end)
end

--- @param depth integer
--- @return string
local function get_depth_highlight_group(depth)
if depth <= MAX_DEPTH_HIGHLIGHT_GROUPS then
local level_hl = string.format('TreesitterContextLevel%d', depth)
if fn.hlexists(level_hl) == 1 then
return level_hl
end
end
return 'TreesitterContext'
end

--- @param bufnr integer
--- @param contexts Range4[]
local function highlight_depth_backgrounds(bufnr, contexts)
local offset = 0
for depth, context in ipairs(contexts) do
local hl_group = get_depth_highlight_group(depth)
local height = util.get_range_height(context)
for row = 0, height - 1 do
add_extmark(bufnr, offset + row, 0, {
end_row = offset + row + 1,
hl_group = hl_group,
hl_eol = true,
})
end
offset = offset + height
end
end

--- @class StatusLineHighlight
--- @field group string
--- @field groups? string[]
Expand Down Expand Up @@ -526,6 +557,7 @@ function M.open(winid, ctx_ranges, ctx_lines, force_hl_update)
highlight_contexts(bufnr, ctx_bufnr, ctx_ranges)
copy_extmarks(bufnr, ctx_bufnr, ctx_ranges)
highlight_bottom(ctx_bufnr, win_height - 1, 'TreesitterContextBottom')
highlight_depth_backgrounds(ctx_bufnr, ctx_ranges)
horizontal_scroll_contexts(winid, window_context.context_winid)
end
end
Expand Down
6 changes: 6 additions & 0 deletions plugin/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ end, {
})

vim.api.nvim_set_hl(0, 'TreesitterContext', { link = 'NormalFloat', default = true })
for i = 1, 8 do
vim.api.nvim_set_hl(0, string.format('TreesitterContextLevel%d', i), {
link = 'TreesitterContext',
default = true,
})
end
vim.api.nvim_set_hl(0, 'TreesitterContextLineNumber', { link = 'LineNr', default = true })
vim.api.nvim_set_hl(0, 'TreesitterContextBottom', { link = 'NONE', default = true })
vim.api.nvim_set_hl(
Expand Down
14 changes: 14 additions & 0 deletions test/ts_context_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local clear = helpers.clear
local exec_lua = helpers.exec_lua
local cmd = helpers.api.nvim_command
local feed = helpers.feed
local eq = helpers.eq

describe('ts_context', function()
local screen --- @type test.screen
Expand Down Expand Up @@ -105,6 +106,19 @@ describe('ts_context', function()
})
end)

it('defines per-depth context highlight groups', function()
local level_hls_exist = exec_lua([[
for i = 1, 8 do
if vim.fn.hlexists('TreesitterContextLevel' .. i) ~= 1 then
return false
end
end
return true
]])

eq(true, level_hls_exist)
end)

it('#627', function()
exec_lua(install_langs, { 'vimdoc' })
exec_lua(tc_helpers.setup, { multiwindow = true })
Expand Down
Loading