refactor: use lua api for user commands and autocommands (#1206)
BREAKING: plugin now requires nvim-0.7
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
## Notice
|
## 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)
|
If you have issues since the recent setup migration, check out [this guide](https://github.com/kyazdani42/nvim-tree.lua/issues/674)
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,9 @@ local utils = require "nvim-tree.utils"
|
|||||||
local change_dir = require "nvim-tree.actions.change-dir"
|
local change_dir = require "nvim-tree.actions.change-dir"
|
||||||
local legacy = require "nvim-tree.legacy"
|
local legacy = require "nvim-tree.legacy"
|
||||||
local core = require "nvim-tree.core"
|
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 = {}
|
local _config = {}
|
||||||
|
|
||||||
@@ -70,7 +73,7 @@ end
|
|||||||
|
|
||||||
function M.tab_change()
|
function M.tab_change()
|
||||||
if view.is_visible { any_tabpage = true } then
|
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
|
if bufname:match "Neogit" ~= nil or bufname:match "--graph" ~= nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -153,7 +156,7 @@ end
|
|||||||
|
|
||||||
local prev_line
|
local prev_line
|
||||||
function M.place_cursor_on_node()
|
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
|
if l == prev_line then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -252,19 +255,29 @@ local function manage_netrw(disable_netrw, hijack_netrw)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function setup_vim_commands()
|
local function setup_vim_commands()
|
||||||
vim.cmd [[
|
api.nvim_create_user_command("NvimTreeOpen", function(res)
|
||||||
command! -nargs=? -complete=dir NvimTreeOpen lua require'nvim-tree'.open("<args>")
|
M.open(res.args)
|
||||||
command! NvimTreeClose lua require'nvim-tree.view'.close()
|
end, { nargs = "?", complete = "dir" })
|
||||||
command! NvimTreeToggle lua require'nvim-tree'.toggle(false)
|
api.nvim_create_user_command("NvimTreeClose", view.close, {})
|
||||||
command! NvimTreeFocus lua require'nvim-tree'.focus()
|
api.nvim_create_user_command("NvimTreeToggle", function()
|
||||||
command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer()
|
M.toggle(false)
|
||||||
command! NvimTreeClipboard lua require'nvim-tree.actions.copy-paste'.print_clipboard()
|
end, {})
|
||||||
command! NvimTreeFindFile lua require'nvim-tree'.find_file(true)
|
api.nvim_create_user_command("NvimTreeFocus", M.focus, {})
|
||||||
command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true)
|
api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, {})
|
||||||
command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize("<args>")
|
api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, {})
|
||||||
command! NvimTreeCollapse lua require'nvim-tree.actions.collapse-all'.fn()
|
api.nvim_create_user_command("NvimTreeFindFile", function()
|
||||||
command! NvimTreeCollapseKeepBuffers lua require'nvim-tree.actions.collapse-all'.fn(true)
|
M.find_file(true)
|
||||||
]]
|
end, {})
|
||||||
|
api.nvim_create_user_command("NvimTreeFindFileToggle", function()
|
||||||
|
M.toggle(true)
|
||||||
|
end, {})
|
||||||
|
api.nvim_create_user_command("NvimTreeResize", function(res)
|
||||||
|
M.resize(res.args)
|
||||||
|
end, { nargs = 1 })
|
||||||
|
api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, {})
|
||||||
|
api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function()
|
||||||
|
collapse_all.fn(true)
|
||||||
|
end, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.change_dir(name)
|
function M.change_dir(name)
|
||||||
@@ -276,40 +289,53 @@ function M.change_dir(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function setup_autocommands(opts)
|
local function setup_autocommands(opts)
|
||||||
vim.cmd "augroup NvimTree"
|
local augroup_id = api.nvim_create_augroup("NvimTree", {})
|
||||||
vim.cmd "autocmd!"
|
local function create_nvim_tree_autocmd(name, custom_opts)
|
||||||
|
local default_opts = { group = augroup_id }
|
||||||
|
api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
|
||||||
|
end
|
||||||
|
|
||||||
-- reset highlights when colorscheme is changed
|
-- 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
|
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
|
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
|
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
|
end
|
||||||
if opts.hijack_cursor then
|
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
|
end
|
||||||
if opts.update_cwd then
|
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
|
end
|
||||||
if opts.update_focused_file.enable then
|
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
|
end
|
||||||
|
|
||||||
if not opts.actions.open_file.quit_on_open then
|
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
|
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
|
end
|
||||||
|
|
||||||
if opts.hijack_directories.enable then
|
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
|
end
|
||||||
|
|
||||||
vim.cmd "augroup end"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
|
||||||
@@ -476,6 +502,11 @@ local function validate_options(conf)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(conf)
|
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 {})
|
legacy.migrate_legacy_options(conf or {})
|
||||||
|
|
||||||
validate_options(conf)
|
validate_options(conf)
|
||||||
|
|||||||
@@ -72,11 +72,10 @@ function M.toggle_file_info(node)
|
|||||||
|
|
||||||
setup_window(node)
|
setup_window(node)
|
||||||
|
|
||||||
vim.cmd [[
|
a.nvim_create_autocmd("CursorMoved", {
|
||||||
augroup NvimTreeRemoveFilePopup
|
group = a.nvim_create_augroup("NvimTreeRemoveFilePopup", {}),
|
||||||
au CursorMoved * lua require'nvim-tree.actions.file-popup'.close_popup()
|
callback = M.close_popup,
|
||||||
augroup END
|
})
|
||||||
]]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -257,13 +257,15 @@ function M.fn(mode, filename)
|
|||||||
if mode == "preview" then
|
if mode == "preview" then
|
||||||
if not buf_loaded then
|
if not buf_loaded then
|
||||||
vim.bo.bufhidden = "delete"
|
vim.bo.bufhidden = "delete"
|
||||||
vim.cmd [[
|
|
||||||
augroup RemoveBufHidden
|
api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
|
||||||
autocmd!
|
group = api.nvim_create_augroup("RemoveBufHidden", {}),
|
||||||
autocmd TextChanged <buffer> setlocal bufhidden= | autocmd! RemoveBufHidden
|
buffer = api.nvim_get_current_buf(),
|
||||||
autocmd TextChangedI <buffer> setlocal bufhidden= | autocmd! RemoveBufHidden
|
callback = function()
|
||||||
augroup end
|
vim.bo.bufhidden = ""
|
||||||
]]
|
end,
|
||||||
|
once = true,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
view.focus()
|
view.focus()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -169,8 +169,13 @@ function M.setup(opts)
|
|||||||
|
|
||||||
if M.enable then
|
if M.enable then
|
||||||
log.line("diagnostics", "setup")
|
log.line("diagnostics", "setup")
|
||||||
vim.cmd "au DiagnosticChanged * lua require'nvim-tree.diagnostics'.update()"
|
a.nvim_create_autocmd("DiagnosticChanged", {
|
||||||
vim.cmd "au User CocDiagnosticChange lua require'nvim-tree.diagnostics'.update()"
|
callback = M.update,
|
||||||
|
})
|
||||||
|
a.nvim_create_autocmd("User", {
|
||||||
|
pattern = "CocDiagnosticChange",
|
||||||
|
callback = M.update,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user