-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelescope.lua
More file actions
107 lines (96 loc) · 3.11 KB
/
Copy pathtelescope.lua
File metadata and controls
107 lines (96 loc) · 3.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
local picker_util = require("peekstack.util.picker")
local M = {}
---@param primary string
---@param fallback string
---@return string
local function hl(primary, fallback)
if vim.fn.hlexists(primary) == 1 then
return primary
end
return fallback
end
---@param displayer fun(chunks: table): string
---@param item PeekstackPickerExternalItem
---@return string
local function display_entry(displayer, item)
local chunks = {}
if type(item.symbol) == "string" and item.symbol ~= "" then
chunks[#chunks + 1] = { item.symbol, hl("TelescopeResultsIdentifier", "Function") }
chunks[#chunks + 1] = { " - ", hl("TelescopeResultsComment", "Comment") }
end
local path = item.path or item.label
picker_util.append_path_chunks(
chunks,
path,
hl("TelescopeResultsComment", "Comment"),
hl("TelescopeResultsIdentifier", "Directory")
)
if type(item.display_lnum) == "number" and item.display_lnum > 0 then
chunks[#chunks + 1] = { ":", hl("TelescopeResultsComment", "Comment") }
chunks[#chunks + 1] = { tostring(item.display_lnum), hl("TelescopeResultsNumber", "Number") }
end
if type(item.display_col) == "number" and item.display_col > 0 then
chunks[#chunks + 1] = { ":", hl("TelescopeResultsComment", "Comment") }
chunks[#chunks + 1] = { tostring(item.display_col), hl("TelescopeResultsNumber", "Number") }
end
return displayer(chunks)
end
---Pick a location using Telescope
---@param locations PeekstackLocation[]
---@param opts? table
---@param cb fun(location: PeekstackLocation)
function M.pick(locations, opts, cb)
local ok, telescope = pcall(require, "telescope.pickers")
if not ok then
vim.notify("telescope not available", vim.log.levels.WARN)
return
end
local finders = require("telescope.finders")
local entry_display = require("telescope.pickers.entry_display")
local conf = require("telescope.config").values
local telescope_opts = opts or {}
local displayer = entry_display.create({
separator = "",
items = {
{ remaining = true },
},
})
local items = picker_util.build_external_items(locations, 1)
local entries = {}
for _, item in ipairs(items) do
table.insert(entries, {
value = item.value,
display = function()
return display_entry(displayer, item)
end,
ordinal = string.format("%s %s", item.label, item.file or ""),
filename = item.file,
lnum = item.lnum,
col = item.col,
})
end
telescope
.new(telescope_opts, {
prompt_title = "Peekstack",
finder = finders.new_table({
results = entries,
entry_maker = function(entry)
return entry
end,
}),
sorter = conf.generic_sorter(telescope_opts),
previewer = conf.grep_previewer(telescope_opts),
attach_mappings = function(_, map)
map("i", "<CR>", function(bufnr)
local selection = require("telescope.actions.state").get_selected_entry()
require("telescope.actions").close(bufnr)
if selection and selection.value then
cb(selection.value)
end
end)
return true
end,
})
:find()
end
return M