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:
@@ -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, '[$]$', '')
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
local actions = require('telescope.actions')
|
||||
local action_state = require('telescope.actions.state')
|
||||
local finders = require('telescope.finders')
|
||||
local make_entry = require('telescope.make_entry')
|
||||
local pickers = require('telescope.pickers')
|
||||
@@ -108,22 +109,35 @@ git.branches = function(opts)
|
||||
end
|
||||
|
||||
git.status = function(opts)
|
||||
local output = utils.get_os_command_output({ 'git', 'status', '-s' }, opts.cwd)
|
||||
local gen_new_finder = function()
|
||||
local output = utils.get_os_command_output({ 'git', 'status', '-s' }, opts.cwd)
|
||||
|
||||
if table.getn(output) == 0 then
|
||||
print('No changes found')
|
||||
return
|
||||
if table.getn(output) == 0 then
|
||||
print('No changes found')
|
||||
return
|
||||
end
|
||||
|
||||
return finders.new_table {
|
||||
results = output,
|
||||
entry_maker = opts.entry_maker or make_entry.gen_from_git_status(opts)
|
||||
}
|
||||
end
|
||||
|
||||
local initial_finder = gen_new_finder()
|
||||
if not initial_finder then return end
|
||||
|
||||
pickers.new(opts, {
|
||||
prompt_title = 'Git Status',
|
||||
finder = finders.new_table {
|
||||
results = output,
|
||||
entry_maker = make_entry.gen_from_git_status(opts)
|
||||
},
|
||||
finder = initial_finder,
|
||||
previewer = previewers.git_file_diff.new(opts),
|
||||
sorter = conf.file_sorter(opts),
|
||||
attach_mappings = function(_, map)
|
||||
attach_mappings = function(prompt_bufnr, map)
|
||||
actions.git_staging_toggle:enhance {
|
||||
post = function()
|
||||
action_state.get_current_picker(prompt_bufnr):refresh(gen_new_finder(), { reset_prompt = true })
|
||||
end,
|
||||
}
|
||||
|
||||
map('i', '<tab>', actions.git_staging_toggle)
|
||||
map('n', '<tab>', actions.git_staging_toggle)
|
||||
return true
|
||||
|
||||
@@ -23,6 +23,7 @@ builtin.live_grep = require('telescope.builtin.files').live_grep
|
||||
builtin.grep_string = require('telescope.builtin.files').grep_string
|
||||
builtin.find_files = require('telescope.builtin.files').find_files
|
||||
builtin.fd = builtin.find_files
|
||||
builtin.file_browser = require('telescope.builtin.files').file_browser
|
||||
builtin.treesitter = require('telescope.builtin.files').treesitter
|
||||
builtin.current_buffer_fuzzy_find = require('telescope.builtin.files').current_buffer_fuzzy_find
|
||||
builtin.tags = require('telescope.builtin.files').tags
|
||||
|
||||
Reference in New Issue
Block a user