feat: Add some more layout options

This commit is contained in:
TJ DeVries
2020-09-11 00:14:37 -04:00
parent fe387d10db
commit 382c491380
7 changed files with 115 additions and 23 deletions

View File

@@ -30,12 +30,13 @@ function config.set_defaults(defaults)
config.values[name] = get(name, default_val)
end
set("sorting_strategy", "descending")
set("selection_strategy", "reset")
set("layout_strategy", "horizontal")
set("width", 0.75)
set("winblend", 0)
set("prompt_position", "top")
set("prompt_position", "bottom")
set("preview_cutoff", 120)
set("border", {})

View File

@@ -2,7 +2,6 @@ local Job = require('plenary.job')
local make_entry = require('telescope.make_entry')
local log = require('telescope.log')
local utils = require('telescope.utils')
local finders = {}
@@ -64,7 +63,12 @@ function JobFinder:new(opts)
end
function JobFinder:_find(prompt, process_result, process_complete)
START = vim.loop.hrtime()
PERF()
PERF('starting...')
if self.job and not self.job.is_shutdown then
PERF('...had to shutdown')
self.job:shutdown()
end
@@ -76,25 +80,22 @@ function JobFinder:_find(prompt, process_result, process_complete)
end
process_complete()
PERF('Num Lines: ', self._cached_lines)
PERF('...finished static')
COMPLETED = true
return
end
if self.static then
self._cached_lines = {}
end
self.done = false
self._cached_lines = {}
-- TODO: Should consider ways to allow "transformers" to be run here.
-- So that a finder can choose to "transform" the text into something much more easily usable.
local on_output = function(_, line, _)
if not line then
return
end
if vim.trim(line) ~= "" then
line = line:gsub("\n", "")
if line ~= "" then
if self.entry_maker then
line = self.entry_maker(line)
end
@@ -129,6 +130,8 @@ function JobFinder:_find(prompt, process_result, process_complete)
self.done = true
process_complete()
PERF('done')
end,
}
@@ -228,9 +231,10 @@ finders.new_oneshot_job = function(command_list, opts)
return JobFinder:new {
static = true,
entry_maker = opts.entry_maker or make_entry.gen_from_string,
entry_maker = opts.entry_maker or make_entry.gen_from_string(),
cwd = opts.cwd,
maximum_results = opts.maximum_results,
fn_command = function()
return {

View File

@@ -47,4 +47,38 @@ function telescope.setup(opts)
require('telescope.config').set_defaults(opts.defaults)
end
-- Until I have better profiling stuff, this will have to do.
PERF = function(...) end
PERF_DEBUG = PERF_DEBUG or nil
START = nil
if PERF_DEBUG then
PERF = function(...)
local new_time = (vim.loop.hrtime() - START) / 1E9
if select('#', ...) == 0 then
vim.schedule(function()
vim.api.nvim_buf_set_lines(PERF_DEBUG, -1, -1, false, { '' })
end)
return
end
local to_insert = ''
if START then
to_insert = tostring(new_time) .. ' | '
end
for _, v in ipairs({...}) do
if type(v) == 'table' then
to_insert = to_insert .. tostring(#v) .. ' | '
else
to_insert = to_insert .. tostring(v) .. ' | '
end
end
vim.schedule(function()
vim.api.nvim_buf_set_lines(PERF_DEBUG, -1, -1, false, { to_insert })
end)
end
end
return telescope

View File

@@ -11,12 +11,22 @@ make_entry.types = {
local transform_devicons
if has_devicons then
_DeviconStore = _DeviconStore or {}
transform_devicons = function(filename, display, opts)
if opts.disable_devicons then
return display
end
return (devicons.get_icon(filename, string.match(filename, '%a+$')) or ' ') .. ' ' .. display
if _DeviconStore[filename] then
return _DeviconStore[filename]
end
local icon_display = (devicons.get_icon(filename, string.match(filename, '%a+$')) or ' ') .. ' ' .. display
_DeviconStore[filename] = icon_display
return icon_display
end
else
transform_devicons = function(_, display, _)

View File

@@ -100,7 +100,7 @@ function Picker:new(opts)
-- mappings = get_default(opts.mappings, default_mappings),
attach_mappings = opts.attach_mappings,
sorting_strategy = 'ascending',
sorting_strategy = get_default(opts.sorting_strategy, config.values.sorting_strategy),
selection_strategy = get_default(opts.selection_strategy, config.values.selection_strategy),
layout_strategy = get_default(opts.layout_strategy, config.values.layout_strategy),
@@ -198,7 +198,6 @@ end
function Picker:get_reset_row()
if self.sorting_strategy == 'ascending' then
log.info("Setting reset row:", 1)
return 1
else
return self.max_results
@@ -214,7 +213,6 @@ function Picker:clear_extra_rows(results_bufnr)
return
end
log.info("start", num_results + 1, "end", self.max_results)
vim.api.nvim_buf_set_lines(results_bufnr, num_results + 1, self.max_results, false, {})
else
local worst_line = self:get_row(self.manager:num_results())
@@ -233,7 +231,7 @@ function Picker:can_select_row(row)
if self.sorting_strategy == 'ascending' then
return row <= self.manager:num_results()
else
return row >= self.manager:num_results()
return row <= self.max_results and row >= self.max_results - self.manager:num_results()
end
end
@@ -303,6 +301,10 @@ function Picker:find()
local prompt = vim.api.nvim_buf_get_lines(prompt_bufnr, first_line, last_line, false)[1]
local filtered_amount = 0
local displayed_amount = 0
local displayed_fn_amount = 0
self.manager = pickers.entry_manager(
self.max_results,
vim.schedule_wrap(function(index, entry)
@@ -316,6 +318,7 @@ function Picker:find()
local display
if type(entry.display) == 'function' then
displayed_fn_amount = displayed_fn_amount + 1
display = entry:display()
elseif type(entry.display) == 'string' then
display = entry.display
@@ -329,6 +332,8 @@ function Picker:find()
-- until then, insert two spaces
display = ' ' .. display
displayed_amount = displayed_amount + 1
-- log.info("Setting row", row, "with value", entry)
local set_ok = pcall(vim.api.nvim_buf_set_lines, results_bufnr, row, row + 1, false, {display})
@@ -361,6 +366,7 @@ function Picker:find()
end
if sort_score == -1 then
filtered_amount = filtered_amount + 1
log.trace("Filtering out result: ", entry)
return
end
@@ -390,6 +396,10 @@ function Picker:find()
end
self:clear_extra_rows(results_bufnr)
PERF("Filtered Amount ", filtered_amount)
PERF("Displayed Amount ", displayed_amount)
PERF("Displayed FN Amount", displayed_fn_amount)
end)
local ok, msg = pcall(function()
@@ -535,13 +545,8 @@ function Picker:set_selection(row)
row = 1
end
-- TODO: Move max results and row and entry management into an overridable funciton.
-- I have this same thing copied all over the place (and it's not good).
-- Particularly if we're going to do something like make it possible to sort
-- top to bottom, rather than bottom to top.
-- TODO: Is this the right logic here?
if not self:can_select_row(row) then
log.info("Cannot select row:", row, self.manager:num_results(), self.max_results)
return
end

View File

@@ -148,6 +148,7 @@ layout_strategies.vertical = function(self, max_columns, max_lines, prompt_title
end
layout_strategies.flex = function(self, max_columns, max_lines, prompt_title)
-- TODO: Make a config option for this that makes sense.
if max_columns < 100 and max_lines > 20 then
return layout_strategies.vertical(self, max_columns, max_lines, prompt_title)
else

View File

@@ -0,0 +1,37 @@
RELOAD('plenary')
RELOAD('telescope')
local finders = require('telescope.finders')
local make_entry = require('telescope.make_entry')
local previewers = require('telescope.previewers')
local pickers = require('telescope.pickers')
local sorters = require('telescope.sorters')
PERF_DEBUG = 182
vim.api.nvim_buf_set_lines(PERF_DEBUG, 0, -1, false, {})
local cwd = vim.fn.expand("~/build/neovim")
pickers.new {
prompt = 'Large search',
finder = finders.new_oneshot_job(
{"fdfind"},
{
cwd = cwd,
entry_maker = make_entry.gen_from_file {cwd = cwd},
-- disable_devicons = true,
-- maximum_results = 1000,
}
),
sorter = sorters.get_fuzzy_file(),
previewer = previewers.cat.new{cwd = cwd},
}:find()
COMPLETED = false
-- vim.wait(3000, function()
-- vim.cmd [[redraw!]]
-- return COMPLETED
-- end, 100)
-- vim.cmd [[bd!]]
-- vim.cmd [[stopinsert]]