Skip to content

Commit

Permalink
Buffers rework (indicators and sorting)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed Nov 16, 2020
1 parent ad7280e commit 6930b34
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 49 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,15 @@ Defaults:
```lua
-- lua/telescope/pickers.lua
Picker:new{
prompt_title = "", -- REQUIRED
finder = FUNCTION, -- see lua/telescope/finder.lua
sorter = FUNCTION, -- see lua/telescope/sorter.lua
previewer = FUNCTION, -- see lua/telescope/previewer.lua
selection_strategy = "reset", -- follow, reset, line
border = {},
borderchars = {"", "", "", "", "", "", "", ""},
preview_cutoff = 120,
prompt_title = "", -- REQUIRED
finder = FUNCTION, -- see lua/telescope/finder.lua
sorter = FUNCTION, -- see lua/telescope/sorter.lua
previewer = FUNCTION, -- see lua/telescope/previewer.lua
selection_strategy = "reset", -- follow, reset, line
border = {},
borderchars = {"", "", "", "", "", "", "", ""},
preview_cutoff = 120,
default_selection_index = 1, -- Change the index of the initial selection row
}
```

Expand Down
42 changes: 33 additions & 9 deletions lua/telescope/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -671,17 +671,40 @@ builtin.fd = builtin.find_files
builtin.buffers = function(opts)
opts = opts or {}

local buffers = filter(function(b)
return
(opts.show_all_buffers
or vim.api.nvim_buf_is_loaded(b))
and 1 == vim.fn.buflisted(b)
local buffers_list = vim.split(vim.fn.execute('buffers'), "\n")

local buffers = {}
opts.bufnr_width = 0
local default_selection_idx = 1
for i = 2, #buffers_list do
local bufnr, indicator, bufname, lnum = string.match(
buffers_list[i], '(%d+)%s*([%%#%s]?[ah%s]?[-=RF?%s]?[+x%s]?)%s*"(.*)"%s*line (%d+)'
)

if opts.show_all_buffers or vim.api.nvim_buf_is_loaded(tonumber(bufnr)) then
if opts.sort_lastused and indicator:sub(1, 1) == "#" then
default_selection_idx = 2
end

end, vim.api.nvim_list_bufs())
local element = {
bufnr = bufnr,
indicator = indicator,
bufname = vim.fn.expand(bufname),
lnum = lnum
}

if opts.sort_lastused and (indicator:sub(1, 1) == "#" or indicator:sub(1, 1) == "%") then
local idx = ((buffers[1] ~= nil and buffers[1].indicator:sub(1, 1) == "%") and 2 or 1)
table.insert(buffers, idx, element)
else
table.insert(buffers, element)
end

if not opts.bufnr_width then
local max_bufnr = math.max(unpack(buffers))
opts.bufnr_width = #tostring(max_bufnr)
-- Determine the max_width for bufnr
if opts.bufnr_width < #bufnr then
opts.bufnr_width = #bufnr
end
end
end

pickers.new(opts, {
Expand All @@ -693,6 +716,7 @@ builtin.buffers = function(opts)
-- previewer = previewers.vim_buffer.new(opts),
previewer = previewers.vimgrep.new(opts),
sorter = conf.generic_sorter(opts),
default_selection_index = default_selection_idx,
}):find()
end

Expand Down
42 changes: 20 additions & 22 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ do
end

local execute_keys = {
path = function(t)
path = function(t)
return t.cwd .. path.separator .. t.filename, false
end,

Expand Down Expand Up @@ -265,18 +265,16 @@ end
function make_entry.gen_from_buffer(opts)
opts = opts or {}

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
local displayer = entry_display.create {
separator = " ",
items = {
{ width = opts.bufnr_width },
{ width = 4 },
{ remaining = true },
},
}

return {}
end
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())

local make_display = function(entry)
local display_bufname
Expand All @@ -286,17 +284,16 @@ function make_entry.gen_from_buffer(opts)
display_bufname = entry.filename
end

return string.format("%" .. opts.bufnr_width .. "d : %s",
entry.bufnr, display_bufname)
return displayer {
entry.bufnr,
entry.indicator,
display_bufname .. ":" .. entry.lnum,
}
end

return function(entry)
local bufnr_str = tostring(entry)
local bufname = path.normalize(vim.api.nvim_buf_get_name(entry), cwd)

-- if bufname is inside the cwd, trim that part of the string

local position = get_position(entry)
local bufname = path.normalize(entry.bufname, cwd)

if '' == bufname then
return nil
Expand All @@ -306,13 +303,14 @@ function make_entry.gen_from_buffer(opts)
valid = true,

value = bufname,
ordinal = bufnr_str .. " : " .. bufname,
ordinal = entry.bufnr .. " : " .. bufname,
display = make_display,

bufnr = entry,
bufnr = entry.bufnr,
filename = bufname,

lnum = position[1] or 1,
lnum = entry.lnum or 1,
indicator = entry.indicator,
}
end
end
Expand Down
29 changes: 21 additions & 8 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function Picker:new(opts)
finder = opts.finder,
sorter = opts.sorter,
previewer = opts.previewer,
default_selection_index = opts.default_selection_index,

_completion_callbacks = {},

Expand Down Expand Up @@ -467,18 +468,30 @@ function Picker:find()

-- 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
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
local index = self.manager:find_entry(self:get_selection())

if index then
local follow_row = self:get_row(index)
self:set_selection(follow_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_reset_row())
local index = self.manager:find_entry(self:get_selection())

if index then
local follow_row = self:get_row(index)
self:set_selection(follow_row)
else
self:set_selection(self:get_reset_row())
end
end
elseif selection_strategy == 'reset' then
self:set_selection(self:get_reset_row())
if self.default_selection_index ~= nil then
self:set_selection(self:get_row(self.default_selection_index))
else
self:set_selection(self:get_reset_row())
end
else
error('Unknown selection strategy: ' .. selection_strategy)
end
Expand Down
4 changes: 2 additions & 2 deletions lua/telescope/pickers/entry_display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ end

local function truncate(str, len)
-- TODO: This doesn't handle multi byte chars...
if vim.fn.strdisplaywidth(str) > len - 1 then
str = str:sub(1, len)
if vim.fn.strdisplaywidth(str) > len then
str = str:sub(1, len - 1)
str = str .. ""
end
return str
Expand Down

0 comments on commit 6930b34

Please sign in to comment.