Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1a3d2db

Browse files
committed
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.
1 parent d760e08 commit 1a3d2db

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

runtime/doc/eval.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5666,23 +5666,24 @@ max({expr}) Return the maximum value of all items in {expr}.
56665666

56675667
menu_get({path}, {modes}) *menu_get()*
56685668
Returns a |List| of |Dictionaries| describing |menus| (defined
5669-
by |:menu|, |:amenu|, etc.).
5670-
{path} limits the result to a subtree of the menu hierarchy
5671-
(empty string matches all menus). E.g. to get items in the
5672-
"File" menu subtree: >
5669+
by |:menu|, |:amenu|, …), including |hidden-menus|.
5670+
5671+
{path} matches a menu by name, or all menus if {path} is an
5672+
empty string. Example: >
56735673
:echo menu_get('File','')
5674+
:echo menu_get('')
56745675
<
56755676
{modes} is a string of zero or more modes (see |maparg()| or
56765677
|creating-menus| for the list of modes). "a" means "all".
56775678

5678-
For example: >
5679+
Example: >
56795680
nnoremenu &Test.Test inormal
56805681
inoremenu Test.Test insert
56815682
vnoremenu Test.Test x
56825683
echo menu_get("")
5683-
<
5684-
returns something like this:
5685-
>
5684+
5685+
< returns something like this: >
5686+
56865687
[ {
56875688
"hidden": 0,
56885689
"name": "Test",

src/nvim/menu.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,27 +735,31 @@ static dict_T *menu_get_recursive(const vimmenu_T *menu, int modes)
735735
/// @return false if could not find path_name
736736
bool menu_get(char_u *const path_name, int modes, list_T *list)
737737
{
738-
vimmenu_T *menu;
739-
menu = find_menu(root_menu, path_name, modes);
738+
vimmenu_T *menu = find_menu(root_menu, path_name, modes);
740739
if (!menu) {
741740
return false;
742741
}
743742
for (; menu != NULL; menu = menu->next) {
744-
dict_T *dict = menu_get_recursive(menu, modes);
745-
if (dict && tv_dict_len(dict) > 0) {
746-
tv_list_append_dict(list, dict);
743+
dict_T *d = menu_get_recursive(menu, modes);
744+
if (d && tv_dict_len(d) > 0) {
745+
tv_list_append_dict(list, d);
746+
}
747+
if (*path_name != NUL) {
748+
// If a (non-empty) path query was given, only the first node in the
749+
// find_menu() result is relevant. Else we want all nodes.
750+
break;
747751
}
748752
}
749753
return true;
750754
}
751755

752756

753-
/// Find menu matching required name and modes
757+
/// Find menu matching `name` and `modes`.
754758
///
755759
/// @param menu top menu to start looking from
756760
/// @param name path towards the menu
757761
/// @return menu if \p name is null, found menu or NULL
758-
static vimmenu_T* find_menu(vimmenu_T *menu, char_u * name, int modes)
762+
static vimmenu_T *find_menu(vimmenu_T *menu, char_u *name, int modes)
759763
{
760764
char_u *p;
761765

test/functional/ex_cmds/menu_spec.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('menu_get', function()
8383
it("path='', modes='a'", function()
8484
local m = funcs.menu_get("","a");
8585
-- HINT: To print the expected table and regenerate the tests:
86-
-- print(require('pl.pretty').dump(m))
86+
-- print(require('inspect')(m))
8787
local expected = {
8888
{
8989
shortcut = "T",
@@ -310,8 +310,11 @@ describe('menu_get', function()
310310

311311
it('matching path, all modes', function()
312312
local m = funcs.menu_get("Export", "a")
313-
local expected = {
314-
{
313+
local expected = { {
314+
hidden = 0,
315+
name = "Export",
316+
priority = 500,
317+
submenus = { {
315318
tooltip = "This is the tooltip",
316319
hidden = 0,
317320
name = "Script",
@@ -325,8 +328,8 @@ describe('menu_get', function()
325328
silent = 0
326329
}
327330
}
328-
}
329-
}
331+
} }
332+
} }
330333
eq(expected, m)
331334
end)
332335

0 commit comments

Comments
 (0)