forked from rest-nvim/rest.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
362 lines (323 loc) · 10.8 KB
/
utils.lua
File metadata and controls
362 lines (323 loc) · 10.8 KB
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
---@mod rest-nvim.utils rest.nvim utilities
---
---@brief [[
---
--- rest.nvim utility functions
---
---@brief ]]
local logger = require("rest-nvim.logger")
-- local config = require("rest-nvim.config")
local utils = {}
-- NOTE: vim.loop has been renamed to vim.uv in Neovim >= 0.10 and will be removed later
local uv = vim.uv or vim.loop
---Encodes a string into its escaped hexadecimal representation
---@param str string Binary string to be encoded
---@param only_necessary? boolean Encode only necessary characters
---@return string
function utils.escape(str, only_necessary)
local ignore = "%w%-%.%_%~%+"
if only_necessary then
ignore = ignore .. "%:%/%?%=%&%#%@"
end
local pattern = "([^" .. ignore .. "])"
local encoded = string.gsub(str, pattern, function(c)
if c == " " then
return "+"
end
return string.format("%%%02x", string.byte(c))
end)
return encoded
end
---@param str string
function utils.url_decode(str)
str = string.gsub(str, "%+", " ")
str = string.gsub(str, "%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
return str
end
---Check if a file exists in the given `path`
---@param path string file path
---@return boolean
function utils.file_exists(path)
---@diagnostic disable-next-line undefined-field
local fd = uv.fs_open(path, "r", 438)
if fd then
---@diagnostic disable-next-line undefined-field
uv.fs_close(fd)
return true
end
return false
end
---Read a file if it exists
---@param path string file path
---@return string
function utils.read_file(path)
---@type string|nil
local content
if utils.file_exists(path) then
---@diagnostic disable-next-line undefined-field
local file = uv.fs_open(path, "r", 438)
---@diagnostic disable-next-line undefined-field
local stat = uv.fs_fstat(file)
---@diagnostic disable-next-line undefined-field
content = uv.fs_read(file, stat.size, 0)
---@diagnostic disable-next-line undefined-field
uv.fs_close(file)
else
---@diagnostic disable-next-line need-check-nil
logger.error("Failed to read file '" .. path .. "'")
return ""
end
---@cast content string
return content
end
local function fix_year(year)
if year < 100 then
return year + 2000
end
return year
end
function utils.parse_http_time(time_str)
local pattern = "(%a+), (%d+)[%s-](%a+)[%s-](%d+) (%d+):(%d+):(%d+) GMT"
local _, day, month_name, year, hour, min, sec = time_str:match(pattern)
-- stylua: ignore
local months = {
Jan = 1, Feb = 2, Mar = 3, Apr = 4, May = 5, Jun = 6,
Jul = 7, Aug = 8, Sep = 9, Oct = 10, Nov = 11, Dec = 12,
}
local time_table = {
year = fix_year(tonumber(year)),
month = months[month_name],
day = tonumber(day),
hour = tonumber(hour),
min = tonumber(min),
sec = tonumber(sec),
isdst = false,
}
---@diagnostic disable-next-line: param-type-mismatch
local gmt_offset = os.difftime(os.time(), os.time(os.date("!*t")))
return os.time(time_table) + gmt_offset
end
---parse url to domain and path
---path will be fallback to "/" if not found
---@param url string
---@return string domain
---@return string path
function utils.parse_url(url)
local domain = url:match("^%a+://([^/]+)") or url:match("^([^/]+)")
local path = url:match("[^:/]+(/[^?#]*)") or "/"
return domain, path
end
--- Default transformers for statistics
local transform = {
---Transform `time` into a readable typed time (e.g. 200ms)
---@param time string
---@return string
time = function(time)
---@diagnostic disable-next-line cast-local-type
time = tonumber(time)
if time >= 60 then
time = string.format("%.2f", time / 60)
return time .. " min"
end
local units = { "s", "ms", "µs", "ns" }
local unit = 1
while time < 1 and unit < #units do
---@diagnostic disable-next-line cast-local-type
time = time * 1000
unit = unit + 1
end
time = string.format("%.2f", time)
return time .. " " .. units[unit]
end,
---Transform `bytes` into another bigger size type if needed
---@param bytes string
---@return string
size = function(bytes)
---@diagnostic disable-next-line cast-local-type
bytes = tonumber(bytes)
local units = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" }
local unit = 1
while bytes >= 1024 and unit <= #units do
---@diagnostic disable-next-line cast-local-type
bytes = bytes / 1024
unit = unit + 1
end
bytes = string.format("%.2f", bytes)
return bytes .. " " .. units[unit]
end,
}
utils.transform_time = transform.time
utils.transform_size = transform.size
---@param bufnr number
---@param node TSNode
---@param ns number
---@param timeout number
function utils.ts_highlight_node(bufnr, node, ns, timeout)
if bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
local higroup = "IncSearch"
local s_row, s_col = node:start()
local e_row, e_col = node:end_()
-- don't try to highlight over the last line
if e_col == 0 then
e_row = e_row - 1
e_col = -1
end
vim.highlight.range(bufnr, ns, higroup, { s_row, s_col }, { e_row, e_col }, { regtype = "v" })
-- Clear buffer highlights again after timeout
vim.defer_fn(function()
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
end
end, timeout)
end
---@param source string|integer
---@return vim.treesitter.LanguageTree
function utils.ts_get_parser(source)
if type(source) == "string" then
return vim.treesitter.get_string_parser(source, "http")
else
return vim.treesitter.get_parser(source, "http")
end
end
---@param source string|integer
---@return vim.treesitter.LanguageTree
---@return TSTree
function utils.ts_parse_source(source)
local ts_parser = utils.ts_get_parser(source)
return ts_parser, assert(ts_parser:parse(false)[1])
end
---@param node TSNode
---@param type string
---@param oneline boolean?
---@return TSNode?
function utils.ts_find(node, type, oneline)
if oneline then
local sr, _, er, ec = node:range()
local is_oneline = (sr == er) or (er - sr == 1 and ec == 0)
if not is_oneline then
return nil
end
end
if node:type() == type then
return node
end
local parent = node:parent()
if parent then
return utils.ts_find(parent, type)
end
return nil
end
---@param node TSNode
function utils.ts_upper_node(node)
local start_row, _, _, _ = node:range()
local end_row = start_row
start_row = start_row - 1
local start_col = 0
local end_col = 0
-- HACK: root node type might not be "document"
local root_node = assert(utils.ts_find(node, "document"))
local min_node = root_node:named_descendant_for_range(start_row, start_col, end_row, end_col)
return min_node
end
---@param node TSNode
---@param expected_type string
---@return table
function utils.ts_node_spec(node, expected_type)
return {
node,
function(n)
return n:type() == expected_type
end,
"(" .. expected_type .. ") TSNode",
}
end
---Create error log for TSNode that has a syntax error
---@param node TSNode Tree-sitter node
---@return string
function utils.ts_node_error_log(node)
local s_row, s_col = node:start()
local e_row, e_col = node:end_()
local range = "["
if s_row == e_row then
range = range .. s_row .. ":" .. s_col .. " - " .. e_col
else
range = range .. s_row .. ":" .. s_col .. " - " .. e_row .. ":" .. e_col
end
range = range .. "]"
return "The tree-sitter node at the range " .. range .. " has a syntax error and cannot be parsed"
end
---Set window-option to specific buffer
---Some options leaves in `vim.wo` while they are actually tied to buffers
---see: <https://github.com/neovim/neovim/issues/11525> and `:h local-options`
---@param bufnr number
---@param name string
---@param value any
function utils.nvim_lazy_set_wo(bufnr, name, value)
vim.api.nvim_create_autocmd("BufWinEnter", {
buffer = bufnr,
callback = function()
vim.api.nvim_set_option_value(name, value, { scope = "local" })
end,
once = true,
})
end
---format lines using native vim `gq` command
---@param lines string[]
---@param filetype string
---@return string[]
---@return boolean ok Whether formatting is done with `gq`
function utils.gq_lines(lines, filetype)
logger.debug("formatting with `gq`")
if #lines == 0 then
logger.debug("content is empty. Formatting is canceled")
return lines, true
end
local format_buf = vim.api.nvim_create_buf(false, true)
local ok, errmsg = pcall(vim.api.nvim_set_option_value, "filetype", filetype, { buf = format_buf })
if not ok then
local msg = ("Can't set filetype to '%s' (%s). Formatting is canceled"):format(filetype, errmsg)
logger.warn(msg)
vim.notify(msg, vim.log.levels.WARN, { title = "rest.nvim" })
return lines, false
end
vim.api.nvim_buf_set_lines(format_buf, 0, -1, false, lines)
local formatexpr = vim.bo[format_buf].formatexpr
local formatprg = vim.bo[format_buf].formatprg
if formatexpr:match("^v:lua%.vim%.lsp%.formatexpr%(.*%)$") then
local clients_count = #vim.lsp.get_clients({ bufnr = format_buf })
logger.warn(
("formatexpr is set to `%s` but %d clients are attached to the buffer %d."):format(
formatexpr,
clients_count,
format_buf
)
)
logger.warn("Skipping lsp formatexpr")
formatexpr = ""
end
if formatexpr ~= "" then
logger.debug(("formatting %s filetype with formatexpr=%s"):format(filetype, formatexpr))
elseif formatprg ~= "" then
logger.debug(("formatting %s filetype with formatprg=%s"):format(filetype, formatprg))
else
logger.debug(("can't find formatexpr or formatprg for %s filetype. Formatting is canceled"):format(filetype))
return lines, false
end
vim.api.nvim_buf_call(format_buf, function()
-- HACK: dirty fix for neovim/neovim#30593
local gq_ok, res = pcall(vim.api.nvim_command, "silent normal gggqG")
if not gq_ok then
local msg = ("formatting %s filetype failed"):format(filetype)
logger.warn(msg, res)
vim.notify(msg, vim.log.levels.WARN, { title = "rest.nvim" })
end
end)
local buf_lines = vim.api.nvim_buf_get_lines(format_buf, 0, -1, false)
vim.api.nvim_buf_delete(format_buf, { force = true })
return buf_lines, true
end
return utils