feat: Buffers rework (indicators and sorting) (#208)

This commit is contained in:
Simon Hauser
2020-11-23 16:11:46 +01:00
committed by GitHub
parent 2ac0582c06
commit 863328a96d
6 changed files with 93 additions and 75 deletions

View File

@@ -478,14 +478,15 @@ This section is an overview of how custom pickers can be created any configured.
```lua ```lua
-- lua/telescope/pickers.lua -- lua/telescope/pickers.lua
Picker:new{ Picker:new{
prompt_title = "", -- REQUIRED prompt_title = "", -- REQUIRED
finder = FUNCTION, -- see lua/telescope/finder.lua finder = FUNCTION, -- see lua/telescope/finder.lua
sorter = FUNCTION, -- see lua/telescope/sorter.lua sorter = FUNCTION, -- see lua/telescope/sorter.lua
previewer = FUNCTION, -- see lua/telescope/previewer.lua previewer = FUNCTION, -- see lua/telescope/previewer.lua
selection_strategy = "reset", -- follow, reset, row selection_strategy = "reset", -- follow, reset, row
border = {}, border = {},
borderchars = {"─", "│", "─", "│", "┌", "┐", "┘", "└"}, borderchars = {"─", "│", "─", "│", "┌", "┐", "┘", "└"},
preview_cutoff = 120, preview_cutoff = 120,
default_selection_index = 1, -- Change the index of the initial selection row
} }
``` ```

View File

@@ -96,7 +96,15 @@ function actions._goto_file_selection(prompt_bufnr, command)
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
if entry_bufnr then if entry_bufnr then
vim.cmd(string.format(":%s #%d", command, entry_bufnr)) if command == 'edit' then
vim.cmd(string.format(":buffer %d", entry_bufnr))
elseif command == 'new' then
vim.cmd(string.format(":sbuffer %d", entry_bufnr))
elseif command == 'vnew' then
vim.cmd(string.format(":vert sbuffer %d", entry_bufnr))
elseif command == 'tabedit' then
vim.cmd(string.format(":tab sb %d", entry_bufnr))
end
else else
filename = path.normalize(filename, vim.fn.getcwd()) filename = path.normalize(filename, vim.fn.getcwd())

View File

@@ -611,16 +611,38 @@ builtin.fd = builtin.find_files
builtin.buffers = function(opts) builtin.buffers = function(opts)
opts = opts or {} opts = opts or {}
local buffers = filter(function(b) local bufnrs = filter(function(b)
return return
(opts.show_all_buffers (opts.show_all_buffers
or vim.api.nvim_buf_is_loaded(b)) or vim.api.nvim_buf_is_loaded(b))
and 1 == vim.fn.buflisted(b) and 1 == vim.fn.buflisted(b)
end, vim.api.nvim_list_bufs()) end, vim.api.nvim_list_bufs())
local buffers = {}
local default_selection_idx = 1
for _, bufnr in ipairs(bufnrs) do
local flag = bufnr == vim.fn.bufnr('') and '%' or (bufnr == vim.fn.bufnr('#') and '#' or ' ')
if opts.sort_lastused and flag == "#" then
default_selection_idx = 2
end
local element = {
bufnr = bufnr,
flag = flag,
info = vim.fn.getbufinfo(bufnr)[1],
}
if opts.sort_lastused and (flag == "#" or flag == "%") then
local idx = ((buffers[1] ~= nil and buffers[1].flag == "%") and 2 or 1)
table.insert(buffers, idx, element)
else
table.insert(buffers, element)
end
end
if not opts.bufnr_width then if not opts.bufnr_width then
local max_bufnr = math.max(unpack(buffers)) local max_bufnr = math.max(unpack(bufnrs))
opts.bufnr_width = #tostring(max_bufnr) opts.bufnr_width = #tostring(max_bufnr)
end end
@@ -630,9 +652,9 @@ builtin.buffers = function(opts)
results = buffers, results = buffers,
entry_maker = make_entry.gen_from_buffer(opts) entry_maker = make_entry.gen_from_buffer(opts)
}, },
-- previewer = previewers.vim_buffer.new(opts), previewer = previewers.vim_buffer.new(opts),
previewer = previewers.vimgrep.new(opts),
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
default_selection_index = default_selection_idx,
}):find() }):find()
end end

View File

@@ -283,19 +283,17 @@ end
function make_entry.gen_from_buffer(opts) function make_entry.gen_from_buffer(opts)
opts = opts or {} opts = opts or {}
local displayer = entry_display.create {
separator = " ",
items = {
{ width = opts.bufnr_width },
{ width = 4 },
{ remaining = true },
},
}
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
local get_position = function(entry)
local tabpage_wins = vim.api.nvim_tabpage_list_wins(0)
for k, v in ipairs(tabpage_wins) do
if entry == vim.api.nvim_win_get_buf(v) then
return vim.api.nvim_win_get_cursor(v)
end
end
return {}
end
local make_display = function(entry) local make_display = function(entry)
local display_bufname local display_bufname
if opts.shorten_path then if opts.shorten_path then
@@ -304,33 +302,35 @@ function make_entry.gen_from_buffer(opts)
display_bufname = entry.filename display_bufname = entry.filename
end end
return string.format("%" .. opts.bufnr_width .. "d : %s", return displayer {
entry.bufnr, display_bufname) entry.bufnr,
entry.indicator,
display_bufname .. ":" .. entry.lnum,
}
end end
return function(entry) return function(entry)
local bufnr_str = tostring(entry) local bufname = entry.info.name ~= "" and entry.info.name or '[No Name]'
local bufname = path.normalize(vim.api.nvim_buf_get_name(entry), cwd)
-- if bufname is inside the cwd, trim that part of the string -- if bufname is inside the cwd, trim that part of the string
bufname = path.normalize(bufname, cwd)
local position = get_position(entry) local hidden = entry.info.hidden == 1 and 'h' or 'a'
local readonly = vim.api.nvim_buf_get_option(entry.bufnr, 'readonly') and '=' or ' '
if '' == bufname then local changed = entry.info.changed == 1 and '+' or ' '
return nil local indicator = entry.flag .. hidden .. readonly .. changed
end
return { return {
valid = true, valid = true,
value = bufname, value = bufname,
ordinal = bufnr_str .. " : " .. bufname, ordinal = entry.bufnr .. " : " .. bufname,
display = make_display, display = make_display,
bufnr = entry, bufnr = entry.bufnr,
filename = bufname, filename = bufname,
lnum = position[1] or 1, lnum = entry.info.lnum or 1,
indicator = indicator,
} }
end end
end end

View File

@@ -79,6 +79,7 @@ function Picker:new(opts)
finder = opts.finder, finder = opts.finder,
sorter = opts.sorter, sorter = opts.sorter,
previewer = opts.previewer, previewer = opts.previewer,
default_selection_index = opts.default_selection_index,
_completion_callbacks = {}, _completion_callbacks = {},
@@ -470,18 +471,30 @@ function Picker:find()
-- TODO: We should either: always leave one result or make sure we actually clean up the results when nothing matches -- TODO: We should either: always leave one result or make sure we actually clean up the results when nothing matches
if selection_strategy == 'row' then if selection_strategy == 'row' then
self:set_selection(self:get_selection_row()) if self._selection_row == nil and self.default_selection_index ~= nil then
self:set_selection(self:get_row(self.default_selection_index))
else
self:set_selection(self:get_selection_row())
end
elseif selection_strategy == 'follow' then elseif selection_strategy == 'follow' then
local index = self.manager:find_entry(self:get_selection()) if self._selection_row == nil and self.default_selection_index ~= nil then
self:set_selection(self:get_row(self.default_selection_index))
else
local index = self.manager:find_entry(self:get_selection())
if index then if index then
local follow_row = self:get_row(index) local follow_row = self:get_row(index)
self:set_selection(follow_row) self:set_selection(follow_row)
else
self:set_selection(self:get_reset_row())
end
end
elseif selection_strategy == 'reset' then
if self.default_selection_index ~= nil then
self:set_selection(self:get_row(self.default_selection_index))
else else
self:set_selection(self:get_reset_row()) self:set_selection(self:get_reset_row())
end end
elseif selection_strategy == 'reset' then
self:set_selection(self:get_reset_row())
else else
error('Unknown selection strategy: ' .. selection_strategy) error('Unknown selection strategy: ' .. selection_strategy)
end end

View File

@@ -356,53 +356,27 @@ previewers.vim_buffer = defaulter(function(_)
end, end,
preview_fn = function(self, entry, status) preview_fn = function(self, entry, status)
-- TODO: Consider using path here? Might not work otherwise. local bufnr = tonumber(entry.bufnr)
local filename = entry.filename
if filename == nil then if not vim.api.nvim_buf_is_loaded(bufnr) then
filename = entry.path
end
if filename == nil then
local value = entry.value
filename = vim.split(value, ":")[1]
end
if filename == nil then
return
end
log.info("Previewing File:", filename)
local bufnr = vim.fn.bufnr(filename)
if bufnr == -1 then
-- TODO: Is this the best way to load the buffer?... I'm not sure tbh
bufnr = vim.fn.bufadd(filename)
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
vim.cmd([[doautocmd filetypedetect BufRead ]] .. filename)
end end
self.state.last_set_bufnr = bufnr self.state.last_set_bufnr = bufnr
-- TODO: We should probably call something like this because we're not always getting highlight and all that stuff.
-- api.nvim_command('doautocmd filetypedetect BufRead ' .. vim.fn.fnameescape(filename))
vim.api.nvim_win_set_buf(status.preview_win, bufnr) vim.api.nvim_win_set_buf(status.preview_win, bufnr)
vim.api.nvim_win_set_option(status.preview_win, 'wrap', false) vim.api.nvim_win_set_option(status.preview_win, 'wrap', false)
vim.api.nvim_win_set_option(status.preview_win, 'winhl', 'Normal:Normal') vim.api.nvim_win_set_option(status.preview_win, 'winhl', 'Normal:Normal')
-- vim.api.nvim_win_set_option(preview_win, 'winblend', 20)
vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no') vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no')
vim.api.nvim_win_set_option(status.preview_win, 'foldlevel', 100) vim.api.nvim_win_set_option(status.preview_win, 'foldlevel', 100)
if entry.lnum then if entry.lnum then
vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1) vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1)
vim.api.nvim_win_set_option(status.preview_win, 'scrolloff', 10) vim.api.nvim_win_set_option(status.preview_win, 'scrolloff', 999)
vim.api.nvim_win_set_cursor(status.preview_win, {entry.lnum, 0}) vim.api.nvim_win_set_cursor(status.preview_win, {entry.lnum, 0})
-- print("LNUM:", entry.lnum) -- print("LNUM:", entry.lnum)
end end
vim.api.nvim_win_set_option(status.preview_win, 'scrolloff', 999) self.state.hl_win = status.preview_win
log.info("Previewed bufnr", bufnr)
end, end,
} }
end, {}) end, {})