add symlink handling
This commit is contained in:
parent
b0d6e153f7
commit
e29c9fb4ee
@ -22,7 +22,6 @@
|
||||
- [ ] buffer / window should not disappear when only the tree is opened
|
||||
- [ ] buffer / window should always stay on the left and never change size (open a file with only the tree open to reproduce this bug)
|
||||
|
||||
- [ ] handle symbolic links
|
||||
- [ ] quickly find file in the directory structure
|
||||
- [ ] update tree automatically on window change
|
||||
|
||||
|
||||
@ -14,6 +14,7 @@ local colors = {
|
||||
}
|
||||
|
||||
local HIGHLIGHTS = {
|
||||
Symlink = { gui = 'bold', fg = colors.cyan },
|
||||
FolderName = { gui = 'bold', fg = colors.blue },
|
||||
FolderIcon = { fg = colors.orange },
|
||||
|
||||
|
||||
@ -73,10 +73,13 @@ local function format_tree(tree)
|
||||
local padding = get_padding(node.depth)
|
||||
local git = node.git
|
||||
local icon = ""
|
||||
if node.icon == true then
|
||||
local name = node.name
|
||||
if node.link == true then
|
||||
name = name .. ' ➛ ' .. node.linkto
|
||||
elseif node.icon == true then
|
||||
icon = get_icon(node.path .. node.name, node.dir, node.open)
|
||||
end
|
||||
dirs[i] = padding .. icon .. git .. node.name
|
||||
dirs[i] = padding .. icon .. git .. name
|
||||
end
|
||||
|
||||
return dirs
|
||||
@ -120,6 +123,9 @@ local function highlight_line(buffer)
|
||||
highlight('LuaTreeFolderIcon', line, 0, text_start)
|
||||
highlight('LuaTreeFolderName', line, text_start + gitlen, -1)
|
||||
|
||||
elseif node.link == true then
|
||||
highlight('LuaTreeSymlink', line, 0, -1)
|
||||
|
||||
elseif is_special(node.name) == true then
|
||||
text_start = text_start - 4
|
||||
highlight('LuaTreeSpecialFile', line, text_start + gitlen, -1)
|
||||
|
||||
@ -6,10 +6,19 @@ local get_git_attr = require 'lib/git'.get_git_attr
|
||||
local Tree = {}
|
||||
|
||||
local function is_dir(path)
|
||||
local stat = vim.loop.fs_stat(path)
|
||||
local stat = vim.loop.fs_lstat(path)
|
||||
return stat and stat.type == 'directory' or false
|
||||
end
|
||||
|
||||
local function is_symlink(path)
|
||||
local stat = vim.loop.fs_lstat(path)
|
||||
return stat and stat.type == 'link' or false
|
||||
end
|
||||
|
||||
local function link_to(path)
|
||||
return vim.loop.fs_readlink(path) or ''
|
||||
end
|
||||
|
||||
local function check_dir_access(path)
|
||||
return vim.loop.fs_access(path, 'R') == true
|
||||
end
|
||||
@ -46,11 +55,16 @@ local function create_nodes(path, relpath, depth, dirs)
|
||||
if not string.find(relpath, '^.*/$') and depth > 0 then relpath = relpath .. '/' end
|
||||
|
||||
for i, name in pairs(dirs) do
|
||||
local dir = is_dir(path..name)
|
||||
local full_path = path..name
|
||||
local dir = is_dir(full_path)
|
||||
local link = is_symlink(full_path)
|
||||
local linkto = link == true and link_to(full_path) or nil
|
||||
local rel_path = relpath ..name
|
||||
tree[i] = {
|
||||
path = path,
|
||||
relpath = rel_path,
|
||||
link = link,
|
||||
linkto = linkto,
|
||||
name = name,
|
||||
depth = depth,
|
||||
dir = dir,
|
||||
@ -137,4 +151,5 @@ return {
|
||||
refresh_tree = refresh_tree;
|
||||
open_dir = open_dir;
|
||||
check_dir_access = check_dir_access;
|
||||
is_dir = is_dir;
|
||||
}
|
||||
|
||||
19
lua/tree.lua
19
lua/tree.lua
@ -11,6 +11,7 @@ local init_tree = stateutils.init_tree
|
||||
local open_dir = stateutils.open_dir
|
||||
local check_dir_access = stateutils.check_dir_access
|
||||
local refresh_tree = stateutils.refresh_tree
|
||||
local is_dir = stateutils.is_dir
|
||||
|
||||
local winutils = require 'lib/winutils'
|
||||
local update_view = winutils.update_view
|
||||
@ -60,6 +61,7 @@ local function open_file(open_type)
|
||||
force_refresh_git()
|
||||
init_tree(new_path)
|
||||
update_view()
|
||||
|
||||
elseif open_type == 'chdir' then
|
||||
if node.dir == false or check_dir_access(node.path .. node.name) == false then return end
|
||||
|
||||
@ -69,9 +71,26 @@ local function open_file(open_type)
|
||||
force_refresh_git()
|
||||
init_tree(new_path)
|
||||
update_view()
|
||||
|
||||
elseif node.link == true then
|
||||
local link_to_dir = is_dir(node.linkto)
|
||||
if link_to_dir == true and check_dir_access(node.linkto) == false then return end
|
||||
|
||||
if link_to_dir == true then
|
||||
api.nvim_command('cd ' .. node.linkto)
|
||||
local new_path = get_cwd() .. '/'
|
||||
set_root_path(new_path)
|
||||
force_refresh_git()
|
||||
init_tree(new_path)
|
||||
update_view()
|
||||
else
|
||||
api.nvim_command('wincmd l | '..open_type..' '.. node.linkto)
|
||||
end
|
||||
|
||||
elseif node.dir == true then
|
||||
open_dir(tree_index)
|
||||
update_view(true)
|
||||
|
||||
else
|
||||
api.nvim_command('wincmd l | '..open_type..' '.. node.path .. node.name)
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user