From fd07d7cf3bb7e40d10782341c19b38842aaaaef2 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Thu, 20 Nov 2014 22:09:41 +0100 Subject: [PATCH] Store a parent menu reference in bundle items instead of a path This is simpler to setup and maintain and requires less storage. --- Frameworks/bundles/src/item.cc | 8 +++--- Frameworks/bundles/src/item.h | 6 ++-- Frameworks/bundles/src/query.cc | 51 +++++++++++---------------------- Frameworks/bundles/src/query.h | 2 +- 4 files changed, 24 insertions(+), 43 deletions(-) diff --git a/Frameworks/bundles/src/item.cc b/Frameworks/bundles/src/item.cc index 9812bf9e..db89ea9d 100644 --- a/Frameworks/bundles/src/item.cc +++ b/Frameworks/bundles/src/item.cc @@ -165,14 +165,14 @@ namespace bundles return name() + (bundle() ? " — " + bundle()->name() : ""); } - std::vector const& item_t::menu_path () const + oak::uuid_t const& item_t::parent_menu () const { - return _menu_path; + return _parent_menu; } - void item_t::set_menu_path (std::vector const& newMenuPath) + void item_t::set_parent_menu (oak::uuid_t const& newParentMenu) { - _menu_path = newMenuPath; + _parent_menu = newParentMenu; } oak::uuid_t const& item_t::uuid () const diff --git a/Frameworks/bundles/src/item.h b/Frameworks/bundles/src/item.h index 22171bc5..9a79b55f 100644 --- a/Frameworks/bundles/src/item.h +++ b/Frameworks/bundles/src/item.h @@ -48,8 +48,8 @@ namespace bundles std::string const& name () const; void set_name (std::string const& newName); std::string full_name () const; - std::vector const& menu_path () const; - void set_menu_path (std::vector const& newMenuPath); + oak::uuid_t const& parent_menu () const; + void set_parent_menu (oak::uuid_t const& uuid); oak::uuid_t const& uuid () const; oak::uuid_t bundle_uuid () const; scope::selector_t const& scope_selector () const; @@ -114,7 +114,7 @@ namespace bundles mutable std::mutex _plist_mutex; std::vector _required_bundles; std::vector _required_executables; - std::vector _menu_path; + oak::uuid_t _parent_menu; }; } /* bundles */ diff --git a/Frameworks/bundles/src/query.cc b/Frameworks/bundles/src/query.cc index e473c3cd..2b69d04a 100644 --- a/Frameworks/bundles/src/query.cc +++ b/Frameworks/bundles/src/query.cc @@ -129,31 +129,6 @@ namespace bundles } } - static void setup_full_name (oak::uuid_t const& menuUUID, std::map< oak::uuid_t, std::vector > const& menus, std::map const& items, std::vector* prefixStack) - { - std::map< oak::uuid_t, std::vector >::const_iterator menu = menus.find(menuUUID); - if(menu != menus.end()) - { - for(auto const& uuid : menu->second) - { - std::map::const_iterator item = items.find(uuid); - if(item == items.end()) - continue; - - if(item->second->kind() == kItemTypeMenu) - { - prefixStack->push_back(item->second->name()); - setup_full_name(item->second->uuid(), menus, items, prefixStack); - prefixStack->pop_back(); - } - else - { - item->second->set_menu_path(*prefixStack); - } - } - } - } - bool set_index (std::vector const& items, std::map< oak::uuid_t, std::vector > const& menus) { Callbacks(&callback_t::bundles_will_change); @@ -162,16 +137,22 @@ namespace bundles AllMenus = menus; cache().clear(); - std::map< item_ptr, std::map > map; // bundle → { item_uuid → bundle_item } + std::map lookupTable; for(auto const& item : items) { - if(item->bundle()) - map[item->bundle()].emplace(item->uuid(), item); + if(item_ptr bundle = item->bundle()) + item->set_parent_menu(bundle->uuid()); + lookupTable.emplace(item->uuid(), item); } - std::vector stack; - for(auto const& bundle : map) - setup_full_name(bundle.first->uuid(), menus, bundle.second, &stack); + for(auto const& pair : menus) + { + for(auto const& itemUUID : pair.second) + { + if(auto item = lookupTable[itemUUID]) + item->set_parent_menu(pair.first); + } + } Callbacks(&callback_t::bundles_did_change); @@ -338,11 +319,11 @@ namespace bundles return format_bundle_item_title(item->name(), hasSelection); } - std::string menu_path (item_ptr const& item) + std::string menu_path (item_ptr item) { - std::vector path = item->menu_path(); - if(item->kind() != kItemTypeBundle && item->bundle()) - path.insert(path.begin(), item->bundle()->name()); + std::deque path; + while(item = lookup(item->parent_menu())) + path.push_front(item->name()); return text::join(path, " ▸ "); } diff --git a/Frameworks/bundles/src/query.h b/Frameworks/bundles/src/query.h index 76e6699d..644a4f52 100644 --- a/Frameworks/bundles/src/query.h +++ b/Frameworks/bundles/src/query.h @@ -24,7 +24,7 @@ namespace bundles PUBLIC std::vector items_for_proxy (item_ptr proxyItem, scope::context_t const& scope = scope::wildcard, int kind = kItemTypeCommand|kItemTypeDragCommand|kItemTypeGrammar|kItemTypeMacro|kItemTypeSnippet|kItemTypeProxy|kItemTypeTheme, oak::uuid_t const& bundle = oak::uuid_t(), bool filter = true, bool includeDisabledItems = false); PUBLIC item_ptr lookup (oak::uuid_t const& uuid); PUBLIC std::string name_with_selection (item_ptr const& item, bool hasSelection); - PUBLIC std::string menu_path (item_ptr const& item); + PUBLIC std::string menu_path (item_ptr item); PUBLIC std::string key_equivalent (item_ptr const& item); } /* bundles */