feat: use conceal overlay

This commit is contained in:
Barrett Ruth 2026-02-02 13:22:22 -05:00
parent 5db0e969bf
commit 3aa7d1d6f8
5 changed files with 31 additions and 30 deletions

View file

@ -65,12 +65,10 @@ CONFIGURATION *fugitive-ts-config*
Skip treesitter highlighting for hunks larger than
this many lines. Prevents lag on massive diffs.
{conceal_prefixes} (boolean, default: true)
Hide diff prefixes (`+`/`-`/` `) using virtual
text overlay. Makes code appear without the
leading diff character. When `highlights.background`
is also enabled, the overlay inherits the line's
background color.
{hide_prefix} (boolean, default: true)
Hide diff prefixes (`+`/`-`/` `) using conceal.
Makes code appear flush left without the leading
diff character. Sets `conceallevel=2` on attach.
{highlights} (table, default: see below)
Controls which highlight features are enabled.
@ -133,7 +131,7 @@ IMPLEMENTATION *fugitive-ts-implementation*
- Background extmarks (`FugitiveTsAdd`/`FugitiveTsDelete`) at priority 198
- `Normal` extmarks at priority 199 clear underlying diff foreground
- Treesitter highlights are applied as extmarks at priority 200
- Virtual text overlays hide diff prefixes when `conceal_prefixes` is enabled
- Conceal extmarks hide diff prefixes when `hide_prefix` is enabled
4. Re-highlighting occurs on `TextChanged` (debounced) and `Syntax` events
==============================================================================

View file

@ -66,7 +66,7 @@ end
---@class fugitive-ts.HunkOpts
---@field max_lines integer
---@field conceal_prefixes boolean
---@field hide_prefix boolean
---@field highlights fugitive-ts.Highlights
---@param bufnr integer
@ -142,11 +142,10 @@ function M.highlight_hunk(bufnr, ns, hunk, opts)
local line_hl = is_diff_line and (prefix == '+' and 'FugitiveTsAdd' or 'FugitiveTsDelete')
or nil
if opts.conceal_prefixes then
local virt_hl = (opts.highlights.background and line_hl) or nil
if opts.hide_prefix then
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
virt_text = { { ' ', virt_hl } },
virt_text_pos = 'overlay',
end_col = 1,
conceal = '',
})
end

View file

@ -11,7 +11,7 @@
---@field disabled_languages string[]
---@field debounce_ms integer
---@field max_lines_per_hunk integer
---@field conceal_prefixes boolean
---@field hide_prefix boolean
---@field highlights fugitive-ts.Highlights
---@class fugitive-ts
@ -33,7 +33,7 @@ local default_config = {
disabled_languages = {},
debounce_ms = 50,
max_lines_per_hunk = 500,
conceal_prefixes = true,
hide_prefix = true,
highlights = {
treesitter = true,
background = true,
@ -75,7 +75,7 @@ local function highlight_buffer(bufnr)
for _, hunk in ipairs(hunks) do
highlight.highlight_hunk(bufnr, ns, hunk, {
max_lines = config.max_lines_per_hunk,
conceal_prefixes = config.conceal_prefixes,
hide_prefix = config.hide_prefix,
highlights = config.highlights,
})
end
@ -122,6 +122,10 @@ function M.attach(bufnr)
dbg('attaching to buffer %d', bufnr)
if config.hide_prefix then
vim.api.nvim_set_option_value('conceallevel', 2, { win = 0 })
end
local debounced = create_debounced_highlight(bufnr)
highlight_buffer(bufnr)
@ -164,7 +168,7 @@ function M.setup(opts)
disabled_languages = { opts.disabled_languages, 'table', true },
debounce_ms = { opts.debounce_ms, 'number', true },
max_lines_per_hunk = { opts.max_lines_per_hunk, 'number', true },
conceal_prefixes = { opts.conceal_prefixes, 'boolean', true },
hide_prefix = { opts.hide_prefix, 'boolean', true },
highlights = { opts.highlights, 'table', true },
})

View file

@ -32,7 +32,7 @@ describe('highlight', function()
local function default_opts(overrides)
local opts = {
max_lines = 500,
conceal_prefixes = false,
hide_prefix = false,
highlights = {
treesitter = true,
background = false,
@ -241,7 +241,7 @@ describe('highlight', function()
delete_buffer(bufnr)
end)
it('applies overlay extmarks when conceal_prefixes enabled', function()
it('applies conceal extmarks when hide_prefix enabled', function()
local bufnr = create_buffer({
'@@ -1,1 +1,2 @@',
' local x = 1',
@ -255,20 +255,20 @@ describe('highlight', function()
lines = { ' local x = 1', '+local y = 2' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts({ conceal_prefixes = true }))
highlight.highlight_hunk(bufnr, ns, hunk, default_opts({ hide_prefix = true }))
local extmarks = get_extmarks(bufnr)
local overlay_count = 0
local conceal_count = 0
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].virt_text_pos == 'overlay' then
overlay_count = overlay_count + 1
if mark[4] and mark[4].conceal == '' then
conceal_count = conceal_count + 1
end
end
assert.are.equal(2, overlay_count)
assert.are.equal(2, conceal_count)
delete_buffer(bufnr)
end)
it('does not apply overlay extmarks when conceal_prefixes disabled', function()
it('does not apply conceal extmarks when hide_prefix disabled', function()
local bufnr = create_buffer({
'@@ -1,1 +1,2 @@',
' local x = 1',
@ -282,16 +282,16 @@ describe('highlight', function()
lines = { ' local x = 1', '+local y = 2' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts({ conceal_prefixes = false }))
highlight.highlight_hunk(bufnr, ns, hunk, default_opts({ hide_prefix = false }))
local extmarks = get_extmarks(bufnr)
local overlay_count = 0
local conceal_count = 0
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].virt_text_pos == 'overlay' then
overlay_count = overlay_count + 1
if mark[4] and mark[4].conceal == '' then
conceal_count = conceal_count + 1
end
end
assert.are.equal(0, overlay_count)
assert.are.equal(0, conceal_count)
delete_buffer(bufnr)
end)

View file

@ -24,7 +24,7 @@ describe('fugitive-ts', function()
disabled_languages = { 'markdown' },
debounce_ms = 100,
max_lines_per_hunk = 1000,
conceal_prefixes = false,
hide_prefix = false,
highlights = {
treesitter = true,
background = true,