diff --git a/README.md b/README.md index 301146b0..29e5b56b 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ Note: support for specific languages is strictly community maintained and can br - [ ] `blueprint` - [ ] `bp` - [ ] `brightscript` + - [ ] `c3` - [ ] `caddy` - [ ] `cairo` - [ ] `chatito` @@ -190,6 +191,7 @@ Note: support for specific languages is strictly community maintained and can br - [ ] `gowork` - [ ] `gpg` - [ ] `gren` + - [ ] `groq` - [ ] `gstlaunch` - [ ] `hack` - [ ] `hare` @@ -222,6 +224,7 @@ Note: support for specific languages is strictly community maintained and can br - [ ] `just` - [ ] `kcl` - [ ] `kconfig` + - [ ] `kitty` - [ ] `koto` - [ ] `kusto` - [ ] `lalrpop` @@ -254,6 +257,7 @@ Note: support for specific languages is strictly community maintained and can br - [ ] `perl` - [ ] `phpdoc` - [ ] `pioasm` + - [ ] `pkl` - [ ] `po` - [ ] `pod` - [ ] `poe_filter` @@ -285,6 +289,7 @@ Note: support for specific languages is strictly community maintained and can br - [ ] `rego` - [ ] `requirements` - [ ] `rescript` + - [ ] `rifleconf` - [ ] `rnoweb` - [ ] `robot` - [ ] `robots` @@ -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` @@ -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` @@ -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`. diff --git a/doc/nvim-treesitter-context.txt b/doc/nvim-treesitter-context.txt index e0b584df..c395eb63 100644 --- a/doc/nvim-treesitter-context.txt +++ b/doc/nvim-treesitter-context.txt @@ -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`) diff --git a/lua/treesitter-context/render.lua b/lua/treesitter-context/render.lua index 817647b2..4db20d1d 100644 --- a/lua/treesitter-context/render.lua +++ b/lua/treesitter-context/render.lua @@ -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[] @@ -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[] @@ -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 diff --git a/plugin/treesitter-context.lua b/plugin/treesitter-context.lua index 8cd4b2e1..22ee1ab9 100644 --- a/plugin/treesitter-context.lua +++ b/plugin/treesitter-context.lua @@ -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( diff --git a/test/ts_context_spec.lua b/test/ts_context_spec.lua index 06cbd5b3..a7d49fbe 100644 --- a/test/ts_context_spec.lua +++ b/test/ts_context_spec.lua @@ -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 @@ -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 })