feat: file browser & refresh interface (#290)

and more picker api stuff for sunjon. refresh is implemented for file_browser and git_status
This commit is contained in:
Simon Hauser
2021-02-23 22:10:19 +01:00
committed by GitHub
parent 0a32b47f20
commit 4dd35ef0e1
8 changed files with 186 additions and 27 deletions

View File

@@ -1,3 +1,4 @@
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local action_set = require('telescope.actions.set')
local finders = require('telescope.finders')
@@ -7,6 +8,10 @@ local previewers = require('telescope.previewers')
local utils = require('telescope.utils')
local conf = require('telescope.config').values
local scan = require('plenary.scandir')
local Path = require('plenary.path')
local os_sep = Path.path.sep
local flatten = vim.tbl_flatten
local files = {}
@@ -191,6 +196,84 @@ local function prepare_match(entry, kind)
return entries
end
files.file_browser = function(opts)
opts = opts or {}
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
local gen_new_finder = function(path)
opts.cwd = path
local data = {}
scan.scan_dir(path, {
add_dirs = true,
depth = 1,
on_insert = function(entry, typ)
table.insert(data, typ == 'directory' and (entry .. os_sep) or entry)
end
})
table.insert(data, 1, '../')
return finders.new_table {
results = data,
entry_maker = (function()
local tele_path = require'telescope.path'
local gen = make_entry.gen_from_file(opts)
return function(entry)
local tmp = gen(entry)
tmp.ordinal = tele_path.make_relative(entry, opts.cwd)
return tmp
end
end)()
}
end
pickers.new(opts, {
prompt_title = 'Find Files',
finder = gen_new_finder(opts.cwd),
previewer = conf.file_previewer(opts),
sorter = conf.file_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
action_set.select:replace_if(function()
return action_state.get_selected_entry().path:sub(-1) == os_sep
end, function()
local new_cwd = vim.fn.expand(action_state.get_selected_entry().path:sub(1, -2))
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker.cwd = new_cwd
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
end)
local create_new_file = function()
local current_picker = action_state.get_current_picker(prompt_bufnr)
local file = action_state.get_current_line()
if file == "" then
print('To create a new file or directory(add ' .. os_sep .. ' at the end of file) ' ..
'write the desired new into the prompt and press <C-e>. ' ..
'It works for not existing nested input as well.' ..
'Example: this' .. os_sep .. 'is' .. os_sep .. 'a' .. os_sep .. 'new_file.lua')
return
end
local fpath = current_picker.cwd .. os_sep .. file
if string.sub(fpath, -1) ~= os_sep then
actions.close(prompt_bufnr)
Path:new(fpath):touch({ parents = true })
vim.cmd(string.format(':e %s', fpath))
else
Path:new(fpath:sub(1, -2)):mkdir({ parents = true })
local new_cwd = vim.fn.expand(fpath)
current_picker.cwd = new_cwd
current_picker:refresh(gen_new_finder(new_cwd), { reset_prompt = true })
end
end
map('i', '<C-e>', create_new_file)
map('n', '<C-e>', create_new_file)
return true
end,
}):find()
end
files.treesitter = function(opts)
opts.show_line = utils.get_default(opts.show_line, true)
@@ -295,7 +378,7 @@ files.tags = function(opts)
attach_mappings = function()
action_set.select:enhance {
post = function()
local selection = action_set.get_selected_entry()
local selection = action_state.get_selected_entry()
if selection.scode then
local scode = string.gsub(selection.scode, '[$]$', '')