-
-
Notifications
You must be signed in to change notification settings - Fork 855
/
__internal.lua
1369 lines (1224 loc) · 40 KB
/
__internal.lua
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local actions = require "telescope.actions"
local action_set = require "telescope.actions.set"
local action_state = require "telescope.actions.state"
local finders = require "telescope.finders"
local make_entry = require "telescope.make_entry"
local Path = require "plenary.path"
local pickers = require "telescope.pickers"
local previewers = require "telescope.previewers"
local p_window = require "telescope.pickers.window"
local sorters = require "telescope.sorters"
local state = require "telescope.state"
local utils = require "telescope.utils"
local conf = require("telescope.config").values
local filter = vim.tbl_filter
-- Makes sure aliased options are set correctly
local function apply_cwd_only_aliases(opts)
local has_cwd_only = opts.cwd_only ~= nil
local has_only_cwd = opts.only_cwd ~= nil
if has_only_cwd and not has_cwd_only then
-- Internally, use cwd_only
opts.cwd_only = opts.only_cwd
opts.only_cwd = nil
end
return opts
end
local internal = {}
internal.builtin = function(opts)
opts.include_extensions = vim.F.if_nil(opts.include_extensions, false)
opts.use_default_opts = vim.F.if_nil(opts.use_default_opts, false)
local objs = {}
for k, v in pairs(require "telescope.builtin") do
local debug_info = debug.getinfo(v)
table.insert(objs, {
filename = string.sub(debug_info.source, 2),
text = k,
})
end
local title = "Telescope Builtin"
if opts.include_extensions then
title = "Telescope Pickers"
for ext, funcs in pairs(require("telescope").extensions) do
for func_name, func_obj in pairs(funcs) do
-- Only include exported functions whose name doesn't begin with an underscore
if type(func_obj) == "function" and string.sub(func_name, 0, 1) ~= "_" then
local debug_info = debug.getinfo(func_obj)
table.insert(objs, {
filename = string.sub(debug_info.source, 2),
text = string.format("%s : %s", ext, func_name),
})
end
end
end
end
opts.bufnr = vim.api.nvim_get_current_buf()
opts.winnr = vim.api.nvim_get_current_win()
pickers
.new(opts, {
prompt_title = title,
finder = finders.new_table {
results = objs,
entry_maker = function(entry)
return make_entry.set_default_entry_mt({
value = entry,
text = entry.text,
display = entry.text,
ordinal = entry.text,
filename = entry.filename,
}, opts)
end,
},
previewer = previewers.builtin.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(_)
actions.select_default:replace(function(_)
local selection = action_state.get_selected_entry()
if not selection then
utils.__warn_no_selection "builtin.builtin"
return
end
-- we do this to avoid any surprises
opts.include_extensions = nil
local picker_opts
if not opts.use_default_opts then
picker_opts = opts
end
if string.match(selection.text, " : ") then
-- Call appropriate function from extensions
local split_string = vim.split(selection.text, " : ")
local ext = split_string[1]
local func = split_string[2]
require("telescope").extensions[ext][func](picker_opts)
else
-- Call appropriate telescope builtin
require("telescope.builtin")[selection.text](picker_opts)
end
end)
return true
end,
})
:find()
end
internal.resume = function(opts)
opts = opts or {}
opts.cache_index = vim.F.if_nil(opts.cache_index, 1)
local cached_pickers = state.get_global_key "cached_pickers"
if cached_pickers == nil or vim.tbl_isempty(cached_pickers) then
utils.notify("builtin.resume", {
msg = "No cached picker(s).",
level = "INFO",
})
return
end
local picker = cached_pickers[opts.cache_index]
if picker == nil then
utils.notify("builtin.resume", {
msg = string.format("Index too large as there are only '%s' pickers cached", #cached_pickers),
level = "ERROR",
})
return
end
-- reset layout strategy and get_window_options if default as only one is valid
-- and otherwise unclear which was actually set
if picker.layout_strategy == conf.layout_strategy then
picker.layout_strategy = nil
end
if picker.get_window_options == p_window.get_window_options then
picker.get_window_options = nil
end
picker.cache_picker.index = opts.cache_index
-- avoid partial `opts.cache_picker` at picker creation
if opts.cache_picker ~= false then
picker.cache_picker = vim.tbl_extend("keep", opts.cache_picker or {}, picker.cache_picker)
else
picker.cache_picker.disabled = true
end
opts.cache_picker = nil
picker.previewer = picker.all_previewers
if picker.hidden_previewer then
picker.hidden_previewer = nil
opts.previewer = vim.F.if_nil(opts.previewer, false)
end
pickers.new(opts, picker):find()
end
internal.pickers = function(opts)
local cached_pickers = state.get_global_key "cached_pickers"
if cached_pickers == nil or vim.tbl_isempty(cached_pickers) then
utils.notify("builtin.pickers", {
msg = "No cached picker(s).",
level = "INFO",
})
return
end
opts = opts or {}
-- clear cache picker for immediate pickers.new and pass option to resumed picker
if opts.cache_picker ~= nil then
opts._cache_picker = opts.cache_picker
opts.cache_picker = nil
end
pickers
.new(opts, {
prompt_title = "Pickers",
finder = finders.new_table {
results = cached_pickers,
entry_maker = make_entry.gen_from_picker(opts),
},
previewer = previewers.pickers.new(opts),
sorter = conf.generic_sorter(opts),
cache_picker = false,
attach_mappings = function(_, map)
actions.select_default:replace(function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
local selection_index = current_picker:get_index(current_picker:get_selection_row())
actions.close(prompt_bufnr)
opts.cache_picker = opts._cache_picker
opts["cache_index"] = selection_index
opts["initial_mode"] = cached_pickers[selection_index].initial_mode
internal.resume(opts)
end)
map("i", "<C-x>", actions.remove_selected_picker)
map("n", "<C-x>", actions.remove_selected_picker)
return true
end,
})
:find()
end
internal.planets = function(opts)
local show_pluto = opts.show_pluto or false
local sourced_file = require("plenary.debug_utils").sourced_filepath()
local base_directory = vim.fn.fnamemodify(sourced_file, ":h:h:h:h")
local globbed_files = vim.fn.globpath(base_directory .. "/data/memes/planets/", "*", true, true)
local acceptable_files = {}
for _, v in ipairs(globbed_files) do
if show_pluto or not v:find "pluto" then
table.insert(acceptable_files, vim.fn.fnamemodify(v, ":t"))
end
end
pickers
.new(opts, {
prompt_title = "Planets",
finder = finders.new_table {
results = acceptable_files,
entry_maker = function(line)
return make_entry.set_default_entry_mt({
ordinal = line,
display = line,
filename = base_directory .. "/data/memes/planets/" .. line,
}, opts)
end,
},
previewer = previewers.cat.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.planets"
return
end
actions.close(prompt_bufnr)
print("Enjoy astronomy! You viewed:", selection.display)
end)
return true
end,
})
:find()
end
internal.symbols = function(opts)
local initial_mode = vim.fn.mode()
local files = vim.api.nvim_get_runtime_file("data/telescope-sources/*.json", true)
local data_path = (function()
if not opts.symbol_path then
return Path:new { vim.fn.stdpath "data", "telescope", "symbols" }
else
return Path:new { opts.symbol_path }
end
end)()
if data_path:exists() then
for _, v in ipairs(require("plenary.scandir").scan_dir(data_path:absolute(), { search_pattern = "%.json$" })) do
table.insert(files, v)
end
end
if #files == 0 then
utils.notify("builtin.symbols", {
msg = "No sources found! Check out https://github.com/nvim-telescope/telescope-symbols.nvim "
.. "for some prebuild symbols or how to create you own symbol source.",
level = "ERROR",
})
return
end
local sources = {}
if opts.sources then
for _, v in ipairs(files) do
for _, s in ipairs(opts.sources) do
if v:find(s) then
table.insert(sources, v)
end
end
end
else
sources = files
end
local results = {}
for _, source in ipairs(sources) do
local data = vim.json.decode(Path:new(source):read())
for _, entry in ipairs(data) do
table.insert(results, entry)
end
end
pickers
.new(opts, {
prompt_title = "Symbols",
finder = finders.new_table {
results = results,
entry_maker = function(entry)
return make_entry.set_default_entry_mt({
value = entry,
ordinal = entry[1] .. " " .. entry[2],
display = entry[1] .. " " .. entry[2],
}, opts)
end,
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_)
if initial_mode == "i" then
actions.select_default:replace(actions.insert_symbol_i)
else
actions.select_default:replace(actions.insert_symbol)
end
return true
end,
})
:find()
end
internal.commands = function(opts)
pickers
.new(opts, {
prompt_title = "Commands",
finder = finders.new_table {
results = (function()
local command_iter = vim.api.nvim_get_commands {}
local commands = {}
for _, cmd in pairs(command_iter) do
table.insert(commands, cmd)
end
local need_buf_command = vim.F.if_nil(opts.show_buf_command, true)
if need_buf_command then
local buf_command_iter = vim.api.nvim_buf_get_commands(0, {})
buf_command_iter[true] = nil -- remove the redundant entry
for _, cmd in pairs(buf_command_iter) do
table.insert(commands, cmd)
end
end
return commands
end)(),
entry_maker = opts.entry_maker or make_entry.gen_from_commands(opts),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.commands"
return
end
actions.close(prompt_bufnr)
local val = selection.value
local cmd = string.format([[:%s ]], val.name)
if val.nargs == "0" then
vim.cmd(cmd)
else
vim.cmd [[stopinsert]]
vim.fn.feedkeys(cmd, "n")
end
end)
return true
end,
})
:find()
end
internal.quickfix = function(opts)
local qf_identifier = opts.id or vim.F.if_nil(opts.nr, "$")
local locations = vim.fn.getqflist({ [opts.id and "id" or "nr"] = qf_identifier, items = true }).items
if vim.tbl_isempty(locations) then
return
end
pickers
.new(opts, {
prompt_title = "Quickfix",
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
},
previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter(opts),
})
:find()
end
internal.quickfixhistory = function(opts)
local qflists = {}
for i = 1, 10 do -- (n)vim keeps at most 10 quickfix lists in full
-- qf weirdness: id = 0 gets id of quickfix list nr
local qflist = vim.fn.getqflist { nr = i, id = 0, title = true, items = true }
if not vim.tbl_isempty(qflist.items) then
table.insert(qflists, qflist)
end
end
local entry_maker = opts.make_entry
or function(entry)
return make_entry.set_default_entry_mt({
value = entry.title or "Untitled",
ordinal = entry.title or "Untitled",
display = entry.title or "Untitled",
nr = entry.nr,
id = entry.id,
items = entry.items,
}, opts)
end
local qf_entry_maker = make_entry.gen_from_quickfix(opts)
pickers
.new(opts, {
prompt_title = "Quickfix History",
finder = finders.new_table {
results = qflists,
entry_maker = entry_maker,
},
previewer = previewers.new_buffer_previewer {
title = "Quickfix List Preview",
dyn_title = function(_, entry)
return entry.title
end,
get_buffer_by_name = function(_, entry)
return "quickfixlist_" .. tostring(entry.nr)
end,
define_preview = function(self, entry)
if self.state.bufname then
return
end
local entries = vim.tbl_map(function(i)
return qf_entry_maker(i):display()
end, entry.items)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, entries)
end,
},
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, _)
action_set.select:replace(function(prompt_bufnr)
local nr = action_state.get_selected_entry().nr
actions.close(prompt_bufnr)
internal.quickfix { nr = nr }
end)
return true
end,
})
:find()
end
internal.loclist = function(opts)
local locations = vim.fn.getloclist(0)
local filenames = {}
for _, value in pairs(locations) do
local bufnr = value.bufnr
if filenames[bufnr] == nil then
filenames[bufnr] = vim.api.nvim_buf_get_name(bufnr)
end
value.filename = filenames[bufnr]
end
if vim.tbl_isempty(locations) then
return
end
pickers
.new(opts, {
prompt_title = "Loclist",
finder = finders.new_table {
results = locations,
entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts),
},
previewer = conf.qflist_previewer(opts),
sorter = conf.generic_sorter(opts),
})
:find()
end
internal.oldfiles = function(opts)
opts = apply_cwd_only_aliases(opts)
opts.include_current_session = vim.F.if_nil(opts.include_current_session, true)
local current_buffer = vim.api.nvim_get_current_buf()
local current_file = vim.api.nvim_buf_get_name(current_buffer)
local results = {}
if opts.include_current_session then
for _, buffer in ipairs(vim.split(vim.fn.execute ":buffers! t", "\n")) do
local match = tonumber(string.match(buffer, "%s*(%d+)"))
local open_by_lsp = string.match(buffer, "line 0$")
if match and not open_by_lsp then
local file = vim.api.nvim_buf_get_name(match)
if vim.loop.fs_stat(file) and match ~= current_buffer then
table.insert(results, file)
end
end
end
end
for _, file in ipairs(vim.v.oldfiles) do
if vim.loop.fs_stat(file) and not vim.tbl_contains(results, file) and file ~= current_file then
table.insert(results, file)
end
end
if opts.cwd_only then
local cwd = vim.loop.cwd()
cwd = cwd:gsub([[\]], [[\\]])
results = vim.tbl_filter(function(file)
return vim.fn.matchstrpos(file, cwd)[2] ~= -1
end, results)
end
pickers
.new(opts, {
prompt_title = "Oldfiles",
finder = finders.new_table {
results = results,
entry_maker = opts.entry_maker or make_entry.gen_from_file(opts),
},
sorter = conf.file_sorter(opts),
previewer = conf.file_previewer(opts),
})
:find()
end
internal.command_history = function(opts)
local history_string = vim.fn.execute "history cmd"
local history_list = vim.split(history_string, "\n")
local results = {}
for i = #history_list, 3, -1 do
local item = history_list[i]
local _, finish = string.find(item, "%d+ +")
table.insert(results, string.sub(item, finish + 1))
end
pickers
.new(opts, {
prompt_title = "Command History",
finder = finders.new_table(results),
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, map)
map("i", "<CR>", actions.set_command_line)
map("n", "<CR>", actions.set_command_line)
map("n", "<C-e>", actions.edit_command_line)
map("i", "<C-e>", actions.edit_command_line)
-- TODO: Find a way to insert the text... it seems hard.
-- map('i', '<C-i>', actions.insert_value, { expr = true })
return true
end,
})
:find()
end
internal.search_history = function(opts)
local search_string = vim.fn.execute "history search"
local search_list = vim.split(search_string, "\n")
local results = {}
for i = #search_list, 3, -1 do
local item = search_list[i]
local _, finish = string.find(item, "%d+ +")
table.insert(results, string.sub(item, finish + 1))
end
pickers
.new(opts, {
prompt_title = "Search History",
finder = finders.new_table(results),
sorter = conf.generic_sorter(opts),
attach_mappings = function(_, map)
map("i", "<CR>", actions.set_search_line)
map("n", "<CR>", actions.set_search_line)
map("n", "<C-e>", actions.edit_search_line)
map("i", "<C-e>", actions.edit_search_line)
-- TODO: Find a way to insert the text... it seems hard.
-- map('i', '<C-i>', actions.insert_value, { expr = true })
return true
end,
})
:find()
end
internal.vim_options = function(opts)
local res = {}
for _, v in pairs(vim.api.nvim_get_all_options_info()) do
table.insert(res, v)
end
table.sort(res, function(left, right)
return left.name < right.name
end)
pickers
.new(opts, {
prompt_title = "options",
finder = finders.new_table {
results = res,
entry_maker = opts.entry_maker or make_entry.gen_from_vimoptions(opts),
},
sorter = conf.generic_sorter(opts),
attach_mappings = function()
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.vim_options"
return
end
local esc = ""
if vim.fn.mode() == "i" then
esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
end
vim.api.nvim_feedkeys(
string.format("%s:set %s=%s", esc, selection.value.name, selection.value.value),
"m",
true
)
end)
return true
end,
})
:find()
end
internal.help_tags = function(opts)
opts.lang = vim.F.if_nil(opts.lang, vim.o.helplang)
opts.fallback = vim.F.if_nil(opts.fallback, true)
opts.file_ignore_patterns = {}
local langs = vim.split(opts.lang, ",", true)
if opts.fallback and not vim.tbl_contains(langs, "en") then
table.insert(langs, "en")
end
local langs_map = {}
for _, lang in ipairs(langs) do
langs_map[lang] = true
end
local tag_files = {}
local function add_tag_file(lang, file)
if langs_map[lang] then
if tag_files[lang] then
table.insert(tag_files[lang], file)
else
tag_files[lang] = { file }
end
end
end
local help_files = {}
local all_files = vim.api.nvim_get_runtime_file("doc/*", true)
for _, fullpath in ipairs(all_files) do
local file = utils.path_tail(fullpath)
if file == "tags" then
add_tag_file("en", fullpath)
elseif file:match "^tags%-..$" then
local lang = file:sub(-2)
add_tag_file(lang, fullpath)
else
help_files[file] = fullpath
end
end
local tags = {}
local tags_map = {}
local delimiter = string.char(9)
for _, lang in ipairs(langs) do
for _, file in ipairs(tag_files[lang] or {}) do
local lines = vim.split(Path:new(file):read(), "\n", true)
for _, line in ipairs(lines) do
-- TODO: also ignore tagComment starting with ';'
if not line:match "^!_TAG_" then
local fields = vim.split(line, delimiter, true)
if #fields == 3 and not tags_map[fields[1]] then
if fields[1] ~= "help-tags" or fields[2] ~= "tags" then
table.insert(tags, {
name = fields[1],
filename = help_files[fields[2]],
cmd = fields[3],
lang = lang,
})
tags_map[fields[1]] = true
end
end
end
end
end
end
pickers
.new(opts, {
prompt_title = "Help",
finder = finders.new_table {
results = tags,
entry_maker = function(entry)
return make_entry.set_default_entry_mt({
value = entry.name .. "@" .. entry.lang,
display = entry.name,
ordinal = entry.name,
filename = entry.filename,
cmd = entry.cmd,
}, opts)
end,
},
previewer = previewers.help.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
action_set.select:replace(function(_, cmd)
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.help_tags"
return
end
actions.close(prompt_bufnr)
if cmd == "default" or cmd == "horizontal" then
vim.cmd("help " .. selection.value)
elseif cmd == "vertical" then
vim.cmd("vert help " .. selection.value)
elseif cmd == "tab" then
vim.cmd("tab help " .. selection.value)
end
end)
return true
end,
})
:find()
end
internal.man_pages = function(opts)
opts.sections = vim.F.if_nil(opts.sections, { "1" })
assert(vim.tbl_islist(opts.sections), "sections should be a list")
opts.man_cmd = utils.get_lazy_default(opts.man_cmd, function()
local is_darwin = vim.loop.os_uname().sysname == "Darwin"
return is_darwin and { "apropos", " " } or { "apropos", "" }
end)
opts.entry_maker = opts.entry_maker or make_entry.gen_from_apropos(opts)
opts.env = { PATH = vim.env.PATH, MANPATH = vim.env.MANPATH }
pickers
.new(opts, {
prompt_title = "Man",
finder = finders.new_oneshot_job(opts.man_cmd, opts),
previewer = previewers.man.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
action_set.select:replace(function(_, cmd)
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.man_pages"
return
end
local args = selection.section .. " " .. selection.value
actions.close(prompt_bufnr)
if cmd == "default" or cmd == "horizontal" then
vim.cmd("Man " .. args)
elseif cmd == "vertical" then
vim.cmd("vert Man " .. args)
elseif cmd == "tab" then
vim.cmd("tab Man " .. args)
end
end)
return true
end,
})
:find()
end
internal.reloader = function(opts)
local package_list = vim.tbl_keys(package.loaded)
-- filter out packages we don't want and track the longest package name
local column_len = 0
for index, module_name in pairs(package_list) do
if
type(require(module_name)) ~= "table"
or module_name:sub(1, 1) == "_"
or package.searchpath(module_name, package.path) == nil
then
table.remove(package_list, index)
elseif #module_name > column_len then
column_len = #module_name
end
end
opts.column_len = vim.F.if_nil(opts.column_len, column_len)
pickers
.new(opts, {
prompt_title = "Packages",
finder = finders.new_table {
results = package_list,
entry_maker = opts.entry_maker or make_entry.gen_from_packages(opts),
},
-- previewer = previewers.vim_buffer.new(opts),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.reloader"
return
end
actions.close(prompt_bufnr)
require("plenary.reload").reload_module(selection.value)
utils.notify("builtin.reloader", {
msg = string.format("[%s] - module reloaded", selection.value),
level = "INFO",
})
end)
return true
end,
})
:find()
end
internal.buffers = function(opts)
opts = apply_cwd_only_aliases(opts)
local bufnrs = filter(function(b)
if 1 ~= vim.fn.buflisted(b) then
return false
end
-- only hide unloaded buffers if opts.show_all_buffers is false, keep them listed if true or nil
if opts.show_all_buffers == false and not vim.api.nvim_buf_is_loaded(b) then
return false
end
if opts.ignore_current_buffer and b == vim.api.nvim_get_current_buf() then
return false
end
if opts.cwd_only and not string.find(vim.api.nvim_buf_get_name(b), vim.loop.cwd(), 1, true) then
return false
end
return true
end, vim.api.nvim_list_bufs())
if not next(bufnrs) then
return
end
if opts.sort_mru then
table.sort(bufnrs, function(a, b)
return vim.fn.getbufinfo(a)[1].lastused > vim.fn.getbufinfo(b)[1].lastused
end)
end
local buffers = {}
local default_selection_idx = 1
for _, bufnr in ipairs(bufnrs) do
local flag = bufnr == vim.fn.bufnr "" and "%" or (bufnr == vim.fn.bufnr "#" and "#" or " ")
if opts.sort_lastused and not opts.ignore_current_buffer and flag == "#" then
default_selection_idx = 2
end
local element = {
bufnr = bufnr,
flag = flag,
info = vim.fn.getbufinfo(bufnr)[1],
}
if opts.sort_lastused and (flag == "#" or flag == "%") then
local idx = ((buffers[1] ~= nil and buffers[1].flag == "%") and 2 or 1)
table.insert(buffers, idx, element)
else
table.insert(buffers, element)
end
end
if not opts.bufnr_width then
local max_bufnr = math.max(unpack(bufnrs))
opts.bufnr_width = #tostring(max_bufnr)
end
pickers
.new(opts, {
prompt_title = "Buffers",
finder = finders.new_table {
results = buffers,
entry_maker = opts.entry_maker or make_entry.gen_from_buffer(opts),
},
previewer = conf.grep_previewer(opts),
sorter = conf.generic_sorter(opts),
default_selection_index = default_selection_idx,
})
:find()
end
internal.colorscheme = function(opts)
local before_background = vim.o.background
local before_color = vim.api.nvim_exec("colorscheme", true)
local need_restore = true
local colors = opts.colors or { before_color }
if not vim.tbl_contains(colors, before_color) then
table.insert(colors, 1, before_color)
end
colors = vim.list_extend(
colors,
vim.tbl_filter(function(color)
return color ~= before_color
end, vim.fn.getcompletion("", "color"))
)
local previewer
if opts.enable_preview then
-- define previewer
local bufnr = vim.api.nvim_get_current_buf()
local p = vim.api.nvim_buf_get_name(bufnr)
-- don't need previewer
if vim.fn.buflisted(bufnr) ~= 1 then
local deleted = false
local function del_win(win_id)
if win_id and vim.api.nvim_win_is_valid(win_id) then
utils.buf_delete(vim.api.nvim_win_get_buf(win_id))
pcall(vim.api.nvim_win_close, win_id, true)
end
end
previewer = previewers.new {
preview_fn = function(_, entry, status)
if not deleted then
deleted = true
del_win(status.preview_win)
del_win(status.preview_border_win)
end
vim.cmd("colorscheme " .. entry.value)
end,
}
else
-- show current buffer content in previewer
previewer = previewers.new_buffer_previewer {
get_buffer_by_name = function()
return p
end,
define_preview = function(self, entry)
if vim.loop.fs_stat(p) then
conf.buffer_previewer_maker(p, self.state.bufnr, { bufname = self.state.bufname })
else
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
end
vim.cmd("colorscheme " .. entry.value)
end,
}
end
end
local picker = pickers.new(opts, {
prompt_title = "Change Colorscheme",
finder = finders.new_table {
results = colors,
},
sorter = conf.generic_sorter(opts),
previewer = previewer,
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "builtin.colorscheme"
return
end
actions.close(prompt_bufnr)
need_restore = false
vim.cmd("colorscheme " .. selection.value)
end)
return true
end,
})
if opts.enable_preview then
-- rewrite picker.close_windows. restore color if needed