Skip to content

Commit

Permalink
feat: one usercommand + autocomplete (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Will Hopkins <willothyh@gmail.com>
Co-authored-by: tris203 <admin@snappeh.com>
  • Loading branch information
3 people authored Dec 30, 2023
1 parent ce2a3f9 commit 1ced8d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
26 changes: 21 additions & 5 deletions lua/hawtkeys/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,27 @@ function M.setup(config)
group = auGroup,
})

vim.api.nvim_create_user_command(
"Hawtkeys",
"lua require('hawtkeys.ui').show()",
{}
)
vim.api.nvim_create_user_command("Hawtkeys", function(args)
local cmd = args.fargs[1]
and args.fargs[1]:gsub("^%s+", ""):gsub("%s$", ""):lower()
if cmd == "all" then
require("hawtkeys.ui").show_all()
elseif cmd == "dupes" then
require("hawtkeys.ui").show_dupes()
else
require("hawtkeys.ui").show()
end
end, {
desc = "Show Hawtkeys",
nargs = "?",
complete = function(arg_lead, _, pos)
if (pos - 9) > #arg_lead then
return {}
end
return { "all", "dupes", "search" }
end,
})

vim.api.nvim_create_user_command(
"HawtkeysAll",
"lua require('hawtkeys.ui').show_all()",
Expand Down
25 changes: 13 additions & 12 deletions tests/hawtkeys/init_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
local hawtkeys = require("hawtkeys")
---@diagnostic disable-next-line: undefined-field
local eq = assert.are.same
---@diagnostic disable-next-line: undefined-field
local falsy = assert.falsy

local userCommands = {
["Hawtkeys"] = "lua require('hawtkeys.ui').show()",
["HawtkeysAll"] = "lua require('hawtkeys.ui').show_all()",
["HawtkeysDupes"] = "lua require('hawtkeys.ui').show_dupes()",
["Hawtkeys"] = { definition = "Show Hawtkeys", nargs = "?" },
["HawtkeysAll"] = {
definition = "lua require('hawtkeys.ui').show_all()",
nargs = "0",
},
["HawtkeysDupes"] = {
definition = "lua require('hawtkeys.ui').show_dupes()",
nargs = "0",
},
}

describe("set up function", function()
Expand Down Expand Up @@ -69,17 +74,13 @@ describe("set up function", function()
end)

it("User commands should be available after setup", function()
local commandspresetup = vim.api.nvim_get_commands({})
hawtkeys.setup({})

local commandsPostSetup = vim.api.nvim_get_commands({})
-- Check that the commands are not present before setup
for command, _ in ipairs(userCommands) do
falsy(commandspresetup[command])
end
-- Check that the commands are present after setup
for command, action in ipairs(userCommands) do
eq(action, commandsPostSetup[command].definition)
for command, action in pairs(userCommands) do
eq(action.definition, commandsPostSetup[command].definition)
eq(action.nargs, commandsPostSetup[command].nargs)
end
end)
end)

0 comments on commit 1ced8d4

Please sign in to comment.