Skip to content

Commit

Permalink
feat(mappins): allow custom values for widthUp and widthDown
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Nov 23, 2023
1 parent 2bcb6b7 commit d582f3d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 12 deletions.
21 changes: 17 additions & 4 deletions lua/no-neck-pain/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,21 @@ local function registerMappings(options, mappings)
return
end

assert(type(options[name]) == "string", string.format("`%s` must be a string", name))
vim.api.nvim_set_keymap("n", options[name], command, { silent = true })
if (name == "widthUp" or name == "widthDown") and type(options[name]) ~= "string" then
assert(
type(options[name]) == "table"
and options[name]["mapping"] ~= nil
and options[name]["value"] ~= nil,
string.format(
"`%s` must be a string or a table with the following properties {mapping: 'your_mapping', value: 5}",
name
)
)
vim.api.nvim_set_keymap("n", options[name].mapping, command, { silent = true })
else
assert(type(options[name]) == "string", string.format("`%s` must be a string", name))
vim.api.nvim_set_keymap("n", options[name], command, { silent = true })
end
end
end

Expand Down Expand Up @@ -205,11 +218,11 @@ NoNeckPain.options = {
toggle = "<Leader>np",
-- Sets a global mapping to Neovim, which allows you to increase the width (+5) of the main window.
-- When `false`, the mapping is not created.
--- @type string
--- @type string | { mapping: string, value: number }
widthUp = "<Leader>n=",
-- Sets a global mapping to Neovim, which allows you to decrease the width (-5) of the main window.
-- When `false`, the mapping is not created.
--- @type string
--- @type string | { mapping: string, value: number }
widthDown = "<Leader>n-",
-- Sets a global mapping to Neovim, which allows you to toggle the scratchpad feature.
-- When `false`, the mapping is not created.
Expand Down
8 changes: 6 additions & 2 deletions plugin/no-neck-pain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ else
end, { desc = "Resizes the main centered window for the given argument.", nargs = 1 })

vim.api.nvim_create_user_command("NoNeckPainWidthUp", function()
require("no-neck-pain").resize(_G.NoNeckPain.config.width + 5)
local increment = _G.NoNeckPain.config.mappings.widthUp.value or 5

require("no-neck-pain").resize(_G.NoNeckPain.config.width + increment)
end, { desc = "Increase the width of the main window by 5." })

vim.api.nvim_create_user_command("NoNeckPainWidthDown", function()
require("no-neck-pain").resize(_G.NoNeckPain.config.width - 5)
local decrement = _G.NoNeckPain.config.mappings.widthDown.value or 5

require("no-neck-pain").resize(_G.NoNeckPain.config.width - decrement)
end, { desc = "Decrease the width of the main window by 5." })

vim.api.nvim_create_user_command("NoNeckPainScratchPad", function()
Expand Down
72 changes: 66 additions & 6 deletions tests/test_mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ T["setup"]["overrides default values"] = function()
})
end

T["setup"]["allow widthUp and widthDown to be configurable"] = function()
child.lua([[require('no-neck-pain').setup({
mappings = {
enabled = true,
toggle = "<Leader>kz",
widthUp = {mapping = "<Leader>k-", value = 12},
widthDown = {mapping = "<Leader>k=", value = 99},
scratchPad = "<Leader>ks"
}
})]])

eq_config(child, "mappings", {
enabled = true,
scratchPad = "<Leader>ks",
toggle = "<Leader>kz",
widthDown = {
mapping = "<Leader>k=",
value = 99,
},
widthUp = {
mapping = "<Leader>k-",
value = 12,
},
})
end

T["setup"]["does not create mappings if false"] = function()
child.lua([[require('no-neck-pain').setup({
mappings = {
Expand Down Expand Up @@ -138,8 +164,8 @@ end

T["setup"]["increase the width with mapping"] = function()
child.lua([[
require('no-neck-pain').setup({width=50,mappings={enabled=true,widthUp="nn"}})
require('no-neck-pain').enable()
require('no-neck-pain').setup({width=50,mappings={enabled=true,widthUp="nn"}})
require('no-neck-pain').enable()
]])

eq_global(child, "_G.NoNeckPain.config.width", 50)
Expand All @@ -153,10 +179,27 @@ T["setup"]["increase the width with mapping"] = function()
eq_global(child, "_G.NoNeckPain.config.width", 70)
end

T["setup"]["increase the width with custom mapping and value"] = function()
child.lua([[
require('no-neck-pain').setup({width=50,mappings={enabled=true,widthUp={mapping="nn", value=10}}})
require('no-neck-pain').enable()
]])

eq_global(child, "_G.NoNeckPain.config.width", 50)
eq(helpers.winsInTab(child), { 1001, 1000, 1002 })

child.lua("vim.api.nvim_input('nn')")
child.lua("vim.api.nvim_input('nn')")
child.lua("vim.api.nvim_input('nn')")
child.lua("vim.api.nvim_input('nn')")

eq_global(child, "_G.NoNeckPain.config.width", 90)
end

T["setup"]["decrease the width with mapping"] = function()
child.lua([[
require('no-neck-pain').setup({width=50,mappings={enabled=true,widthDown="nn"}})
require('no-neck-pain').enable()
require('no-neck-pain').setup({width=50,mappings={enabled=true,widthDown="nn"}})
require('no-neck-pain').enable()
]])

eq_global(child, "_G.NoNeckPain.config.width", 50)
Expand All @@ -170,10 +213,27 @@ T["setup"]["decrease the width with mapping"] = function()
eq_global(child, "_G.NoNeckPain.config.width", 30)
end

T["setup"]["decrease the width with custom mapping and value"] = function()
child.lua([[
require('no-neck-pain').setup({width=50,mappings={enabled=true,widthDown={mapping="nn",value=7}}})
require('no-neck-pain').enable()
]])

eq_global(child, "_G.NoNeckPain.config.width", 50)
eq(helpers.winsInTab(child), { 1001, 1000, 1002 })

child.lua("vim.api.nvim_input('nn')")
child.lua("vim.api.nvim_input('nn')")
child.lua("vim.api.nvim_input('nn')")
child.lua("vim.api.nvim_input('nn')")

eq_global(child, "_G.NoNeckPain.config.width", 22)
end

T["setup"]["toggles scratchPad"] = function()
child.lua([[
require('no-neck-pain').setup({width=50,mappings={enabled=true,scratchPad="ns"}})
require('no-neck-pain').enable()
require('no-neck-pain').setup({width=50,mappings={enabled=true,scratchPad="ns"}})
require('no-neck-pain').enable()
]])

eq_global(child, "_G.NoNeckPain.config.buffers.scratchPad.enabled", false)
Expand Down

0 comments on commit d582f3d

Please sign in to comment.