* refactor(nvim-tree.lua): extract open_nvim_tree function and add comments
* feat(nvim-tree.lua): add auto open and auto close autocmds for NvimTree
This commit is contained in:
		
							parent
							
								
									f1e8caeca2
								
							
						
					
					
						commit
						42ca0e4245
					
				| 
						 | 
					@ -1,12 +1,23 @@
 | 
				
			||||||
 | 
					local function open_nvim_tree(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  -- buffer is a real file on the disk
 | 
				
			||||||
 | 
					  local real_file = vim.fn.filereadable(data.file) == 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  -- buffer is a [No Name]
 | 
				
			||||||
 | 
					  local no_name = data.file == "" and vim.bo[data.buf].buftype == ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if not real_file and not no_name then
 | 
				
			||||||
 | 
					    return
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  -- open the tree, find the file but don't focus it
 | 
				
			||||||
 | 
					  require("nvim-tree.api").tree.toggle({ focus = false, find_file = true, })
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- Auto open
 | 
				
			||||||
 | 
					vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					-- Auto close
 | 
				
			||||||
vim.api.nvim_create_autocmd({"QuitPre"}, {
 | 
					vim.api.nvim_create_autocmd({"QuitPre"}, {
 | 
				
			||||||
    callback = function() vim.cmd("NvimTreeClose") end,
 | 
					    callback = function() vim.cmd("NvimTreeClose") end,
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
local function open_nvim_tree()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  -- open the tree
 | 
					 | 
				
			||||||
  require("nvim-tree.api").tree.open()
 | 
					 | 
				
			||||||
end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue