From 827ed144fb4cfafd2727f55e6b25e07c96962138 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 28 Mar 2018 01:45:00 +0200 Subject: fix ":menu Item.SubItem" :menu should print sub-menu contents. E.g. this should print the "File.Save" submenu: nvim -u NORC :source $VIMRUNTIME/menu.vim :menu File.Save Regressed in dc685387a3d6 Blocks #8173 menu_get() also was missing some results for some cases. --- src/nvim/menu.c | 24 +++++++++++++++--------- test/functional/ex_cmds/menu_spec.lua | 26 ++++++++++++++++++-------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/nvim/menu.c b/src/nvim/menu.c index 074feb9906..0183f1d276 100644 --- a/src/nvim/menu.c +++ b/src/nvim/menu.c @@ -754,22 +754,27 @@ bool menu_get(char_u *const path_name, int modes, list_T *list) /// @param menu top menu to start looking from /// @param name path towards the menu /// @return menu if \p name is null, found menu or NULL -vimmenu_T * -find_menu(vimmenu_T *menu, char_u * name, int modes) +static vimmenu_T* find_menu(vimmenu_T *menu, char_u * name, int modes) { char_u *p; while (*name) { + // find the end of one dot-separated name and put a NUL at the dot p = menu_name_skip(name); while (menu != NULL) { if (menu_name_equal(name, menu)) { - /* Found menu */ + // Found menu if (*p != NUL && menu->children == NULL) { - EMSG(_(e_notsubmenu)); - return NULL; - } else if ((menu->modes & modes) == 0x0) { - EMSG(_(e_othermode)); - return NULL; + if (*p != NUL) { + EMSG(_(e_notsubmenu)); + return NULL; + } else if ((menu->modes & modes) == 0x0) { + EMSG(_(e_othermode)); + return NULL; + } + } + if (*p == NUL) { // found a full match + return menu; } break; } @@ -780,6 +785,7 @@ find_menu(vimmenu_T *menu, char_u * name, int modes) EMSG2(_(e_nomenu), name); return NULL; } + // Found a match, search the sub-menu. name = p; menu = menu->children; } @@ -1235,7 +1241,7 @@ static char_u *popup_mode_name(char_u *name, int idx) /// /// @return a pointer to allocated memory. static char_u *menu_text(const char_u *str, int *mnemonic, char_u **actext) - FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT + FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1) { char_u *p; diff --git a/test/functional/ex_cmds/menu_spec.lua b/test/functional/ex_cmds/menu_spec.lua index 2c0535acda..d06af45be0 100644 --- a/test/functional/ex_cmds/menu_spec.lua +++ b/test/functional/ex_cmds/menu_spec.lua @@ -363,14 +363,24 @@ describe('menu_get', function() local m = funcs.menu_get("Test","i") local expected = { { - mappings = { - i = { - sid = 1, - noremap = 1, - enabled = 1, - rhs = "insert", - silent = 0 - } + shortcut = "T", + submenus = { + { + mappings = { + i = { + sid = 1, + noremap = 1, + enabled = 1, + rhs = "insert", + silent = 0 + }, + }, + priority = 500, + name = "Test", + hidden = 0 + }, + { + }, }, priority = 500, name = "Test", -- cgit From d760e08facda964c04c08d589135cd6b53e16196 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 26 Jan 2019 21:55:45 +0100 Subject: menu_get(): Do not include empty items Caused by a typo: `dict` instead of `dic`. Renamed variable to `d` to make it less similar. --- src/nvim/menu.c | 11 ++++++----- test/functional/ex_cmds/menu_spec.lua | 30 ++++++++++++++---------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/nvim/menu.c b/src/nvim/menu.c index 0183f1d276..805952f395 100644 --- a/src/nvim/menu.c +++ b/src/nvim/menu.c @@ -660,7 +660,8 @@ static void free_menu_string(vimmenu_T *menu, int idx) /// /// @param[in] menu if null, starts from root_menu /// @param modes, a choice of \ref MENU_MODES -/// @return a dict with name/commands +/// @return dict with name/commands +/// @see show_menus_recursive /// @see menu_get static dict_T *menu_get_recursive(const vimmenu_T *menu, int modes) { @@ -715,10 +716,10 @@ static dict_T *menu_get_recursive(const vimmenu_T *menu, int modes) // visit recursively all children list_T *const children_list = tv_list_alloc(kListLenMayKnow); for (menu = menu->children; menu != NULL; menu = menu->next) { - dict_T *dic = menu_get_recursive(menu, modes); - if (tv_dict_len(dict) > 0) { - tv_list_append_dict(children_list, dic); - } + dict_T *d = menu_get_recursive(menu, modes); + if (tv_dict_len(d) > 0) { + tv_list_append_dict(children_list, d); + } } tv_dict_add_list(dict, S_LEN("submenus"), children_list); } diff --git a/test/functional/ex_cmds/menu_spec.lua b/test/functional/ex_cmds/menu_spec.lua index d06af45be0..07a40c7a2c 100644 --- a/test/functional/ex_cmds/menu_spec.lua +++ b/test/functional/ex_cmds/menu_spec.lua @@ -63,19 +63,21 @@ describe('menu_get', function() before_each(function() clear() - command('nnoremenu &Test.Test inormal') - command('inoremenu Test.Test insert') - command('vnoremenu Test.Test x') - command('cnoremenu Test.Test cmdmode') - command('menu Test.Nested.test level1') - command('menu Test.Nested.Nested2 level2') + command([=[ + nnoremenu &Test.Test inormal + inoremenu Test.Test insert + vnoremenu Test.Test x + cnoremenu Test.Test cmdmode + menu Test.Nested.test level1 + menu Test.Nested.Nested2 level2 - command('nnoremenu