Skip to content

Commit d363f6d

Browse files
committed
wip: use invalidate signs
wip range signs
1 parent fa052c2 commit d363f6d

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

lua/gitsigns/hunks.lua

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,83 @@ function M.calc_signs(hunk, next, min_lnum, max_lnum, untracked)
225225
return signs
226226
end
227227

228+
--- Calculate signs needed to be applied from a hunk for a specified line range.
229+
--- @param hunk Gitsigns.Hunk.Hunk
230+
--- @param untracked boolean
231+
--- @return Gitsigns.Sign[]
232+
function M.calc_sign_ranges(hunk, untracked)
233+
assert(
234+
not untracked or hunk.type == 'add',
235+
string.format('Invalid hunk with untracked=%s hunk="%s"', untracked, hunk.head)
236+
)
237+
238+
local start = hunk.added.start
239+
local added = hunk.added.count
240+
local removed = hunk.removed.count
241+
local end_lnum = change_end(hunk)
242+
243+
if hunk.type == 'delete' and start == 0 then
244+
-- topdelete signs get placed one row lower
245+
return { {
246+
type = 'topdelete',
247+
count = removed,
248+
lnum = 1
249+
} }
250+
elseif hunk.type == 'delete' then
251+
return { {
252+
type = 'delete',
253+
count = removed,
254+
lnum = start,
255+
end_lnum = start
256+
} }
257+
elseif untracked or hunk.type == 'add' then
258+
return { {
259+
type = untracked and 'untracked' or 'add',
260+
count = added,
261+
lnum = start,
262+
end_lnum = end_lnum
263+
} }
264+
end
265+
266+
--- @type Gitsigns.Sign[]
267+
local signs = {}
268+
269+
-- changedelete
270+
if removed > added then
271+
if end_lnum > start then
272+
signs[#signs + 1] = {
273+
type = 'change',
274+
lnum = start,
275+
end_lnum = end_lnum - 1
276+
}
277+
end
278+
279+
signs[#signs + 1] = {
280+
type = 'changedelete',
281+
count = removed,
282+
lnum = end_lnum
283+
}
284+
else -- change
285+
signs[#signs + 1] = {
286+
type = 'change',
287+
lnum = start,
288+
end_lnum = end_lnum
289+
}
290+
291+
-- Added lines of a 'change' hunk
292+
if added > removed then
293+
signs[#signs + 1] = {
294+
type = 'add',
295+
count = added - removed,
296+
lnum = end_lnum,
297+
end_lnum = hunk.vend,
298+
}
299+
end
300+
end
301+
302+
return signs
303+
end
304+
228305
--- @param relpath string
229306
--- @param hunks Gitsigns.Hunk.Hunk[]
230307
--- @param mode_bits string

lua/gitsigns/signs.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local dprint = require('gitsigns.debug.log').dprint
66
--- @field type Gitsigns.SignType
77
--- @field count? integer
88
--- @field lnum integer
9+
--- @field end_lnum? integer
910

1011
--- @class Gitsigns.Signs
1112
--- @field hls table<Gitsigns.SignType,Gitsigns.SignConfig>

lua/gitsigns/signs/extmarks.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ local M = {}
77

88
local group_base = 'gitsigns_extmark_signs_'
99

10+
local use_invalidate_signs = vim.fn.has('nvim-0.10') > 0
11+
1012
--- @param cfg Gitsigns.SignConfig
1113
--- @param hls table<Gitsigns.SignType,Gitsigns.SignConfig>
1214
--- @param name string
@@ -24,6 +26,9 @@ end
2426
--- @param last_orig? integer
2527
--- @param last_new? integer
2628
function M:on_lines(buf, _, last_orig, last_new)
29+
if use_invalidate_signs then
30+
return
31+
end
2732
-- Remove extmarks on line deletions to mimic
2833
-- the behaviour of vim signs.
2934
if last_orig > last_new then
@@ -65,11 +70,15 @@ function M:add(bufnr, signs)
6570

6671
local ok, err = pcall(api.nvim_buf_set_extmark, bufnr, self.ns, s.lnum - 1, -1, {
6772
id = s.lnum,
73+
end_row = s.end_lnum and s.end_lnum - 1 or nil,
6874
sign_text = config.signcolumn and text or '',
6975
priority = config.sign_priority,
7076
sign_hl_group = hls.hl,
7177
number_hl_group = config.numhl and hls.numhl or nil,
7278
line_hl_group = config.linehl and hls.linehl or nil,
79+
80+
undo_restore = not use_invalidate_signs,
81+
invalidate = use_invalidate_signs,
7382
})
7483

7584
if not ok and config.debug_mode then

0 commit comments

Comments
 (0)