-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrender.lua
More file actions
258 lines (215 loc) · 6.22 KB
/
Copy pathrender.lua
File metadata and controls
258 lines (215 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
local M = {}
local ns = vim.api.nvim_create_namespace("jumpy")
local function build_virt_lines(added_lines)
local virt_lines = {}
for _, line in ipairs(added_lines) do
table.insert(virt_lines, { { line, "JumpyAdded" } })
end
return virt_lines
end
local function insertion_anchor(bufnr, old_start)
local line_count = vim.api.nvim_buf_line_count(bufnr)
local is_eof = old_start > line_count
local anchor_line = is_eof and line_count - 1 or old_start - 1
return math.max(0, anchor_line), not is_eof
end
local buf_states = {}
local function copy_lines(lines)
return vim.deepcopy(lines)
end
local function copy_hunk(hunk)
return {
removed_lines = copy_lines(hunk.removed_lines),
added_lines = copy_lines(hunk.added_lines),
old_start = hunk.old_start,
old_count = hunk.old_count,
new_start = hunk.new_start,
new_count = hunk.new_count,
}
end
function M.get_state(bufnr)
return buf_states[bufnr]
end
function M.get_all_states()
local states = {}
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(buf) then
local state = M.get_state(buf)
if state then
states[buf] = state
end
end
end
return states
end
function M.snapshot(bufnr)
local state = buf_states[bufnr]
if not state then
return nil
end
local hunks = {}
for idx, hunk in pairs(state.hunks) do
if hunk then
hunks[idx] = copy_hunk(hunk)
end
end
return {
hunks = hunks,
original_lines = copy_lines(state.original_lines),
proposed_lines = copy_lines(state.proposed_lines),
}
end
function M.show(bufnr, hunks, original_lines, proposed_lines)
M.clear(bufnr)
local state = {
hunks = {},
original_lines = original_lines,
proposed_lines = proposed_lines,
extmark_ids = {},
}
local entries = {}
for idx, hunk in pairs(hunks) do
table.insert(entries, { idx = idx, hunk = hunk })
end
table.sort(entries, function(a, b)
return a.idx < b.idx
end)
for _, entry in ipairs(entries) do
local i, hunk = entry.idx, entry.hunk
local display_hunk = {
removed_lines = hunk.removed_lines,
added_lines = hunk.added_lines,
old_start = hunk.old_start,
old_count = hunk.old_count,
new_start = hunk.new_start,
new_count = hunk.new_count,
buf_line = hunk.old_start - 1,
extmarks = {},
}
for j = 0, hunk.old_count - 1 do
local line = hunk.old_start - 1 + j
if line < vim.api.nvim_buf_line_count(bufnr) then
local id = vim.api.nvim_buf_set_extmark(bufnr, ns, line, 0, {
line_hl_group = "JumpyRemoved",
sign_text = "-",
sign_hl_group = "JumpyRemovedSign",
priority = 100,
})
table.insert(display_hunk.extmarks, id)
end
end
if #hunk.added_lines > 0 then
local virt_lines = build_virt_lines(hunk.added_lines)
local anchor_line = math.min(hunk.old_start - 1 + hunk.old_count - 1, vim.api.nvim_buf_line_count(bufnr) - 1)
anchor_line = math.max(0, anchor_line)
local id = vim.api.nvim_buf_set_extmark(bufnr, ns, anchor_line, 0, {
virt_lines = virt_lines,
virt_lines_above = false,
priority = 100,
})
table.insert(display_hunk.extmarks, id)
end
if hunk.old_count == 0 and #hunk.added_lines > 0 then
for _, eid in ipairs(display_hunk.extmarks) do
vim.api.nvim_buf_del_extmark(bufnr, ns, eid)
end
display_hunk.extmarks = {}
local virt_lines = build_virt_lines(hunk.added_lines)
local anchor_line, virt_lines_above = insertion_anchor(bufnr, hunk.old_start)
local id = vim.api.nvim_buf_set_extmark(bufnr, ns, anchor_line, 0, {
virt_lines = virt_lines,
virt_lines_above = virt_lines_above,
sign_text = "+",
sign_hl_group = "JumpyAddedSign",
priority = 100,
})
table.insert(display_hunk.extmarks, id)
end
state.hunks[i] = display_hunk
end
buf_states[bufnr] = state
end
function M.restore(bufnr, snapshot)
M.clear(bufnr)
if not snapshot then
return
end
M.show(bufnr, snapshot.hunks, snapshot.original_lines, snapshot.proposed_lines)
end
function M.clear(bufnr)
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
buf_states[bufnr] = nil
end
function M.clear_hunk(bufnr, hunk_idx)
local state = buf_states[bufnr]
if not state then
return
end
local hunk = state.hunks[hunk_idx]
if not hunk then
return
end
for _, eid in ipairs(hunk.extmarks) do
vim.api.nvim_buf_del_extmark(bufnr, ns, eid)
end
state.hunks[hunk_idx] = nil
local any_remaining = false
for _, h in pairs(state.hunks) do
if h then
any_remaining = true
break
end
end
if not any_remaining then
buf_states[bufnr] = nil
vim.notify("jumpy: all hunks resolved", vim.log.levels.INFO)
end
end
function M.update_hunk_lines(bufnr, hunk_idx, new_added_lines)
local state = buf_states[bufnr]
if not state then
return
end
local hunk = state.hunks[hunk_idx]
if not hunk then
return
end
for _, eid in ipairs(hunk.extmarks) do
vim.api.nvim_buf_del_extmark(bufnr, ns, eid)
end
hunk.extmarks = {}
hunk.added_lines = new_added_lines
for j = 0, hunk.old_count - 1 do
local line = hunk.old_start - 1 + j
if line < vim.api.nvim_buf_line_count(bufnr) then
local id = vim.api.nvim_buf_set_extmark(bufnr, ns, line, 0, {
line_hl_group = "JumpyRemoved",
sign_text = "-",
sign_hl_group = "JumpyRemovedSign",
priority = 100,
})
table.insert(hunk.extmarks, id)
end
end
if #new_added_lines > 0 then
local virt_lines = build_virt_lines(new_added_lines)
local anchor_line
local virt_lines_above = false
if hunk.old_count > 0 then
anchor_line = math.min(hunk.old_start - 1 + hunk.old_count - 1, vim.api.nvim_buf_line_count(bufnr) - 1)
anchor_line = math.max(0, anchor_line)
else
anchor_line, virt_lines_above = insertion_anchor(bufnr, hunk.old_start)
end
local id = vim.api.nvim_buf_set_extmark(bufnr, ns, anchor_line, 0, {
virt_lines = virt_lines,
virt_lines_above = virt_lines_above,
priority = 100,
})
table.insert(hunk.extmarks, id)
end
end
function M.get_namespace()
return ns
end
return M