-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathutil.lua
More file actions
335 lines (295 loc) · 8.7 KB
/
util.lua
File metadata and controls
335 lines (295 loc) · 8.7 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
local api, lsp = vim.api, vim.lsp
---@diagnostic disable-next-line: deprecated
local uv = vim.version().minor >= 10 and vim.uv or vim.loop
local M = {}
M.iswin = uv.os_uname().sysname:match('Windows')
M.ismac = uv.os_uname().sysname == 'Darwin'
M.is_ten = vim.version().minor >= 10
M.is_eleven = vim.version().minor >= 11
M.path_sep = M.iswin and '\\' or '/'
function M.path_join(...)
return table.concat({ ... }, M.path_sep)
end
-- 0.11+ warns on dot calls; 0.10- breaks on colon calls.
local function client_method_wrapper(client, name, ...)
if M.is_eleven then
return client[name](client, ...)
end
return client[name](...)
end
function M.client_request(client, ...)
return client_method_wrapper(client, 'request', ...)
end
function M.client_request_sync(client, ...)
return client_method_wrapper(client, 'request_sync', ...)
end
function M.client_supports_method(client, ...)
return client_method_wrapper(client, 'supports_method', ...)
end
function M.path_itera(buf)
local parts = vim.split(api.nvim_buf_get_name(buf), M.path_sep, { trimempty = true })
local index = #parts + 1
return function()
index = index - 1
if index > 0 then
return parts[index]
end
end
end
function M.path_sub(fname, root)
local pwd = uv.cwd()
if root and fname:sub(1, #root) == root then
root = root
elseif fname:sub(1, #pwd) == pwd then
root = pwd
else
root = vim.env.HOME
end
root = root:sub(#root - #M.path_sep + 1) == M.path_sep and root or root .. M.path_sep
return fname:gsub(vim.pesc(root), '')
end
--get icon hlgroup color
function M.icon_from_devicon(ft)
local ok, devicons = pcall(require, 'nvim-web-devicons')
if not ok then
return ''
end
return devicons.get_icon_by_filetype(ft)
end
---get index from a list-like table
function M.tbl_index(tbl, val)
for index, v in ipairs(tbl) do
if v == val then
return index
end
end
end
-- get client by methods
function M.get_client_by_method(method)
if vim.version().minor >= 10 then
return lsp.get_clients({ bufnr = 0, method = method })
end
---@diagnostic disable-next-line: deprecated
local clients = lsp.get_active_clients({ bufnr = 0 })
local supports = {}
for _, client in ipairs(clients or {}) do
if M.client_supports_method(client, method) then
supports[#supports + 1] = client
end
end
return supports
end
--- generate LSP RPC method `textDocument/references` parameters by configuration.
---@param method string LSP RPC method name
---@param params table parameters for LSP RPC method
---@param config table|boolean configuration for config.finder.ref_opt
---@return table
function M.gen_param_by_config(method, params, config)
if type(config) == 'table' and method == 'textDocument/references' then
local bufnr = vim.api.nvim_get_current_buf()
local clients = lsp.get_clients({ bufnr = bufnr })
for _, client in ipairs(clients or {}) do
-- Replace characters dash('-') to underscore('_') for LSP client name,
-- which is useful for some LSP client like `rust-analyzer`.
local client_name = string.gsub(client.name, '-', '_')
local v = vim.tbl_get(config, client_name)
if v ~= nil then
params.context = {
includeDeclaration = v,
}
return { method, params }
end
end
end
return { method, params }
end
function M.feedkeys(key)
local k = api.nvim_replace_termcodes(key, true, false, true)
api.nvim_feedkeys(k, 'nx', false)
end
function M.scroll_in_float(bufnr, winid)
local config = require('lspsaga').config
if not api.nvim_win_is_valid(winid) or not api.nvim_buf_is_valid(bufnr) then
return
end
for i, map in ipairs({ config.scroll_preview.scroll_down, config.scroll_preview.scroll_up }) do
M.map_keys(bufnr, map, function()
if api.nvim_win_is_valid(winid) then
api.nvim_win_call(winid, function()
local key = i == 1 and '<C-d>' or '<C-u>'
M.feedkeys(key)
end)
end
end)
end
end
function M.delete_scroll_map(bufnr)
local config = require('lspsaga').config
api.nvim_buf_del_keymap(bufnr, 'n', config.scroll_preview.scroll_down)
api.nvim_buf_del_keymap(bufnr, 'n', config.scroll_preview.scroll_up)
end
function M.gen_truncate_line(width)
return ('─'):rep(width)
end
function M.get_max_content_length(contents)
vim.validate('contents', contents, 'table')
local cells = {}
for _, v in pairs(contents) do
if v:find('\n.') then
local tbl = vim.split(v, '\n')
vim.tbl_map(function(s)
table.insert(cells, #s)
end, tbl)
else
table.insert(cells, #v)
end
end
table.sort(cells)
return cells[#cells]
end
function M.close_win(winid)
for _, id in ipairs(M.as_table(winid)) do
if api.nvim_win_is_valid(id) then
api.nvim_win_close(id, true)
end
end
end
function M.get_max_float_width(percent)
percent = percent or 0.6
return math.floor(vim.o.columns * percent)
end
function M.win_height_increase(content, percent)
local increase = 0
local max_width = M.get_max_float_width(percent)
local max_len = M.get_max_content_length(content)
local new = {}
for _, v in pairs(content) do
if v:find('\n.') then
vim.list_extend(new, vim.split(v, '\n'))
else
new[#new + 1] = v
end
end
if max_len > max_width then
vim.tbl_map(function(s)
local cols = vim.fn.strdisplaywidth(s)
if cols > max_width then
increase = increase + math.floor(cols / max_width)
end
end, new)
end
return increase
end
function M.as_table(value)
return type(value) ~= 'table' and { value } or value
end
--- Creates a buffer local mapping.
---@param buffer number
---@param keys string|table<string>
---@param rhs string|function
---@param modes string|table<string>|nil
---@param opts table|nil
function M.map_keys(buffer, keys, rhs, modes, opts)
opts = opts or {}
opts.nowait = true
opts.noremap = true
modes = modes or 'n'
if type(rhs) == 'function' then
opts.callback = rhs
rhs = ''
end
for _, mode in ipairs(M.as_table(modes)) do
for _, lhs in ipairs(M.as_table(keys)) do
api.nvim_buf_set_keymap(buffer, mode, lhs, rhs, opts)
end
end
end
function M.res_isempty(results)
-- handle {{}}
if vim.tbl_isempty(results) then
return true
end
for _, res in pairs(results) do
if res.result and #res.result > 0 then
return false
end
end
return true
end
function M.nvim_ten()
return vim.version().minor >= 10
end
---sub c/ cpp header file path when in macos
---@return string
function M.sub_mac_c_header(fname)
local pos = fname:find('./usr/include')
if not pos then
return fname
end
return fname:sub(pos + 1)
end
--- Key value pairs used to filter the approach
--- Use client directly
--- @class lspsaga.util.get_offset_encoding.Filter
--- @inlinedoc
--- @field client? table
---
--- Try to use the first client that matches bufnr
--- @field bufnr? integer
---
--- Try to use the given method to retrieve the client
--- @field method? string
---
---@param filter table
---@param fallback string
---@return string 'utf-8'|'utf-16'|'utf-32'
function M.get_offset_encoding(filter, fallback)
if vim.fn.has('nvim-0.11') == 1 then
vim.validate('filter', filter, 'table', true)
else
vim.validate({ filter = { filter, 'table' } })
end
filter = filter or {}
fallback = fallback or 'utf-16'
if filter.client then
if filter.client and filter.client.offset_encoding then
return filter.client.offset_encoding
end
elseif filter.bufnr then
local clients = lsp.get_clients({ bufnr = filter.bufnr })
if #clients > 0 and clients[1].offset_encoding then
return clients[1].offset_encoding
end
elseif filter.method then
local clients = M.get_client_by_method(filter.method)[1].offset_encoding
if #clients > 0 and clients[1].offset_encoding then
return clients[1].offset_encoding
end
end
return fallback
end
function M.valid_markdown_parser()
local parsers = { 'parser/markdown.so', 'parser/markdown_inline.so' }
for _, p in ipairs(parsers) do
if #api.nvim_get_runtime_file(p, true) == 0 then
vim.notify_once('[Lspsaga] for better experience instal markdown relate tresitter parser')
return
end
end
end
function M.get_bold_num()
local line = api.nvim_get_current_line()
local num = line:match('%*%*(%d+)%*%*')
if num then
num = tonumber(num)
end
return num
end
function M.sub_rust_toolchains(fname)
local rustup_home = os.getenv('RUSTUP_HOME') or vim.fs.joinpath(vim.env.HOME, '.rustup')
local toolchains = vim.fs.joinpath(rustup_home, 'toolchains')
local parts = vim.split(fname, M.path_sep, { trimempty = true })
local count = #vim.split(toolchains, M.path_sep, { trimempty = true })
return vim.fs.joinpath(unpack(parts, count + 1))
end
return M