Skip to content

Commit 4601444

Browse files
authored
refactor: use lua api for user commands and autocommands (#1206)
BREAKING: plugin now requires nvim-0.7
1 parent 90d7b8e commit 4601444

File tree

5 files changed

+81
-44
lines changed

5 files changed

+81
-44
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Notice
66

7-
This plugin requires [neovim >=0.6.0](https://github.com/neovim/neovim/wiki/Installing-Neovim).
7+
This plugin requires [neovim >=0.7.0](https://github.com/neovim/neovim/wiki/Installing-Neovim).
88

99
If you have issues since the recent setup migration, check out [this guide](https://github.com/kyazdani42/nvim-tree.lua/issues/674)
1010

lua/nvim-tree.lua

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ local utils = require "nvim-tree.utils"
1010
local change_dir = require "nvim-tree.actions.change-dir"
1111
local legacy = require "nvim-tree.legacy"
1212
local core = require "nvim-tree.core"
13+
local reloaders = require "nvim-tree.actions.reloaders"
14+
local copy_paste = require "nvim-tree.actions.copy-paste"
15+
local collapse_all = require "nvim-tree.actions.collapse-all"
1316

1417
local _config = {}
1518

@@ -70,7 +73,7 @@ end
7073

7174
function M.tab_change()
7275
if view.is_visible { any_tabpage = true } then
73-
local bufname = vim.api.nvim_buf_get_name(0)
76+
local bufname = api.nvim_buf_get_name(0)
7477
if bufname:match "Neogit" ~= nil or bufname:match "--graph" ~= nil then
7578
return
7679
end
@@ -153,7 +156,7 @@ end
153156

154157
local prev_line
155158
function M.place_cursor_on_node()
156-
local l = vim.api.nvim_win_get_cursor(0)[1]
159+
local l = api.nvim_win_get_cursor(0)[1]
157160
if l == prev_line then
158161
return
159162
end
@@ -252,19 +255,29 @@ local function manage_netrw(disable_netrw, hijack_netrw)
252255
end
253256

254257
local function setup_vim_commands()
255-
vim.cmd [[
256-
command! -nargs=? -complete=dir NvimTreeOpen lua require'nvim-tree'.open("<args>")
257-
command! NvimTreeClose lua require'nvim-tree.view'.close()
258-
command! NvimTreeToggle lua require'nvim-tree'.toggle(false)
259-
command! NvimTreeFocus lua require'nvim-tree'.focus()
260-
command! NvimTreeRefresh lua require'nvim-tree.actions.reloaders'.reload_explorer()
261-
command! NvimTreeClipboard lua require'nvim-tree.actions.copy-paste'.print_clipboard()
262-
command! NvimTreeFindFile lua require'nvim-tree'.find_file(true)
263-
command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true)
264-
command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize("<args>")
265-
command! NvimTreeCollapse lua require'nvim-tree.actions.collapse-all'.fn()
266-
command! NvimTreeCollapseKeepBuffers lua require'nvim-tree.actions.collapse-all'.fn(true)
267-
]]
258+
api.nvim_create_user_command("NvimTreeOpen", function(res)
259+
M.open(res.args)
260+
end, { nargs = "?", complete = "dir" })
261+
api.nvim_create_user_command("NvimTreeClose", view.close, {})
262+
api.nvim_create_user_command("NvimTreeToggle", function()
263+
M.toggle(false)
264+
end, {})
265+
api.nvim_create_user_command("NvimTreeFocus", M.focus, {})
266+
api.nvim_create_user_command("NvimTreeRefresh", reloaders.reload_explorer, {})
267+
api.nvim_create_user_command("NvimTreeClipboard", copy_paste.print_clipboard, {})
268+
api.nvim_create_user_command("NvimTreeFindFile", function()
269+
M.find_file(true)
270+
end, {})
271+
api.nvim_create_user_command("NvimTreeFindFileToggle", function()
272+
M.toggle(true)
273+
end, {})
274+
api.nvim_create_user_command("NvimTreeResize", function(res)
275+
M.resize(res.args)
276+
end, { nargs = 1 })
277+
api.nvim_create_user_command("NvimTreeCollapse", collapse_all.fn, {})
278+
api.nvim_create_user_command("NvimTreeCollapseKeepBuffers", function()
279+
collapse_all.fn(true)
280+
end, {})
268281
end
269282

270283
function M.change_dir(name)
@@ -276,40 +289,53 @@ function M.change_dir(name)
276289
end
277290

278291
local function setup_autocommands(opts)
279-
vim.cmd "augroup NvimTree"
280-
vim.cmd "autocmd!"
292+
local augroup_id = api.nvim_create_augroup("NvimTree", {})
293+
local function create_nvim_tree_autocmd(name, custom_opts)
294+
local default_opts = { group = augroup_id }
295+
api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
296+
end
281297

282298
-- reset highlights when colorscheme is changed
283-
vim.cmd "au ColorScheme * lua require'nvim-tree'.reset_highlight()"
299+
create_nvim_tree_autocmd("ColorScheme", { callback = M.reset_highlight })
300+
284301
if opts.auto_reload_on_write then
285-
vim.cmd "au BufWritePost * lua require'nvim-tree.actions.reloaders'.reload_explorer()"
302+
create_nvim_tree_autocmd("BufWritePost", { callback = reloaders.reload_explorer })
286303
end
287-
vim.cmd "au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree.actions.reloaders'.reload_git()"
304+
create_nvim_tree_autocmd("User", {
305+
pattern = { "FugitiveChanged", "NeogitStatusRefreshed" },
306+
callback = reloaders.reload_git,
307+
})
288308

289309
if opts.open_on_tab then
290-
vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()"
310+
create_nvim_tree_autocmd("TabEnter", { callback = M.tab_change })
291311
end
292312
if opts.hijack_cursor then
293-
vim.cmd "au CursorMoved NvimTree_* lua require'nvim-tree'.place_cursor_on_node()"
313+
create_nvim_tree_autocmd("CursorMoved", { pattern = "NvimTree_*", callback = M.place_cursor_on_node })
294314
end
295315
if opts.update_cwd then
296-
vim.cmd "au DirChanged * lua require'nvim-tree'.change_dir(vim.loop.cwd())"
316+
create_nvim_tree_autocmd("DirChanged", {
317+
callback = function()
318+
M.change_dir(vim.loop.cwd())
319+
end,
320+
})
297321
end
298322
if opts.update_focused_file.enable then
299-
vim.cmd "au BufEnter * lua require'nvim-tree'.find_file(false)"
323+
create_nvim_tree_autocmd("BufEnter", {
324+
callback = function()
325+
M.find_file(false)
326+
end,
327+
})
300328
end
301329

302330
if not opts.actions.open_file.quit_on_open then
303-
vim.cmd "au BufWipeout NvimTree_* lua require'nvim-tree.view'._prevent_buffer_override()"
331+
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view._prevent_buffer_override })
304332
else
305-
vim.cmd "au BufWipeout NvimTree_* lua require'nvim-tree.view'.abandon_current_window()"
333+
create_nvim_tree_autocmd("BufWipeout", { pattern = "NvimTree_*", callback = view.abandon_current_window })
306334
end
307335

308336
if opts.hijack_directories.enable then
309-
vim.cmd "au BufEnter,BufNewFile * lua require'nvim-tree'.open_on_directory()"
337+
create_nvim_tree_autocmd({ "BufEnter", "BufNewFile" }, { callback = M.open_on_directory })
310338
end
311-
312-
vim.cmd "augroup end"
313339
end
314340

315341
local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
@@ -476,6 +502,11 @@ local function validate_options(conf)
476502
end
477503

478504
function M.setup(conf)
505+
if vim.fn.has "nvim-0.7" == 0 then
506+
utils.warn "nvim-tree.lua requires Neovim 0.7 or higher"
507+
return
508+
end
509+
479510
legacy.migrate_legacy_options(conf or {})
480511

481512
validate_options(conf)

lua/nvim-tree/actions/file-popup.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ function M.toggle_file_info(node)
7272

7373
setup_window(node)
7474

75-
vim.cmd [[
76-
augroup NvimTreeRemoveFilePopup
77-
au CursorMoved * lua require'nvim-tree.actions.file-popup'.close_popup()
78-
augroup END
79-
]]
75+
a.nvim_create_autocmd("CursorMoved", {
76+
group = a.nvim_create_augroup("NvimTreeRemoveFilePopup", {}),
77+
callback = M.close_popup,
78+
})
8079
end
8180

8281
return M

lua/nvim-tree/actions/open-file.lua

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,15 @@ function M.fn(mode, filename)
257257
if mode == "preview" then
258258
if not buf_loaded then
259259
vim.bo.bufhidden = "delete"
260-
vim.cmd [[
261-
augroup RemoveBufHidden
262-
autocmd!
263-
autocmd TextChanged <buffer> setlocal bufhidden= | autocmd! RemoveBufHidden
264-
autocmd TextChangedI <buffer> setlocal bufhidden= | autocmd! RemoveBufHidden
265-
augroup end
266-
]]
260+
261+
api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
262+
group = api.nvim_create_augroup("RemoveBufHidden", {}),
263+
buffer = api.nvim_get_current_buf(),
264+
callback = function()
265+
vim.bo.bufhidden = ""
266+
end,
267+
once = true,
268+
})
267269
end
268270
view.focus()
269271
return

lua/nvim-tree/diagnostics.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,13 @@ function M.setup(opts)
169169

170170
if M.enable then
171171
log.line("diagnostics", "setup")
172-
vim.cmd "au DiagnosticChanged * lua require'nvim-tree.diagnostics'.update()"
173-
vim.cmd "au User CocDiagnosticChange lua require'nvim-tree.diagnostics'.update()"
172+
a.nvim_create_autocmd("DiagnosticChanged", {
173+
callback = M.update,
174+
})
175+
a.nvim_create_autocmd("User", {
176+
pattern = "CocDiagnosticChange",
177+
callback = M.update,
178+
})
174179
end
175180
end
176181

0 commit comments

Comments
 (0)