API: nvim_get_commands(): return Dictionary

This commit is contained in:
Justin M. Keyes
2018-05-11 21:26:05 +02:00
parent cb6672853a
commit 137eedb4ed
4 changed files with 29 additions and 30 deletions

View File

@@ -467,7 +467,7 @@ Integer nvim_buf_get_changedtick(Buffer buffer, Error *err)
/// @returns Array of maparg()-like dictionaries describing mappings. /// @returns Array of maparg()-like dictionaries describing mappings.
/// The "buffer" key holds the associated buffer handle. /// The "buffer" key holds the associated buffer handle.
ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err)
FUNC_API_SINCE(3) FUNC_API_SINCE(3)
{ {
buf_T *buf = find_buffer_by_handle(buffer, err); buf_T *buf = find_buffer_by_handle(buffer, err);
@@ -478,16 +478,15 @@ 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 buffer-local |user-commands|. /// Gets a map of buffer-local |user-commands|.
/// ///
/// @param buffer Buffer handle. /// @param buffer Buffer handle.
/// @param opts Optional parameters. Currently not used. /// @param opts Optional parameters. Currently not used.
/// @param[out] err Error details, if any. /// @param[out] err Error details, if any.
/// ///
/// @returns Array of dictionaries describing commands. /// @returns Map of maps describing commands.
ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts, Dictionary nvim_buf_get_commands(Buffer buffer, Dictionary opts, Error *err)
Error *err) FUNC_API_SINCE(4)
FUNC_API_SINCE(4)
{ {
bool global = (buffer == -1); bool global = (buffer == -1);
bool builtin = false; bool builtin = false;
@@ -497,7 +496,7 @@ ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts,
Object v = opts.items[i].value; Object v = opts.items[i].value;
if (!strequal("builtin", k.data)) { if (!strequal("builtin", k.data)) {
api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data); api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data);
return (Array)ARRAY_DICT_INIT; return (Dictionary)ARRAY_DICT_INIT;
} }
if (strequal("builtin", k.data)) { if (strequal("builtin", k.data)) {
builtin = v.data.boolean; builtin = v.data.boolean;
@@ -507,14 +506,14 @@ ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts,
if (global) { if (global) {
if (builtin) { if (builtin) {
api_set_error(err, kErrorTypeValidation, "builtin=true not implemented"); api_set_error(err, kErrorTypeValidation, "builtin=true not implemented");
return (Array)ARRAY_DICT_INIT; return (Dictionary)ARRAY_DICT_INIT;
} }
return commands_array(NULL); return commands_array(NULL);
} }
buf_T *buf = find_buffer_by_handle(buffer, err); buf_T *buf = find_buffer_by_handle(buffer, err);
if (builtin || !buf) { if (builtin || !buf) {
return (Array)ARRAY_DICT_INIT; return (Dictionary)ARRAY_DICT_INIT;
} }
return commands_array(buf); return commands_array(buf);
} }

View File

@@ -954,12 +954,12 @@ Dictionary nvim_get_mode(void)
/// @returns Array of maparg()-like dictionaries describing mappings. /// @returns Array of maparg()-like dictionaries describing mappings.
/// The "buffer" key is always zero. /// The "buffer" key is always zero.
ArrayOf(Dictionary) nvim_get_keymap(String mode) ArrayOf(Dictionary) nvim_get_keymap(String mode)
FUNC_API_SINCE(3) FUNC_API_SINCE(3)
{ {
return keymap_array(mode, NULL); return keymap_array(mode, NULL);
} }
/// Gets a list of global (non-buffer-local) Ex commands. /// Gets a map of global (non-buffer-local) Ex commands.
/// ///
/// Currently only |user-commands| are supported, not builtin Ex commands. /// Currently only |user-commands| are supported, not builtin Ex commands.
/// ///
@@ -967,9 +967,9 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode)
/// {"builtin":false} /// {"builtin":false}
/// @param[out] err Error details, if any. /// @param[out] err Error details, if any.
/// ///
/// @returns Array of dictionaries describing commands. /// @returns Map of maps describing commands.
ArrayOf(Dictionary) nvim_get_commands(Dictionary opts, Error *err) Dictionary nvim_get_commands(Dictionary opts, Error *err)
FUNC_API_SINCE(4) FUNC_API_SINCE(4)
{ {
return nvim_buf_get_commands(-1, opts, err); return nvim_buf_get_commands(-1, opts, err);
} }

View File

@@ -9969,15 +9969,15 @@ bool cmd_can_preview(char_u *cmd)
return false; return false;
} }
/// Gets a list of maps describing user-commands defined for buffer `buf` /// Gets a map of maps describing user-commands defined for buffer `buf` or
/// or defined globally if `buf` is NULL. /// defined globally if `buf` is NULL.
/// ///
/// @param buf Buffer to inspect, or NULL to get global user-commands. /// @param buf Buffer to inspect, or NULL to get global commands.
/// ///
/// @return Array of dictionaries describing commands /// @return Map of maps describing commands
ArrayOf(Dictionary) commands_array(buf_T *buf) Dictionary commands_array(buf_T *buf)
{ {
Array rv = ARRAY_DICT_INIT; Dictionary rv = ARRAY_DICT_INIT;
Object obj = NIL; Object obj = NIL;
char str[10]; char str[10];
garray_T *gap = (buf == NULL) ? &ucmds : &buf->b_ucmds; garray_T *gap = (buf == NULL) ? &ucmds : &buf->b_ucmds;
@@ -10043,7 +10043,7 @@ ArrayOf(Dictionary) commands_array(buf_T *buf)
} }
PUT(d, "addr", obj); PUT(d, "addr", obj);
ADD(rv, DICTIONARY_OBJ(d)); PUT(rv, (char *)cmd->uc_name, DICTIONARY_OBJ(d));
} }
return rv; return rv;
} }

View File

@@ -28,25 +28,25 @@ describe('nvim_get_commands', function()
it('gets global user-defined commands', function() it('gets global user-defined commands', function()
-- Define a command. -- Define a command.
command('command -nargs=1 Hello echo "Hello World"') command('command -nargs=1 Hello echo "Hello World"')
eq({cmd_dict}, meths.get_commands({builtin=false})) eq({Hello=cmd_dict}, meths.get_commands({builtin=false}))
-- Define another command. -- Define another command.
command('command -nargs=? Pwd pwd'); command('command -nargs=? Pwd pwd');
eq({cmd_dict, cmd_dict2}, meths.get_commands({builtin=false})) eq({Hello=cmd_dict, Pwd=cmd_dict2}, meths.get_commands({builtin=false}))
-- Delete a command. -- Delete a command.
command('delcommand Pwd') command('delcommand Pwd')
eq({cmd_dict}, meths.get_commands({builtin=false})) eq({Hello=cmd_dict}, meths.get_commands({builtin=false}))
end) end)
it('gets buffer-local user-defined commands', function() it('gets buffer-local user-defined commands', function()
-- Define a buffer-local command. -- Define a buffer-local command.
command('command -buffer -nargs=1 Hello echo "Hello World"') command('command -buffer -nargs=1 Hello echo "Hello World"')
eq({cmd_dict}, curbufmeths.get_commands({builtin=false})) eq({Hello=cmd_dict}, curbufmeths.get_commands({builtin=false}))
-- Define another buffer-local command. -- Define another buffer-local command.
command('command -buffer -nargs=? Pwd pwd') command('command -buffer -nargs=? Pwd pwd')
eq({cmd_dict, cmd_dict2}, curbufmeths.get_commands({builtin=false})) eq({Hello=cmd_dict, Pwd=cmd_dict2}, curbufmeths.get_commands({builtin=false}))
-- Delete a command. -- Delete a command.
command('delcommand Pwd') command('delcommand Pwd')
eq({cmd_dict}, curbufmeths.get_commands({builtin=false})) eq({Hello=cmd_dict}, curbufmeths.get_commands({builtin=false}))
-- {builtin=true} always returns empty for buffer-local case. -- {builtin=true} always returns empty for buffer-local case.
eq({}, curbufmeths.get_commands({builtin=true})) eq({}, curbufmeths.get_commands({builtin=true}))
@@ -61,9 +61,9 @@ describe('nvim_get_commands', function()
source([[ source([[
command -complete=custom,ListUsers -nargs=+ Finger !finger <args> command -complete=custom,ListUsers -nargs=+ Finger !finger <args>
]]) ]])
eq({cmd1}, meths.get_commands({builtin=false})) eq({Finger=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, cmd0}, meths.get_commands({builtin=false})) eq({Finger=cmd1, TestCmd=cmd0}, meths.get_commands({builtin=false}))
source([[ source([[
command -bang -nargs=* Cmd2 call <SID>foo(<q-args>) command -bang -nargs=* Cmd2 call <SID>foo(<q-args>)
@@ -75,6 +75,6 @@ describe('nvim_get_commands', function()
command -register Cmd4 call <SID>just_great() command -register Cmd4 call <SID>just_great()
]]) ]])
-- TODO(justinmk): Order is stable but undefined. Sort before return? -- TODO(justinmk): Order is stable but undefined. Sort before return?
eq({cmd2, cmd3, cmd4, cmd1, cmd0}, meths.get_commands({builtin=false})) eq({Cmd2=cmd2, Cmd3=cmd3, Cmd4=cmd4, Finger=cmd1, TestCmd=cmd0}, meths.get_commands({builtin=false}))
end) end)
end) end)