-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfuzzy.lua
More file actions
292 lines (251 loc) · 9 KB
/
fuzzy.lua
File metadata and controls
292 lines (251 loc) · 9 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
local fzf = require "fzf"
local devicons = require "nvim-web-devicons"
local lists = require "lists"
local utils = require "utils"
local lsp = require "lsp"
local git = require "git"
local M = {}
local nbs = " "
local get_diff_files = function()
local diff_files = {}
if not git.is_repo() then
return diff_files
end
local status = vim.fn.systemlist("git diff --name-status " .. vim.g.git_base .. " --")
if vim.v.shell_error ~= 0 then
return diff_files
end
for i = 1, #status do
local split = vim.split(status[i], " ")
diff_files[split[2]] = split[1]
end
return diff_files
end
local get_untracked_files = function()
local untracked_files = {}
if not git.is_repo() then
return untracked_files
end
local status = vim.fn.systemlist "git ls-files --exclude-standard --others"
for i = 1, #status do
untracked_files[status[i]] = "?"
end
return untracked_files
end
local icon_color_map = {
[""] = "cyan",
[""] = "cyan",
[""] = "dark_grey",
[""] = "dark_grey",
[""] = "white",
[""] = "white",
[""] = "white",
[""] = "cyan",
[""] = "blue",
[""] = "yellow",
[""] = "yellow",
[""] = "green",
[""] = "green",
[""] = "blue",
[""] = "cyan",
[""] = "yellow",
[""] = "blue",
[""] = "yellow",
[""] = "yellow",
["✖"] = "red",
["✚"] = "green",
[""] = "blue",
}
local get_color_icon = function(icon)
return utils.ansi_codes[icon_color_map[icon] or "green"](utils.ansi_codes, icon)
end
local get_git_icon = function(file, diff_files, untracked_files)
local git_icon_map = {
["M"] = "",
["D"] = "✖",
["A"] = "✚",
["?"] = "",
}
local icon = nbs
if untracked_files[file] then
icon = untracked_files[file]
end
if diff_files[file] then
icon = diff_files[file]
end
return git_icon_map[icon] or icon
end
local get_window_options = function()
local height = math.floor(vim.o.lines * 0.4)
local width = math.floor(vim.o.columns * 0.8)
local row = math.floor((vim.o.lines - height) * 0.2)
return { height = height, width = width, row = row }
end
M.fzf_files = function(files, prompt, show_git)
coroutine.wrap(function()
local diff_files = get_diff_files()
local untracked_files = get_untracked_files()
for i = 1, #files do
local extension = vim.fn.fnamemodify(files[i], ":e")
local icon = devicons.get_icon(files[i], extension)
local git_icon = get_git_icon(files[i], diff_files, untracked_files)
files[i] = get_color_icon(git_icon) .. nbs .. get_color_icon(icon) .. " " .. files[i]
end
local preview = "--preview 'bat --italic-text=always --style=numbers --color always {2..-1}'"
if show_git then
preview =
"--preview 'bat --italic-text=always --style=numbers,changes --color always {2..-1} | grep -A5 -B5 --color=never -P \"^..\\d+.{0,7}[+|_|~]\"'"
end
local result =
fzf.fzf(files, string.format("--ansi --multi %s --prompt %s/", preview, prompt), get_window_options())
if not result then
return
end
local qf_list = {}
for i = 1, #result do
result[i] = result[i]:gsub("^[^ ]* ", "")
table.insert(qf_list, { filename = result[i], lnum = 1, col = 1 })
end
if #result == 1 then
vim.cmd(string.format("silent edit %s", result[1]))
if show_git then
local timer = vim.loop.new_timer()
timer:start(100, 0, function()
timer:close()
vim.schedule_wrap(function()
vim.cmd [[normal gg]]
require("gitsigns").next_hunk()
end)()
end)
end
else
vim.fn.setqflist(qf_list)
lists.change_active [[Quickfix]]
vim.cmd [[copen]]
vim.cmd [[cc]]
end
end)()
end
M.files = function(path)
local files = vim.fn.systemlist(os.getenv "FZF_DEFAULT_COMMAND" .. " -- " .. path)
return M.fzf_files(files, path)
end
M.git_files = function()
local diff_files = vim.fn.systemlist("git diff --name-only " .. vim.g.git_base .. " --")
local untracked_files = vim.fn.systemlist "git ls-files --exclude-standard --others"
local files = vim.list_extend(diff_files, untracked_files)
return M.fzf_files(files, "git", true)
end
local symbols_to_items = function(symbols)
local function _symbols_to_items(_symbols, _items, _parent)
for _, symbol in ipairs(_symbols) do
local kind = vim.lsp.protocol.SymbolKind[symbol.kind] or "Unknown"
local range = symbol.location and symbol.location.range or symbol.range
local name = utils._if(_parent, string.format("%s -> %s", _parent, symbol.name), symbol.name)
table.insert(_items, {
lnum = range.start.line + 1,
col = range.start.character,
kind = kind,
icon = lsp.symbol_kind_icons[kind],
name = name,
})
if symbol.children then
for _, v in ipairs(_symbols_to_items(symbol.children, _items, name)) do
vim.list_extend(_items, v)
end
end
end
return _items
end
return _symbols_to_items(symbols, {})
end
M.symbols = function()
coroutine.wrap(function()
local params = vim.lsp.util.make_position_params()
local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params, 5000)
if not results_lsp or vim.tbl_isempty(results_lsp) then
print "No results"
return
end
local locations = {}
for _, server_results in pairs(results_lsp) do
if server_results.result then
vim.list_extend(locations, symbols_to_items(server_results.result) or {})
end
end
if vim.tbl_isempty(locations) then
print "No results"
return
end
local lines = {}
local name_length = math.floor(vim.o.columns * 0.5)
for i = 1, #locations do
local short_name = utils.shorten_string(locations[i].name, name_length - 1)
local name = short_name:gsub("->", utils.ansi_codes:grey "->") .. (" "):rep(name_length - #short_name)
local color = lsp.symbol_kind_colors[locations[i].kind] or "green"
local kind = utils.ansi_codes:grey "["
.. utils.ansi_codes[color](utils.ansi_codes, locations[i].kind)
.. utils.ansi_codes:grey "]"
.. (" "):rep(10 - #locations[i].kind)
local line = string.format(
"%s %s %s%s%s",
utils.ansi_codes[color](utils.ansi_codes, locations[i].icon or "?"),
name,
kind,
nbs,
utils.ansi_codes:red(locations[i].lnum .. ":" .. locations[i].col)
)
table.insert(lines, line)
end
local result = fzf.fzf(lines, "--ansi --multi --prompt 'Symbols '", get_window_options())
if not result then
return
end
local lnum, col = unpack(vim.split(vim.split(result[1], nbs)[2], ":"))
vim.cmd [[normal m']]
vim.api.nvim_win_set_cursor(0, { tonumber(lnum), tonumber(col) })
vim.cmd [[normal zt]]
end)()
end
M.headlines = function(pattern, symbol)
local headline_colors = {
"green",
"blue",
"red",
"magenta",
"yellow",
"green",
"blue",
"red",
}
coroutine.wrap(function()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local headlines = {}
local name_length = math.floor(vim.o.columns * 0.5)
for i, line in ipairs(lines) do
local _, num = line:find(pattern)
if num then
local short_name = utils.shorten_string(line:sub(num + 1, #line), name_length - 1)
table.insert(
headlines,
string.format(
"%s%s%s%s",
utils.ansi_codes[headline_colors[num]](utils.ansi_codes, symbol:rep(num) .. (" "):rep(6 - num)),
short_name .. (" "):rep(name_length - #short_name),
nbs,
utils.ansi_codes:red(i .. ":0")
)
)
end
end
local result = fzf.fzf(headlines, "--ansi --multi --prompt 'Headlines '", get_window_options())
if not result then
return
end
local lnum, col = unpack(vim.split(vim.split(result[1], nbs)[2], ":"))
vim.cmd [[normal m']]
vim.api.nvim_win_set_cursor(0, { tonumber(lnum), tonumber(col) })
vim.cmd [[normal zt]]
end)()
end
return M