Browse Source

[Refactor]: consistent update of bufferline offset with nvim-tree (#1351)

* Refactor nvim-tree

* Fix stylua comments

* Delete useless functions

* Remove autocmd and fix tasty's bug

* Fix luacheck

* Fix icon issue

* Fix formatting

* Fix formatting again

* Resolve Tasty's request

* Replace double dashes with blank line
Ahmed Khalf 3 years ago
parent
commit
5959872307
2 changed files with 28 additions and 51 deletions
  1. 28 50
      lua/core/nvimtree.lua
  2. 0 1
      lua/core/which-key.lua

+ 28 - 50
lua/core/nvimtree.lua

@@ -1,6 +1,6 @@
 local M = {}
 local M = {}
 local Log = require "core.log"
 local Log = require "core.log"
---
+
 M.config = function()
 M.config = function()
   lvim.builtin.nvimtree = {
   lvim.builtin.nvimtree = {
     active = true,
     active = true,
@@ -47,7 +47,7 @@ M.config = function()
     },
     },
   }
   }
 end
 end
---
+
 M.setup = function()
 M.setup = function()
   local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
   local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
   if not status_ok then
   if not status_ok then
@@ -73,64 +73,42 @@ M.setup = function()
       { key = { "l", "<CR>", "o" }, cb = tree_cb "edit" },
       { key = { "l", "<CR>", "o" }, cb = tree_cb "edit" },
       { key = "h", cb = tree_cb "close_node" },
       { key = "h", cb = tree_cb "close_node" },
       { key = "v", cb = tree_cb "vsplit" },
       { key = "v", cb = tree_cb "vsplit" },
-      { key = "q", cb = ":lua require('core.nvimtree').toggle_tree()<cr>" },
     }
     }
   end
   end
-end
---
-M.focus_or_close = function()
-  local view_status_ok, view = pcall(require, "nvim-tree.view")
-  if not view_status_ok then
-    return
-  end
-  local a = vim.api
 
 
-  local curwin = a.nvim_get_current_win()
-  local curbuf = a.nvim_win_get_buf(curwin)
-  local bufnr = view.View.bufnr
-  local winnr = view.get_winnr()
+  lvim.builtin.which_key.mappings["e"] = { "<cmd>NvimTreeToggle<CR>", "Explorer" }
+
+  local tree_view = require "nvim-tree.view"
 
 
-  if view.win_open() then
-    if curwin == winnr and curbuf == bufnr then
-      view.close()
-      if package.loaded["bufferline.state"] then
-        require("bufferline.state").set_offset(0)
-      end
-    else
-      view.focus()
-    end
-  else
-    view.open()
-    if package.loaded["bufferline.state"] and lvim.builtin.nvimtree.side == "left" then
-      -- require'bufferline.state'.set_offset(lvim.builtin.nvimtree.width + 1, 'File Explorer')
-      require("bufferline.state").set_offset(lvim.builtin.nvimtree.width + 1, "")
-    end
+  -- Add nvim_tree open callback
+  local open = tree_view.open
+  tree_view.open = function()
+    M.on_open()
+    open()
   end
   end
+
+  vim.cmd "au WinClosed * lua require('core.nvimtree').on_close()"
 end
 end
---
-M.toggle_tree = function()
-  local view_status_ok, view = pcall(require, "nvim-tree.view")
-  if not view_status_ok then
-    return
+
+M.on_open = function()
+  if package.loaded["bufferline.state"] and lvim.builtin.nvimtree.side == "left" then
+    require("bufferline.state").set_offset(lvim.builtin.nvimtree.width + 1, "")
   end
   end
-  if view.win_open() then
-    require("nvim-tree").close()
-    if package.loaded["bufferline.state"] then
-      require("bufferline.state").set_offset(0)
-    end
-  else
-    if package.loaded["bufferline.state"] and lvim.builtin.nvimtree.side == "left" then
-      -- require'bufferline.state'.set_offset(lvim.builtin.nvimtree.width + 1, 'File Explorer')
-      require("bufferline.state").set_offset(lvim.builtin.nvimtree.width + 1, "")
-    end
-    require("nvim-tree").toggle()
+end
+
+M.on_close = function()
+  local buf = tonumber(vim.fn.expand "<abuf>")
+  local ft = vim.api.nvim_buf_get_option(buf, "filetype")
+  if ft == "NvimTree" and package.loaded["bufferline.state"] then
+    require("bufferline.state").set_offset(0)
   end
   end
 end
 end
---
+
 function M.change_tree_dir(dir)
 function M.change_tree_dir(dir)
-  if vim.g.loaded_tree then
-    require("nvim-tree.lib").change_dir(dir)
+  local lib_status_ok, lib = pcall(require, "nvim-tree.lib")
+  if lib_status_ok then
+    lib.change_dir(dir)
   end
   end
 end
 end
---
+
 return M
 return M

+ 0 - 1
lua/core/which-key.lua

@@ -66,7 +66,6 @@ M.config = function()
       ["q"] = { "<cmd>q!<CR>", "Quit" },
       ["q"] = { "<cmd>q!<CR>", "Quit" },
       ["/"] = { "<cmd>CommentToggle<CR>", "Comment" },
       ["/"] = { "<cmd>CommentToggle<CR>", "Comment" },
       ["c"] = { "<cmd>BufferClose!<CR>", "Close Buffer" },
       ["c"] = { "<cmd>BufferClose!<CR>", "Close Buffer" },
-      ["e"] = { "<cmd>lua require'core.nvimtree'.toggle_tree()<CR>", "Explorer" },
       ["f"] = { "<cmd>Telescope find_files<CR>", "Find File" },
       ["f"] = { "<cmd>Telescope find_files<CR>", "Find File" },
       ["h"] = { "<cmd>nohlsearch<CR>", "No Highlight" },
       ["h"] = { "<cmd>nohlsearch<CR>", "No Highlight" },
       b = {
       b = {