-
Notifications
You must be signed in to change notification settings - Fork 42
/
telescope.lua
128 lines (118 loc) · 3.66 KB
/
telescope.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
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local action_utils = require("telescope.actions.utils")
local entry_display = require("telescope.pickers.entry_display")
local previewers = require("telescope.previewers")
local M = {}
M.note_picker_list_api_selection = { "title", "absPath", "path" }
function M.create_note_entry_maker(_)
return function(note)
local title = note.title or note.path
return {
value = note,
path = note.absPath,
display = title,
ordinal = title,
}
end
end
function M.create_tag_entry_maker(opts)
return function(tag)
local displayer = entry_display.create({
separator = " ",
items = {
{ width = opts.note_count_width or 4 },
{ remaining = true },
},
})
local make_display = function(e)
return displayer({
{ e.value.note_count, "TelescopeResultsNumber" },
e.value.name,
})
end
return {
value = tag,
display = make_display,
ordinal = tag.name,
}
end
end
function M.make_note_previewer()
return previewers.new_buffer_previewer({
define_preview = function(self, entry)
conf.buffer_previewer_maker(entry.value.absPath, self.state.bufnr, {
bufname = entry.value.title or entry.value.path,
winid = self.state.winid,
})
end,
})
end
function M.show_note_picker(notes, options, cb)
options = options or {}
local telescope_options = vim.tbl_extend("force", { prompt_title = options.title }, options.telescope or {})
pickers
.new(telescope_options, {
finder = finders.new_table({
results = notes,
entry_maker = M.create_note_entry_maker(options),
}),
sorter = conf.file_sorter(options),
previewer = M.make_note_previewer(),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
if options.multi_select then
local selection = {}
action_utils.map_selections(prompt_bufnr, function(entry, _)
table.insert(selection, entry.value)
end)
if vim.tbl_isempty(selection) then
selection = { action_state.get_selected_entry().value }
end
actions.close(prompt_bufnr)
cb(selection)
else
actions.close(prompt_bufnr)
cb(action_state.get_selected_entry().value)
end
end)
return true
end,
})
:find()
end
function M.show_tag_picker(tags, options, cb)
options = options or {}
local telescope_options = vim.tbl_extend("force", { prompt_title = options.title }, options.telescope or {})
pickers
.new(telescope_options, {
finder = finders.new_table({
results = tags,
entry_maker = M.create_tag_entry_maker(options),
}),
sorter = conf.generic_sorter(options),
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
if options.multi_select then
local selection = {}
action_utils.map_selections(prompt_bufnr, function(entry, _)
table.insert(selection, entry.value)
end)
if vim.tbl_isempty(selection) then
selection = { action_state.get_selected_entry().value }
end
actions.close(prompt_bufnr)
cb(selection)
else
cb(action_state.get_selected_entry().value)
end
end)
return true
end,
})
:find()
end
return M