From 988108142e1da421432889e63da94dc5ac1374ba Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 27 Apr 2022 18:29:08 +0300 Subject: [PATCH 01/12] refactor: use lua for preview autocommands --- lua/nvim-tree/actions/open-file.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lua/nvim-tree/actions/open-file.lua b/lua/nvim-tree/actions/open-file.lua index 42cc32b4c8a..681a1769a40 100644 --- a/lua/nvim-tree/actions/open-file.lua +++ b/lua/nvim-tree/actions/open-file.lua @@ -257,13 +257,13 @@ function M.fn(mode, filename) if mode == "preview" then if not buf_loaded then vim.bo.bufhidden = "delete" - vim.cmd [[ - augroup RemoveBufHidden - autocmd! - autocmd TextChanged setlocal bufhidden= | autocmd! RemoveBufHidden - autocmd TextChangedI setlocal bufhidden= | autocmd! RemoveBufHidden - augroup end - ]] + + vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { + group = vim.api.nvim_create_augroup("RemoveBufHidden", {}), + buffer = vim.api.nvim_get_current_buf(), + command = "setlocal bufhidden=", + once = true, + }) end view.focus() return From be42735c0d734dbd154e32cb9b93da52786bd559 Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 27 Apr 2022 20:06:34 +0300 Subject: [PATCH 02/12] refactor: use lua for nvim-tree autocmd --- lua/nvim-tree.lua | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 7af73cac286..f8468633ed1 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -10,6 +10,7 @@ local utils = require "nvim-tree.utils" local change_dir = require "nvim-tree.actions.change-dir" local legacy = require "nvim-tree.legacy" local core = require "nvim-tree.core" +local reloaders = require "nvim-tree.actions.reloaders" local _config = {} @@ -276,40 +277,53 @@ function M.change_dir(name) end local function setup_autocommands(opts) - vim.cmd "augroup NvimTree" - vim.cmd "autocmd!" + local augroup_id = vim.api.nvim_create_augroup("NvimTree", {}) + local function create_nvim_tree_autocmd(name, custom_opts) + local default_opts = { group = augroup_id } + vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) + end -- reset highlights when colorscheme is changed - vim.cmd "au ColorScheme * lua require'nvim-tree'.reset_highlight()" + create_nvim_tree_autocmd("ColorScheme", { callback = M.reset_highlight }) + if opts.auto_reload_on_write then - vim.cmd "au BufWritePost * lua require'nvim-tree.actions.reloaders'.reload_explorer()" + create_nvim_tree_autocmd("BufWritePost", { callback = reloaders.reload_explorer }) end - vim.cmd "au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree.actions.reloaders'.reload_git()" + create_nvim_tree_autocmd("User", { + pattern = { "FugitiveChanged", "NeogitStatusRefreshed" }, + callback = reloaders.reload_git, + }) if opts.open_on_tab then - vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()" + create_nvim_tree_autocmd("TabEnter", { callback = M.tab_change }) end if opts.hijack_cursor then - vim.cmd "au CursorMoved NvimTree_* lua require'nvim-tree'.place_cursor_on_node()" + create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node }) end if opts.update_cwd then - vim.cmd "au DirChanged * lua require'nvim-tree'.change_dir(vim.loop.cwd())" + create_nvim_tree_autocmd("DirChanged", { + callback = function() + M.change_dir(vim.loop.cwd) + end, + }) end if opts.update_focused_file.enable then - vim.cmd "au BufEnter * lua require'nvim-tree'.find_file(false)" + create_nvim_tree_autocmd("BufEnter", { + callback = function() + M.find_file(false) + end, + }) end if not opts.actions.open_file.quit_on_open then - vim.cmd "au BufWipeout NvimTree_* lua require'nvim-tree.view'._prevent_buffer_override()" + create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view._prevent_buffer_override }) else - vim.cmd "au BufWipeout NvimTree_* lua require'nvim-tree.view'.abandon_current_window()" + create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view.abandon_current_window }) end if opts.hijack_directories.enable then - vim.cmd "au BufEnter,BufNewFile * lua require'nvim-tree'.open_on_directory()" + create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory }) end - - vim.cmd "augroup end" end local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS From d736e90b1108d2bcbe50ba640099d207f6dd8da3 Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 27 Apr 2022 20:06:49 +0300 Subject: [PATCH 03/12] refactor: use lua for file-popup autocmd --- lua/nvim-tree/actions/file-popup.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree/actions/file-popup.lua b/lua/nvim-tree/actions/file-popup.lua index dcd47dd9034..9e8b45f526b 100644 --- a/lua/nvim-tree/actions/file-popup.lua +++ b/lua/nvim-tree/actions/file-popup.lua @@ -72,11 +72,10 @@ function M.toggle_file_info(node) setup_window(node) - vim.cmd [[ - augroup NvimTreeRemoveFilePopup - au CursorMoved * lua require'nvim-tree.actions.file-popup'.close_popup() - augroup END - ]] + vim.api.nvim_create_autocmd("CursorMoved", { + group = vim.api.nvim_create_augroup("NvimTreeRemoveFilePopup", {}), + callback = M.close_popup, + }) end return M From d3134f58ffdd63d83d8de06b8f46360915e74a3f Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 27 Apr 2022 20:42:23 +0300 Subject: [PATCH 04/12] refactor: use lua api for user commands --- lua/nvim-tree.lua | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index f8468633ed1..5c9c57a9478 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -11,6 +11,8 @@ local change_dir = require "nvim-tree.actions.change-dir" local legacy = require "nvim-tree.legacy" local core = require "nvim-tree.core" local reloaders = require "nvim-tree.actions.reloaders" +local copy_paste = require "nvim-tree.actions.copy-paste" +local collapse_all = require "nvim-tree.actions.collapse-all" local _config = {} @@ -253,19 +255,29 @@ local function manage_netrw(disable_netrw, hijack_netrw) end local function setup_vim_commands() - vim.cmd [[ - command! -nargs=? -complete=dir NvimTreeOpen lua require'nvim-tree'.open("") - command! NvimTreeClose lua require'nvim-tree.view'.close() - command! NvimTreeToggle lua require'nvim-tree'.toggle(false) - command! NvimTreeFocus lua require'nvim-tree'.focus() - command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer() - command! NvimTreeClipboard lua require'nvim-tree.actions.copy-paste'.print_clipboard() - command! NvimTreeFindFile lua require'nvim-tree'.find_file(true) - command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true) - command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize("") - command! NvimTreeCollapse lua require'nvim-tree.actions.collapse-all'.fn() - command! NvimTreeCollapseKeepBuffers lua require'nvim-tree.actions.collapse-all'.fn(true) - ]] + vim.api.nvim_create_user_command("NvimTreeOpen", function(res) + M.open(res.args) + end, { nargs = "?", complete = "dir" }) + vim.api.nvim_create_user_command("NvimTreeClose", view.close, {}) + vim.api.nvim_create_user_command("NvimTreeToggle", function() + M.toggle(false) + end, {}) + vim.api.nvim_create_user_command("NvimTreeFocus", M.focus, {}) + vim.api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, {}) + vim.api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, {}) + vim.api.nvim_create_user_command("NvimTreeFindFile", function() + M.find_file(true) + end, {}) + vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function() + M.toggle(true) + end, {}) + vim.api.nvim_create_user_command("NvimTreeResize", function(res) + M.resize(res.args) + end, { nargs = 1 }) + vim.api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, {}) + vim.api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function() + collapse_all.fn(true) + end, {}) end function M.change_dir(name) From a61946531a3f32ac6e9355919f98904e729b4057 Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 27 Apr 2022 20:49:09 +0300 Subject: [PATCH 05/12] docs: update notice for the required nvim version to 0.7 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 71efe78dadf..d8d2f026169 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Notice -This plugin requires [neovim >=0.6.0](https://github.com/neovim/neovim/wiki/Installing-Neovim). +This plugin requires [neovim >=0.7.0](https://github.com/neovim/neovim/wiki/Installing-Neovim). If you have issues since the recent setup migration, check out [this guide](https://github.com/kyazdani42/nvim-tree.lua/issues/674) From 72312e2c71c053435d000e8aac2bd02917904279 Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 8 May 2022 08:26:58 +0300 Subject: [PATCH 06/12] improvements per review --- lua/nvim-tree.lua | 2 +- lua/nvim-tree/actions/open-file.lua | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 5c9c57a9478..562938f3c2a 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -315,7 +315,7 @@ local function setup_autocommands(opts) if opts.update_cwd then create_nvim_tree_autocmd("DirChanged", { callback = function() - M.change_dir(vim.loop.cwd) + M.change_dir(vim.loop.cwd()) end, }) end diff --git a/lua/nvim-tree/actions/open-file.lua b/lua/nvim-tree/actions/open-file.lua index 681a1769a40..dffefafa8e3 100644 --- a/lua/nvim-tree/actions/open-file.lua +++ b/lua/nvim-tree/actions/open-file.lua @@ -261,7 +261,9 @@ function M.fn(mode, filename) vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { group = vim.api.nvim_create_augroup("RemoveBufHidden", {}), buffer = vim.api.nvim_get_current_buf(), - command = "setlocal bufhidden=", + callback = function() + vim.bo.bufhidden = "" + end, once = true, }) end From 575082ae34decf78d1adb27194606417899f0e7e Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 8 May 2022 08:42:59 +0300 Subject: [PATCH 07/12] check version --- lua/nvim-tree.lua | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 562938f3c2a..4ba3b945f9f 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -502,6 +502,10 @@ local function validate_options(conf) end function M.setup(conf) + if vim.fn.has "nvim-0.7" == 1 then + utils.warn "nvim-tree.lua requires Neovim 0.7 or higher" + end + legacy.migrate_legacy_options(conf or {}) validate_options(conf) From 1515ddfbd2826a4836371ef218a344f47dc2ba24 Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 8 May 2022 12:08:51 +0300 Subject: [PATCH 08/12] fix version check --- lua/nvim-tree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 4ba3b945f9f..11a91865461 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -502,7 +502,7 @@ local function validate_options(conf) end function M.setup(conf) - if vim.fn.has "nvim-0.7" == 1 then + if vim.fn.has "nvim-0.7" == 0 then utils.warn "nvim-tree.lua requires Neovim 0.7 or higher" end From ab7d5652ad5eab1ae7773da5f16c22f748258a51 Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 8 May 2022 13:20:17 +0300 Subject: [PATCH 09/12] update coc autocommands --- lua/nvim-tree/diagnostics.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index d6e23961179..578881c3e2e 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -170,8 +170,13 @@ function M.setup(opts) if M.enable then log.line("diagnostics", "setup") - vim.cmd "au DiagnosticChanged * lua require'nvim-tree.diagnostics'.update()" - vim.cmd "au User CocDiagnosticChange lua require'nvim-tree.diagnostics'.update()" + a.nvim_create_autocmd("DiagnosticChanged", { + callback = M.update, + }) + a.nvim_create_autocmd("User", { + pattern = "CocDiagnosticChanged", + callback = M.update, + }) end end From 0682b5d2f50f286677982b2efbd723e58dd9be0a Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 8 May 2022 13:22:53 +0300 Subject: [PATCH 10/12] use api instead of vim.api --- lua/nvim-tree.lua | 30 ++++++++++++++-------------- lua/nvim-tree/actions/file-popup.lua | 4 ++-- lua/nvim-tree/actions/open-file.lua | 6 +++--- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 11a91865461..c6ed7d57fc7 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -73,7 +73,7 @@ end function M.tab_change() if view.is_visible { any_tabpage = true } then - local bufname = vim.api.nvim_buf_get_name(0) + local bufname = api.nvim_buf_get_name(0) if bufname:match "Neogit" ~= nil or bufname:match "--graph" ~= nil then return end @@ -156,7 +156,7 @@ end local prev_line function M.place_cursor_on_node() - local l = vim.api.nvim_win_get_cursor(0)[1] + local l = api.nvim_win_get_cursor(0)[1] if l == prev_line then return end @@ -255,27 +255,27 @@ local function manage_netrw(disable_netrw, hijack_netrw) end local function setup_vim_commands() - vim.api.nvim_create_user_command("NvimTreeOpen", function(res) + api.nvim_create_user_command("NvimTreeOpen", function(res) M.open(res.args) end, { nargs = "?", complete = "dir" }) - vim.api.nvim_create_user_command("NvimTreeClose", view.close, {}) - vim.api.nvim_create_user_command("NvimTreeToggle", function() + api.nvim_create_user_command("NvimTreeClose", view.close, {}) + api.nvim_create_user_command("NvimTreeToggle", function() M.toggle(false) end, {}) - vim.api.nvim_create_user_command("NvimTreeFocus", M.focus, {}) - vim.api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, {}) - vim.api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, {}) - vim.api.nvim_create_user_command("NvimTreeFindFile", function() + api.nvim_create_user_command("NvimTreeFocus", M.focus, {}) + api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, {}) + api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, {}) + api.nvim_create_user_command("NvimTreeFindFile", function() M.find_file(true) end, {}) - vim.api.nvim_create_user_command("NvimTreeFindFileToggle", function() + api.nvim_create_user_command("NvimTreeFindFileToggle", function() M.toggle(true) end, {}) - vim.api.nvim_create_user_command("NvimTreeResize", function(res) + api.nvim_create_user_command("NvimTreeResize", function(res) M.resize(res.args) end, { nargs = 1 }) - vim.api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, {}) - vim.api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function() + api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, {}) + api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function() collapse_all.fn(true) end, {}) end @@ -289,10 +289,10 @@ function M.change_dir(name) end local function setup_autocommands(opts) - local augroup_id = vim.api.nvim_create_augroup("NvimTree", {}) + local augroup_id = api.nvim_create_augroup("NvimTree", {}) local function create_nvim_tree_autocmd(name, custom_opts) local default_opts = { group = augroup_id } - vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) + api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts)) end -- reset highlights when colorscheme is changed diff --git a/lua/nvim-tree/actions/file-popup.lua b/lua/nvim-tree/actions/file-popup.lua index 9e8b45f526b..ef907575927 100644 --- a/lua/nvim-tree/actions/file-popup.lua +++ b/lua/nvim-tree/actions/file-popup.lua @@ -72,8 +72,8 @@ function M.toggle_file_info(node) setup_window(node) - vim.api.nvim_create_autocmd("CursorMoved", { - group = vim.api.nvim_create_augroup("NvimTreeRemoveFilePopup", {}), + a.nvim_create_autocmd("CursorMoved", { + group = a.nvim_create_augroup("NvimTreeRemoveFilePopup", {}), callback = M.close_popup, }) end diff --git a/lua/nvim-tree/actions/open-file.lua b/lua/nvim-tree/actions/open-file.lua index dffefafa8e3..9079120cd2e 100644 --- a/lua/nvim-tree/actions/open-file.lua +++ b/lua/nvim-tree/actions/open-file.lua @@ -258,9 +258,9 @@ function M.fn(mode, filename) if not buf_loaded then vim.bo.bufhidden = "delete" - vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { - group = vim.api.nvim_create_augroup("RemoveBufHidden", {}), - buffer = vim.api.nvim_get_current_buf(), + api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { + group = api.nvim_create_augroup("RemoveBufHidden", {}), + buffer = api.nvim_get_current_buf(), callback = function() vim.bo.bufhidden = "" end, From d368c7ed2376b755c2d9e5ef66061cbc2f88842f Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 8 May 2022 13:27:05 +0300 Subject: [PATCH 11/12] add return if lower version --- lua/nvim-tree.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index c6ed7d57fc7..81326b52c71 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -504,6 +504,7 @@ end function M.setup(conf) if vim.fn.has "nvim-0.7" == 0 then utils.warn "nvim-tree.lua requires Neovim 0.7 or higher" + return end legacy.migrate_legacy_options(conf or {}) From 19598bef618935016d3e99019fe03e70ebb4439e Mon Sep 17 00:00:00 2001 From: michael Date: Fri, 13 May 2022 21:34:19 +0300 Subject: [PATCH 12/12] fix typo --- lua/nvim-tree/diagnostics.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/diagnostics.lua b/lua/nvim-tree/diagnostics.lua index 578881c3e2e..34771ec696f 100644 --- a/lua/nvim-tree/diagnostics.lua +++ b/lua/nvim-tree/diagnostics.lua @@ -174,7 +174,7 @@ function M.setup(opts) callback = M.update, }) a.nvim_create_autocmd("User", { - pattern = "CocDiagnosticChanged", + pattern = "CocDiagnosticChange", callback = M.update, }) end