forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.lua
More file actions
64 lines (56 loc) · 1.77 KB
/
Copy pathdiff.lua
File metadata and controls
64 lines (56 loc) · 1.77 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
local Overlay = require('CopilotChat.ui.overlay')
local utils = require('CopilotChat.utils')
local class = utils.class
---@class CopilotChat.ui.Diff.Diff
---@field change string
---@field reference string
---@field filename string
---@field filetype string
---@field start_line number
---@field end_line number
---@field bufnr number?
---@class CopilotChat.ui.Diff : CopilotChat.ui.Overlay
---@field hl_ns number
---@field diff CopilotChat.ui.Diff.Diff?
local Diff = class(function(self, help, on_buf_create)
Overlay.init(self, 'copilot-diff', help, on_buf_create)
self.hl_ns = vim.api.nvim_create_namespace('copilot-chat-highlights')
vim.api.nvim_set_hl(self.hl_ns, '@diff.plus', { bg = utils.blend_color('DiffAdd', 20) })
vim.api.nvim_set_hl(self.hl_ns, '@diff.minus', { bg = utils.blend_color('DiffDelete', 20) })
vim.api.nvim_set_hl(self.hl_ns, '@diff.delta', { bg = utils.blend_color('DiffChange', 20) })
self.diff = nil
end, Overlay)
---@param diff CopilotChat.ui.Diff.Diff
---@param winnr number
function Diff:show(diff, winnr)
self.diff = diff
self:validate()
vim.api.nvim_win_set_hl_ns(winnr, self.hl_ns)
Overlay.show(
self,
tostring(vim.diff(diff.reference, diff.change, {
result_type = 'unified',
ignore_blank_lines = true,
ignore_whitespace = true,
ignore_whitespace_change = true,
ignore_whitespace_change_at_eol = true,
ignore_cr_at_eol = true,
algorithm = 'myers',
ctxlen = #diff.reference,
})),
winnr,
diff.filetype,
'diff'
)
end
---@param winnr number
---@param bufnr number
function Diff:restore(winnr, bufnr)
Overlay.restore(self, winnr, bufnr)
vim.api.nvim_win_set_hl_ns(winnr, 0)
end
---@return CopilotChat.ui.Diff.Diff?
function Diff:get_diff()
return self.diff
end
return Diff