feat: add buffer list (#30)

NOTE: Buffer entry sometimes uses unstyled window, and I can't figure out why.

* feat: add buffers

* fixup
This commit is contained in:
TJ DeVries
2020-09-05 07:34:38 -04:00
committed by GitHub
parent 4abb5f7867
commit c70d6e58a0
3 changed files with 66 additions and 7 deletions

View File

@@ -34,6 +34,7 @@ function actions.get_selected_entry(prompt_bufnr)
return actions.get_current_picker(prompt_bufnr):get_selection() return actions.get_current_picker(prompt_bufnr):get_selection()
end end
-- TODO: It seems sometimes we get bad styling.
local function goto_file_selection(prompt_bufnr, command) local function goto_file_selection(prompt_bufnr, command)
local picker = actions.get_current_picker(prompt_bufnr) local picker = actions.get_current_picker(prompt_bufnr)
local entry = actions.get_selected_entry(prompt_bufnr) local entry = actions.get_selected_entry(prompt_bufnr)
@@ -68,9 +69,21 @@ local function goto_file_selection(prompt_bufnr, command)
col = tonumber(sections[3]) col = tonumber(sections[3])
end end
vim.cmd(string.format([[bwipeout! %s]], prompt_bufnr)) local preview_win = state.get_status(prompt_bufnr).preview_win
if preview_win then
a.nvim_win_set_config(preview_win, {style = ''})
end
a.nvim_set_current_win(picker.original_win_id or 0) actions.close(prompt_bufnr)
local original_win_id = picker.original_win_id or 0
local entry_bufnr = entry.bufnr
-- TODO: Sometimes we open something with missing line numbers and stuff...
a.nvim_set_current_win(original_win_id)
if entry_bufnr then
a.nvim_win_set_buf(original_win_id, entry_bufnr)
else
vim.cmd(string.format(":%s %s", command, filename)) vim.cmd(string.format(":%s %s", command, filename))
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
@@ -78,6 +91,7 @@ local function goto_file_selection(prompt_bufnr, command)
if row and col then if row and col then
a.nvim_win_set_cursor(0, {row, col}) a.nvim_win_set_cursor(0, {row, col})
end end
end
vim.cmd [[stopinsert]] vim.cmd [[stopinsert]]
end end

View File

@@ -22,6 +22,7 @@ local pickers = require('telescope.pickers')
local sorters = require('telescope.sorters') local sorters = require('telescope.sorters')
local utils = require('telescope.utils') local utils = require('telescope.utils')
local filter = vim.tbl_filter
local flatten = vim.tbl_flatten local flatten = vim.tbl_flatten
-- TODO: Support silver search here. -- TODO: Support silver search here.
@@ -340,4 +341,26 @@ builtin.fd = function(opts)
}):find() }):find()
end end
-- TODO: This is partially broken, but I think it might be an nvim bug.
builtin.buffers = function(opts)
opts = opts or {}
local buffers = filter(function(b)
return
vim.api.nvim_buf_is_loaded(b)
and 1 == vim.fn.buflisted(b)
end, vim.api.nvim_list_bufs())
pickers.new(opts, {
prompt = 'Buffers',
finder = finders.new_table {
results = buffers,
entry_maker = make_entry.gen_from_buffer(opts)
},
previewer = previewers.vim_buffer.new(opts),
sorter = sorters.get_norcalli_sorter(),
}):find()
end
return builtin return builtin

View File

@@ -169,4 +169,26 @@ function make_entry.gen_from_quickfix(opts)
end end
end end
function make_entry.gen_from_buffer(opts)
return function(entry)
local bufnr_str = tostring(entry)
local bufname = vim.api.nvim_buf_get_name(entry)
if '' == bufname then
return nil
end
return {
valid = true,
value = bufname,
ordinal = bufnr_str .. " : " .. bufname,
display = bufnr_str .. " : " .. bufname,
bufnr = entry,
filename = bufname,
}
end
end
return make_entry return make_entry