feat: use conceal overlay
This commit is contained in:
parent
5db0e969bf
commit
3aa7d1d6f8
5 changed files with 31 additions and 30 deletions
|
|
@ -65,12 +65,10 @@ CONFIGURATION *fugitive-ts-config*
|
||||||
Skip treesitter highlighting for hunks larger than
|
Skip treesitter highlighting for hunks larger than
|
||||||
this many lines. Prevents lag on massive diffs.
|
this many lines. Prevents lag on massive diffs.
|
||||||
|
|
||||||
{conceal_prefixes} (boolean, default: true)
|
{hide_prefix} (boolean, default: true)
|
||||||
Hide diff prefixes (`+`/`-`/` `) using virtual
|
Hide diff prefixes (`+`/`-`/` `) using conceal.
|
||||||
text overlay. Makes code appear without the
|
Makes code appear flush left without the leading
|
||||||
leading diff character. When `highlights.background`
|
diff character. Sets `conceallevel=2` on attach.
|
||||||
is also enabled, the overlay inherits the line's
|
|
||||||
background color.
|
|
||||||
|
|
||||||
{highlights} (table, default: see below)
|
{highlights} (table, default: see below)
|
||||||
Controls which highlight features are enabled.
|
Controls which highlight features are enabled.
|
||||||
|
|
@ -133,7 +131,7 @@ IMPLEMENTATION *fugitive-ts-implementation*
|
||||||
- Background extmarks (`FugitiveTsAdd`/`FugitiveTsDelete`) at priority 198
|
- Background extmarks (`FugitiveTsAdd`/`FugitiveTsDelete`) at priority 198
|
||||||
- `Normal` extmarks at priority 199 clear underlying diff foreground
|
- `Normal` extmarks at priority 199 clear underlying diff foreground
|
||||||
- Treesitter highlights are applied as extmarks at priority 200
|
- 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
|
4. Re-highlighting occurs on `TextChanged` (debounced) and `Syntax` events
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ end
|
||||||
|
|
||||||
---@class fugitive-ts.HunkOpts
|
---@class fugitive-ts.HunkOpts
|
||||||
---@field max_lines integer
|
---@field max_lines integer
|
||||||
---@field conceal_prefixes boolean
|
---@field hide_prefix boolean
|
||||||
---@field highlights fugitive-ts.Highlights
|
---@field highlights fugitive-ts.Highlights
|
||||||
|
|
||||||
---@param bufnr integer
|
---@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')
|
local line_hl = is_diff_line and (prefix == '+' and 'FugitiveTsAdd' or 'FugitiveTsDelete')
|
||||||
or nil
|
or nil
|
||||||
|
|
||||||
if opts.conceal_prefixes then
|
if opts.hide_prefix then
|
||||||
local virt_hl = (opts.highlights.background and line_hl) or nil
|
|
||||||
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
pcall(vim.api.nvim_buf_set_extmark, bufnr, ns, buf_line, 0, {
|
||||||
virt_text = { { ' ', virt_hl } },
|
end_col = 1,
|
||||||
virt_text_pos = 'overlay',
|
conceal = '',
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
---@field disabled_languages string[]
|
---@field disabled_languages string[]
|
||||||
---@field debounce_ms integer
|
---@field debounce_ms integer
|
||||||
---@field max_lines_per_hunk integer
|
---@field max_lines_per_hunk integer
|
||||||
---@field conceal_prefixes boolean
|
---@field hide_prefix boolean
|
||||||
---@field highlights fugitive-ts.Highlights
|
---@field highlights fugitive-ts.Highlights
|
||||||
|
|
||||||
---@class fugitive-ts
|
---@class fugitive-ts
|
||||||
|
|
@ -33,7 +33,7 @@ local default_config = {
|
||||||
disabled_languages = {},
|
disabled_languages = {},
|
||||||
debounce_ms = 50,
|
debounce_ms = 50,
|
||||||
max_lines_per_hunk = 500,
|
max_lines_per_hunk = 500,
|
||||||
conceal_prefixes = true,
|
hide_prefix = true,
|
||||||
highlights = {
|
highlights = {
|
||||||
treesitter = true,
|
treesitter = true,
|
||||||
background = true,
|
background = true,
|
||||||
|
|
@ -75,7 +75,7 @@ local function highlight_buffer(bufnr)
|
||||||
for _, hunk in ipairs(hunks) do
|
for _, hunk in ipairs(hunks) do
|
||||||
highlight.highlight_hunk(bufnr, ns, hunk, {
|
highlight.highlight_hunk(bufnr, ns, hunk, {
|
||||||
max_lines = config.max_lines_per_hunk,
|
max_lines = config.max_lines_per_hunk,
|
||||||
conceal_prefixes = config.conceal_prefixes,
|
hide_prefix = config.hide_prefix,
|
||||||
highlights = config.highlights,
|
highlights = config.highlights,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
@ -122,6 +122,10 @@ function M.attach(bufnr)
|
||||||
|
|
||||||
dbg('attaching to buffer %d', 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)
|
local debounced = create_debounced_highlight(bufnr)
|
||||||
|
|
||||||
highlight_buffer(bufnr)
|
highlight_buffer(bufnr)
|
||||||
|
|
@ -164,7 +168,7 @@ function M.setup(opts)
|
||||||
disabled_languages = { opts.disabled_languages, 'table', true },
|
disabled_languages = { opts.disabled_languages, 'table', true },
|
||||||
debounce_ms = { opts.debounce_ms, 'number', true },
|
debounce_ms = { opts.debounce_ms, 'number', true },
|
||||||
max_lines_per_hunk = { opts.max_lines_per_hunk, '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 },
|
highlights = { opts.highlights, 'table', true },
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ describe('highlight', function()
|
||||||
local function default_opts(overrides)
|
local function default_opts(overrides)
|
||||||
local opts = {
|
local opts = {
|
||||||
max_lines = 500,
|
max_lines = 500,
|
||||||
conceal_prefixes = false,
|
hide_prefix = false,
|
||||||
highlights = {
|
highlights = {
|
||||||
treesitter = true,
|
treesitter = true,
|
||||||
background = false,
|
background = false,
|
||||||
|
|
@ -241,7 +241,7 @@ describe('highlight', function()
|
||||||
delete_buffer(bufnr)
|
delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('applies overlay extmarks when conceal_prefixes enabled', function()
|
it('applies conceal extmarks when hide_prefix enabled', function()
|
||||||
local bufnr = create_buffer({
|
local bufnr = create_buffer({
|
||||||
'@@ -1,1 +1,2 @@',
|
'@@ -1,1 +1,2 @@',
|
||||||
' local x = 1',
|
' local x = 1',
|
||||||
|
|
@ -255,20 +255,20 @@ describe('highlight', function()
|
||||||
lines = { ' local x = 1', '+local y = 2' },
|
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 extmarks = get_extmarks(bufnr)
|
||||||
local overlay_count = 0
|
local conceal_count = 0
|
||||||
for _, mark in ipairs(extmarks) do
|
for _, mark in ipairs(extmarks) do
|
||||||
if mark[4] and mark[4].virt_text_pos == 'overlay' then
|
if mark[4] and mark[4].conceal == '' then
|
||||||
overlay_count = overlay_count + 1
|
conceal_count = conceal_count + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
assert.are.equal(2, overlay_count)
|
assert.are.equal(2, conceal_count)
|
||||||
delete_buffer(bufnr)
|
delete_buffer(bufnr)
|
||||||
end)
|
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({
|
local bufnr = create_buffer({
|
||||||
'@@ -1,1 +1,2 @@',
|
'@@ -1,1 +1,2 @@',
|
||||||
' local x = 1',
|
' local x = 1',
|
||||||
|
|
@ -282,16 +282,16 @@ describe('highlight', function()
|
||||||
lines = { ' local x = 1', '+local y = 2' },
|
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 extmarks = get_extmarks(bufnr)
|
||||||
local overlay_count = 0
|
local conceal_count = 0
|
||||||
for _, mark in ipairs(extmarks) do
|
for _, mark in ipairs(extmarks) do
|
||||||
if mark[4] and mark[4].virt_text_pos == 'overlay' then
|
if mark[4] and mark[4].conceal == '' then
|
||||||
overlay_count = overlay_count + 1
|
conceal_count = conceal_count + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
assert.are.equal(0, overlay_count)
|
assert.are.equal(0, conceal_count)
|
||||||
delete_buffer(bufnr)
|
delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ describe('fugitive-ts', function()
|
||||||
disabled_languages = { 'markdown' },
|
disabled_languages = { 'markdown' },
|
||||||
debounce_ms = 100,
|
debounce_ms = 100,
|
||||||
max_lines_per_hunk = 1000,
|
max_lines_per_hunk = 1000,
|
||||||
conceal_prefixes = false,
|
hide_prefix = false,
|
||||||
highlights = {
|
highlights = {
|
||||||
treesitter = true,
|
treesitter = true,
|
||||||
background = true,
|
background = true,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue