opening files and browsing is working properly

This commit is contained in:
kyazdani42 2020-02-06 20:21:13 +01:00
parent 29daa4d00b
commit 9dfc4b27ef
2 changed files with 105 additions and 93 deletions

View File

@ -2,23 +2,17 @@
## Notice ## Notice
This is under development, do not try to use it right now. The best you could do is wait or give me tips to work better with lua. - This plugin is under development, it does not yet have all basic functionnalities required by an explorer.
- I am working on this plugin to learn lua, learn neovim api and create a **simple** file explorer with features i need.
I am working on this plugin to learn lua, learn neovim api and create a **simple** file explorer with features i need. - I really don't like any of the vim trees, they are all too complicated for their purposes and are kind of buggy. I have my shell to do most commands.
- This plugin wont work on windows.
I really don't like any of the vim trees, they are all too complicated for their purpose. I do have my shell to do most commands.
## Goals / Features ## Goals / Features
### 0.1 - [x] moving around the file structure like any basic tree
- [ ] moving around the file structure like any basic tree
- [x] open file in current buffer or in split with FzF like bindings (CR, C-v, C-x) - [x] open file in current buffer or in split with FzF like bindings (CR, C-v, C-x)
### 0.2
- [ ] add / delete file in directory - [ ] add / delete file in directory
- [ ] quickly find file in the directory structure - [ ] quickly find file in the directory structure
### 0.3
- [ ] icons for files - [ ] icons for files
- [ ] simple git integration (color of file changing when staged/changed) - [ ] simple git integration (color of file changing when staged/changed)

View File

@ -3,40 +3,53 @@ local function sys(v) return vim.fn.system(v) end
local function syslist(v) return vim.fn.systemlist(v) end local function syslist(v) return vim.fn.systemlist(v) end
local BUF_NAME = '_LuaTree_' local BUF_NAME = '_LuaTree_'
local ROOT_PATH = string.sub(sys('pwd'), 1, -2) .. '/' -- get rid of \n and add leading '/'
-- TODO: think of a better way to implement the whole thing local function is_dir(path)
-- because right now the code is quite bad return string.match(sys('ls -l '..path), 'total [0-9].*') ~= nil
-- I shouldnt base the code on tree indentation to handle logic
-- But for now its a first draft. It works a little
-- TODO: maybe this should not be required, as the tree is only used in dev projects.
-- In the README we should then precise to install vim-rooter.
-- Or maybe we should just keep this functionnality
local function add_dotdot(dirs)
table.insert(dirs, 1, '..')
return dirs
end end
local dir_struct = add_dotdot(syslist('ls')) local function create_dirs(path, depth, dirs)
local tree = {}
local function open() if not string.find(path, '^.*/$') then path = path .. '/' end
local win_width = 30
local options = { for i, name in pairs(dirs) do
bufhidden = 'wipe'; tree[i] = {
buftype = 'nowrite'; path = path,
modifiable = false; name = name,
depth = depth,
dir = is_dir(path .. name)
} }
local buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_name(buf, BUF_NAME)
for opt, val in pairs(options) do
api.nvim_buf_set_option(buf, opt, val)
end end
api.nvim_command('topleft '..win_width..'vnew') table.sort(tree, function(n) return n.dir == true end)
api.nvim_win_set_buf(0, buf)
api.nvim_command('setlocal nonumber norelativenumber winfixwidth winfixheight') return tree
end
local Tree = create_dirs(ROOT_PATH, 0, syslist('ls'))
local function get_padding(depth)
local str = ""
while 0 < depth do
str = str .. " "
depth = depth - 1
end
return str
end
local function format_tree(tree)
local dirs = {}
local previous_parent_index = -1
for i, node in pairs(tree) do
dirs[i] = get_padding(node.depth) .. node.name
end
return dirs
end end
local function get_buf() local function get_buf()
@ -63,19 +76,25 @@ local function get_win()
return nil return nil
end end
local function update_view() local function open()
local buf = get_buf(); local win_width = 30
if not buf then return end local options = {
bufhidden = 'wipe';
buftype = 'nowrite';
modifiable = false;
}
local cursor_pos = api.nvim_win_get_cursor(0) local buf = api.nvim_create_buf(false, true)
api.nvim_buf_set_option(buf, 'modifiable', true) api.nvim_buf_set_name(buf, BUF_NAME)
api.nvim_buf_set_lines(buf, 1, -1, false, dir_struct)
api.nvim_buf_set_option(buf, 'modifiable', false) for opt, val in pairs(options) do
api.nvim_win_set_cursor(0, cursor_pos) api.nvim_buf_set_option(buf, opt, val)
end end
local function is_dir(path) api.nvim_command('topleft '..win_width..'vnew')
return string.match(sys('ls -l '..path), 'total [0-9].*') ~= nil api.nvim_win_set_buf(0, buf)
api.nvim_command('setlocal nonumber norelativenumber winfixwidth winfixheight')
api.nvim_command('echo "'..ROOT_PATH..'"')
end end
local function close() local function close()
@ -85,6 +104,47 @@ local function close()
api.nvim_win_close(win, true) api.nvim_win_close(win, true)
end end
local function update_view()
local buf = get_buf();
if not buf then return end
local cursor_pos = api.nvim_win_get_cursor(0)
api.nvim_buf_set_option(buf, 'modifiable', true)
api.nvim_buf_set_lines(buf, 0, -1, false, format_tree(Tree))
api.nvim_buf_set_option(buf, 'modifiable', false)
api.nvim_win_set_cursor(0, cursor_pos)
end
local function is_win_open()
return get_buf() ~= nil
end
local function open_file(open_type)
local tree_index = api.nvim_win_get_cursor(0)[1]
local node = Tree[tree_index]
if is_dir(node.path .. node.name) then
local index = tree_index + 1;
local next_node = Tree[index]
if next_node ~= nil and next_node.depth > node.depth then
while next_node ~= nil and next_node.depth ~= node.depth do
table.remove(Tree, index)
next_node = Tree[index]
end
else
local dirlist = syslist('ls ' .. node.path .. node.name)
local child_dirs = create_dirs(node.path .. node.name .. '/', node.depth + 1, dirlist)
for i, node in pairs(child_dirs) do
table.insert(Tree, tree_index + i, node)
end
end
update_view()
else
api.nvim_command('wincmd l | '..open_type..' '.. node.path .. node.name)
end
end
local function set_mappings() local function set_mappings()
local buf = get_buf() local buf = get_buf()
if not buf then return end if not buf then return end
@ -103,50 +163,8 @@ local function set_mappings()
end end
end end
local function update_struct(folder_name)
local dirs = syslist('ls '..folder_name)
local index = 0
for i, v in pairs(dir_struct) do
if v == folder_name then
index = i
break
end
end
if string.match(dir_struct[index + 1] or '', '^ .*$') ~= nil then
while string.match(dir_struct[index + 1] or '', '^ .*$') ~= nil do
table.remove(dir_struct, index + 1)
end
else
for i, v in pairs(dirs) do
table.insert(dir_struct, index+i, ' '..v)
end
end
end
local function open_file(open_type)
local file = api.nvim_get_current_line()
if is_dir(file) then
update_struct(file)
update_view()
else
api.nvim_command('wincmd l | '..open_type..' '..file)
end
end
local function find_file()
local str = api.nvim_get_current_line()
print(str)
end
local function win_open()
return get_buf() ~= nil
end
local function toggle() local function toggle()
if win_open() == true then if is_win_open() == true then
close() close()
else else
open() open()
@ -158,5 +176,5 @@ end
return { return {
toggle = toggle; toggle = toggle;
open_file = open_file; open_file = open_file;
find_file = find_file;
} }