add indent marker format, start improving the explorer

This commit is contained in:
kiyan42 2020-10-24 22:50:35 +02:00
parent 5421f6b89a
commit 6c7027e108
4 changed files with 108 additions and 6 deletions

View File

@ -4,8 +4,18 @@ return {
setup = require'nvim-tree.config'.setup,
open = function()
explorer = require'nvim-tree.explorer'.Explorer:new()
local lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree)
require'nvim-tree.buffers.tree'.open()
require'nvim-tree.buffers.tree'.render(lines, highlights)
end,
close = require'nvim-tree.buffers.tree'.close,
open_file = function()
local node, idx = explorer:get_node_under_cursor()
if node.entries and not node.opened and #node.entries == 0 then
explorer:explore_children(node, idx)
local lines, highlights = require'nvim-tree.format'.format_nodes(explorer.node_tree)
require'nvim-tree.buffers.tree'.render(lines, highlights)
end
end,
ex = function() return explorer end
}

View File

@ -60,7 +60,7 @@ function M.render(lines, highlights)
vim.bo[bufnr].modifiable = true
a.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
a.nvim_buf_clear_namespace(bufnr, ns_id)
a.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)
for _, hl in ipairs(highlights) do
a.nvim_buf_set_extmark(bufnr, ns_id, hl.line, hl.col, {
end_line = hl.line,

View File

@ -6,10 +6,11 @@ local M = {}
M.Explorer = {
cwd = uv.cwd(),
node_tree = {},
node_pool = {}
node_pool = {},
node_flat = {}
}
local path_sep = vim.fn.has('win32') == 1 and '\\' or '/'
local path_sep = vim.fn.has('win32') == 1 and [[\]] or '/'
local function path_join(root, path)
return root..path_sep..path
@ -60,8 +61,8 @@ function M.Explorer:is_file_ignored(file)
or (M.config.show_ignored and M.config.ignore[file] == true)
end
function M.Explorer:explore(dir_extension)
local cwd = dir_extension and path_join(self.cwd, dir_extension) or self.cwd
function M.Explorer:explore(root)
local cwd = root or self.cwd
local handle = uv.fs_scandir(cwd)
if type(handle) == 'string' then
return nil
@ -94,7 +95,38 @@ function M.Explorer:explore(dir_extension)
end
end
return vim.tbl_extend("keep", entries.directories, entries.symlinks, entries.files)
for _, node in pairs(entries.symlinks) do
table.insert(entries.directories, node)
end
for _, node in pairs(entries.files) do
table.insert(entries.directories, node)
end
return entries.directories
end
function M.Explorer:get_node_under_cursor()
local curpos = a.nvim_win_get_cursor(0)
for i, node in ipairs(self.node_flat) do
if i == curpos[1] then
return node, i
end
end
return nil, nil
end
function M.Explorer:explore_children(node, idx)
local entries = self:explore(node.absolute_path)
if entries then
-- TODO: this will not work
-- we need to properly add entries to self.tree_node.node
node.entries = require'nvim-tree.git'.gitify(entries)
for i, n in ipairs(entries) do
self.node_pool[n.absolute_path] = n
table.insert(self.node_flat, idx+i, node)
end
end
end
function M.Explorer:new()
@ -104,6 +136,7 @@ function M.Explorer:new()
self.node_tree = require'nvim-tree.git'.gitify(entries)
for _, node in ipairs(entries) do
self.node_pool[node.absolute_path] = node
table.insert(self.node_flat, node)
end
end

59
lua/nvim-tree/format.lua Normal file
View File

@ -0,0 +1,59 @@
local M = {}
local function get_padding(depth, last_node)
local padding = ""
local hl = nil
if depth > 0 then
if M.config.show_indent_markers then
if last_node then
padding = string.rep('', depth-1)..''
else
padding = string.rep('', depth)
end
hl = {}
else
padding = string.rep(' ', depth)
end
end
return padding, hl
end
local function format_node(lines, highlights, node, depth, last_node)
local padding, padding_hl = get_padding(depth, last_node)
if padding_hl then
table.insert(highlights, padding_hl)
end
-- if node.children then
-- elseif node.link_to then
-- else
-- end
table.insert(lines, padding..node.name)
end
local function walk(lines, highlights, children, depth)
for i, node in ipairs(children) do
format_node(lines, highlights, node, depth, i == #children)
if node.children then
walk(lines, highlights, node.children, depth + 1)
end
end
end
function M.format_nodes(node_tree)
local lines = {}
local highlights = {}
walk(lines, highlights, node_tree, 0)
return lines, vim.tbl_flatten(highlights)
end
function M.configure(opts)
M.config = {
show_indent_markers = opts.show_indent_markers
}
end
return M