Reputation: 85
I am writing a lua function as custom command for my neovim config.
As the documentation says "The function is called with a single table argument that contains the following keys"
, but how can i access these keys when the table is not defined to a variable.
I tried calling the function like this:
vim.api.nvim_create_user_command('Build', fn_build(args), { nargs='?' })
and access the values with:
function fn_build(args)
run = args["args"] or nil
end
but i would get a nil error.
@Ani commented:
Try to see if there is anything close to this, in github.com/nanotee/nvim-lua-guide
I found the guide, but it didn't helped me to fix it. I'm not sure if desc
is the right variable to use. And how would i even use it. The guide says:
Two additional attributes are available:
- desc allows you to control what gets displayed when you run :command {cmd} on a command defined as a Lua callback. Similarly to keymaps, it is recommended to add a desc key to commands defined as Lua functions.
- force is equivalent to calling :command! and replaces a command if one with the same name already exists. It is true by default, unlike its Vimscript equivalent.
Am i blind and overseeing something?
Please point me in the right direction
Upvotes: 7
Views: 7647
Reputation: 41
As supermeia said your function should receive opts, you should be able to do it directly when you create your command if you don't want to change your fn_build
function, like this:
vim.api.nvim_create_user_command(
function(opts)
fn_build(opts.args)
end,
{ nargs = '?' }
)
Upvotes: 2
Reputation: 21
fn_build should receive opts as an argument try the following:
function fn_build(opts)
run = opts.args or nil
end
Upvotes: 2
Reputation: 63
You are calling the function fn_build
and registering the return value. Instead you should just pass the function as a parameter. Try the following
vim.api.nvim_create_user_command('Build', fn_build, { nargs='?' })
Upvotes: 4