Separate out location and line for alignment

This commit is contained in:
Joao Sa 2022-10-30 00:14:03 +02:00
parent 777cca967a
commit c2410a3723

View File

@ -2,8 +2,14 @@ local orgmode = require('orgmode.api')
local pickers = require("telescope.pickers") local pickers = require("telescope.pickers")
local finders = require("telescope.finders") local finders = require("telescope.finders")
local entry_display = require("telescope.pickers.entry_display")
local conf = require("telescope.config").values local conf = require("telescope.config").values
-- TODO: include headline.level and headline.is_archived() as part of the
-- public orgmode api
-- TODO: add highlight groups
-- TODO: add action to refile/capture
local function get_entries(opts) local function get_entries(opts)
local file_results = vim.tbl_map(function(file) local file_results = vim.tbl_map(function(file)
@ -33,8 +39,6 @@ local function get_entries(opts)
filename = file_entry.filename, filename = file_entry.filename,
headline = headline headline = headline
} }
assert(headline.position.start_line == headline._section.line_number)
table.insert(results, entry) table.insert(results, entry)
end end
end end
@ -46,32 +50,44 @@ end
local function search_headings(opts) local function search_headings(opts)
opts = opts or {} opts = opts or {}
local displayer = entry_display.create({
separator = ' ',
items = {
{ width = vim.F.if_nil(opts.location_width, 20) },
{ remaining = true }
}
})
local function make_display(entry)
return displayer({ entry.location, entry.line })
end
pickers.new(opts, { pickers.new(opts, {
prompt_title = "Search Headings", prompt_title = "Search Headings",
finder = finders.new_table { finder = finders.new_table {
results = get_entries(opts), results = get_entries(opts),
entry_maker = opts.entry_maker or function (entry) entry_maker = opts.entry_maker or function(entry)
local headline = entry.headline local headline = entry.headline
local display = entry.filename
local lnum = nil local lnum = nil
local location = vim.fn.fnamemodify(entry.filename, ':t')
local line = ""
if headline then if headline then
lnum = headline.position.start_line lnum = headline.position.start_line
location = string.format('%s:%i', location, lnum)
local text = string.format('%s %s', string.rep('*', headline._section.level), headline.title) line = string.format('%s %s', string.rep('*', headline._section.level), headline.title)
local line = string.format('%s:%i:%s', entry.filename, lnum, text)
display = line
end end
return { return {
value = display, value = entry,
ordinal = display, ordinal = location .. ' ' .. line,
display = display,
filename = entry.filename, filename = entry.filename,
lnum = lnum lnum = lnum,
display = make_display,
location = location,
line = line
} }
end, end,
}, },
@ -81,7 +97,7 @@ local function search_headings(opts)
end end
return require("telescope").register_extension { return require("telescope").register_extension {
exports = { exports = {
search_headings = search_headings search_headings = search_headings,
}, },
} }