-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.lua
349 lines (314 loc) · 8.79 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
local M = {}
local Util = require("lazy.core.util")
M.root_patterns = { ".git", "lua", "package.json", "mvnw", "gradlew", "pom.xml", "build.gradle", "release", ".project" }
M.augroup = function(name)
return vim.api.nvim_create_augroup("lazyman_" .. name, { clear = true })
end
M.has = function(plugin)
return require("lazy.core.config").plugins[plugin] ~= nil
end
--- @param on_attach fun(client, buffer)
M.on_attach = function(on_attach)
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local buffer = args.buf
local client = vim.lsp.get_client_by_id(args.data.client_id)
on_attach(client, buffer)
end,
})
end
M.map = function(mode, lhs, rhs, opts)
local keys = require("lazy.core.handler").handlers.keys
-- do not create the keymap if a lazy keys handler exists
if keys ~= nil then
if not keys.active[keys.parse({ lhs, mode = mode }).id] then
opts = opts or {}
opts.silent = opts.silent ~= false
vim.keymap.set(mode, lhs, rhs, opts)
end
end
end
M.get_highlight_value = function(group)
local found, hl = pcall(vim.api.nvim_get_hl_by_name, group, true)
if not found then
return {}
end
local hl_config = {}
for key, value in pairs(hl) do
_, hl_config[key] = pcall(string.format, "#%02x", value)
end
return hl_config
end
M.float_term = function(cmd, opts)
opts = vim.tbl_deep_extend("force", {
size = { width = 0.9, height = 0.9 },
}, opts or {})
require("lazy.util").float_term(cmd, opts)
end
--- get os type
---@return string the os type "osx|linux|other|unknown"
M.get_os_type = function()
local homedir = os.getenv("HOME")
local user = os.getenv("USER")
local start_i = 0
local end_i = 0
if homedir == nil or user == nil then
return "unknown"
end
start_i, end_i = string.find(homedir, "/home/" .. user)
if start_i ~= nil and end_i ~= nil then
return "linux"
else
start_i, end_i = string.find(homedir, "/Users/" .. user)
if start_i ~= nil and end_i ~= nil then
return "osx"
else
return "other"
end
end
end
-- returns the root directory based on:
-- * lsp workspace folders
-- * lsp root_dir
-- * root pattern of filename of the current buffer
-- * root pattern of cwd
M.get_root = function()
---@type string?
local path = vim.api.nvim_buf_get_name(0)
path = path ~= "" and vim.loop.fs_realpath(path) or nil
---@type string[]
local roots = {}
if path then
for _, client in pairs(vim.lsp.get_active_clients({ bufnr = 0 })) do
local workspace = client.config.workspace_folders
local paths = workspace and vim.tbl_map(function(ws)
return vim.uri_to_fname(ws.uri)
end, workspace) or client.config.root_dir and { client.config.root_dir } or {}
for _, p in ipairs(paths) do
local r = vim.loop.fs_realpath(p)
if path:find(r, 1, true) then
roots[#roots + 1] = r
end
end
end
end
table.sort(roots, function(a, b)
return #a > #b
end)
---@type string?
local root = roots[1]
if not root then
path = path and vim.fs.dirname(path) or vim.loop.cwd()
---@type string?
root = vim.fs.find(M.root_patterns, { path = path, upward = true })[1]
root = root and vim.fs.dirname(root) or vim.loop.cwd()
end
---@cast root string
return root
end
M.set_root = function(dir)
vim.api.nvim_set_current_dir(dir)
end
---@param silent boolean?
---@param values? {[1]:any, [2]:any}
M.toggle = function(option, silent, values)
if values then
if vim.opt_local[option]:get() == values[1] then
vim.opt_local[option] = values[2]
else
vim.opt_local[option] = values[1]
end
return Util.info("Set " .. option .. " to " .. vim.opt_local[option]:get(), { title = "Option" })
end
vim.opt_local[option] = not vim.opt_local[option]:get()
if not silent then
if vim.opt_local[option]:get() then
Util.info("Enabled " .. option, { title = "Option" })
else
Util.warn("Disabled " .. option, { title = "Option" })
end
end
end
---@param type "ivy" | "dropdown" | "cursor" | nil
M.telescope_theme = function(type)
if type == nil then
return {
borderchars = M.generate_borderchars("thick"),
layout_config = {
width = 80,
height = 0.5,
},
}
end
return require("telescope.themes")["get_" .. type]({
cwd = M.get_root(),
borderchars = M.generate_borderchars("thick", nil, { top = "█", top_left = "█", top_right = "█" }),
})
end
---@param type "ivy" | "dropdown" | "cursor" | nil
M.telescope = function(builtin, type, opts)
local params = { builtin = builtin, type = type, opts = opts }
return function()
builtin = params.builtin
type = params.type
opts = params.opts
opts = vim.tbl_deep_extend("force", { cwd = M.get_root() }, opts or {})
local theme
if vim.tbl_contains({ "ivy", "dropdown", "cursor" }, type) then
theme = M.telescope_theme(type)
else
theme = opts
end
require("telescope.builtin")[builtin](theme)
end
end
---@param name "autocmds" | "options" | "keymaps"
M.load = function(name)
local mod = require("configuration").namespace .. "." .. name
Util.try(function()
require(mod)
end, {
msg = "Failed loading " .. mod,
on_error = function(msg)
local modpath = require("lazy.core.cache").find(mod)
if modpath then
Util.error(msg)
end
end,
})
end
M.on_very_lazy = function(fn)
vim.api.nvim_create_autocmd("User", {
pattern = "VeryLazy",
callback = function()
fn()
end,
})
end
M.capabilities = function(ext)
return vim.tbl_deep_extend(
"force",
{},
ext or {},
require("cmp_nvim_lsp").default_capabilities(),
{ textDocument = { foldingRange = { dynamicRegistration = false, lineFoldingOnly = true } } }
)
end
-- check if a variable is not empty nor nil
M.isNotEmpty = function(s)
return s ~= nil and s ~= ""
end
-- check if a variable is empty or nil
M.isEmpty = function(s)
return s == nil or s == ""
end
--- Check if path exists
M.path_exists = function(path)
return vim.loop.fs_stat(path)
end
-- Return telescope files command
M.project_files = function()
local path = vim.loop.cwd() .. "/.git"
if M.path_exists(path) then
return "Telescope git_files"
else
return "Telescope find_files"
end
end
--- Check if a file or directory exists in this path
M.file_or_dir_exists = function(file)
local ok, err, code = os.rename(file, file)
if not ok then
if code == 13 then
-- Permission denied, but it exists
return true
end
end
return ok, err
end
--- Check if a directory exists in this path
M.isdir = function(path)
-- "/" works on both Unix and Windows
return M.file_or_dir_exists(path .. "/")
end
M.notify = function(msg, level, opts)
opts = opts or {}
level = vim.log.levels[level:upper()]
if type(msg) == "table" then
msg = table.concat(msg, "\n")
end
local nopts = { title = "Nvim" }
if opts.once then
return vim.schedule(function()
vim.notify_once(msg, level, nopts)
end)
end
vim.schedule(function()
vim.notify(msg, level, nopts)
end)
end
M.concat_tables = function(t1, t2)
for _,v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
M.table_contains = function(tbl, x)
local found = false
for _, v in pairs(tbl) do
if v == x then
found = true
end
end
return found
end
--- @param type "thin" | "thick" | "empty" | nil
--- @param order "t-r-b-l-tl-tr-br-bl" | "tl-t-tr-r-bl-b-br-l" | nil
--- @param opts BorderIcons | nil
M.generate_borderchars = function(type, order, opts)
if order == nil then
order = "t-r-b-l-tl-tr-br-bl"
end
local border_icons = require("icons").borders
--- @type BorderIcons
local border = vim.tbl_deep_extend("force", border_icons[type or "empty"], opts or {})
local borderchars = {}
local extractDirections = (function()
local index = 1
return function()
if index == nil then
return nil
end
-- Find the next occurence of `char`
local nextIndex = string.find(order, "-", index)
-- Extract the first direction
local direction = string.sub(order, index, nextIndex and nextIndex - 1)
-- Update the index to nextIndex
index = nextIndex and nextIndex + 1 or nil
return direction
end
end)()
local mappings = {
t = "top",
r = "right",
b = "bottom",
l = "left",
tl = "top_left",
tr = "top_right",
br = "bottom_right",
bl = "bottom_left",
}
local direction = extractDirections()
while direction do
if mappings[direction] == nil then
M.notify(string.format("Invalid direction '%s'", direction), "error")
end
borderchars[#borderchars + 1] = border[mappings[direction]]
direction = extractDirections()
end
if #borderchars ~= 8 then
M.notify(string.format("Invalid order '%s'", order), "error")
end
return borderchars
end
return M