fix: Add first working example

This commit is contained in:
Myzel394 2024-01-17 20:14:54 +01:00
commit 49bda5a50c
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B

View File

@ -0,0 +1,131 @@
local finders = require "telescope.finders"
local make_entry = require "telescope.make_entry"
local Path = require "plenary.path"
local pickers = require "telescope.pickers"
local last_positions = require"smoothcursor.last_positions"
local entry_display = require "telescope.pickers.entry_display"
local utils = require "telescope.utils"
local strings = require "plenary.strings"
local conf = require("telescope.config").values
local DEFAULT_MODE_ICON_MAP = {
v = "",
V = "",
i = "",
R = "󰊄",
}
-- 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
return require"telescope".register_extension {
setup = function() end,
exports = {
last_positions = function(opts)
opts = apply_cwd_only_aliases(opts)
local results = {}
for buffer_raw, last_positions_for_buffer in pairs(last_positions.last_positions) do
local buffer = tonumber(buffer_raw)
local info = vim.fn.getbufinfo(buffer)[1]
for mode, pos in pairs(last_positions_for_buffer) do
local line = pos[1]
local element = {
bufnr = buffer,
info = info,
indicator = DEFAULT_MODE_ICON_MAP[mode] or mode,
lnum = line,
}
table.insert(results, element)
end
end
if not opts.bufnr_width then
local max_bufnr = math.max(
unpack(
vim.tbl_map(
function (buffer) return buffer.bufnr end,
results
)
)
)
opts.bufnr_width = #tostring(max_bufnr)
end
-- Adapted from https://github.com/nvim-telescope/telescope.nvim/blob/da8b3d485975a8727bea127518b65c980521ae22/lua/telescope/make_entry.lua#L574
local disable_devicons = opts.disable_devicons
local icon_width = 0
if not disable_devicons then
local icon, _ = utils.get_devicons("fname", disable_devicons)
icon_width = strings.strdisplaywidth(icon)
end
local displayer = entry_display.create {
separator = " ",
items = {
{ width = opts.bufnr_width },
{ width = 4 },
{ width = icon_width },
{ remaining = true },
},
}
local cwd = vim.fn.expand(opts.cwd or vim.loop.cwd())
local make_display = function(entry)
opts.__prefix = opts.bufnr_width + 4 + icon_width + 3 + 1 + #tostring(entry.lnum)
local display_bufname = utils.transform_path(opts, entry.filename)
local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons)
return displayer {
{ entry.bufnr, "TelescopeResultsNumber" },
{ entry.indicator, "TelescopeResultsComment" },
{ icon, hl_group },
display_bufname .. ":" .. entry.lnum,
}
end
pickers
.new(opts, {
prompt_title = "Last Positions",
finder = finders.new_table {
results = results,
entry_maker = function(entry)
local bufname = entry.info.name ~= "" and entry.info.name or "[No Name]"
-- if bufname is inside the cwd, trim that part of the string
bufname = Path:new(bufname):normalize(cwd)
return make_entry.set_default_entry_mt({
value = bufname,
ordinal = entry.bufnr .. " : " .. bufname,
display = make_display,
bufnr = entry.bufnr,
filename = bufname,
lnum = entry.lnum,
indicator = entry.indicator,
}, opts)
end
},
previewer = conf.grep_previewer(opts),
sorter = conf.generic_sorter(opts),
}):find()
end
}
}