menu_get(): fix query behavior

- Return the menu properties, not only its children.
- If the {path} param is given, return only the first node. The "next"
  nodes in the linked-list are irrelevant.
This commit is contained in:
Justin M. Keyes
2019-01-27 00:21:22 +01:00
parent d760e08fac
commit 1a3d2dbfe7
3 changed files with 28 additions and 20 deletions

View File

@@ -5666,23 +5666,24 @@ max({expr}) Return the maximum value of all items in {expr}.
menu_get({path}, {modes}) *menu_get()* menu_get({path}, {modes}) *menu_get()*
Returns a |List| of |Dictionaries| describing |menus| (defined Returns a |List| of |Dictionaries| describing |menus| (defined
by |:menu|, |:amenu|, etc.). by |:menu|, |:amenu|, …), including |hidden-menus|.
{path} limits the result to a subtree of the menu hierarchy
(empty string matches all menus). E.g. to get items in the {path} matches a menu by name, or all menus if {path} is an
"File" menu subtree: > empty string. Example: >
:echo menu_get('File','') :echo menu_get('File','')
:echo menu_get('')
< <
{modes} is a string of zero or more modes (see |maparg()| or {modes} is a string of zero or more modes (see |maparg()| or
|creating-menus| for the list of modes). "a" means "all". |creating-menus| for the list of modes). "a" means "all".
For example: > Example: >
nnoremenu &Test.Test inormal nnoremenu &Test.Test inormal
inoremenu Test.Test insert inoremenu Test.Test insert
vnoremenu Test.Test x vnoremenu Test.Test x
echo menu_get("") echo menu_get("")
<
returns something like this: < returns something like this: >
>
[ { [ {
"hidden": 0, "hidden": 0,
"name": "Test", "name": "Test",

View File

@@ -735,27 +735,31 @@ static dict_T *menu_get_recursive(const vimmenu_T *menu, int modes)
/// @return false if could not find path_name /// @return false if could not find path_name
bool menu_get(char_u *const path_name, int modes, list_T *list) bool menu_get(char_u *const path_name, int modes, list_T *list)
{ {
vimmenu_T *menu; vimmenu_T *menu = find_menu(root_menu, path_name, modes);
menu = find_menu(root_menu, path_name, modes);
if (!menu) { if (!menu) {
return false; return false;
} }
for (; menu != NULL; menu = menu->next) { for (; menu != NULL; menu = menu->next) {
dict_T *dict = menu_get_recursive(menu, modes); dict_T *d = menu_get_recursive(menu, modes);
if (dict && tv_dict_len(dict) > 0) { if (d && tv_dict_len(d) > 0) {
tv_list_append_dict(list, dict); tv_list_append_dict(list, d);
}
if (*path_name != NUL) {
// If a (non-empty) path query was given, only the first node in the
// find_menu() result is relevant. Else we want all nodes.
break;
} }
} }
return true; return true;
} }
/// Find menu matching required name and modes /// Find menu matching `name` and `modes`.
/// ///
/// @param menu top menu to start looking from /// @param menu top menu to start looking from
/// @param name path towards the menu /// @param name path towards the menu
/// @return menu if \p name is null, found menu or NULL /// @return menu if \p name is null, found menu or NULL
static 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; char_u *p;

View File

@@ -83,7 +83,7 @@ describe('menu_get', function()
it("path='', modes='a'", function() it("path='', modes='a'", function()
local m = funcs.menu_get("","a"); local m = funcs.menu_get("","a");
-- HINT: To print the expected table and regenerate the tests: -- HINT: To print the expected table and regenerate the tests:
-- print(require('pl.pretty').dump(m)) -- print(require('inspect')(m))
local expected = { local expected = {
{ {
shortcut = "T", shortcut = "T",
@@ -310,8 +310,11 @@ describe('menu_get', function()
it('matching path, all modes', function() it('matching path, all modes', function()
local m = funcs.menu_get("Export", "a") local m = funcs.menu_get("Export", "a")
local expected = { local expected = { {
{ hidden = 0,
name = "Export",
priority = 500,
submenus = { {
tooltip = "This is the tooltip", tooltip = "This is the tooltip",
hidden = 0, hidden = 0,
name = "Script", name = "Script",
@@ -325,8 +328,8 @@ describe('menu_get', function()
silent = 0 silent = 0
} }
} }
} } }
} } }
eq(expected, m) eq(expected, m)
end) end)