some refactos, and start adding the git stuff

This commit is contained in:
kiyan42 2020-11-01 23:34:17 +01:00
parent 664e9b95c7
commit 4da032e4e5
6 changed files with 100 additions and 19 deletions

View File

@ -48,6 +48,7 @@ end
local function get_links()
return {
FolderName = 'Directory',
Popup = 'Normal',
Normal = 'Normal',
EndOfBuffer = 'EndOfBuffer',
CursorLine = 'CursorLine',
@ -66,15 +67,14 @@ local opts = nil
return {
configure = function(o)
opts = o
end,
setup = function()
if opts and opts.web_devicons.show == true then
if not require'nvim-web-devicons'.has_loaded() then
require'nvim-web-devicons'.setup({ default = opts.web_devicons.default })
end
end
opts = o
local higlight_groups = get_hl_groups()
for k, d in pairs(higlight_groups) do
local gui = d.gui or 'NONE'

View File

@ -79,15 +79,14 @@ M.config = {
function M.setup(opts)
M.config = vim.tbl_deep_extend("keep", opts or {}, M.config)
require'nvim-tree.git'.configure(M.config)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
require'nvim-tree.colors'.configure(M.config)
require'nvim-tree.buffers.tree'.configure(M.config)
require'nvim-tree.explorer'.configure(M.config)
require'nvim-tree.format'.configure(M.config)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.cmd "hi def link NvimTreePopup Normal"
require'nvim-tree.git'.configure(M.config)
-- vim.cmd "au BufWritePost * lua require'nvim-tree'.refresh()"
-- nope, maybe with watcher ?
@ -104,8 +103,7 @@ function M.setup(opts)
vim.cmd "command! NvimTreeRefresh lua require'nvim-tree'.refresh()"
vim.cmd "command! NvimTreeClipboard lua require'nvim-tree'.print_clipboard()"
vim.cmd "command! NvimTreeFindFile lua require'nvim-tree'.find_file(true)"
vim.cmd "au! Colorscheme * lua require'nvim-tree.colors'.setup()"
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()"

View File

@ -61,8 +61,10 @@ 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)
function M.Explorer:explore(root, idxlist)
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
@ -86,8 +88,9 @@ function M.Explorer:explore(root)
local entry = funcs.create(cwd, entry_name)
if not funcs.check or funcs.check(entry) then
self.file_pool[entry.absolute_path] = 1
self.file_pool[entry.absolute_path] = idxpath
table.insert(entries[entry_type], entry)
idxpath[#idxpath] = idxpath[#idxpath] + 1
end
end
end
@ -142,13 +145,21 @@ function M.Explorer:switch_open_dir(node)
local entries = self:explore(node.absolute_path)
if entries then
node.entries = require'nvim-tree.git'.gitify(entries)
node.entries = require'nvim-tree.git'.gitify(entries, node.absolute_path)
end
end
function M.Explorer:refresh(files)
return function()
dump(files)
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
@ -156,10 +167,9 @@ function M.Explorer:new(cwd)
self.cwd = cwd or uv.cwd()
local entries = self:explore()
if entries then
self.node_tree = require'nvim-tree.git'.gitify(entries)
self.node_tree = require'nvim-tree.git'.gitify(entries, self.cwd)
end
return self
end

View File

@ -10,7 +10,8 @@ function M.rename(node_name)
-- rename node
-- for each buf that has .*/nodename/?.*, replace nodename with new name
-- send refresh node
-- send refresh node or just leave the watcher handle that
end
return M

View File

@ -1,10 +1,76 @@
local a = vim.api
local uv = vim.loop
local utils = require'nvim-tree.utils'
local M = {}
function M.configure(opts)
local map_to_icon_name = {
['?'] = 'untracked',
-- 'staged is M on index'
M = 'unstaged',
A = '',
D = 'deleted',
R = 'renamed',
U = 'unmerged',
[' '] = ''
-- C = 'copy',
}
local function map_status_to_icons(status, cwd)
local t = {}
for _, s in ipairs(status) do
local path = utils.path_join(cwd, s:sub(4))
local i1 = M.opts.icons[map_to_icon_name[s:sub(1,1)]] or ''
local i2 = M.opts.icons[map_to_icon_name[s:sub(2,2)]] or ''
t[path] = i1..i2
-- this is bullshit, rewrite please
while path ~= cwd do
path = path:gsub('/[^/]*$', '')
local icons = t[path]
if icons then
if not icons:find(i1) then
t[path] = icons..' '..i1
end
if not icons:find(i2) then
t[path] = t[path]..i2
end
else
t[path] = i1..i2
end
end
end
return t
end
function M.gitify(entries)
function M.gitify(entries, cwd)
if not M.opts.show.icons or not M.opts.show.highlight then
return entries
end
local git_root = vim.fn.system("cd '"..cwd.."' && git rev-parse --show-toplevel"):sub(0, -2)
if git_root:match("fatal") then
return entries
end
local status = vim.fn.systemlist("cd '"..git_root.."' && git status --porcelain=1 -u")
local mapped_statuses = map_status_to_icons(status, git_root)
-- dump(mapped_statuses)
for _, entry in pairs(entries) do
local s = mapped_statuses[entry.absolute_path]
if s then
entry.git_icon = s
end
end
-- dump(entries)
return entries
end
function M.configure(opts)
M.opts = opts.git
end
return M

View File

@ -6,4 +6,10 @@ function M.path_join(root, path)
return root..path_sep..path
end
function M.warn(msg)
vim.api.nvim_command(
string.format([[echohl WarningMsg | echo "%s" | echohl None]], msg)
)
end
return M