feat: add vim buffer directory viewer using plenary scandir.ls_async (#421)

feat: git_status (??) now shows file or directory
fix: attempt to index field 'state' (a nil value)
This commit is contained in:
Simon Hauser
2021-01-22 21:03:39 +01:00
committed by GitHub
parent 36ff7d5078
commit 223ec81774

View File

@@ -6,6 +6,7 @@ local Previewer = require('telescope.previewers.previewer')
local conf = require('telescope.config').values
local pfiletype = require('plenary.filetype')
local pscan = require('plenary.scandir')
local buf_delete = utils.buf_delete
local defaulter = utils.make_default_callable
@@ -20,6 +21,12 @@ previewers.file_maker = function(filepath, bufnr, opts)
local ft = opts.use_ft_detect and pfiletype.detect(filepath)
if opts.bufname ~= filepath then
if vim.loop.fs_stat(filepath).type == 'directory' then
pscan.ls_async(filepath, { hidden = true, on_exit = vim.schedule_wrap(function(data)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, data)
if opts.callback then opts.callback(bufnr) end
end)})
else
path.read_file_async(vim.fn.expand(filepath), vim.schedule_wrap(function(data)
if not vim.api.nvim_buf_is_valid(bufnr) then return end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, vim.split(data, '[\r]?\n'))
@@ -27,6 +34,7 @@ previewers.file_maker = function(filepath, bufnr, opts)
if opts.callback then opts.callback(bufnr) end
putils.highlighter(bufnr, ft)
end))
end
else
if opts.callback then opts.callback(bufnr) end
end
@@ -45,6 +53,7 @@ previewers.new_buffer_previewer = function(opts)
local bufname_table = {}
local global_state = require'telescope.state'
local preview_window_id
local function get_bufnr(self)
if not self.state then return nil end
@@ -81,8 +90,10 @@ previewers.new_buffer_previewer = function(opts)
if opts.keep_last_buf then
last_nr = global_state.get_global_key('last_preview_bufnr')
-- Push in another buffer so the last one will not be cleaned up
if preview_window_id then
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_win_set_buf(self.state.winid, bufnr)
vim.api.nvim_win_set_buf(preview_window_id, bufnr)
end
end
set_bufnr(self, nil)
@@ -98,6 +109,7 @@ previewers.new_buffer_previewer = function(opts)
function opts.preview_fn(self, entry, status)
if get_bufnr(self) == nil then
set_bufnr(self, vim.api.nvim_win_get_buf(status.preview_win))
preview_window_id = status.preview_win
end
if opts.get_buffer_by_name and get_bufnr_by_bufname(self, opts.get_buffer_by_name(self, entry)) then
@@ -459,12 +471,20 @@ previewers.git_file_diff = defaulter(function(_)
end,
define_preview = function(self, entry, status)
if entry.status and entry.status == '??' then
local p = from_entry.path(entry, true)
if p == nil or p == '' then return end
conf.buffer_previewer_maker(p, self.state.bufnr, {
bufname = self.state.bufname
})
else
putils.job_maker({ 'git', '-P', 'diff', entry.value }, self.state.bufnr, {
value = entry.value,
bufname = self.state.bufname
})
putils.regex_highlighter(self.state.bufnr, 'diff')
end
end
}
end, {})