Skip to content

Commit

Permalink
feat(tabs): provide an option to automatically enable the plugin (#166)
Browse files Browse the repository at this point in the history
## 📃 Summary

Now that we have tabs support, we can provide a way to automatically
enable the plugin when entering a new tab, similarly to when entering
Neovim.
  • Loading branch information
shortcuts authored Feb 4, 2023
1 parent 6456975 commit 87fac46
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ require("no-neck-pain").setup({
debug = false,
-- When `true`, enables the plugin when you start Neovim.
enableOnVimEnter = false,
-- When `true`, enables the plugin when you enter a new Tab.
-- note: it does not trigger if it's an existing tab, to prevent unwanted interfer with user's decisions.
enableOnTabEnter = false,
-- The width of the focused window that will be centered, accepted values are:
-- - Any integer > 0.
-- - "textwidth", which retrieves the value of the `vim.bo.textwidth` option.
Expand Down
3 changes: 3 additions & 0 deletions doc/no-neck-pain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Default values:
debug = false,
-- When `true`, enables the plugin when you start Neovim.
enableOnVimEnter = false,
-- When `true`, enables the plugin when you enter a new Tab.
-- note: it does not trigger if it's an existing tab, to prevent unwanted interfer with user's decisions.
enableOnTabEnter = false,
-- The width of the focused window that will be centered, accepted values are:
-- - Any integer > 0.
-- - "textwidth", which retrieves the value of the `vim.bo.textwidth` option.
Expand Down
3 changes: 3 additions & 0 deletions lua/no-neck-pain/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ NoNeckPain.options = {
debug = false,
-- When `true`, enables the plugin when you start Neovim.
enableOnVimEnter = false,
-- When `true`, enables the plugin when you enter a new Tab.
-- note: it does not trigger if it's an existing tab, to prevent unwanted interfer with user's decisions.
enableOnTabEnter = false,
-- The width of the focused window that will be centered, accepted values are:
-- - Any integer > 0.
-- - "textwidth", which retrieves the value of the `vim.bo.textwidth` option.
Expand Down
28 changes: 21 additions & 7 deletions lua/no-neck-pain/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,15 @@ end
function NoNeckPain.setup(opts)
_G.NoNeckPain.config = require("no-neck-pain.config").setup(opts)

if _G.NoNeckPain.config.enableOnVimEnter or _G.NoNeckPain.config.enableOnTabEnter then
vim.api.nvim_create_augroup("NoNeckPainAutocmd", { clear = true })
end

if _G.NoNeckPain.config.enableOnVimEnter then
vim.api.nvim_create_augroup("NoNeckPainBufWinEnter", { clear = true })
vim.api.nvim_create_autocmd({ "BufWinEnter" }, {
group = "NoNeckPainBufWinEnter",
pattern = "*",
callback = function(p)
vim.schedule(function()
-- in the `enableOnVimEnter` hooks. It exists to prevent
-- conflicts with other plugins:
-- netrw: it works
-- dashboard: we skip until we open an other buffer
-- nvim-tree: we skip until we open an other buffer
if _G.NoNeckPain.state ~= nil and _G.NoNeckPain.state.enabled == true then
return
end
Expand All @@ -74,9 +71,26 @@ function NoNeckPain.setup(opts)
vim.api.nvim_del_autocmd(p.id)
end)
end,
group = "NoNeckPainAutocmd",
desc = "Triggers until it find the correct moment/buffer to enable the plugin.",
})
end

if _G.NoNeckPain.config.enableOnTabEnter then
vim.api.nvim_create_autocmd({ "TabNewEntered" }, {
callback = function()
vim.schedule(function()
if vim.bo.filetype == "dashboard" or vim.bo.filetype == "NvimTree" then
return
end

NoNeckPain.enable()
end)
end,
group = "NoNeckPainAutocmd",
desc = "Enables the plugin when entering a new tab.",
})
end
end

_G.NoNeckPain = NoNeckPain
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_auto_open.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ vim.cmd([[let &rtp.=','.getcwd()]])
vim.cmd("set rtp+=deps/mini.nvim")

-- Auto open enabled for the test
require("no-neck-pain").setup({ width = 50, enableOnVimEnter = true })
require("no-neck-pain").setup({ width = 50, enableOnVimEnter = true, enableOnTabEnter = true })
require("mini.test").setup()
require("mini.doc").setup()
3 changes: 3 additions & 0 deletions tests/test_API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ T["setup"]["sets exposed methods and default options value"] = function()

eq_config(child, "width", 100)
eq_config(child, "enableOnVimEnter", false)
eq_config(child, "enableOnTabEnter", false)
eq_config(child, "toggleMapping", "<Leader>np")
eq_config(child, "debug", false)
eq_config(child, "disableOnLastBuffer", false)
Expand Down Expand Up @@ -115,6 +116,7 @@ T["setup"]["overrides default values"] = function()
child.lua([[require('no-neck-pain').setup({
width = 42,
enableOnVimEnter = true,
enableOnTabEnter = true,
toggleMapping = "<Leader>kz",
debug = true,
disableOnLastBuffer = true,
Expand All @@ -123,6 +125,7 @@ T["setup"]["overrides default values"] = function()

eq_config(child, "width", 42)
eq_config(child, "enableOnVimEnter", true)
eq_config(child, "enableOnTabEnter", true)
eq_config(child, "toggleMapping", "<Leader>kz")
eq_config(child, "debug", true)
eq_config(child, "disableOnLastBuffer", true)
Expand Down
67 changes: 67 additions & 0 deletions tests/test_tabs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,71 @@ T["tabs"]["previous tab kept side buffers if enabled"] = function()
eq_state(child, "activeTab", 1)
end

T["TabNewEntered"] = MiniTest.new_set()

T["TabNewEntered"]["starts the plugin on new tab"] = function()
child.restart({ "-u", "scripts/test_auto_open.lua" })

eq_state(child, "enabled", true)

-- tab 1
eq(
child.lua_get("vim.api.nvim_tabpage_list_wins(_G.NoNeckPain.state.activeTab)"),
{ 1001, 1000, 1002 }
)

eq_state(child, "activeTab", 1)

-- tab 2
child.cmd("tabnew")
eq_state(child, "activeTab", 2)

eq(
child.lua_get("vim.api.nvim_tabpage_list_wins(_G.NoNeckPain.state.activeTab)"),
{ 1004, 1003, 1005 }
)

child.stop()
end

T["TabNewEntered"]["does not re-enable if the user disables it"] = function()
child.restart({ "-u", "scripts/test_auto_open.lua" })

eq_state(child, "enabled", true)

-- tab 1
eq(
child.lua_get("vim.api.nvim_tabpage_list_wins(_G.NoNeckPain.state.activeTab)"),
{ 1001, 1000, 1002 }
)

eq_state(child, "activeTab", 1)

-- tab 2
child.cmd("tabnew")
eq_state(child, "activeTab", 2)

eq(
child.lua_get("vim.api.nvim_tabpage_list_wins(_G.NoNeckPain.state.activeTab)"),
{ 1004, 1003, 1005 }
)

child.cmd("NoNeckPain")

eq(child.lua_get("vim.api.nvim_tabpage_list_wins(_G.NoNeckPain.state.activeTab)"), { 1003 })
eq_state(child, "activeTab", 2)

-- tab 1
child.cmd("tabprevious")
eq_state(child, "activeTab", 1)

-- tab 2
child.cmd("tabnext")
eq_state(child, "activeTab", 2)

eq(child.lua_get("vim.api.nvim_tabpage_list_wins(_G.NoNeckPain.state.activeTab)"), { 1003 })

child.stop()
end

return T

0 comments on commit 87fac46

Please sign in to comment.