-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathinit.lua
362 lines (324 loc) · 9.18 KB
/
init.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
local Config = require("lazy.core.config")
local Diff = require("lazy.view.diff")
local Float = require("lazy.view.float")
local Git = require("lazy.manage.git")
local Render = require("lazy.view.render")
local Util = require("lazy.util")
local ViewConfig = require("lazy.view.config")
---@class LazyViewState
---@field mode string
---@field plugin? {name:string, kind?: LazyPluginKind}
local default_state = {
mode = "home",
profile = {
threshold = 0,
sort_time_taken = false,
},
}
---@class LazyView: LazyFloat
---@field render LazyRender
---@field state LazyViewState
local M = {}
---@type LazyView
M.view = nil
function M.visible()
return M.view and M.view.win and vim.api.nvim_win_is_valid(M.view.win)
end
---@param mode? string
function M.show(mode)
if Config.headless() then
return
end
M.view = M.visible() and M.view or M.create()
if mode then
M.view.state.mode = mode
end
M.view:update()
end
---@param plugin LazyPlugin
function M:is_selected(plugin)
return vim.deep_equal(self.state.plugin, { name = plugin.name, kind = plugin._.kind })
end
function M.create()
local self = setmetatable({}, { __index = setmetatable(M, { __index = Float }) })
---@cast self LazyView
Float.init(self, {
title = Config.options.ui.title,
title_pos = Config.options.ui.title_pos,
noautocmd = false,
})
if Config.options.ui.wrap then
Util.wo(self.win, "wrap", true)
Util.wo(self.win, "linebreak", true)
Util.wo(self.win, "breakindent", true)
else
Util.wo(self.win, "wrap", false)
end
self.state = vim.deepcopy(default_state)
self.render = Render.new(self)
local update = self.update
self.update = Util.throttle(Config.options.ui.throttle, function()
update(self)
end)
for _, pattern in ipairs({ "LazyRender", "LazyFloatResized" }) do
self:on({ "User" }, function()
if not (self.buf and vim.api.nvim_buf_is_valid(self.buf)) then
return true
end
self:update()
end, { pattern = pattern })
end
vim.keymap.set("n", ViewConfig.keys.abort, function()
require("lazy.manage.process").abort()
require("lazy.async").abort()
return ViewConfig.keys.abort
end, { silent = true, buffer = self.buf, expr = true })
vim.keymap.set("n", "gx", "K", { buffer = self.buf, remap = true })
-- plugin details
self:on_key(ViewConfig.keys.details, function()
local plugin = self.render:get_plugin()
if plugin then
local selected = {
name = plugin.name,
kind = plugin._.kind,
}
local open = not vim.deep_equal(self.state.plugin, selected)
if not open then
local row = self.render:get_row(selected)
if row then
vim.api.nvim_win_set_cursor(self.view.win, { row, 8 })
end
end
self.state.plugin = open and selected or nil
self:update()
end
end)
self:on_key(ViewConfig.keys.next, function()
local cursor = vim.api.nvim_win_get_cursor(self.view.win)
for l = 1, #self.render.locations, 1 do
local loc = self.render.locations[l]
if loc.from > cursor[1] then
vim.api.nvim_win_set_cursor(self.view.win, { loc.from, 8 })
return
end
end
end)
self:on_key(ViewConfig.keys.prev, function()
local cursor = vim.api.nvim_win_get_cursor(self.view.win)
for l = #self.render.locations, 1, -1 do
local loc = self.render.locations[l]
if loc.from < cursor[1] then
vim.api.nvim_win_set_cursor(self.view.win, { loc.from, 8 })
return
end
end
end)
self:on_key(ViewConfig.keys.profile_sort, function()
if self.state.mode == "profile" then
self.state.profile.sort_time_taken = not self.state.profile.sort_time_taken
self:update()
end
end)
self:on_key(ViewConfig.keys.profile_filter, function()
if self.state.mode == "profile" then
vim.ui.input({
prompt = "Enter time threshold in ms: ",
default = tostring(self.state.profile.threshold),
}, function(input)
if not input then
return
end
local num = input == "" and 0 or tonumber(input)
if not num then
Util.error("Please input a number")
else
self.state.profile.threshold = num
self:update()
end
end)
end
end)
for lhs, rhs in pairs(Config.options.ui.custom_keys) do
if rhs then
local handler = type(rhs) == "table" and rhs[1] or rhs
self:on_key(lhs, function()
local plugin = self.render:get_plugin()
if plugin then
handler(plugin)
end
end)
end
end
self:setup_patterns()
self:setup_modes()
return self
end
function M:update()
if self.buf and vim.api.nvim_buf_is_valid(self.buf) then
self.render:update()
vim.cmd.redraw()
end
end
function M:open_url(path)
local plugin = self.render:get_plugin()
if plugin then
if plugin.url then
local url = plugin.url:gsub("%.git$", "")
Util.open(url .. path)
else
Util.error("No url for " .. plugin.name)
end
end
end
function M:setup_patterns()
local commit_pattern = "%f[%w](" .. string.rep("[a-f0-9]", 7) .. ")%f[%W]"
self:on_pattern(ViewConfig.keys.hover, {
[commit_pattern] = function(hash)
self:diff({ commit = hash, browser = true })
end,
["#(%d+)"] = function(issue)
self:open_url("/issues/" .. issue)
end,
["README.md"] = function()
local plugin = self.render:get_plugin()
if plugin then
Util.open(plugin.dir .. "/README.md")
end
end,
["|(%S-)|"] = function(h)
vim.cmd.help(h)
self:close()
end,
["(https?://%S+)"] = function(url)
Util.open(url)
end,
}, self.hover)
self:on_pattern(ViewConfig.keys.diff, {
[commit_pattern] = function(hash)
self:diff({ commit = hash })
end,
}, self.diff)
self:on_pattern(ViewConfig.commands.restore.key_plugin, {
[commit_pattern] = function(hash)
self:restore({ commit = hash })
end,
}, self.restore)
end
---@param opts? {commit:string}
function M:restore(opts)
opts = opts or {}
local Lockfile = require("lazy.manage.lock")
local Commands = require("lazy.view.commands")
local plugin = self.render:get_plugin()
if plugin then
if opts.commit then
Lockfile.get(plugin).commit = opts.commit
end
Commands.cmd("restore", { plugins = { plugin } })
end
end
function M:hover()
if self:diff({ browser = true, hover = true }) then
return
end
self:open_url("")
end
---@param opts? {commit?:string, browser:boolean, hover:boolean}
function M:diff(opts)
opts = opts or {}
local plugin = self.render:get_plugin()
if plugin then
local diff
if opts.commit then
diff = { commit = opts.commit }
elseif plugin._.updated then
diff = vim.deepcopy(plugin._.updated)
else
local info = assert(Git.info(plugin.dir))
local target = assert(Git.get_target(plugin))
diff = { from = info.commit, to = target.commit }
if opts.hover and diff.from == diff.to then
return
end
end
if not diff then
return
end
for k, v in pairs(diff) do
diff[k] = v:sub(1, 7)
end
if opts.browser then
Diff.handlers.browser(plugin, diff)
else
Diff.handlers[Config.options.diff.cmd](plugin, diff)
end
return true
end
end
--- will create a key mapping that can be used on certain patterns
---@param key string
---@param patterns table<string, fun(str:string)>
---@param fallback? fun(self)
function M:on_pattern(key, patterns, fallback)
self:on_key(key, function()
local line = vim.api.nvim_get_current_line()
local pos = vim.api.nvim_win_get_cursor(0)
local col = pos[2] + 1
for pattern, handler in pairs(patterns) do
local from = 1
local to, url
while from do
from, to, url = line:find(pattern, from)
if from and col >= from and col <= to then
return handler(url)
end
if from then
from = to + 1
end
end
end
if fallback then
fallback(self)
end
end)
end
function M:setup_modes()
local Commands = require("lazy.view.commands")
for name, m in pairs(ViewConfig.commands) do
if m.key then
self:on_key(m.key, function()
if self.state.mode == name and m.toggle then
self.state.mode = "home"
return self:update()
end
Commands.cmd(name)
end, m.desc)
end
if m.key_plugin and name ~= "restore" then
self:on_key(m.key_plugin, function()
local esc = vim.api.nvim_replace_termcodes("<esc>", true, true, true)
vim.api.nvim_feedkeys(esc, "n", false)
local plugins = {}
if vim.api.nvim_get_mode().mode:lower() == "v" then
local f, t = vim.fn.line("."), vim.fn.line("v")
if f > t then
f, t = t, f
end
for i = f, t do
local plugin = self.render:get_plugin(i)
if plugin then
plugins[plugin.name] = plugin
end
end
plugins = vim.tbl_values(plugins)
else
plugins[1] = self.render:get_plugin()
end
if #plugins > 0 then
Commands.cmd(name, { plugins = plugins })
end
end, m.desc_plugin, { "n", "x" })
end
end
end
return M