commit 7ec6a588d58c1ad0e95187422d93f342786ff52b Author: kyazdani42 Date: Tue Feb 4 19:59:14 2020 +0100 init plugin diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..7161aeaf --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +Tree.lua is a simple tree for neovim +Copyright © 2012 Yazdani Kiyan + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . diff --git a/README.md b/README.md new file mode 100644 index 00000000..4ada016e --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# A simple tree for neovim written in lua + +## Notice + +This work is under development, do not try to use it, it does not work. + +I am working on this plugin to learn lua, become better with 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 purpose. I do have my shell to do most commands. + + +## Goals / Features + +- [] moving around the file structure like any basic tree +- [] open file in current buffer or in split with FzF like bindings (CR, C-v, C-x) +- [] add / delete file in directory +- [] quickly find file in the directory structure +- [] icons for files +- [] simple git integration (color of file changing when staged/changed) + diff --git a/lua/tree.lua b/lua/tree.lua new file mode 100644 index 00000000..eb666568 --- /dev/null +++ b/lua/tree.lua @@ -0,0 +1,95 @@ +local dir_struct = vim.fn.systemlist('ls') +local sys = function(v) vim.fn.system(v) end +local syslist = function(v) vim.fn.systemlist(v) end +local api = vim.api +local buf = nil +local win = nil + +local function open() + local win_width = 30 + local options = { + bufhidden = 'wipe'; + buftype = 'nowrite'; + modifiable = false; + } + + buf = api.nvim_create_buf(false, true) + + for opt, val in pairs(options) do + api.nvim_buf_set_option(buf, opt, val) + end + + api.nvim_command('topleft '..win_width..'vnew | set nonumber norelativenumber') + win = api.nvim_win_get_number(0) + api.nvim_win_set_buf(win, buf) +end + +local function update_view() + api.nvim_buf_set_option(buf, 'modifiable', true) + api.nvim_buf_set_lines(buf, 1, -1, false, dir_struct) + api.nvim_buf_set_option(buf, 'modifiable', false) +end + +local function is_dir(path) + return string.match(sys('ls -l '..path), 'total [0-9].*') ~= nil +end + +local function close() + api.nvim_win_close(win, true) + win = nil + buf = nil +end + +local function set_mappings() + local mappings = { + [''] = 'open_file("edit")'; + [''] = 'open_file("vsplit")'; + [''] = 'open_file("split")'; + f = 'find_file()'; + } + + for k,v in pairs(mappings) do + api.nvim_buf_set_keymap(buf, 'n', k, ':lua require"tree".'..v..'', { + nowait = true, noremap = true, silent = true + }) + end +end + +local function open_file(open_type) + local str = api.nvim_get_current_line() + if is_dir(str) then + local cur_dir = syslist('ls') + local sub_dir = syslist('ls') + -- local final_data = {} + + update_view() + else + print(open_type) + -- api.nvim_command(open_type..' '..str) + end +end + +local function find_file() + local str = api.nvim_get_current_line() + print(str) +end + +local function win_open() + return false +end + +local function toggle() + if win_open() then + close() + else + open() + update_view() + set_mappings() + end +end + +return { + toggle = toggle; + open_file = open_file; + find_file = find_file; +} diff --git a/plugin/tree.vim b/plugin/tree.vim new file mode 100644 index 00000000..80a72863 --- /dev/null +++ b/plugin/tree.vim @@ -0,0 +1,12 @@ +if exists('g:loaded_tree') | finish | endif + +let s:save_cpo = &cpo +set cpo&vim + +command! LuaTree lua require'tree'.toggle() + +let &cpo = s:save_cpo +unlet s:save_cpo + +let g:loaded_tree = 1 +