.config/nvim/lua/config/util.lua

Wed, 28 Aug 2024 04:44:05 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Wed, 28 Aug 2024 04:44:05 -0500
changeset 1178
8993dc4128de
parent 1166
34bf03fa07e4
permissions
-rw-r--r--

add assign types gopls hint too

1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
1 local M = {}
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
2
1095
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
3 local api, fn = vim.api, vim.fn
1085
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
4
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
5 --- Bulk-define aliases.
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
6 ---@param aliases table<string, string>
1095
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
7 function M.calias(aliases)
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
8 for abbrev, expand in pairs(aliases) do
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
9 vim.cmd.cnoreabbrev(
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
10 string.format(
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
11 [[<expr> %s (getcmdtype() == ":" && getcmdline() == "%s") ? "%s" : "%s"]],
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
12 abbrev,
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
13 abbrev,
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
14 expand,
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
15 abbrev
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
16 )
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
17 )
1095
226221dc1d6b Simplify command aliases
Meredith Howard <mhoward@roomag.org>
parents: 1085
diff changeset
18 end
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
19 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
21 --- Shortcut for autocmd creation.
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
22 --- @param group string | number
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
23 --- @param event string | string[]
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
24 --- @param pattern string | string[]
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
25 --- @param opts table | string | fun(ctx) `nvim_create_autocmd` options OR a function for the `callback` option OR a string for the command option
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
26 function M.autocmd(group, event, pattern, opts)
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
27 if type(opts) == "function" then
1166
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
28 opts = { callback = opts }
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
29 elseif type(opts) == "string" then
34bf03fa07e4 normalize config.util.autocmd
Meredith Howard <mhoward@roomag.org>
parents: 1161
diff changeset
30 opts = { command = opts }
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32
1085
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
33 api.nvim_create_autocmd(
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34 event,
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
35 vim.tbl_extend("keep", opts, {
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 group = group,
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37 pattern = pattern,
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
38 })
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
39 )
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
40 end
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
41
1085
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
42 function M.safe_filter_file(cmd)
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
43 local errorfile = fn.tempname()
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
44 vim.cmd([[silent %!]] .. cmd .. [[ 2>]] .. fn.shellescape(errorfile))
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
45 if vim.v.shell_error ~= 0 then
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
46 vim.cmd("silent undo")
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
47 api.nvim_err_write(io.open(errorfile):read("*a"))
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
48 end
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
49 fn.delete(errorfile)
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
50 end
c2d11f022cf0 Port encrypted file edit to lua
Meredith Howard <mhoward@roomag.org>
parents: 1078
diff changeset
51
1156
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
52 function M.last_modified_days(fname)
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
53 return (os.time() - vim.fn.getftime(fname)) / 86400
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
54 end
0a7162df160d pull out last_modified_days
Meredith Howard <mhoward@roomag.org>
parents: 1095
diff changeset
55
1078
aa4c1aa529a5 Start porting custom commands to lua
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
56 return M

mercurial