-
-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathwindow.lua
293 lines (240 loc) · 7.52 KB
/
window.lua
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
local lazy = require("diffview.lazy")
local oop = require("diffview.oop")
local Diff1 = lazy.access("diffview.scene.layouts.diff_1", "Diff1") ---@type Diff1|LazyModule
local File = lazy.access("diffview.vcs.file", "File") ---@type vcs.File|LazyModule
local FileHistoryView = lazy.access("diffview.scene.views.file_history.file_history_view", "FileHistoryView") ---@type FileHistoryView|LazyModule
local GitAdapter = lazy.access("diffview.vcs.adapters.git", "GitAdapter") ---@type GitAdapter|LazyModule
local RevType = lazy.access("diffview.vcs.rev", "RevType") ---@type RevType|LazyModule
local config = lazy.require("diffview.config") ---@module "diffview.config"
local gs_actions = lazy.require("gitsigns.actions") ---@module "gitsigns.actions"
local lib = lazy.require("diffview.lib") ---@module "diffview.lib"
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"
local api = vim.api
local M = {}
---@class Window : diffview.Object
---@field id integer
---@field file vcs.File
---@field parent Layout
local Window = oop.create_class("Window")
Window.winopt_store = {}
---@class Window.init.opt
---@field id integer
---@field file vcs.File
---@field parent Layout
---@param opt Window.init.opt
function Window:init(opt)
self.id = opt.id
self.file = opt.file
self.parent = opt.parent
end
function Window:destroy()
self:_restore_winopts()
self:close(true)
end
function Window:clone()
return Window({ file = self.file })
end
---@return boolean
function Window:is_valid()
return self.id and api.nvim_win_is_valid(self.id)
end
---@param force? boolean
function Window:close(force)
if self:is_valid() then
api.nvim_win_close(self.id, not not force)
self:set_id(nil)
end
end
function Window:focus()
if self:is_valid() then
api.nvim_set_current_win(self.id)
end
end
function Window:is_focused()
return self:is_valid() and api.nvim_get_current_win() == self.id
end
function Window:pre_open()
if vim.fn.has("nvim-0.8") == 1 then
vim.wo[self.id].winbar = nil
end
end
function Window:is_file_open()
return self:is_valid()
and self.file:is_valid()
and api.nvim_win_get_buf(self.id) == self.file.bufnr
end
---@param callback fun(file: vcs.File)
function Window:load_file(callback)
assert(self.file)
if self.file.bufnr and api.nvim_buf_is_valid(self.file.bufnr) then
return callback(self.file)
end
self.file:create_buffer(function()
callback(self.file)
end)
end
---@param callback? fun(file: vcs.File)
function Window:open_file(callback)
assert(self.file)
if self:is_valid() and self.file.active then
local function on_load()
local conf = config.get_config()
api.nvim_win_set_buf(self.id, self.file.bufnr)
if self.file.rev.type == RevType.LOCAL then
self:_save_winopts()
end
local winopt_overrides
local base_rev = utils.tbl_access(self, "parent.parent.revs.a") --[[@as Rev? ]]
local use_inline_diff = self.file.kind ~= "conflicting"
and self.parent:instanceof(Diff1.__get())
and self.file.adapter:instanceof(GitAdapter.__get())
if use_inline_diff then
winopt_overrides = { foldmethod = "manual", diff = false }
end
self:apply_file_winopts(winopt_overrides)
self.file:attach_buffer(false, {
keymaps = config.get_layout_keymaps(self.parent),
disable_diagnostics = self.file.kind == "conflicting"
and conf.view.merge_tool.disable_diagnostics,
inline_diff = {
enabled = use_inline_diff,
base = base_rev and base_rev:object_name(),
}
})
if self:show_winbar_info() then
vim.wo[self.id].winbar = self.file.winbar
end
api.nvim_win_call(self.id, function()
DiffviewGlobal.emitter:emit("diff_buf_win_enter", self.file.bufnr, self.id)
end)
if vim.is_callable(callback) then
---@cast callback -?
callback(self.file)
end
end
self:pre_open()
if self.file:is_valid() then
on_load()
else
self:load_file(on_load)
end
end
end
---@return boolean
function Window:show_winbar_info()
if self.file and self.file.winbar and vim.fn.has("nvim-0.8") == 1 then
local conf = config.get_config()
local view = lib.get_current_view()
if self.file.kind == "conflicting" then
return conf.view.merge_tool.winbar_info
else
if view and view:class() == FileHistoryView.__get() then
return conf.view.file_history.winbar_info
else
return conf.view.default.winbar_info
end
end
end
return false
end
function Window:open_null()
if self:is_valid() then
self:pre_open()
File.load_null_buffer(self.id)
end
end
function Window:detach_file()
if self.file and self.file:is_valid() then
self.file:detach_buffer()
end
end
---Check if the file buffer is in use in the current view's layout.
---@private
---@return boolean
function Window:_is_file_in_use()
local view = lib.get_current_view() --[[@as StandardView? ]]
if view and view.cur_layout ~= self.parent then
local main = view.cur_layout:get_main_win()
return main.file.bufnr and main.file.bufnr == self.file.bufnr
end
return false
end
function Window:_save_winopts()
if Window.winopt_store[self.file.bufnr] then return end
Window.winopt_store[self.file.bufnr] = {}
api.nvim_win_call(self.id, function()
for option, _ in pairs(self.file.winopts) do
Window.winopt_store[self.file.bufnr][option] = vim.o[option]
end
end)
end
function Window:_restore_winopts()
if
Window.winopt_store[self.file.bufnr]
and api.nvim_buf_is_loaded(self.file.bufnr)
and not self:_is_file_in_use()
then
utils.no_win_event_call(function()
local winid = utils.temp_win(self.file.bufnr)
utils.set_local(winid, Window.winopt_store[self.file.bufnr])
api.nvim_win_close(winid, true)
end)
end
end
---@param overrides WindowOptions?
function Window:apply_file_winopts(overrides)
assert(self.file)
if self.file.winopts then
if overrides then
utils.set_local(self.id, vim.tbl_extend("force", self.file.winopts, overrides))
else
utils.set_local(self.id, self.file.winopts)
end
end
end
function Window:apply_null_winopts()
if File.NULL_FILE.winopts then
utils.set_local(self.id, File.NULL_FILE.winopts)
end
end
function Window:set_id(id)
self.id = id
end
function Window:set_file(file)
self.file = file
end
function Window:gs_update_folds()
if self:is_file_open() and vim.wo[self.id].foldenable then
api.nvim_win_call(self.id, function()
pcall(vim.cmd, "norm! zE") -- Delete all folds in window
local hunks = gs_actions.get_hunks(self.file.bufnr) or {}
local context
for _, v in ipairs(vim.opt.diffopt:get()) do
context = tonumber(v:match("^context:(%d+)"))
if context then break end
end
context = math.max(1, context or 6)
local prev_last = -context + 1
local lcount = api.nvim_buf_line_count(self.file.bufnr)
for i = 1, #hunks + 1 do
local hunk = hunks[i]
local first, last
if hunk then
first = hunk.added.start
last = first + hunk.added.count - 1
else
first = lcount + context
last = first
end
-- print(prev_last, first, last, hunk and "hunk" or "nil")
if first - prev_last > context * 2 + 1 then
-- print("FOLD:", prev_last + context, first - context)
vim.cmd(("%d,%dfold"):format(prev_last + context, first - context))
end
prev_last = last
end
end)
end
end
M.Window = Window
return M