create file

This commit is contained in:
kiyan42 2020-11-08 12:00:40 +01:00
parent 4da032e4e5
commit 198296f09c
7 changed files with 153 additions and 33 deletions

View File

@ -2,14 +2,16 @@ return {
setup = require'nvim-tree.actions'.setup,
close = require'nvim-tree.actions'.close,
toggle = require'nvim-tree.actions'.toggle,
defer_redraw = require'nvim-tree.actions'.defer_redraw,
redraw = require'nvim-tree.actions'.redraw,
open = require'nvim-tree.actions'.open,
open_file = require'nvim-tree.actions'.open_file,
change_cwd = require'nvim-tree.actions'.change_cwd,
rename_file = require'nvim-tree.actions'.rename_file,
add_file = require'nvim-tree.actions'.add_file,
create_file = require'nvim-tree.actions'.create_file,
remove_file = require'nvim-tree.actions'.remove_file,
copy = require'nvim-tree.actions'.copy,
cut = require'nvim-tree.actions'.cut,
paste = require'nvim-tree.actions'.paste,
refresh = require'nvim-tree.actions'.refresh,
}

View File

@ -1,6 +1,8 @@
local M = {}
local explorer = nil
local lines = {}
local highlights = {}
M.setup = require'nvim-tree.config'.setup
M.close = require'nvim-tree.buffers.tree'.close
@ -12,7 +14,7 @@ function M.toggle()
end
end
function M.redraw()
function M.defer_redraw()
local buffers_tree = require'nvim-tree.buffers.tree'
vim.defer_fn(
function()
@ -23,12 +25,16 @@ function M.redraw()
end, 1)
end
function M.redraw()
require'nvim-tree.buffers.tree'.render(lines, highlights)
end
function M.open(dirname)
if dirname and dirname ~= "." then
vim.cmd("cd "..dirname)
end
explorer = require'nvim-tree.explorer'.Explorer:new()
local lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree, explorer.cwd)
lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree, explorer.cwd)
if require'nvim-tree.buffers.tree'.open() == 'norestore' then
require'nvim-tree.buffers.tree'.render(lines, highlights)
end
@ -38,7 +44,7 @@ local function cd(to)
if not to or #to == 0 then return end
vim.cmd(":cd "..to)
explorer = require'nvim-tree.explorer'.Explorer:new()
local lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree, explorer.cwd)
lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree, explorer.cwd)
require'nvim-tree.buffers.tree'.render(lines, highlights)
end
@ -65,7 +71,7 @@ function M.open_file(mode)
if node.entries ~= nil then
explorer:switch_open_dir(node)
local lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree, explorer.cwd)
lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree, explorer.cwd)
return require'nvim-tree.buffers.tree'.render(lines, highlights)
end
@ -103,4 +109,28 @@ function M.rename_file()
require'nvim-tree.buffers.popups'.rename(node, idx)
end
function M.refresh()
end
function M.create_file()
local node, idx = explorer:get_node_under_cursor()
if not node then return end
local _lines = vim.deepcopy(lines)
table.insert(_lines, idx+2, "")
local _highlights = vim.tbl_map(
function(hl)
if hl.line > idx then
hl.line = hl.line+1
end
return hl
end,
vim.deepcopy(highlights)
)
require'nvim-tree.buffers.tree'.render(_lines, _highlights)
require'nvim-tree.buffers.popups'.create(node, idx+1)
end
return M

View File

@ -38,4 +38,55 @@ function M.rename(node, row)
});
end
function M.create(node, row)
local prev_win = a.nvim_get_current_win()
local prev_win_width = a.nvim_win_get_width(prev_win)
local numcol = 2
local cwd = node.absolute_path
if node.parent then
local tmp = node.parent
while tmp do
tmp = tmp.parent
numcol = numcol + 2
end
end
if node.opened then
numcol = numcol + 2
else
cwd = node.absolute_path:gsub(vim.pesc(node.name)..'$', '')
end
local buf = a.nvim_create_buf(false, true)
a.nvim_open_win(buf, true, {
relative = "win",
row = row,
col = numcol,
win = prev_win,
height = 1,
width = prev_win_width - numcol,
style = 'minimal'
})
a.nvim_buf_attach(buf, false, {
on_detach = vim.schedule_wrap(require'nvim-tree.actions'.redraw)
})
vim.cmd "startinsert"
a.nvim_buf_set_keymap(buf, 'i', '<esc>', '<esc>:bd!<cr>', {silent=true});
a.nvim_buf_set_keymap(buf, 'i', '<C-c>', '<esc>:bd!<cr>', {silent=true});
a.nvim_buf_set_keymap(buf, 'i', '<C-]>', '<esc>:bd!<cr>', {silent=true});
a.nvim_buf_set_keymap(buf, 'n', 'q', ':bd!<Cr>', {silent=true});
a.nvim_buf_set_keymap(
buf,
'i',
'<CR>',
'<esc>:lua require"nvim-tree.fs".create("'..cwd..'")<cr>',
{
noremap = true,
silent = true
});
end
return M

View File

@ -106,7 +106,7 @@ function M.setup(opts)
vim.cmd "au! Colorscheme * lua require'nvim-tree.colors'.configure()"
if M.config.keep_open_on_tabenter then
vim.cmd "au TabEnter * lua require'nvim-tree'.redraw()"
vim.cmd "au TabEnter * lua require'nvim-tree'.defer_redraw()"
end
if M.config.resize_replace_on_bufenter then

View File

@ -12,16 +12,16 @@ M.Explorer = {
cwd = uv.cwd(),
cursor = nil,
node_tree = {},
file_pool = {}
}
local node_type_funcs = {
directory = {
create = function(parent, name)
local absolute_path = utils.path_join(parent, name)
create = function(parent_cwd, name, parent_node)
local absolute_path = utils.path_join(parent_cwd, name)
return {
name = name,
absolute_path = absolute_path,
parent = parent_node,
opened = false,
entries = {}
}
@ -31,24 +31,26 @@ local node_type_funcs = {
end
},
file = {
create = function(parent, name)
local absolute_path = utils.path_join(parent, name)
create = function(parent_cwd, name, parent_node)
local absolute_path = utils.path_join(parent_cwd, name)
local executable = uv.fs_access(absolute_path, 'X')
return {
name = name,
absolute_path = absolute_path,
executable = executable,
parent = parent_node,
extension = vim.fn.fnamemodify(name, ':e') or ""
}
end
},
link = {
create = function(parent, name)
local absolute_path = utils.path_join(parent, name)
create = function(parent_cwd, name, parent_node)
local absolute_path = utils.path_join(parent_cwd, name)
local link_to = uv.fs_realpath(absolute_path)
return {
name = name,
absolute_path = absolute_path,
parent = parent_node,
link_to = link_to
}
end,
@ -61,10 +63,8 @@ function M.Explorer:is_file_ignored(file)
or (not M.config.show_ignored and M.config.ignore[file] == true)
end
function M.Explorer:explore(root, idxlist)
function M.Explorer:explore(root, parent)
local cwd = root or self.cwd
local idxpath = idxlist or {}
table.insert(idxpath, 1)
local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then
@ -85,21 +85,18 @@ function M.Explorer:explore(root, idxlist)
local funcs = node_type_funcs[entry_type]
-- handle fifo and sockets at some point ?
if funcs then
local entry = funcs.create(cwd, entry_name)
local entry = funcs.create(cwd, entry_name, parent)
if not funcs.check or funcs.check(entry) then
self.file_pool[entry.absolute_path] = idxpath
table.insert(entries[entry_type], entry)
idxpath[#idxpath] = idxpath[#idxpath] + 1
end
end
end
end
require'nvim-tree.watcher'.run(self, cwd)
for _, dir in pairs(entries.directory) do
require'nvim-tree.watcher'.run(self, dir.absolute_path)
end
-- require'nvim-tree.watcher'.run(self, cwd)
-- for _, dir in pairs(entries.directory) do
-- require'nvim-tree.watcher'.run(self, dir.absolute_path)
-- end
for _, node in pairs(entries.link) do
table.insert(entries.directory, node)
@ -143,7 +140,7 @@ function M.Explorer:switch_open_dir(node)
if not node.opened then return end
if #node.entries > 0 then return end
local entries = self:explore(node.absolute_path)
local entries = self:explore(node.absolute_path, node)
if entries then
node.entries = require'nvim-tree.git'.gitify(entries, node.absolute_path)
end
@ -152,13 +149,6 @@ end
function M.Explorer:refresh(files)
return function()
for _, file in ipairs(files) do
if not self.file_pool[file] then
-- print('new file')
elseif uv.fs_stat(file) ~= nil then
-- print('old file has been edited')
else
-- print('file was removed or renamed')
end
end
end
end

View File

@ -14,4 +14,27 @@ function M.rename(node_name)
-- send refresh node or just leave the watcher handle that
end
function M.create(cwd)
local line = a.nvim_get_current_line()
vim.cmd(":bd!")
if #line == 0 then return end
local utils = require'nvim-tree.utils';
local joined = utils.path_join(cwd, line)
if utils.ends_with_sep(line) then
pcall(vim.fn.mkdir, joined, 'p', '0755')
return require'nvim-tree.actions'.refresh()
end
if utils.has_sep(line) then
local dirs = utils.remove_last_part(line)
pcall(vim.fn.mkdir, utils.path_join(cwd, dirs), 'p', '0755')
end
vim.fn.system('touch '..joined)
require'nvim-tree.actions'.refresh()
end
return M

View File

@ -3,7 +3,31 @@ local M = {}
local path_sep = vim.fn.has('win32') == 1 and [[\]] or '/'
function M.path_join(root, path)
return root..path_sep..path
if M.ends_with_sep(root) then
return root..path
else
return root..path_sep..path
end
end
function M.ends_with_sep(path)
return vim.endswith(path, path_sep)
end
function M.has_sep(path)
return vim.fn.stridx(path, path_sep) ~= -1
end
function M.split_path(path)
return vim.split(path, path_sep, true)
end
function M.concat_path(paths)
return table.concat(paths, path_sep)
end
function M.remove_last_part(path)
return path:gsub(path_sep..'[^'..path_sep..']*$', '')
end
function M.warn(msg)