Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept for running command in visual mode #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lua/whop/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ end

--- Runs a selected command
--- @param cmd string: command to run
whop.run_cmd = function(cmd)
--- @param mode string: current mode (n or v)
whop.run_cmd = function(cmd, mode)
if mode == "v" then
local range_cmd = cmd:gsub("%%", "'<,'>")
vim.cmd(range_cmd)
return
end
if type(cmd) == "function" then
cmd()
elseif type(cmd) == "string" then
Expand All @@ -62,21 +68,25 @@ whop.select = function()
return item
end,
}, function(choice)
whop.find_and_run_cmd(choice)
local cmd = whop.find_cmd(choice)
if cmd == nil then
return
end
whop.run_cmd(cmd.cmd, "n")
end)
end

--- Find a command by name and run it.
--- Do nothing if the command was not found.
--- @param name string: the name of the command to run
whop.find_and_run_cmd = function(name)
whop.find_cmd = function(name)
for _, v in ipairs(whop._commands) do
if v.name == name then
whop.run_cmd(v.cmd)
return true
return v
end
end
return false
print("Command not found")
return nil
end

return whop
14 changes: 12 additions & 2 deletions plugin/whop.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
vim.api.nvim_create_user_command("Whop", function(arg)
-- print(arg.mode)
local mode = "n"
if arg.range ~= 0 then
mode = "v"
end
local whop = require("whop")
if arg.args ~= "" then
whop.find_and_run_cmd(arg.args)
local cmd = whop.find_cmd(arg.args)
if cmd == nil then
return
end
whop.run_cmd(cmd.cmd, mode)
return
end
whop.select()
whop.select(mode)
end, {
range = true,
nargs = "?",
complete = function()
local whop = require("whop")
Expand Down