-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.lua
799 lines (731 loc) · 22.1 KB
/
util.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
local uc = vim.api.nvim_create_user_command
function Reload(m)
vim.notify("attempting to reload " .. m, vim.log.levels.INFO)
require("plenary.reload").reload_module(m)
vim.notify("assuming reload was successful, you should now have the current version of " .. m, vim.log.levels.INFO)
end
function LazyHas(name)
return require("lazyvim.util").has(name)
-- require("lazyvim.util").has("easy-dotnet")
end
function LazyHasI(name)
vim.notify(vim.inspect(LazyHas(name)))
end
uc("Reload", function()
vim.cmd.w()
Reload(vim.api.nvim_buf_get_name(0))
end, {
desc = "Reload module",
})
-- local lazyvim = require("lazyvim")
local M = {}
function M.stringContains(str, searchedForString)
return string.match(str, searchedForString)
end
function M.getMasonBinCommandIfExists(name)
local result = ""
local masonBinPath = vim.fs.normalize(vim.fn.stdpath("data") .. "/mason/bin")
local binPath = masonBinPath .. "/" .. name
if vim.fn.executable(binPath) == 1 then
result = binPath
else
vim.notify(
"Unable to find "
.. name
.. " in "
.. masonBinPath
.. ".\nAre you sure you installed mason.nvim, that the path exists, and that the command is really named "
.. name
.. "?"
)
end
return result
end
---tries to get an environment variable's value, and if it's not found or empty returns an empty string
---@param name string
---@return string
function M.getEnvVariableOrEmptyString(name)
local var = os.getenv(name)
if var then
if var == "" then
return ""
end
return var
end
return ""
end
function M.tbl_flatten()
return vim.fn.has("nvim-0.10") == 1 and function(x)
return vim.iter(x):flatten(math.huge):totable()
end or vim.tbl_flatten
end
function M.hasEnvironmentVariableSet(name)
local EnvVar = M.getEnvVariableOrEmptyString(name)
if EnvVar then
if EnvVar == "" then
return false
end
return true
end
return false
end
---tries to get an environment variable's value, and if it's not found or empty returns the default value, or an empty string
---@param name string
---@param defaultValueIfEmpty string
---@return string
function M.getEnvVariableOrDefault(name, defaultValueIfEmpty)
defaultValueIfEmpty = defaultValueIfEmpty or ""
local var = M.getEnvVariableOrEmptyString(name)
if var == "" then
return defaultValueIfEmpty
end
return var
end
function M.hasReposEnvironmentVarSet()
return M.hasEnvironmentVariableSet("repos")
end
function M.getReposVariableIfSet()
return M.getEnvVariableOrEmptyString("repos")
end
-- Finds any files or directories given in {names} starting from {path}. If
-- {upward} is "true" then the search traverses upward through parent
-- directories; otherwise, the search traverses downward. Note that downward
-- searches are recursive and may search through many directories! If {stop}
-- is non-nil, then the search stops when the directory given in {stop} is
-- reached. The search terminates when {limit} (default 1) matches are found.
-- The search can be narrowed to find only files or only directories by
-- specifying {type} to be "file" or "directory", respectively.
--
-- Examples:
--
-- -- location of Cargo.toml from the current buffer's path
-- local cargo = vim.fs.find('Cargo.toml', {
-- upward = true,
-- stop = vim.loop.os_homedir(),
-- path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)),
-- })
--
-- -- list all test directories under the runtime directory
-- local test_dirs = vim.fs.find(
-- {'test', 'tst', 'testdir'},
-- {limit = math.huge, type = 'directory', path = './runtime/'}
-- )
--
-- -- get all files ending with .cpp or .hpp inside lib/
-- local cpp_hpp = vim.fs.find(function(name, path)
-- return name:match('.*%.[ch]pp$') and path:match('[/\\\\]lib$')
-- end, {limit = math.huge, type = 'file'})
function M.getRepoWithName(name)
if M.hasReposEnvironmentVarSet() then
return (
vim.fs.find(name, { upward = false, limit = 1, path = M.getReposVariableIfSet(), type = "directory" })[1] or ""
)
else
return ""
end
end
function M.hasRepoWithName(name)
if M.hasReposEnvironmentVarSet() then
local repoWithName = M.getRepoWithName(name)
if repoWithName == "" then
return false
else
return true
end
else
return false
end
end
---tries to get an environment variable's value, and if it's not found or empty returns the default value, or an empty string
---@param name string
---@param defaultValueIfEmpty string
---@return string
function M.getRepoWithNameOrDefault(name, defaultValueIfEmpty)
defaultValueIfEmpty = defaultValueIfEmpty or ""
local var = M.getRepoWithName(name)
if var == "" then
return defaultValueIfEmpty
end
return var
end
-- M.WatchEvent = vim.uv.new_fs_event()
local function on_change(err, fname, status)
-- Do work...
vim.api.nvim_command("checktime")
-- Debounce: stop/start.
M.WatchEvent:stop()
Watch_file(fname)
end
function Watch_file(fname)
local fullpath = vim.api.nvim_call_function("fnamemodify", { fname, ":p" })
M.WatchEvent:start(
fullpath,
{},
vim.schedule_wrap(function(...)
on_change(...)
end)
)
end
vim.api.nvim_command("command! -nargs=1 Watch call luaeval('Watch_file(_A)', expand('<args>'))")
vim.api.nvim_create_user_command("WatchCurrentFile", function()
-- local success, fwatch = pcall(require, "fwatch")
-- if success then
local path = vim.fn.fnamemodify("%", ":p")
Watch_file(path)
-- fwatch.watch(path, {
-- on_event = function()
-- -- reload colorscheme whenever path changes
-- vim.
-- end,
-- })
-- end
end, { desc = "watches current file " })
function M.file_exists(file)
return vim.loop.fs_stat(file) ~= nil
end
---@param opts? LazyFloatOptions
function M.float(opts)
return require("lazy.view.float")(opts)
end
---@param msg string|string[]
---@param opts? table
function M.markdown(msg, opts)
if type(msg) == "table" then
msg = table.concat(msg, "\n") or msg
end
vim.notify(
msg,
vim.log.levels.INFO,
vim.tbl_deep_extend("force", {
title = "lazy.nvim",
on_open = function(win)
vim.wo[win].conceallevel = 3
vim.wo[win].concealcursor = "n"
vim.wo[win].spell = false
vim.treesitter.start(vim.api.nvim_win_get_buf(win), "markdown")
end,
}, opts or {})
)
end
function M.GetVisualSelection(keepSelectionIfNotInBlockMode, advanceCursorOneLine, debugNotify)
local line_start, column_start
local line_end, column_end
-- if debugNotify is true, use vim.notify to show debug info.
debugNotify = debugNotify or false
-- keep selection defaults to false, but if true the selection will
-- be reinstated after it's cleared to set '> and '<
-- only relevant in visual or visual line mode, block always keeps selection.
keepSelectionIfNotInBlockMode = keepSelectionIfNotInBlockMode or false
-- advance cursor one line defaults to true, but is turned off for
-- visual block mode regardless.
advanceCursorOneLine = (function()
if keepSelectionIfNotInBlockMode == true then
return false
else
return advanceCursorOneLine or true
end
end)()
if vim.fn.visualmode() == "\22" then
line_start, column_start = unpack(vim.fn.getpos("v"), 2)
line_end, column_end = unpack(vim.fn.getpos("."), 2)
else
-- if not in visual block mode then i want to escape to normal mode.
-- if this isn't done here, then the '< and '> do not get set,
-- and the selection will only be whatever was LAST selected.
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<esc>", true, false, true), "x", true)
line_start, column_start = unpack(vim.fn.getpos("'<"), 2)
line_end, column_end = unpack(vim.fn.getpos("'>"), 2)
end
if column_start > column_end then
column_start, column_end = column_end, column_start
if debugNotify == true then
vim.notify(
"switching column start and end, \nWas "
.. column_end
.. ","
.. column_start
.. "\nNow "
.. column_start
.. ","
.. column_end
)
end
end
if line_start > line_end then
line_start, line_end = line_end, line_start
if debugNotify == true then
vim.notify(
"switching line start and end, \nWas "
.. line_end
.. ","
.. line_start
.. "\nNow "
.. line_start
.. ","
.. line_end
)
end
end
if vim.g.selection == "exclusive" then
column_end = column_end - 1 -- Needed to remove the last character to make it match the visual selection
end
if debugNotify == true then
vim.notify(
"vim.fn.visualmode(): "
.. vim.fn.visualmode()
.. "\nsel start "
.. vim.inspect(line_start)
.. " "
.. vim.inspect(column_start)
.. "\nSel end "
.. vim.inspect(line_end)
.. " "
.. vim.inspect(column_end)
)
end
local n_lines = math.abs(line_end - line_start) + 1
local lines = vim.api.nvim_buf_get_lines(0, line_start - 1, line_end, false)
if #lines == 0 then
return { "" }
end
if vim.fn.visualmode() == "\22" then
-- this is what actually sets the lines to only what is found between start and end columns
for i = 1, #lines do
lines[i] = string.sub(lines[i], column_start, column_end)
end
else
lines[1] = string.sub(lines[1], column_start, -1)
if n_lines == 1 then
lines[n_lines] = string.sub(lines[n_lines], 1, column_end - column_start + 1)
else
lines[n_lines] = string.sub(lines[n_lines], 1, column_end)
end
-- if advanceCursorOneLine == true, then i do want the cursor to advance once.
if advanceCursorOneLine == true then
if debugNotify == true then
vim.notify("advancing cursor one line past the end of the selection to line " .. vim.inspect(line_end + 1))
end
vim.api.nvim_win_set_cursor(0, { line_end + 1, 0 })
end
if keepSelectionIfNotInBlockMode then
vim.api.nvim_feedkeys("gv", "n", true)
end
end
if debugNotify == true then
vim.notify(table.concat(lines, "\n"))
end
return lines -- use this return if you want an array of text lines
-- return table.concat(lines, "\n") -- use this return instead if you need a text block
end
-- uc("Scratch", function(arg)
-- M.scratch(arg)
-- --local M = {}
-- end, {})
---this is a test to see about structure and calling things
---@param s string
---@return string
function M.StringAppendWithBLAH(s)
local result = s .. "BLAH"
return result
end
--- Merge extended options with a default table of options
-- @param default the default table that you want to merge into
-- @param opts the new options that should be merged with the default table
-- @return the merged table
function M.extend_tbl(default, opts)
opts = opts or {}
return default and vim.tbl_deep_extend("force", default, opts) or opts
end
--- Call function if a condition is met
-- @param func the function to run
-- @param condition a boolean value of whether to run the function or not
function M.conditional_func(func, condition, ...)
-- if the condition is true or no condition is provided, evaluate the function with the rest of the parameters and return the result
if condition and type(func) == "function" then
return func(...)
end
end
--- Open a URL under the cursor with the current operating system
-- @param path the path of the file to open with the system opener
function M.system_open(path)
local cmd
if vim.fn.has("win32") == 1 and vim.fn.executable("explorer") == 1 then
cmd = "explorer"
elseif vim.fn.has("unix") == 1 and vim.fn.executable("xdg-open") == 1 then
cmd = "xdg-open"
elseif (vim.fn.has("mac") == 1 or vim.fn.has("unix") == 1) and vim.fn.executable("open") == 1 then
cmd = "open"
end
if not cmd then
M.notify("Available system opening tool not found!", "error")
end
if require("lazyvim.util").has("open-browser.vim") then
-- require("openbrowser-smart-search")
vim.cmd.OpenBrowserSmartSearch(path)
else
vim.fn.jobstart({ cmd, path or vim.fn.expand("<cfile>") }, { detach = true })
end
end
function M.open(uri)
if M.file_exists(uri) then
return M.float({ style = "", file = uri })
end
local Config = require("lazy.core.config")
local cmd
if Config.options.ui.browser then
cmd = { Config.options.ui.browser, uri }
elseif vim.fn.has("win32") == 1 then
cmd = { "explorer", uri }
elseif vim.fn.has("macunix") == 1 then
cmd = { "open", uri }
else
if vim.fn.executable("xdg-open") == 1 then
cmd = { "xdg-open", uri }
elseif vim.fn.executable("wslview") == 1 then
cmd = { "wslview", uri }
else
cmd = { "open", uri }
end
end
local ret = vim.fn.jobstart(cmd, { detach = true })
if ret <= 0 then
local msg = {
"Failed to open uri",
ret,
vim.inspect(cmd),
}
vim.notify(table.concat(msg, "\n"), vim.log.levels.ERROR)
end
end
uc("SystemOpen", function()
M.open(vim.fn.expand("<cfile>"))
-- M.system_open(vim.fn.expand("<cfile>"))
end, {})
-- term_details can be either a string for just a command or
-- a complete table to provide full access to configuration when calling Terminal:new()
--- Toggle a user terminal if it exists, if not then create a new one and save it
-- @param term_details a terminal command string or a table of options for Terminal:new() (Check toggleterm.nvim documentation for table format)
function M.toggle_term_cmd(opts)
local terms = {}
-- if a command string is provided, create a basic table for Terminal:new() options
if type(opts) == "string" then
opts = { cmd = opts, hidden = true }
end
local num = vim.v.count > 0 and vim.v.count or 1
-- if terminal doesn't exist yet, create it
if not terms[opts.cmd] then
terms[opts.cmd] = {}
end
if not terms[opts.cmd][num] then
if not opts.count then
opts.count = vim.tbl_count(terms) * 100 + num
end
if not opts.on_exit then
opts.on_exit = function()
terms[opts.cmd][num] = nil
end
end
terms[opts.cmd][num] = require("toggleterm.terminal").Terminal:new(opts)
end
-- toggle the terminal
terms[opts.cmd][num]:toggle()
end
--- regex used for matching a valid URL/URI string
local url_matcher =
"\\v\\c%(%(h?ttps?|ftp|file|ssh|git)://|[a-z]+[@][a-z]+[.][a-z]+:)%([&:#*@~%_\\-=?!+;/0-9a-z]+%(%([.;/?]|[.][.]+)[&:#*@~%_\\-=?!+/0-9a-z]+|:\\d+|,%(%(%(h?ttps?|ftp|file|ssh|git)://|[a-z]+[@][a-z]+[.][a-z]+:)@![0-9a-z]+))*|\\([&:#*@~%_\\-=?!+;/.0-9a-z]*\\)|\\[[&:#*@~%_\\-=?!+;/.0-9a-z]*\\]|\\{%([&:#*@~%_\\-=?!+;/.0-9a-z]*|\\{[&:#*@~%_\\-=?!+;/.0-9a-z]*})\\})+"
--- Delete the syntax matching rules for URLs/URIs if set
function M.delete_url_match()
for _, match in ipairs(vim.fn.getmatches()) do
if match.group == "HighlightURL" then
vim.fn.matchdelete(match.id)
end
end
end
function M.read_file(file)
local fd = assert(io.open(file, "r"))
---@type string
local data = fd:read("*a")
fd:close()
return data
end
function M.write_file(file, contents)
local fd = assert(io.open(file, "w+"))
fd:write(contents)
fd:close()
end
---@class LazyCmdOptions: LazyFloatOptions
---@field cwd? string
---@field env? table<string,string>
---@field float? LazyFloatOptions
-- Opens a floating terminal (interactive by default)
---@param cmd? string[]|string
---@param opts? LazyCmdOptions|{interactive?:boolean}
function M.float_term(cmd, opts)
cmd = cmd or {}
if type(cmd) == "string" then
cmd = { cmd }
end
if #cmd == 0 then
cmd = { vim.env.SHELL or vim.o.shell }
end
opts = opts or {}
local float = M.float(opts)
vim.fn.termopen(cmd, vim.tbl_isempty(opts) and vim.empty_dict() or opts)
if opts.interactive ~= false then
vim.cmd.startinsert()
vim.api.nvim_create_autocmd("TermClose", {
once = true,
buffer = float.buf,
callback = function()
float:close()
vim.cmd.checktime()
end,
})
end
return float
end
--- Runs the command and shows it in a floating window
---@param cmd string[]
---@param opts? LazyCmdOptions|{filetype?:string}
function M.float_cmd(cmd, opts)
opts = opts or {}
local float = M.float(opts)
if opts.filetype then
vim.bo[float.buf].filetype = opts.filetype
end
local Process = require("lazy.manage.process")
local lines = Process.exec(cmd, { cwd = opts.cwd })
vim.api.nvim_buf_set_lines(float.buf, 0, -1, false, lines)
vim.bo[float.buf].modifiable = false
return float
end
---@alias FileType "file"|"directory"|"link"
---@param path string
---@param fn fun(path: string, name:string, type:FileType):boolean?
function M.ls(path, fn)
local handle = vim.loop.fs_scandir(path)
while handle do
local name, t = vim.loop.fs_scandir_next(handle)
if not name then
break
end
local fname = path .. "/" .. name
-- HACK: type is not always returned due to a bug in luv,
-- so fecth it with fs_stat instead when needed.
-- see https://github.com/folke/lazy.nvim/issues/306
if fn(fname, name, t or vim.loop.fs_stat(fname).type) == false then
break
end
end
end
---@param path string
---@param fn fun(path: string, name:string, type:FileType)
function M.walk(path, fn)
M.ls(path, function(child, name, type)
if type == "directory" then
M.walk(child, fn)
end
fn(child, name, type)
end)
end
---@param root string
---@param fn fun(modname:string, modpath:string)
---@param modname? string
function M.walkmods(root, fn, modname)
modname = modname and (modname:gsub("%.$", "") .. ".") or ""
M.ls(root, function(path, name, type)
if name == "init.lua" then
fn(modname:gsub("%.$", ""), path)
elseif (type == "file" or type == "link") and name:sub(-4) == ".lua" then
fn(modname .. name:sub(1, -5), path)
elseif type == "directory" then
M.walkmods(path, fn, modname .. name .. ".")
end
end)
end
---@generic V
---@param t table<string, V>
---@param fn fun(key:string, value:V)
function M.foreach(t, fn)
---@type string[]
local keys = vim.tbl_keys(t)
pcall(table.sort, keys, function(a, b)
return a:lower() < b:lower()
end)
for _, key in ipairs(keys) do
fn(key, t[key])
end
end
--- Add syntax matching rules for highlighting URLs/URIs
function M.set_url_match()
M.delete_url_match()
if vim.g.highlighturl_enabled then
vim.fn.matchadd("HighlightURL", url_matcher, 15)
end
end
--- Run a shell command and capture the output and if the command succeeded or failed
-- @param cmd the terminal command to execute
-- @param show_error boolean of whether or not to show an unsuccessful command as an error to the user
-- @return the result of a successfully executed command or nil
function M.cmd(cmd, show_error)
if vim.fn.has("win32") == 1 then
cmd = { "cmd.exe", "/C", cmd }
end
local result = vim.fn.system(cmd)
local success = vim.api.nvim_get_vvar("shell_error") == 0
if not success and (show_error == nil or show_error) then
vim.api.nvim_err_writeln("Error running command: " .. cmd .. "\nError message:\n" .. result)
end
return success and result:gsub("[\27\155][][()#;?%d]*[A-PRZcf-ntqry=><~]", "") or nil
end
---@generic F: fun()
---@param ms number
---@param fn F
---@return F
function M.throttle(ms, fn)
local timer = vim.loop.new_timer()
local running = false
local first = true
return function(...)
local args = { ... }
local wrapped = function()
fn(unpack(args))
end
if not running then
if first then
wrapped()
first = false
end
timer:start(ms, 0, function()
running = false
vim.schedule(wrapped)
end)
running = true
end
end
end
function M._dump(value, result)
local t = type(value)
if t == "number" or t == "boolean" then
table.insert(result, tostring(value))
elseif t == "string" then
table.insert(result, ("%q"):format(value))
elseif t == "table" then
table.insert(result, "{")
local i = 1
---@diagnostic disable-next-line: no-unknown
for k, v in pairs(value) do
if k == i then
elseif type(k) == "string" then
table.insert(result, ("[%q]="):format(k))
else
table.insert(result, k .. "=")
end
M._dump(v, result)
table.insert(result, ",")
i = i + 1
end
table.insert(result, "}")
else
error("Unsupported type " .. t)
end
end
-- For pretty printing lua objects (`:lua dump(vim.fn)`)
M.dump = function(...)
-- local objects = vim.tbl_map(vim.inspect, { ... })
local result = {}
M.foreach(..., function(val)
M._dump(val, result)
-- table.concat(result, "")
-- print(unpack(objects))
end)
print(unpack(result))
return ...
end
---@param opts? {finally:fun()}
function M.try(fn, opts)
opts = opts or {}
local ok, err = pcall(fn)
if opts.finally then
pcall(opts.finally)
end
if not ok then
M.error(err)
end
end
function M.notify(msg, level)
vim.notify(msg, level, { title = "edgy.nvim" })
end
function M.error(msg)
M.notify(msg, vim.log.levels.ERROR)
end
function M.warn(msg)
M.notify(msg, vim.log.levels.WARN)
end
function M.info(msg)
M.notify(msg, vim.log.levels.INFO)
end
function M.debug(msg)
if require("edgy.config").debug then
M.info(msg)
end
end
---@generic F: fun()
---@param fn F
---@param max_retries? number
---@return F
function M.with_retry(fn, max_retries)
max_retries = max_retries or 3
local retries = 0
local function try()
local ok, ret = pcall(fn)
if ok then
retries = 0
else
if retries >= max_retries or require("edgy.config").debug then
M.error(ret)
end
if retries < max_retries then
return vim.schedule(try)
end
end
end
return try
end
---@generic F: fun()
---@param fn F
---@return F
function M.noautocmd(fn)
return function(...)
vim.o.eventignore = "all"
local ok, ret = pcall(fn, ...)
vim.o.eventignore = ""
if not ok then
error(ret)
end
return ret
end
end
--- @generic F: function
--- @param fn F
--- @param ms? number
--- @return F
function M.debounce(fn, ms)
ms = ms or 50
local timer = assert(vim.loop.new_timer())
local waiting = 0
return function()
if timer:is_active() then
waiting = waiting + 1
else
waiting = 0
fn()
end
timer:start(ms, 0, function()
if waiting then
vim.schedule(fn) -- only execute if there are calls waiting
end
end)
end
end
return M