-
-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathutil.lua
More file actions
257 lines (224 loc) · 6.11 KB
/
util.lua
File metadata and controls
257 lines (224 loc) · 6.11 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
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.path_sep = M.iswin and '\\' or '/'
function M.path_join(...)
return table.concat({ ... }, M.path_sep)
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 client.supports_method(method) then
supports[#supports + 1] = client
end
end
return supports
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, 't' },
})
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
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
---@param location integer[] [row, col]
---@param range { end : { character : number, line : number }, start : { character : number, line : number } }
---@return boolean
function M.is_in_range(location, range)
local row = location[1]
local col = location[2]
local start_row = range.start.line + 1
local end_row = range['end'].line + 1
local end_col = range['end'].character
local start_col = range.start.character
if row >= start_row and row <= end_row and col >= start_col and col <= end_col then
return true
end
return false
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
return M