From 25b630484023716977c3fe2790c13b41f9db9f30 Mon Sep 17 00:00:00 2001 From: Nimit Bhardwaj Date: Sat, 24 Feb 2018 15:44:51 +0530 Subject: [PATCH 1/5] API: nvim_get_commands() --- src/nvim/api/buffer.c | 21 +++ src/nvim/api/private/helpers.c | 4 +- src/nvim/api/vim.c | 13 ++ src/nvim/ex_docmd.c | 222 ++++++++++++++++++++------- test/functional/api/command_spec.lua | 74 +++++++++ test/functional/api/keymap_spec.lua | 2 +- 6 files changed, 274 insertions(+), 62 deletions(-) create mode 100644 test/functional/api/command_spec.lua diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 023f434f9d..c4508a0c81 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -24,6 +24,7 @@ #include "nvim/syntax.h" #include "nvim/window.h" #include "nvim/undo.h" +#include "nvim/ex_docmd.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "api/buffer.c.generated.h" @@ -478,6 +479,26 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) return keymap_array(mode, buf); } +/// Gets a list of dictionaries describing buffer-local commands. +/// The "buffer" key in the returned dictionary reflects the buffer +/// handle where the command is present. +/// +/// @param buffer Buffer handle. +/// @param opts Optional parameters, currently always +/// @param[out] err Error details, if any. +/// +/// @returns Array of dictionaries describing commands. +ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts, + Error *err) + FUNC_API_SINCE(4) +{ + buf_T *buf = find_buffer_by_handle(buffer, err); + if (!buf) { + return (Array)ARRAY_DICT_INIT; + } + return commands_array(buf); +} + /// Sets a buffer-scoped (b:) variable /// /// @param buffer Buffer handle diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index b922036893..03f1a882d3 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -701,7 +701,7 @@ String cchar_to_string(char c) String cstr_to_string(const char *str) { if (str == NULL) { - return (String) STRING_INIT; + return (String) STRING_INIT; } size_t len = strlen(str); @@ -1149,7 +1149,7 @@ void api_set_error(Error *err, ErrorType errType, const char *format, ...) /// /// @param mode The abbreviation for the mode /// @param buf The buffer to get the mapping array. NULL for global -/// @returns An array of maparg() like dictionaries describing mappings +/// @returns Array of maparg()-like dictionaries describing mappings ArrayOf(Dictionary) keymap_array(String mode, buf_T *buf) { Array mappings = ARRAY_DICT_INIT; diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index df4912a51e..38e64fd6ed 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -959,6 +959,19 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode) return keymap_array(mode, NULL); } +/// Gets a list of dictionaries describing global(non-buffer) commands. +/// +/// @param opts Holds the API Metadata describing what type of commands +/// are needed. +/// @param[out] err Error details, if any. +/// +/// @returns Array of dictionaries describing commands. +ArrayOf(Dictionary) nvim_get_commands(Dictionary opts, Error *err) + FUNC_API_SINCE(4) +{ + return commands_array(NULL); +} + /// Returns a 2-tuple (Array), where item 0 is the current channel id and item /// 1 is the |api-metadata| map (Dictionary). /// diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 3dbeccaaa5..4cdb0d2cf1 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -73,6 +73,7 @@ #include "nvim/shada.h" #include "nvim/lua/executor.h" #include "nvim/globals.h" +#include "nvim/api/private/helpers.h" static int quitmore = 0; static int ex_pressedreturn = FALSE; @@ -4862,51 +4863,56 @@ static struct { * List of names for completion for ":command" with the EXPAND_ flag. * Must be alphabetical for completion. */ -static struct { - int expand; - char *name; -} command_complete[] = +static const char *command_complete[] = { - { EXPAND_AUGROUP, "augroup" }, - { EXPAND_BEHAVE, "behave" }, - { EXPAND_BUFFERS, "buffer" }, - { EXPAND_CHECKHEALTH, "checkhealth" }, - { EXPAND_COLORS, "color" }, - { EXPAND_COMMANDS, "command" }, - { EXPAND_COMPILER, "compiler" }, - { EXPAND_CSCOPE, "cscope" }, - { EXPAND_USER_DEFINED, "custom" }, - { EXPAND_USER_LIST, "customlist" }, - { EXPAND_DIRECTORIES, "dir" }, - { EXPAND_ENV_VARS, "environment" }, - { EXPAND_EVENTS, "event" }, - { EXPAND_EXPRESSION, "expression" }, - { EXPAND_FILES, "file" }, - { EXPAND_FILES_IN_PATH, "file_in_path" }, - { EXPAND_FILETYPE, "filetype" }, - { EXPAND_FUNCTIONS, "function" }, - { EXPAND_HELP, "help" }, - { EXPAND_HIGHLIGHT, "highlight" }, - { EXPAND_HISTORY, "history" }, + [EXPAND_AUGROUP] = "augroup", + [EXPAND_BEHAVE] = "behave", + [EXPAND_BUFFERS] = "buffer", + [EXPAND_CHECKHEALTH] = "checkhealth", + [EXPAND_COLORS] = "color", + [EXPAND_COMMANDS] = "command", + [EXPAND_COMPILER] = "compiler", + [EXPAND_CSCOPE] = "cscope", + [EXPAND_USER_DEFINED] = "custom", + [EXPAND_USER_LIST] = "customlist", + [EXPAND_DIRECTORIES] = "dir", + [EXPAND_ENV_VARS] = "environment", + [EXPAND_EVENTS] = "event", + [EXPAND_EXPRESSION] = "expression", + [EXPAND_FILES] = "file", + [EXPAND_FILES_IN_PATH] = "file_in_path", + [EXPAND_FILETYPE] = "filetype", + [EXPAND_FUNCTIONS] = "function", + [EXPAND_HELP] = "help", + [EXPAND_HIGHLIGHT] = "highlight", + [EXPAND_HISTORY] = "history", #ifdef HAVE_WORKING_LIBINTL - { EXPAND_LOCALES, "locale" }, + [EXPAND_LOCALES] = "locale", #endif - { EXPAND_MAPPINGS, "mapping" }, - { EXPAND_MENUS, "menu" }, - { EXPAND_MESSAGES, "messages" }, - { EXPAND_OWNSYNTAX, "syntax" }, - { EXPAND_SYNTIME, "syntime" }, - { EXPAND_SETTINGS, "option" }, - { EXPAND_PACKADD, "packadd" }, - { EXPAND_SHELLCMD, "shellcmd" }, - { EXPAND_SIGN, "sign" }, - { EXPAND_TAGS, "tag" }, - { EXPAND_TAGS_LISTFILES, "tag_listfiles" }, - { EXPAND_USER, "user" }, - { EXPAND_USER_VARS, "var" }, - { 0, NULL } + [EXPAND_MAPPINGS] = "mapping", + [EXPAND_MENUS] = "menu", + [EXPAND_MESSAGES] = "messages", + [EXPAND_OWNSYNTAX] = "syntax", + [EXPAND_SYNTIME] = "syntime", + [EXPAND_SETTINGS] = "option", + [EXPAND_PACKADD] = "packadd", + [EXPAND_SHELLCMD] = "shellcmd", + [EXPAND_SIGN] = "sign", + [EXPAND_TAGS] = "tag", + [EXPAND_TAGS_LISTFILES] = "tag_listfiles", + [EXPAND_USER] = "user", + [EXPAND_USER_VARS] = "var", }; +static char *get_command_complete(int arg) +{ + if (arg >= (int)(ARRAY_SIZE(command_complete))) { + return NULL; + } else { + return (char *)command_complete[arg]; + } +} + static void uc_list(char_u *name, size_t name_len) { int i, j; @@ -5000,13 +5006,12 @@ static void uc_list(char_u *name, size_t name_len) IObuff[len++] = ' '; } while (len < 21); - /* Completion */ - for (j = 0; command_complete[j].expand != 0; ++j) - if (command_complete[j].expand == cmd->uc_compl) { - STRCPY(IObuff + len, command_complete[j].name); - len += (int)STRLEN(IObuff + len); - break; - } + // Completion + char *cmd_compl = get_command_complete(cmd->uc_compl); + if (cmd_compl != NULL) { + STRCPY(IObuff + len, get_command_complete(cmd->uc_compl)); + len += (int)STRLEN(IObuff + len); + } do { IObuff[len++] = ' '; @@ -5797,7 +5802,15 @@ char_u *get_user_cmd_nargs(expand_T *xp, int idx) */ char_u *get_user_cmd_complete(expand_T *xp, int idx) { - return (char_u *)command_complete[idx].name; + if (idx >= (int)ARRAY_SIZE(command_complete)) { + return NULL; + } + char *cmd_compl = get_command_complete(idx); + if (cmd_compl == NULL) { + return (char_u *)""; + } else { + return (char_u *)cmd_compl; + } } /* @@ -5857,20 +5870,23 @@ int parse_compl_arg(const char_u *value, int vallen, int *complp, } } - for (i = 0; command_complete[i].expand != 0; ++i) { - if ((int)STRLEN(command_complete[i].name) == valend - && STRNCMP(value, command_complete[i].name, valend) == 0) { - *complp = command_complete[i].expand; - if (command_complete[i].expand == EXPAND_BUFFERS) + for (i = 0; i < (int)ARRAY_SIZE(command_complete); i++) { + if (get_command_complete(i) == NULL) { + continue; + } + if ((int)STRLEN(command_complete[i]) == valend + && STRNCMP(value, command_complete[i], valend) == 0) { + *complp = i; + if (i == EXPAND_BUFFERS) { *argt |= BUFNAME; - else if (command_complete[i].expand == EXPAND_DIRECTORIES - || command_complete[i].expand == EXPAND_FILES) + } else if (i == EXPAND_DIRECTORIES || i == EXPAND_FILES) { *argt |= XFILE; + } break; } } - if (command_complete[i].expand == 0) { + if (i == (int)ARRAY_SIZE(command_complete)) { EMSG2(_("E180: Invalid complete value: %s"), value); return FAIL; } @@ -5894,9 +5910,13 @@ int parse_compl_arg(const char_u *value, int vallen, int *complp, int cmdcomplete_str_to_type(char_u *complete_str) { - for (int i = 0; command_complete[i].expand != 0; i++) { - if (STRCMP(complete_str, command_complete[i].name) == 0) { - return command_complete[i].expand; + for (int i = 0; i < (int)(ARRAY_SIZE(command_complete)); i++) { + char *cmd_compl = get_command_complete(i); + if (cmd_compl == NULL) { + continue; + } + if (STRCMP(complete_str, command_complete[i]) == 0) { + return i; } } @@ -9948,3 +9968,87 @@ bool cmd_can_preview(char_u *cmd) return false; } + +/// Gets a list of maps describing user-commands defined for buffer `buf` +/// or defined globally if `buf` is NULL. +/// +/// @param buf Buffer to inspect, or NULL to get global commands. +/// +/// @return Array of dictionaries describing commands +ArrayOf(Dictionary) commands_array(buf_T *buf) +{ + Array rv = ARRAY_DICT_INIT; + garray_T *gap; + if (buf == NULL) { + gap = &ucmds; + } else { + gap = &buf->b_ucmds; + } + for (int i = 0; i < gap->ga_len; i++) { + char arg[2] = { 0, 0 }; + Dictionary d = ARRAY_DICT_INIT; + char Range[10] = ""; + ucmd_T *cmd = USER_CMD_GA(gap, i); + + // Name + PUT(d, "name", STRING_OBJ(cstr_to_string((char *)cmd->uc_name))); + + // Argument + switch (cmd->uc_argt & (EXTRA|NOSPC|NEEDARG)) { + case 0: arg[0] = '0'; break; + case(EXTRA): arg[0] = '*'; break; + case(EXTRA|NOSPC): arg[0] = '?'; break; + case(EXTRA|NEEDARG): arg[0] = '+'; break; + case(EXTRA|NOSPC|NEEDARG): arg[0] = '1'; break; + } + PUT(d, "nargs", STRING_OBJ(cstr_to_string((char *)arg))); + + // Definition + PUT(d, "definition", STRING_OBJ(cstr_to_string((char *)cmd->uc_rep))); + + // Complete + char *cmd_compl = get_command_complete(cmd->uc_compl); + if (cmd_compl != NULL) { + PUT(d, "complete", STRING_OBJ(cstr_to_string(cmd_compl))); + } + + // Complete Arg + 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) { + // -count=N + snprintf((char *)Range, sizeof(Range), "%" PRId64 "c", + (int64_t)cmd->uc_def); + } 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 { + Range[0] = '.'; + } + PUT(d, "range", STRING_OBJ(cstr_to_string((char *)Range))); + } + + // Address + for (int j = 0; addr_type_complete[j].expand != -1; j++) { + if (addr_type_complete[j].expand != ADDR_LINES + && addr_type_complete[j].expand == cmd->uc_addr_type) { + PUT(d, "addr", STRING_OBJ(cstr_to_string(addr_type_complete[j].name))); + break; + } + } + + // ScriptID + PUT(d, "script_id", INTEGER_OBJ(cmd->uc_scriptID)); + + ADD(rv, DICTIONARY_OBJ(d)); + } + return rv; +} diff --git a/test/functional/api/command_spec.lua b/test/functional/api/command_spec.lua new file mode 100644 index 0000000000..db03edf6d2 --- /dev/null +++ b/test/functional/api/command_spec.lua @@ -0,0 +1,74 @@ +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local command = helpers.command +local curbufmeths = helpers.curbufmeths +local eq = helpers.eq +local meths = helpers.meths + +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 = { + name='Hello', + nargs='0', + script_id=0, + definition='echo "Hello World"', + } + local cmd_dict2 = { + name='Pwd', + nargs='0', + script_id=0, + definition='pwd', + } + before_each(clear) + it('gets empty list if no commands were defined', function() + eq({}, meths.get_commands(dummy_dict)) + end) + it('gets user-def commands', function() + -- Insert a command + command(cmd_string) + eq({cmd_dict}, meths.get_commands(dummy_dict)) + -- Insert a another command + 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) + it('considers different buffers', function() + -- Insert a command + command('command -buffer Hello echo "Hello World"') + eq({cmd_dict}, curbufmeths.get_commands(dummy_dict)) + -- Insert a another command + command('command -buffer Pwd pwd') + eq({cmd_dict, cmd_dict2}, curbufmeths.get_commands(dummy_dict)) + -- Delete a command + command('delcommand Pwd') + eq({cmd_dict}, curbufmeths.get_commands(dummy_dict)) + end) + it('gets different attributes of different commands', function() + local cmd1 = { + complete='custom', + nargs='1', + name='Finger', + script_id=0, + complete_arg='ListUsers', + definition='!finger ', + } + local cmd2 = { + complete='dir', + nargs='0', + name='TestCmd', + range='10c', + addr='arguments', + script_id=0, + definition='pwd ', + } + command('command -complete=custom,ListUsers -nargs=1 Finger !finger ') + eq({cmd1}, meths.get_commands(dummy_dict)) + command('command -complete=dir -addr=arguments -count=10 TestCmd pwd ') + eq({cmd1, cmd2}, meths.get_commands(dummy_dict)) + end) +end) diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua index 3a10f9c60f..f52372bee3 100644 --- a/test/functional/api/keymap_spec.lua +++ b/test/functional/api/keymap_spec.lua @@ -11,7 +11,7 @@ local source = helpers.source local shallowcopy = global_helpers.shallowcopy -describe('get_keymap', function() +describe('nvim_get_keymap', function() before_each(clear) -- Basic mapping and table to be used to describe results From 9fa7727ce047e8e2e13f9fbf60d5defe9cf45060 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 10 May 2018 23:37:56 +0200 Subject: [PATCH 2/5] 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 --- src/nvim/api/buffer.c | 28 ++++++-- src/nvim/api/private/helpers.c | 16 ++--- src/nvim/api/vim.c | 8 +-- src/nvim/ex_docmd.c | 79 ++++++++++---------- test/functional/api/command_spec.lua | 103 ++++++++++++++++----------- 5 files changed, 134 insertions(+), 100 deletions(-) diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index c4508a0c81..878c8aad87 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -479,12 +479,11 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) return keymap_array(mode, buf); } -/// Gets a list of dictionaries describing buffer-local commands. -/// The "buffer" key in the returned dictionary reflects the buffer -/// handle where the command is present. +/// Gets a list of maps describing buffer-local |user-commands|. /// -/// @param buffer Buffer handle. -/// @param opts Optional parameters, currently always +/// @param buffer Buffer handle. +/// @param opts Optional parameters. Currently only supports +/// {"builtin":false} /// @param[out] err Error details, if any. /// /// @returns Array of dictionaries describing commands. @@ -492,6 +491,25 @@ ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts, Error *err) 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); if (!buf) { return (Array)ARRAY_DICT_INIT; diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 03f1a882d3..17ee3ed711 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -685,7 +685,7 @@ tabpage_T *find_tab_by_handle(Tabpage tabpage, Error *err) String cchar_to_string(char c) { char buf[] = { c, NUL }; - return (String) { + return (String){ .data = xmemdupz(buf, 1), .size = (c != NUL) ? 1 : 0 }; @@ -701,13 +701,13 @@ String cchar_to_string(char c) String cstr_to_string(const char *str) { if (str == NULL) { - return (String) STRING_INIT; + return (String)STRING_INIT; } size_t len = strlen(str); - return (String) { - .data = xmemdupz(str, len), - .size = len + return (String){ + .data = xmemdupz(str, len), + .size = len, }; } @@ -722,7 +722,7 @@ String cstr_to_string(const char *str) String cbuf_to_string(const char *buf, size_t size) FUNC_ATTR_NONNULL_ALL { - return (String) { + return (String){ .data = xmemdupz(buf, size), .size = size }; @@ -737,9 +737,9 @@ String cbuf_to_string(const char *buf, size_t size) String cstr_as_string(char *str) FUNC_ATTR_PURE { if (str == NULL) { - return (String) STRING_INIT; + return (String)STRING_INIT; } - return (String) { .data = str, .size = strlen(str) }; + return (String){ .data = str, .size = strlen(str) }; } /// Converts from type Object to a VimL value. diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 38e64fd6ed..065b8d1ce2 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -959,17 +959,17 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode) 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 -/// are needed. +/// @param opts Optional parameters. Currently only supports +/// {"builtin":false} /// @param[out] err Error details, if any. /// /// @returns Array of dictionaries describing commands. ArrayOf(Dictionary) nvim_get_commands(Dictionary opts, Error *err) 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 diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 4cdb0d2cf1..121b913bb7 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -9972,28 +9972,26 @@ bool cmd_can_preview(char_u *cmd) /// Gets a list of maps describing user-commands defined for buffer `buf` /// 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 ArrayOf(Dictionary) commands_array(buf_T *buf) { Array rv = ARRAY_DICT_INIT; - garray_T *gap; - if (buf == NULL) { - gap = &ucmds; - } else { - gap = &buf->b_ucmds; - } + Object obj = NIL; + char str[10]; + garray_T *gap = (buf == NULL) ? &ucmds : &buf->b_ucmds; + for (int i = 0; i < gap->ga_len; i++) { char arg[2] = { 0, 0 }; Dictionary d = ARRAY_DICT_INIT; - char Range[10] = ""; ucmd_T *cmd = USER_CMD_GA(gap, i); - // 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)) { case 0: arg[0] = '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|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); - if (cmd_compl != NULL) { - PUT(d, "complete", STRING_OBJ(cstr_to_string(cmd_compl))); - } + PUT(d, "complete", (cmd_compl == NULL + ? 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 - 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) { - // -count=N - snprintf((char *)Range, sizeof(Range), "%" PRId64 "c", - (int64_t)cmd->uc_def); - } 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); + obj = NIL; + if (cmd->uc_argt & COUNT) { + if (cmd->uc_def >= 0) { + snprintf(str, sizeof(str), "%" PRId64, (int64_t)cmd->uc_def); + obj = STRING_OBJ(cstr_to_string(str)); // -count=N } 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++) { if (addr_type_complete[j].expand != ADDR_LINES && 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; } } - - // ScriptID - PUT(d, "script_id", INTEGER_OBJ(cmd->uc_scriptID)); + PUT(d, "addr", obj); ADD(rv, DICTIONARY_OBJ(d)); } diff --git a/test/functional/api/command_spec.lua b/test/functional/api/command_spec.lua index db03edf6d2..d24cdc0493 100644 --- a/test/functional/api/command_spec.lua +++ b/test/functional/api/command_spec.lua @@ -1,74 +1,97 @@ local helpers = require('test.functional.helpers')(after_each) +local NIL = helpers.NIL local clear = helpers.clear local command = helpers.command local curbufmeths = helpers.curbufmeths local eq = helpers.eq +local expect_err = helpers.expect_err local meths = helpers.meths +local source = helpers.source 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 = { - name='Hello', - nargs='0', - script_id=0, + addr=NIL, + complete=NIL, + complete_arg=NIL, + count=NIL, definition='echo "Hello World"', + name='Hello', + nargs='1', + range=NIL, + script_id=0, } local cmd_dict2 = { - name='Pwd', - nargs='0', - script_id=0, + addr=NIL, + complete=NIL, + complete_arg=NIL, + count=NIL, definition='pwd', + name='Pwd', + nargs='?', + range=NIL, + script_id=0, } before_each(clear) it('gets empty list if no commands were defined', function() - eq({}, meths.get_commands(dummy_dict)) + eq({}, meths.get_commands({builtin=false})) end) - it('gets user-def commands', function() - -- Insert a command - command(cmd_string) - eq({cmd_dict}, meths.get_commands(dummy_dict)) - -- Insert a another command - 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)) + it('validates input', function() + expect_err('builtin commands not supported yet', meths.get_commands, + {builtin=true}) + expect_err('unexpected key: foo', meths.get_commands, + {foo='blah'}) end) - it('considers different buffers', function() - -- Insert a command - command('command -buffer Hello echo "Hello World"') - eq({cmd_dict}, curbufmeths.get_commands(dummy_dict)) - -- Insert a another command - command('command -buffer Pwd pwd') - eq({cmd_dict, cmd_dict2}, curbufmeths.get_commands(dummy_dict)) - -- Delete a command + it('gets global user-defined commands', function() + -- Define a command. + command('command -nargs=1 Hello echo "Hello World"') + eq({cmd_dict}, meths.get_commands({builtin=false})) + -- Define another command. + command('command -nargs=? Pwd pwd'); + eq({cmd_dict, cmd_dict2}, meths.get_commands({builtin=false})) + -- Delete a command. 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) it('gets different attributes of different commands', function() local cmd1 = { + addr=NIL, complete='custom', - nargs='1', - name='Finger', - script_id=0, complete_arg='ListUsers', + count=NIL, definition='!finger ', + name='Finger', + nargs='+', + range=NIL, + script_id=1, } local cmd2 = { - complete='dir', - nargs='0', - name='TestCmd', - range='10c', addr='arguments', - script_id=0, + complete='dir', + complete_arg=NIL, + count='10', definition='pwd ', + name='TestCmd', + nargs='0', + range='10', + script_id=0, } - command('command -complete=custom,ListUsers -nargs=1 Finger !finger ') - eq({cmd1}, meths.get_commands(dummy_dict)) + source([[ + command -complete=custom,ListUsers -nargs=+ Finger !finger + ]]) + eq({cmd1}, meths.get_commands({builtin=false})) command('command -complete=dir -addr=arguments -count=10 TestCmd pwd ') - eq({cmd1, cmd2}, meths.get_commands(dummy_dict)) + eq({cmd1, cmd2}, meths.get_commands({builtin=false})) end) end) From 738bffea2cbb810ad1d3a3aa5bfeb88392260a8a Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 11 May 2018 07:50:00 +0200 Subject: [PATCH 3/5] API: nvim_get_commands(): more attributes Support more :command attributes: -bang -bar -register --- src/nvim/api/buffer.c | 9 ++-- src/nvim/api/vim.c | 10 ++-- src/nvim/ex_docmd.c | 14 +++--- test/functional/api/command_spec.lua | 72 ++++++++++------------------ 4 files changed, 44 insertions(+), 61 deletions(-) diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 878c8aad87..19f92f002d 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -459,14 +459,13 @@ Integer nvim_buf_get_changedtick(Buffer buffer, Error *err) return buf->b_changedtick; } -/// Gets a list of dictionaries describing buffer-local mappings. -/// The "buffer" key in the returned dictionary reflects the buffer -/// handle where the mapping is present. +/// Gets a list of buffer-local |mapping| definitions. /// /// @param mode Mode short-name ("n", "i", "v", ...) /// @param buffer Buffer handle /// @param[out] err Error details, if any -/// @returns Array of maparg()-like dictionaries describing mappings +/// @returns Array of maparg()-like dictionaries describing mappings. +/// The "buffer" key holds the associated buffer handle. ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) FUNC_API_SINCE(3) { @@ -479,7 +478,7 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) return keymap_array(mode, buf); } -/// Gets a list of maps describing buffer-local |user-commands|. +/// Gets a list of buffer-local |user-commands|. /// /// @param buffer Buffer handle. /// @param opts Optional parameters. Currently only supports diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 065b8d1ce2..305cc1a43c 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -948,18 +948,20 @@ Dictionary nvim_get_mode(void) return rv; } -/// Gets a list of dictionaries describing global (non-buffer) mappings. -/// The "buffer" key in the returned dictionary is always zero. +/// Gets a list of global (non-buffer-local) |mapping| definitions. /// /// @param mode Mode short-name ("n", "i", "v", ...) -/// @returns Array of maparg()-like dictionaries describing mappings +/// @returns Array of maparg()-like dictionaries describing mappings. +/// The "buffer" key is always zero. ArrayOf(Dictionary) nvim_get_keymap(String mode) FUNC_API_SINCE(3) { return keymap_array(mode, NULL); } -/// Gets a list of maps describing global |user-commands|. +/// Gets a list of global (non-buffer-local) Ex commands. +/// +/// Currently only |user-commands| are supported, not builtin Ex commands. /// /// @param opts Optional parameters. Currently only supports /// {"builtin":false} diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 121b913bb7..e92dbc4cc8 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -9990,14 +9990,16 @@ ArrayOf(Dictionary) commands_array(buf_T *buf) 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)); + PUT(d, "bang", BOOLEAN_OBJ(!!(cmd->uc_argt & BANG))); + PUT(d, "bar", BOOLEAN_OBJ(!!(cmd->uc_argt & TRLBAR))); + PUT(d, "register", BOOLEAN_OBJ(!!(cmd->uc_argt & REGSTR))); - // "nargs" key switch (cmd->uc_argt & (EXTRA|NOSPC|NEEDARG)) { - case 0: arg[0] = '0'; break; - case(EXTRA): arg[0] = '*'; break; - case(EXTRA|NOSPC): arg[0] = '?'; break; - case(EXTRA|NEEDARG): arg[0] = '+'; break; - case(EXTRA|NOSPC|NEEDARG): arg[0] = '1'; break; + case 0: arg[0] = '0'; break; + case(EXTRA): arg[0] = '*'; break; + case(EXTRA|NOSPC): arg[0] = '?'; break; + case(EXTRA|NEEDARG): arg[0] = '+'; break; + case(EXTRA|NOSPC|NEEDARG): arg[0] = '1'; break; } PUT(d, "nargs", STRING_OBJ(cstr_to_string(arg))); diff --git a/test/functional/api/command_spec.lua b/test/functional/api/command_spec.lua index d24cdc0493..149a5dd111 100644 --- a/test/functional/api/command_spec.lua +++ b/test/functional/api/command_spec.lua @@ -10,38 +10,21 @@ local meths = helpers.meths local source = helpers.source describe('nvim_get_commands', function() - local cmd_dict = { - addr=NIL, - complete=NIL, - complete_arg=NIL, - count=NIL, - definition='echo "Hello World"', - name='Hello', - nargs='1', - range=NIL, - script_id=0, - } - local cmd_dict2 = { - addr=NIL, - complete=NIL, - complete_arg=NIL, - count=NIL, - definition='pwd', - name='Pwd', - nargs='?', - range=NIL, - script_id=0, - } + local cmd_dict = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='echo "Hello World"', name='Hello', nargs='1', range=NIL, register=false, script_id=0, } + local cmd_dict2 = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='pwd', name='Pwd', nargs='?', range=NIL, register=false, script_id=0, } before_each(clear) + it('gets empty list if no commands were defined', function() eq({}, meths.get_commands({builtin=false})) end) + it('validates input', function() expect_err('builtin commands not supported yet', meths.get_commands, {builtin=true}) expect_err('unexpected key: foo', meths.get_commands, {foo='blah'}) end) + it('gets global user-defined commands', function() -- Define a command. command('command -nargs=1 Hello echo "Hello World"') @@ -53,6 +36,7 @@ describe('nvim_get_commands', function() command('delcommand Pwd') 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"') @@ -64,34 +48,30 @@ describe('nvim_get_commands', function() command('delcommand Pwd') eq({cmd_dict}, curbufmeths.get_commands({builtin=false})) end) - it('gets different attributes of different commands', function() - local cmd1 = { - addr=NIL, - complete='custom', - complete_arg='ListUsers', - count=NIL, - definition='!finger ', - name='Finger', - nargs='+', - range=NIL, - script_id=1, - } - local cmd2 = { - addr='arguments', - complete='dir', - complete_arg=NIL, - count='10', - definition='pwd ', - name='TestCmd', - nargs='0', - range='10', - script_id=0, - } + + it('gets various command attributes', function() + local cmd0 = { addr='arguments', bang=false, bar=false, complete='dir', complete_arg=NIL, count='10', definition='pwd ', name='TestCmd', nargs='0', range='10', register=false, script_id=0, } + local cmd1 = { addr=NIL, bang=false, bar=false, complete='custom', complete_arg='ListUsers', count=NIL, definition='!finger ', name='Finger', nargs='+', range=NIL, register=false, script_id=1, } + local cmd2 = { addr=NIL, bang=true, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253Q2_foo()', name='Cmd2', nargs='*', range=NIL, register=false, script_id=2, } + local cmd3 = { addr=NIL, bang=false, bar=true, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253Q3_ohyeah()', name='Cmd3', nargs='0', range=NIL, register=false, script_id=3, } + local cmd4 = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='call \128\253Q4_just_great()', name='Cmd4', nargs='0', range=NIL, register=true, script_id=4, } source([[ command -complete=custom,ListUsers -nargs=+ Finger !finger ]]) eq({cmd1}, meths.get_commands({builtin=false})) command('command -complete=dir -addr=arguments -count=10 TestCmd pwd ') - eq({cmd1, cmd2}, meths.get_commands({builtin=false})) + eq({cmd1, cmd0}, meths.get_commands({builtin=false})) + + source([[ + command -bang -nargs=* Cmd2 call foo() + ]]) + source([[ + command -bar -nargs=0 Cmd3 call ohyeah() + ]]) + source([[ + command -register Cmd4 call just_great() + ]]) + -- TODO(justinmk): Order is stable but undefined. Sort before return? + eq({cmd2, cmd3, cmd4, cmd1, cmd0}, meths.get_commands({builtin=false})) end) end) From cb6672853a0df3dbe5202621d818ef1c2da7432c Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 11 May 2018 20:59:53 +0200 Subject: [PATCH 4/5] API: nvim_get_commands(): builtin is irrelevant for buffer-local builtin commands are never buffer-local, so we can return empty for that case. --- src/nvim/api/buffer.c | 23 +++++++++++++---------- test/functional/api/command_spec.lua | 5 ++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 19f92f002d..eda7fcd74c 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -481,8 +481,7 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) /// Gets a list of buffer-local |user-commands|. /// /// @param buffer Buffer handle. -/// @param opts Optional parameters. Currently only supports -/// {"builtin":false} +/// @param opts Optional parameters. Currently not used. /// @param[out] err Error details, if any. /// /// @returns Array of dictionaries describing commands. @@ -490,27 +489,31 @@ ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts, Error *err) FUNC_API_SINCE(4) { + bool global = (buffer == -1); + bool builtin = false; + 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); + 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 (strequal("builtin", k.data)) { + builtin = v.data.boolean; } } - if (buffer == -1) { + if (global) { + if (builtin) { + api_set_error(err, kErrorTypeValidation, "builtin=true not implemented"); + return (Array)ARRAY_DICT_INIT; + } return commands_array(NULL); } buf_T *buf = find_buffer_by_handle(buffer, err); - if (!buf) { + if (builtin || !buf) { return (Array)ARRAY_DICT_INIT; } return commands_array(buf); diff --git a/test/functional/api/command_spec.lua b/test/functional/api/command_spec.lua index 149a5dd111..c7cf081355 100644 --- a/test/functional/api/command_spec.lua +++ b/test/functional/api/command_spec.lua @@ -19,7 +19,7 @@ describe('nvim_get_commands', function() end) it('validates input', function() - expect_err('builtin commands not supported yet', meths.get_commands, + expect_err('builtin=true not implemented', meths.get_commands, {builtin=true}) expect_err('unexpected key: foo', meths.get_commands, {foo='blah'}) @@ -47,6 +47,9 @@ describe('nvim_get_commands', function() -- Delete a command. command('delcommand Pwd') eq({cmd_dict}, curbufmeths.get_commands({builtin=false})) + + -- {builtin=true} always returns empty for buffer-local case. + eq({}, curbufmeths.get_commands({builtin=true})) end) it('gets various command attributes', function() From 137eedb4edab1643b47282cce4ca07dd2ee42a63 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Fri, 11 May 2018 21:26:05 +0200 Subject: [PATCH 5/5] API: nvim_get_commands(): return Dictionary --- src/nvim/api/buffer.c | 17 ++++++++--------- src/nvim/api/vim.c | 10 +++++----- src/nvim/ex_docmd.c | 14 +++++++------- test/functional/api/command_spec.lua | 18 +++++++++--------- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index eda7fcd74c..fa4ad27e60 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -467,7 +467,7 @@ Integer nvim_buf_get_changedtick(Buffer buffer, Error *err) /// @returns Array of maparg()-like dictionaries describing mappings. /// The "buffer" key holds the associated buffer handle. 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); @@ -478,16 +478,15 @@ ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err) 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 opts Optional parameters. Currently not used. /// @param[out] err Error details, if any. /// -/// @returns Array of dictionaries describing commands. -ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts, - Error *err) - FUNC_API_SINCE(4) +/// @returns Map of maps describing commands. +Dictionary nvim_buf_get_commands(Buffer buffer, Dictionary opts, Error *err) + FUNC_API_SINCE(4) { bool global = (buffer == -1); bool builtin = false; @@ -497,7 +496,7 @@ ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts, 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; + return (Dictionary)ARRAY_DICT_INIT; } if (strequal("builtin", k.data)) { builtin = v.data.boolean; @@ -507,14 +506,14 @@ ArrayOf(Dictionary) nvim_buf_get_commands(Buffer buffer, Dictionary opts, if (global) { if (builtin) { api_set_error(err, kErrorTypeValidation, "builtin=true not implemented"); - return (Array)ARRAY_DICT_INIT; + return (Dictionary)ARRAY_DICT_INIT; } return commands_array(NULL); } buf_T *buf = find_buffer_by_handle(buffer, err); if (builtin || !buf) { - return (Array)ARRAY_DICT_INIT; + return (Dictionary)ARRAY_DICT_INIT; } return commands_array(buf); } diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 305cc1a43c..7a951d4e67 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -954,12 +954,12 @@ Dictionary nvim_get_mode(void) /// @returns Array of maparg()-like dictionaries describing mappings. /// The "buffer" key is always zero. ArrayOf(Dictionary) nvim_get_keymap(String mode) - FUNC_API_SINCE(3) + FUNC_API_SINCE(3) { 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. /// @@ -967,9 +967,9 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode) /// {"builtin":false} /// @param[out] err Error details, if any. /// -/// @returns Array of dictionaries describing commands. -ArrayOf(Dictionary) nvim_get_commands(Dictionary opts, Error *err) - FUNC_API_SINCE(4) +/// @returns Map of maps describing commands. +Dictionary nvim_get_commands(Dictionary opts, Error *err) + FUNC_API_SINCE(4) { return nvim_buf_get_commands(-1, opts, err); } diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index e92dbc4cc8..52b810085c 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -9969,15 +9969,15 @@ bool cmd_can_preview(char_u *cmd) return false; } -/// Gets a list of maps describing user-commands defined for buffer `buf` -/// or defined globally if `buf` is NULL. +/// Gets a map of maps describing user-commands defined for buffer `buf` or +/// 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 -ArrayOf(Dictionary) commands_array(buf_T *buf) +/// @return Map of maps describing commands +Dictionary commands_array(buf_T *buf) { - Array rv = ARRAY_DICT_INIT; + Dictionary rv = ARRAY_DICT_INIT; Object obj = NIL; char str[10]; garray_T *gap = (buf == NULL) ? &ucmds : &buf->b_ucmds; @@ -10043,7 +10043,7 @@ ArrayOf(Dictionary) commands_array(buf_T *buf) } PUT(d, "addr", obj); - ADD(rv, DICTIONARY_OBJ(d)); + PUT(rv, (char *)cmd->uc_name, DICTIONARY_OBJ(d)); } return rv; } diff --git a/test/functional/api/command_spec.lua b/test/functional/api/command_spec.lua index c7cf081355..f3f9f93649 100644 --- a/test/functional/api/command_spec.lua +++ b/test/functional/api/command_spec.lua @@ -28,25 +28,25 @@ describe('nvim_get_commands', function() it('gets global user-defined commands', function() -- Define a command. 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. 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. command('delcommand Pwd') - eq({cmd_dict}, meths.get_commands({builtin=false})) + eq({Hello=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})) + eq({Hello=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})) + eq({Hello=cmd_dict, Pwd=cmd_dict2}, curbufmeths.get_commands({builtin=false})) -- Delete a command. 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. eq({}, curbufmeths.get_commands({builtin=true})) @@ -61,9 +61,9 @@ describe('nvim_get_commands', function() source([[ command -complete=custom,ListUsers -nargs=+ Finger !finger ]]) - 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 ') - eq({cmd1, cmd0}, meths.get_commands({builtin=false})) + eq({Finger=cmd1, TestCmd=cmd0}, meths.get_commands({builtin=false})) source([[ command -bang -nargs=* Cmd2 call foo() @@ -75,6 +75,6 @@ describe('nvim_get_commands', function() command -register Cmd4 call just_great() ]]) -- 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)