mirror of
https://github.com/neovim/neovim.git
synced 2025-09-18 17:28:23 +00:00
API: nvim_get_commands(): always return keys
- Always return all keys, with at least NIL value. - Require `opts` param to be {"builtin":false} - Validate `opts` param
This commit is contained in:
@@ -479,12 +479,11 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err)
|
|||||||
return keymap_array(mode, buf);
|
return keymap_array(mode, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a list of dictionaries describing buffer-local commands.
|
/// Gets a list of maps describing buffer-local |user-commands|.
|
||||||
/// The "buffer" key in the returned dictionary reflects the buffer
|
|
||||||
/// handle where the command is present.
|
|
||||||
///
|
///
|
||||||
/// @param buffer Buffer handle.
|
/// @param buffer Buffer handle.
|
||||||
/// @param opts Optional parameters, currently always
|
/// @param opts Optional parameters. Currently only supports
|
||||||
|
/// {"builtin":false}
|
||||||
/// @param[out] err Error details, if any.
|
/// @param[out] err Error details, if any.
|
||||||
///
|
///
|
||||||
/// @returns Array of dictionaries describing commands.
|
/// @returns Array of dictionaries describing commands.
|
||||||
@@ -492,6 +491,25 @@ ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts,
|
|||||||
Error *err)
|
Error *err)
|
||||||
FUNC_API_SINCE(4)
|
FUNC_API_SINCE(4)
|
||||||
{
|
{
|
||||||
|
for (size_t i = 0; i < opts.size; i++) {
|
||||||
|
String k = opts.items[i].key;
|
||||||
|
Object v = opts.items[i].value;
|
||||||
|
if (!strequal("builtin", k.data)) {
|
||||||
|
api_set_error(err, kErrorTypeValidation, "unexpected key: %s",
|
||||||
|
k.data);
|
||||||
|
return (Array)ARRAY_DICT_INIT;
|
||||||
|
}
|
||||||
|
if (v.type != kObjectTypeBoolean || v.data.boolean != false) {
|
||||||
|
api_set_error(err, kErrorTypeValidation,
|
||||||
|
"builtin commands not supported yet");
|
||||||
|
return (Array)ARRAY_DICT_INIT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buffer == -1) {
|
||||||
|
return commands_array(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
buf_T *buf = find_buffer_by_handle(buffer, err);
|
buf_T *buf = find_buffer_by_handle(buffer, err);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
return (Array)ARRAY_DICT_INIT;
|
return (Array)ARRAY_DICT_INIT;
|
||||||
|
@@ -707,7 +707,7 @@ String cstr_to_string(const char *str)
|
|||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
return (String){
|
return (String){
|
||||||
.data = xmemdupz(str, len),
|
.data = xmemdupz(str, len),
|
||||||
.size = len
|
.size = len,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -959,17 +959,17 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode)
|
|||||||
return keymap_array(mode, NULL);
|
return keymap_array(mode, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a list of dictionaries describing global(non-buffer) commands.
|
/// Gets a list of maps describing global |user-commands|.
|
||||||
///
|
///
|
||||||
/// @param opts Holds the API Metadata describing what type of commands
|
/// @param opts Optional parameters. Currently only supports
|
||||||
/// are needed.
|
/// {"builtin":false}
|
||||||
/// @param[out] err Error details, if any.
|
/// @param[out] err Error details, if any.
|
||||||
///
|
///
|
||||||
/// @returns Array of dictionaries describing commands.
|
/// @returns Array of dictionaries describing commands.
|
||||||
ArrayOf(Dictionary) nvim_get_commands(Dictionary opts, Error *err)
|
ArrayOf(Dictionary) nvim_get_commands(Dictionary opts, Error *err)
|
||||||
FUNC_API_SINCE(4)
|
FUNC_API_SINCE(4)
|
||||||
{
|
{
|
||||||
return commands_array(NULL);
|
return nvim_buf_get_commands(-1, opts, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a 2-tuple (Array), where item 0 is the current channel id and item
|
/// Returns a 2-tuple (Array), where item 0 is the current channel id and item
|
||||||
|
@@ -9972,28 +9972,26 @@ bool cmd_can_preview(char_u *cmd)
|
|||||||
/// Gets a list of maps describing user-commands defined for buffer `buf`
|
/// Gets a list of maps describing user-commands defined for buffer `buf`
|
||||||
/// or defined globally if `buf` is NULL.
|
/// or defined globally if `buf` is NULL.
|
||||||
///
|
///
|
||||||
/// @param buf Buffer to inspect, or NULL to get global commands.
|
/// @param buf Buffer to inspect, or NULL to get global user-commands.
|
||||||
///
|
///
|
||||||
/// @return Array of dictionaries describing commands
|
/// @return Array of dictionaries describing commands
|
||||||
ArrayOf(Dictionary) commands_array(buf_T *buf)
|
ArrayOf(Dictionary) commands_array(buf_T *buf)
|
||||||
{
|
{
|
||||||
Array rv = ARRAY_DICT_INIT;
|
Array rv = ARRAY_DICT_INIT;
|
||||||
garray_T *gap;
|
Object obj = NIL;
|
||||||
if (buf == NULL) {
|
char str[10];
|
||||||
gap = &ucmds;
|
garray_T *gap = (buf == NULL) ? &ucmds : &buf->b_ucmds;
|
||||||
} else {
|
|
||||||
gap = &buf->b_ucmds;
|
|
||||||
}
|
|
||||||
for (int i = 0; i < gap->ga_len; i++) {
|
for (int i = 0; i < gap->ga_len; i++) {
|
||||||
char arg[2] = { 0, 0 };
|
char arg[2] = { 0, 0 };
|
||||||
Dictionary d = ARRAY_DICT_INIT;
|
Dictionary d = ARRAY_DICT_INIT;
|
||||||
char Range[10] = "";
|
|
||||||
ucmd_T *cmd = USER_CMD_GA(gap, i);
|
ucmd_T *cmd = USER_CMD_GA(gap, i);
|
||||||
|
|
||||||
// Name
|
|
||||||
PUT(d, "name", STRING_OBJ(cstr_to_string((char *)cmd->uc_name)));
|
PUT(d, "name", STRING_OBJ(cstr_to_string((char *)cmd->uc_name)));
|
||||||
|
PUT(d, "definition", STRING_OBJ(cstr_to_string((char *)cmd->uc_rep)));
|
||||||
|
PUT(d, "script_id", INTEGER_OBJ(cmd->uc_scriptID));
|
||||||
|
|
||||||
// Argument
|
// "nargs" key
|
||||||
switch (cmd->uc_argt & (EXTRA|NOSPC|NEEDARG)) {
|
switch (cmd->uc_argt & (EXTRA|NOSPC|NEEDARG)) {
|
||||||
case 0: arg[0] = '0'; break;
|
case 0: arg[0] = '0'; break;
|
||||||
case(EXTRA): arg[0] = '*'; break;
|
case(EXTRA): arg[0] = '*'; break;
|
||||||
@@ -10001,52 +9999,47 @@ ArrayOf(Dictionary) commands_array(buf_T *buf)
|
|||||||
case(EXTRA|NEEDARG): arg[0] = '+'; break;
|
case(EXTRA|NEEDARG): arg[0] = '+'; break;
|
||||||
case(EXTRA|NOSPC|NEEDARG): arg[0] = '1'; break;
|
case(EXTRA|NOSPC|NEEDARG): arg[0] = '1'; break;
|
||||||
}
|
}
|
||||||
PUT(d, "nargs", STRING_OBJ(cstr_to_string((char *)arg)));
|
PUT(d, "nargs", STRING_OBJ(cstr_to_string(arg)));
|
||||||
|
|
||||||
// Definition
|
|
||||||
PUT(d, "definition", STRING_OBJ(cstr_to_string((char *)cmd->uc_rep)));
|
|
||||||
|
|
||||||
// Complete
|
|
||||||
char *cmd_compl = get_command_complete(cmd->uc_compl);
|
char *cmd_compl = get_command_complete(cmd->uc_compl);
|
||||||
if (cmd_compl != NULL) {
|
PUT(d, "complete", (cmd_compl == NULL
|
||||||
PUT(d, "complete", STRING_OBJ(cstr_to_string(cmd_compl)));
|
? NIL : STRING_OBJ(cstr_to_string(cmd_compl))));
|
||||||
}
|
PUT(d, "complete_arg", cmd->uc_compl_arg == NULL
|
||||||
|
? NIL : STRING_OBJ(cstr_to_string((char *)cmd->uc_compl_arg)));
|
||||||
|
|
||||||
// Complete Arg
|
obj = NIL;
|
||||||
if (cmd->uc_compl_arg != NULL) {
|
|
||||||
PUT(d, "complete_arg",
|
|
||||||
STRING_OBJ(cstr_to_string((char *)cmd->uc_compl_arg)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Range
|
|
||||||
if (cmd->uc_argt & (RANGE|COUNT)) {
|
|
||||||
if (cmd->uc_argt & COUNT) {
|
if (cmd->uc_argt & COUNT) {
|
||||||
// -count=N
|
if (cmd->uc_def >= 0) {
|
||||||
snprintf((char *)Range, sizeof(Range), "%" PRId64 "c",
|
snprintf(str, sizeof(str), "%" PRId64, (int64_t)cmd->uc_def);
|
||||||
(int64_t)cmd->uc_def);
|
obj = STRING_OBJ(cstr_to_string(str)); // -count=N
|
||||||
} else if (cmd->uc_argt & DFLALL) {
|
|
||||||
Range[0] = '%';
|
|
||||||
} else if (cmd->uc_def >= 0) {
|
|
||||||
// -range=N
|
|
||||||
snprintf((char *)Range, sizeof(Range), "%" PRId64 "",
|
|
||||||
(int64_t)cmd->uc_def);
|
|
||||||
} else {
|
} else {
|
||||||
Range[0] = '.';
|
obj = STRING_OBJ(cstr_to_string("0")); // -count
|
||||||
}
|
}
|
||||||
PUT(d, "range", STRING_OBJ(cstr_to_string((char *)Range)));
|
|
||||||
}
|
}
|
||||||
|
PUT(d, "count", obj);
|
||||||
|
|
||||||
// Address
|
obj = NIL;
|
||||||
|
if (cmd->uc_argt & RANGE) {
|
||||||
|
if (cmd->uc_argt & DFLALL) {
|
||||||
|
obj = STRING_OBJ(cstr_to_string("%")); // -range=%
|
||||||
|
} else if (cmd->uc_def >= 0) {
|
||||||
|
snprintf(str, sizeof(str), "%" PRId64, (int64_t)cmd->uc_def);
|
||||||
|
obj = STRING_OBJ(cstr_to_string(str)); // -range=N
|
||||||
|
} else {
|
||||||
|
obj = STRING_OBJ(cstr_to_string(".")); // -range
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PUT(d, "range", obj);
|
||||||
|
|
||||||
|
obj = NIL;
|
||||||
for (int j = 0; addr_type_complete[j].expand != -1; j++) {
|
for (int j = 0; addr_type_complete[j].expand != -1; j++) {
|
||||||
if (addr_type_complete[j].expand != ADDR_LINES
|
if (addr_type_complete[j].expand != ADDR_LINES
|
||||||
&& addr_type_complete[j].expand == cmd->uc_addr_type) {
|
&& addr_type_complete[j].expand == cmd->uc_addr_type) {
|
||||||
PUT(d, "addr", STRING_OBJ(cstr_to_string(addr_type_complete[j].name)));
|
obj = STRING_OBJ(cstr_to_string(addr_type_complete[j].name));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
PUT(d, "addr", obj);
|
||||||
// ScriptID
|
|
||||||
PUT(d, "script_id", INTEGER_OBJ(cmd->uc_scriptID));
|
|
||||||
|
|
||||||
ADD(rv, DICTIONARY_OBJ(d));
|
ADD(rv, DICTIONARY_OBJ(d));
|
||||||
}
|
}
|
||||||
|
@@ -1,74 +1,97 @@
|
|||||||
local helpers = require('test.functional.helpers')(after_each)
|
local helpers = require('test.functional.helpers')(after_each)
|
||||||
|
|
||||||
|
local NIL = helpers.NIL
|
||||||
local clear = helpers.clear
|
local clear = helpers.clear
|
||||||
local command = helpers.command
|
local command = helpers.command
|
||||||
local curbufmeths = helpers.curbufmeths
|
local curbufmeths = helpers.curbufmeths
|
||||||
local eq = helpers.eq
|
local eq = helpers.eq
|
||||||
|
local expect_err = helpers.expect_err
|
||||||
local meths = helpers.meths
|
local meths = helpers.meths
|
||||||
|
local source = helpers.source
|
||||||
|
|
||||||
describe('nvim_get_commands', function()
|
describe('nvim_get_commands', function()
|
||||||
local dummy_dict = {dummy=''}
|
|
||||||
local cmd_string = 'command Hello echo "Hello World"'
|
|
||||||
local cmd_string2 = 'command Pwd pwd'
|
|
||||||
local cmd_dict = {
|
local cmd_dict = {
|
||||||
name='Hello',
|
addr=NIL,
|
||||||
nargs='0',
|
complete=NIL,
|
||||||
script_id=0,
|
complete_arg=NIL,
|
||||||
|
count=NIL,
|
||||||
definition='echo "Hello World"',
|
definition='echo "Hello World"',
|
||||||
|
name='Hello',
|
||||||
|
nargs='1',
|
||||||
|
range=NIL,
|
||||||
|
script_id=0,
|
||||||
}
|
}
|
||||||
local cmd_dict2 = {
|
local cmd_dict2 = {
|
||||||
name='Pwd',
|
addr=NIL,
|
||||||
nargs='0',
|
complete=NIL,
|
||||||
script_id=0,
|
complete_arg=NIL,
|
||||||
|
count=NIL,
|
||||||
definition='pwd',
|
definition='pwd',
|
||||||
|
name='Pwd',
|
||||||
|
nargs='?',
|
||||||
|
range=NIL,
|
||||||
|
script_id=0,
|
||||||
}
|
}
|
||||||
before_each(clear)
|
before_each(clear)
|
||||||
it('gets empty list if no commands were defined', function()
|
it('gets empty list if no commands were defined', function()
|
||||||
eq({}, meths.get_commands(dummy_dict))
|
eq({}, meths.get_commands({builtin=false}))
|
||||||
end)
|
end)
|
||||||
it('gets user-def commands', function()
|
it('validates input', function()
|
||||||
-- Insert a command
|
expect_err('builtin commands not supported yet', meths.get_commands,
|
||||||
command(cmd_string)
|
{builtin=true})
|
||||||
eq({cmd_dict}, meths.get_commands(dummy_dict))
|
expect_err('unexpected key: foo', meths.get_commands,
|
||||||
-- Insert a another command
|
{foo='blah'})
|
||||||
command(cmd_string2);
|
|
||||||
eq({cmd_dict, cmd_dict2}, meths.get_commands(dummy_dict))
|
|
||||||
-- Delete a command
|
|
||||||
command('delcommand Pwd')
|
|
||||||
eq({cmd_dict}, meths.get_commands(dummy_dict))
|
|
||||||
end)
|
end)
|
||||||
it('considers different buffers', function()
|
it('gets global user-defined commands', function()
|
||||||
-- Insert a command
|
-- Define a command.
|
||||||
command('command -buffer Hello echo "Hello World"')
|
command('command -nargs=1 Hello echo "Hello World"')
|
||||||
eq({cmd_dict}, curbufmeths.get_commands(dummy_dict))
|
eq({cmd_dict}, meths.get_commands({builtin=false}))
|
||||||
-- Insert a another command
|
-- Define another command.
|
||||||
command('command -buffer Pwd pwd')
|
command('command -nargs=? Pwd pwd');
|
||||||
eq({cmd_dict, cmd_dict2}, curbufmeths.get_commands(dummy_dict))
|
eq({cmd_dict, cmd_dict2}, meths.get_commands({builtin=false}))
|
||||||
-- Delete a command
|
-- Delete a command.
|
||||||
command('delcommand Pwd')
|
command('delcommand Pwd')
|
||||||
eq({cmd_dict}, curbufmeths.get_commands(dummy_dict))
|
eq({cmd_dict}, meths.get_commands({builtin=false}))
|
||||||
|
end)
|
||||||
|
it('gets buffer-local user-defined commands', function()
|
||||||
|
-- Define a buffer-local command.
|
||||||
|
command('command -buffer -nargs=1 Hello echo "Hello World"')
|
||||||
|
eq({cmd_dict}, curbufmeths.get_commands({builtin=false}))
|
||||||
|
-- Define another buffer-local command.
|
||||||
|
command('command -buffer -nargs=? Pwd pwd')
|
||||||
|
eq({cmd_dict, cmd_dict2}, curbufmeths.get_commands({builtin=false}))
|
||||||
|
-- Delete a command.
|
||||||
|
command('delcommand Pwd')
|
||||||
|
eq({cmd_dict}, curbufmeths.get_commands({builtin=false}))
|
||||||
end)
|
end)
|
||||||
it('gets different attributes of different commands', function()
|
it('gets different attributes of different commands', function()
|
||||||
local cmd1 = {
|
local cmd1 = {
|
||||||
|
addr=NIL,
|
||||||
complete='custom',
|
complete='custom',
|
||||||
nargs='1',
|
|
||||||
name='Finger',
|
|
||||||
script_id=0,
|
|
||||||
complete_arg='ListUsers',
|
complete_arg='ListUsers',
|
||||||
|
count=NIL,
|
||||||
definition='!finger <args>',
|
definition='!finger <args>',
|
||||||
|
name='Finger',
|
||||||
|
nargs='+',
|
||||||
|
range=NIL,
|
||||||
|
script_id=1,
|
||||||
}
|
}
|
||||||
local cmd2 = {
|
local cmd2 = {
|
||||||
complete='dir',
|
|
||||||
nargs='0',
|
|
||||||
name='TestCmd',
|
|
||||||
range='10c',
|
|
||||||
addr='arguments',
|
addr='arguments',
|
||||||
script_id=0,
|
complete='dir',
|
||||||
|
complete_arg=NIL,
|
||||||
|
count='10',
|
||||||
definition='pwd <args>',
|
definition='pwd <args>',
|
||||||
|
name='TestCmd',
|
||||||
|
nargs='0',
|
||||||
|
range='10',
|
||||||
|
script_id=0,
|
||||||
}
|
}
|
||||||
command('command -complete=custom,ListUsers -nargs=1 Finger !finger <args>')
|
source([[
|
||||||
eq({cmd1}, meths.get_commands(dummy_dict))
|
command -complete=custom,ListUsers -nargs=+ Finger !finger <args>
|
||||||
|
]])
|
||||||
|
eq({cmd1}, meths.get_commands({builtin=false}))
|
||||||
command('command -complete=dir -addr=arguments -count=10 TestCmd pwd <args>')
|
command('command -complete=dir -addr=arguments -count=10 TestCmd pwd <args>')
|
||||||
eq({cmd1, cmd2}, meths.get_commands(dummy_dict))
|
eq({cmd1, cmd2}, meths.get_commands({builtin=false}))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user