From 53c8eb3ef1f44206bcbcde2f5e2820f68b627307 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 5 Feb 2023 14:27:52 +0300 Subject: feat(nvim): custom open_on_startup callback --- nvim/.config/nvim/lua/filetree.lua | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/nvim/.config/nvim/lua/filetree.lua b/nvim/.config/nvim/lua/filetree.lua index 761aca6..0760163 100644 --- a/nvim/.config/nvim/lua/filetree.lua +++ b/nvim/.config/nvim/lua/filetree.lua @@ -1,9 +1,10 @@ local tree_cb = require'nvim-tree.config'.nvim_tree_callback + require'nvim-tree'.setup { - open_on_setup = true, hijack_cursor = true, view = { + width = 30, mappings = { list = { { key = "l", cb = tree_cb("edit") }, @@ -15,5 +16,27 @@ require'nvim-tree'.setup { } } +local function open_on_startup(data) + + -- buffer is a [No Name] + local no_name = data.file == "" and vim.bo[data.buf].buftype == "" + + -- buffer is a directory + local directory = vim.fn.isdirectory(data.file) == 1 + + if not no_name and not directory then + return + end + + -- change to the directory + if directory then + vim.cmd.cd(data.file) + end + + -- open the tree + require("nvim-tree.api").tree.open() +end + vim.api.nvim_set_keymap('n', '', ':NvimTreeToggle', { noremap = true, silent = true }) vim.api.nvim_command('autocmd BufEnter * ++nested if winnr("$") == 1 && bufname() == "NvimTree_" . tabpagenr() | quit | endif') +vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_on_startup }) -- cgit v1.2.3