-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
utils.lua
373 lines (325 loc) · 8.34 KB
/
utils.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
local async = require('plenary.async')
local curl = require('plenary.curl')
local scandir = require('plenary.scandir')
local M = {}
M.timers = {}
---@class Class
---@field new fun(...):table
---@field init fun(self, ...)
--- Create class
---@param fn function The class constructor
---@param parent table? The parent class
---@return Class
function M.class(fn, parent)
local out = {}
out.__index = out
local mt = {
__call = function(cls, ...)
return cls.new(...)
end,
}
if parent then
mt.__index = parent
end
setmetatable(out, mt)
function out.new(...)
local self = setmetatable({}, out)
fn(self, ...)
return self
end
function out.init(self, ...)
fn(self, ...)
end
return out
end
---@class OrderedMap
---@field set fun(self:OrderedMap, key:any, value:any)
---@field get fun(self:OrderedMap, key:any):any
---@field keys fun(self:OrderedMap):table
---@field values fun(self:OrderedMap):table
--- Create an ordered map
---@return OrderedMap
function M.ordered_map()
return {
_keys = {},
_data = {},
set = function(self, key, value)
if not self._data[key] then
table.insert(self._keys, key)
end
self._data[key] = value
end,
get = function(self, key)
return self._data[key]
end,
keys = function(self)
return self._keys
end,
values = function(self)
local result = {}
for _, key in ipairs(self._keys) do
table.insert(result, self._data[key])
end
return result
end,
}
end
--- Check if the current version of neovim is stable
---@return boolean
function M.is_stable()
return vim.fn.has('nvim-0.10.0') == 0
end
--- Writes text to a temporary file and returns path
---@param text string The text to write
---@return string?
function M.temp_file(text)
local temp_file = os.tmpname()
local f = io.open(temp_file, 'w+')
if f == nil then
error('Could not open file: ' .. temp_file)
end
f:write(text)
f:close()
return temp_file
end
--- Finds the path to the user's config directory
---@return string?
function M.config_path()
local config = vim.fn.expand('$XDG_CONFIG_HOME')
if config and vim.fn.isdirectory(config) > 0 then
return config
end
if vim.fn.has('win32') > 0 then
config = vim.fn.expand('$LOCALAPPDATA')
if not config or vim.fn.isdirectory(config) == 0 then
config = vim.fn.expand('$HOME/AppData/Local')
end
else
config = vim.fn.expand('$HOME/.config')
end
if config and vim.fn.isdirectory(config) > 0 then
return config
end
end
--- Blend a color with the neovim background
---@param color_name string The color name
---@param blend number The blend percentage
---@return string?
function M.blend_color(color_name, blend)
local color_int = vim.api.nvim_get_hl(0, { name = color_name }).fg
local bg_int = vim.api.nvim_get_hl(0, { name = 'Normal' }).bg
if not color_int or not bg_int then
return
end
local color = { (color_int / 65536) % 256, (color_int / 256) % 256, color_int % 256 }
local bg = { (bg_int / 65536) % 256, (bg_int / 256) % 256, bg_int % 256 }
local r = math.floor((color[1] * blend + bg[1] * (100 - blend)) / 100)
local g = math.floor((color[2] * blend + bg[2] * (100 - blend)) / 100)
local b = math.floor((color[3] * blend + bg[3] * (100 - blend)) / 100)
return string.format('#%02x%02x%02x', r, g, b)
end
--- Return to normal mode
function M.return_to_normal_mode()
local mode = vim.fn.mode():lower()
if mode:find('v') then
vim.cmd([[execute "normal! \<Esc>"]])
end
vim.cmd('stopinsert')
end
--- Mark a function as deprecated
function M.deprecate(old, new)
vim.deprecate(old, new, '3.0.X', 'CopilotChat.nvim', false)
end
--- Debounce a function
function M.debounce(id, fn, delay)
if M.timers[id] then
M.timers[id]:stop()
M.timers[id] = nil
end
M.timers[id] = vim.defer_fn(fn, delay)
end
--- Create key-value list from table
---@param tbl table The table
---@return table
function M.kv_list(tbl)
local result = {}
for k, v in pairs(tbl) do
table.insert(result, {
key = k,
value = v,
})
end
return result
end
--- Check if a buffer is valid
---@param bufnr number? The buffer number
---@return boolean
function M.buf_valid(bufnr)
return bufnr and vim.api.nvim_buf_is_valid(bufnr) and vim.api.nvim_buf_is_loaded(bufnr) or false
end
--- Check if file paths are the same
---@param file1 string? The first file path
---@param file2 string? The second file path
---@return boolean
function M.filename_same(file1, file2)
if not file1 or not file2 then
return false
end
return vim.fn.fnamemodify(file1, ':p') == vim.fn.fnamemodify(file2, ':p')
end
--- Get the filetype of a file
---@param filename string The file name
---@return string|nil
function M.filetype(filename)
local ft = vim.filetype.match({ filename = filename })
if ft == '' then
return nil
end
return ft
end
--- Get the file path
---@param filename string The file name
---@return string
function M.filepath(filename)
return vim.fn.fnamemodify(filename, ':p:.')
end
--- Generate a UUID
---@return string
function M.uuid()
local template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return (
string.gsub(template, '[xy]', function(c)
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
return string.format('%x', v)
end)
)
end
--- Generate machine id
---@return string
function M.machine_id()
local length = 65
local hex_chars = '0123456789abcdef'
local hex = ''
for _ = 1, length do
local index = math.random(1, #hex_chars)
hex = hex .. hex_chars:sub(index, index)
end
return hex
end
--- Generate a quick hash
---@param str string The string to hash
---@return string
function M.quick_hash(str)
return #str .. str:sub(1, 32) .. str:sub(-32)
end
--- Make a string from arguments
---@vararg any The arguments
---@return string
function M.make_string(...)
local t = {}
for i = 1, select('#', ...) do
local x = select(i, ...)
if type(x) == 'table' then
x = vim.inspect(x)
else
x = tostring(x)
end
t[#t + 1] = x
end
return table.concat(t, ' ')
end
--- Get current working directory for target window
---@param winnr number? The buffer number
---@return string
function M.win_cwd(winnr)
if not winnr then
return '.'
end
local dir = vim.w[winnr].cchat_cwd
if not dir or dir == '' then
return '.'
end
return dir
end
--- Send curl get request
---@param url string The url
---@param opts table? The options
M.curl_get = async.wrap(function(url, opts, callback)
curl.get(
url,
vim.tbl_deep_extend('force', opts or {}, {
callback = callback,
on_error = function(err)
err = M.make_string(err and err.stderr or err)
callback(nil, err)
end,
})
)
end, 3)
--- Send curl post request
---@param url string The url
---@param opts table? The options
M.curl_post = async.wrap(function(url, opts, callback)
curl.post(
url,
vim.tbl_deep_extend('force', opts or {}, {
callback = callback,
on_error = function(err)
err = M.make_string(err and err.stderr or err)
callback(nil, err)
end,
})
)
end, 3)
--- Scan a directory
---@param path string The directory path
---@param opts table The options
M.scan_dir = async.wrap(function(path, opts, callback)
scandir.scan_dir_async(
path,
vim.tbl_deep_extend('force', opts, {
on_exit = callback,
})
)
end, 3)
--- Check if a file exists
---@param path string The file path
M.file_exists = function(path)
local err, stat = async.uv.fs_stat(path)
return err == nil and stat ~= nil
end
--- Get last modified time of a file
---@param path string The file path
---@return number?
M.file_mtime = function(path)
local err, stat = async.uv.fs_stat(path)
if err or not stat then
return nil
end
return stat.mtime.sec
end
--- Read a file
---@param path string The file path
M.read_file = function(path)
local err, fd = async.uv.fs_open(path, 'r', 438)
if err or not fd then
return nil
end
local err, stat = async.uv.fs_fstat(fd)
if err or not stat then
async.uv.fs_close(fd)
return nil
end
local err, data = async.uv.fs_read(fd, stat.size, 0)
async.uv.fs_close(fd)
if err or not data then
return nil
end
return data
end
--- Call a system command
---@param cmd table The command
M.system = async.wrap(function(cmd, callback)
vim.system(cmd, { text = true }, callback)
end, 2)
return M