forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelescope.lua
More file actions
413 lines (365 loc) · 10 KB
/
telescope.lua
File metadata and controls
413 lines (365 loc) · 10 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
-- plugin: telescope.nvim
-- see: https://github.com/nvim-telescope/telescope.nvim
-- rafi settings
-- Helpers
-- Returns visually selected text
local visual_selection = function()
local save_previous = vim.fn.getreg("a")
vim.api.nvim_command('silent! normal! "ay')
local selection = vim.fn.trim(vim.fn.getreg("a"))
vim.fn.setreg("a", save_previous)
return vim.fn.substitute(selection, [[\n]], [[\\n]], "g")
end
-- Custom actions
local myactions = {}
function myactions.send_to_qflist(prompt_bufnr)
require("telescope.actions").send_to_qflist(prompt_bufnr)
require("user").qflist.open()
end
function myactions.smart_send_to_qflist(prompt_bufnr)
require("telescope.actions").smart_send_to_qflist(prompt_bufnr)
require("user").qflist.open()
end
function myactions.page_up(prompt_bufnr)
require("telescope.actions.set").shift_selection(prompt_bufnr, -5)
end
function myactions.page_down(prompt_bufnr)
require("telescope.actions.set").shift_selection(prompt_bufnr, 5)
end
function myactions.change_directory(prompt_bufnr)
local entry = require("telescope.actions.state").get_selected_entry()
require("telescope.actions").close(prompt_bufnr)
vim.cmd("lcd " .. entry.path)
end
-- Custom pickers
local pickers = {}
pickers.grep_string_visual = function()
require("telescope.builtin").live_grep({
default_text = visual_selection(),
})
end
pickers.grep_string_cursor = function()
require("telescope.builtin").live_grep({
default_text = vim.fn.expand("<cword>"),
})
end
pickers.find_files_cursor = function()
require("telescope.builtin").find_files({
default_text = vim.fn.expand("<cword>"),
})
end
pickers.lsp_workspace_symbols_cursor = function()
require("telescope.builtin").lsp_workspace_symbols({
default_text = vim.fn.expand("<cword>"),
})
end
pickers.zoxide = function()
require("telescope").extensions.zoxide.list({
layout_config = {
width = 0.5,
height = 0.6,
},
})
end
pickers.notebook = function()
require("telescope.builtin").find_files({
prompt_title = "[ Notebook ]",
cwd = "$HOME/docs/blog",
})
end
pickers.plugin_directories = function(opts)
local utils = require("telescope.utils")
local dir = vim.fn.expand("$VIM_DATA_PATH/dein/repos/github.com")
opts = opts or {}
opts.cmd = utils.get_default(opts.cmd, {
vim.o.shell,
"-c",
"find " .. vim.fn.shellescape(dir) .. " -mindepth 2 -maxdepth 2 -type d",
})
local dir_len = dir:len()
opts.entry_maker = function(line)
return {
value = line,
ordinal = line,
display = line:sub(dir_len + 2),
path = line,
}
end
require("telescope.pickers")
.new(opts, {
layout_config = {
width = 0.65,
height = 0.7,
},
prompt_title = "[ Plugin directories ]",
finder = require("telescope.finders").new_table({
results = utils.get_os_command_output(opts.cmd),
entry_maker = opts.entry_maker,
}),
sorter = require("telescope.sorters").get_fuzzy_file(),
previewer = require("telescope.previewers.term_previewer").cat.new(opts),
attach_mappings = function(_, map)
map("i", "<cr>", myactions.change_directory)
map("n", "<cr>", myactions.change_directory)
return true
end,
})
:find()
end
-- Custom window-sizes
local horizontal_preview_width = function(_, cols, _)
if cols > 200 then
return math.floor(cols * 0.7)
else
return math.floor(cols * 0.6)
end
end
local width_for_nopreview = function(_, cols, _)
if cols > 200 then
return math.floor(cols * 0.5)
elseif cols > 110 then
return math.floor(cols * 0.6)
else
return math.floor(cols * 0.75)
end
end
local height_dropdown_nopreview = function(_, _, rows)
return math.floor(rows * 0.7)
end
-- Enable indent-guides in telescope preview
vim.cmd([[
augroup telescope_events
autocmd!
autocmd User TelescopePreviewerLoaded setlocal wrap list number
augroup END
]])
-- On-demand setup
local setup = function()
local telescope = require("telescope")
local transform_mod = require("telescope.actions.mt").transform_mod
local actions = require("telescope.actions")
-- Transform to Telescope proper actions.
myactions = transform_mod(myactions)
-- Setup Telescope
-- See telescope.nvim/lua/telescope/config.lua for defaults.
telescope.setup({
defaults = {
sorting_strategy = "ascending",
selection_strategy = "closest",
scroll_strategy = "cycle",
cache_picker = {
num_pickers = 3,
limit_entries = 300,
},
prompt_prefix = "❯ ",
-- ♥ ❥ ➤ 🔭
selection_caret = "▍ ",
multi_icon = "v",
set_env = { COLORTERM = "truecolor" },
-- Flex layout swaps between horizontal and vertical strategies
-- based on the window width. See :h telescope.layout
layout_strategy = "flex",
layout_config = {
width = 0.9,
height = 0.85,
prompt_position = "top",
-- center = {
-- preview_cutoff = 40
-- },
horizontal = {
-- width_padding = 0.1,
-- height_padding = 0.1,
-- preview_cutoff = 60,
preview_width = horizontal_preview_width,
},
vertical = {
-- width_padding = 0.05,
-- height_padding = 1,
width = 0.75,
height = 0.85,
preview_height = 0.4,
mirror = true,
},
flex = {
-- change to horizontal after 120 cols
flip_columns = 120,
},
},
mappings = {
i = {
["jj"] = { "<Esc>", type = "command" },
["<Tab>"] = actions.move_selection_next,
["<S-Tab>"] = actions.move_selection_previous,
["<C-u>"] = myactions.page_up,
["<C-d>"] = myactions.page_down,
["<C-q>"] = myactions.smart_send_to_qflist,
-- ['<C-l'] = actions.complete_tag,
["<Down>"] = actions.cycle_history_next,
["<Up>"] = actions.cycle_history_prev,
["<C-n>"] = actions.cycle_history_next,
["<C-p>"] = actions.cycle_history_prev,
["<C-b>"] = actions.preview_scrolling_up,
["<C-f>"] = actions.preview_scrolling_down,
},
n = {
["q"] = actions.close,
["<Esc>"] = actions.close,
["<Tab>"] = actions.move_selection_next,
["<S-Tab>"] = actions.move_selection_previous,
["<C-u>"] = myactions.page_up,
["<C-d>"] = myactions.page_down,
["<C-b>"] = actions.preview_scrolling_up,
["<C-f>"] = actions.preview_scrolling_down,
["<C-n>"] = actions.cycle_history_next,
["<C-p>"] = actions.cycle_history_prev,
["*"] = actions.toggle_all,
["u"] = actions.drop_all,
["J"] = actions.toggle_selection + actions.move_selection_next,
["K"] = actions.toggle_selection + actions.move_selection_previous,
["<Space>"] = {
actions.toggle_selection + actions.move_selection_next,
type = "action",
opts = { nowait = true },
},
["gg"] = actions.move_to_top,
["G"] = actions.move_to_bottom,
["sv"] = actions.select_horizontal,
["sg"] = actions.select_vertical,
["st"] = actions.select_tab,
["l"] = actions.select_default,
["w"] = myactions.smart_send_to_qflist,
["e"] = myactions.send_to_qflist,
["!"] = actions.edit_command_line,
},
},
},
pickers = {
buffers = {
theme = "dropdown",
previewer = false,
sort_lastused = true,
sort_mru = true,
show_all_buffers = true,
ignore_current_buffer = true,
path_display = { shorten = 5 },
layout_config = {
width = width_for_nopreview,
height = height_dropdown_nopreview,
},
mappings = {
n = {
["dd"] = actions.delete_buffer,
},
},
},
find_files = {
theme = "dropdown",
previewer = false,
layout_config = {
width = width_for_nopreview,
height = height_dropdown_nopreview,
},
find_command = {
"rg",
"--smart-case",
"--hidden",
"--no-ignore-vcs",
"--glob",
"!.git",
"--files",
},
},
live_grep = {
dynamic_preview_title = true,
},
colorscheme = {
enable_preview = true,
-- previewer = false,
-- theme = 'dropdown',
layout_config = { width = 0.45, height = 0.8 },
},
highlights = {
layout_strategy = "horizontal",
layout_config = { preview_width = 0.80 },
},
-- jumplist = {
-- layout_strategy = 'horizontal',
-- layout_config = { preview_width = 0.60 },
-- },
vim_options = {
theme = "dropdown",
previewer = false,
layout_config = { width = 0.6, height = 0.7 },
},
command_history = {
theme = "dropdown",
previewer = false,
layout_config = { width = 0.5, height = 0.7 },
},
search_history = {
theme = "dropdown",
layout_config = { width = 0.4, height = 0.6 },
},
spell_suggest = {
theme = "cursor",
layout_config = { width = 0.27, height = 0.45 },
},
registers = {
theme = "cursor",
previewer = false,
layout_config = { width = 0.45, height = 0.6 },
},
oldfiles = {
theme = "dropdown",
previewer = false,
path_display = { shorten = 5 },
layout_config = {
width = width_for_nopreview,
height = height_dropdown_nopreview,
},
},
lsp_definitions = {
layout_strategy = "horizontal",
layout_config = { width = 0.95, height = 0.85, preview_width = 0.45 },
},
lsp_implementations = {
layout_strategy = "horizontal",
layout_config = { width = 0.95, height = 0.85, preview_width = 0.45 },
},
lsp_references = {
layout_strategy = "horizontal",
layout_config = { width = 0.95, height = 0.85, preview_width = 0.45 },
},
lsp_code_actions = {
theme = "cursor",
previewer = false,
layout_config = { width = 0.3, height = 0.4 },
},
lsp_range_code_actions = {
theme = "cursor",
previewer = false,
layout_config = { width = 0.3, height = 0.4 },
},
},
extensions = {
["ui-select"] = {
require("telescope.themes").get_cursor({
layout_config = { width = 0.35, height = 0.35 },
}),
},
fzf = {
fuzzy = true, -- false will only do exact matching
override_generic_sorter = true, -- override the generic sorter
override_file_sorter = true, -- override the file sorter
case_mode = "smart_case", -- or "ignore_case" or "respect_case"
-- the default case_mode is "smart_case"
},
},
})
-- Telescope extensions are loaded in each plugin.
end
-- Public functions
return {
setup = setup,
pickers = pickers,
}