From 6e23560e80be784cc858bd0c4de2160dc0271b24 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 20 Mar 2023 14:11:43 +0100 Subject: [PATCH] feat: Added all menu items to command palette --- .../include/hex/api/content_registry.hpp | 21 ++++++++++++++ lib/libimhex/source/api/content_registry.cpp | 12 ++++++++ .../content/command_palette_commands.cpp | 28 +++++++++++++++++++ .../content/views/view_command_palette.cpp | 20 +++++++++++++ 4 files changed, 81 insertions(+) diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index ec945dd17..eec1de791 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -95,8 +95,14 @@ namespace hex { KeywordCommand }; + struct QueryResult { + std::string name; + std::function callback; + }; + using DisplayCallback = std::function; using ExecuteCallback = std::function; + using QueryCallback = std::function(std::string)>; struct Entry { Type type; @@ -106,13 +112,28 @@ namespace hex { ExecuteCallback executeCallback; }; + struct Handler { + Type type; + std::string command; + QueryCallback queryCallback; + DisplayCallback displayCallback; + }; + void add( Type type, const std::string &command, const std::string &unlocalizedDescription, const DisplayCallback &displayCallback, const ExecuteCallback &executeCallback = [](auto) {}); + + void addHandler( + Type type, + const std::string &command, + const QueryCallback &queryCallback, + const DisplayCallback &displayCallback); + std::vector &getEntries(); + std::vector &getHandlers(); } /* Pattern Language Function Registry. Allows adding of new functions that may be used inside the pattern language */ diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 65177748e..347016ebf 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -220,12 +220,24 @@ namespace hex { getEntries().push_back(ContentRegistry::CommandPaletteCommands::Entry { type, command, unlocalizedDescription, displayCallback, executeCallback }); } + void addHandler(Type type, const std::string &command, const QueryCallback &queryCallback, const DisplayCallback &displayCallback) { + log::debug("Registered new command palette command handler: {}", command); + + getHandlers().push_back(ContentRegistry::CommandPaletteCommands::Handler { type, command, queryCallback, displayCallback }); + } + std::vector &getEntries() { static std::vector commands; return commands; } + std::vector &getHandlers() { + static std::vector commands; + + return commands; + } + } diff --git a/plugins/builtin/source/content/command_palette_commands.cpp b/plugins/builtin/source/content/command_palette_commands.cpp index a5a95bd33..bec643d10 100644 --- a/plugins/builtin/source/content/command_palette_commands.cpp +++ b/plugins/builtin/source/content/command_palette_commands.cpp @@ -52,6 +52,34 @@ namespace hex::plugin::builtin { [](auto input) { hex::runCommand(input); }); + + ContentRegistry::CommandPaletteCommands::addHandler( + ContentRegistry::CommandPaletteCommands::Type::SymbolCommand, + ">", + [](const auto &input) { + std::vector result; + + for (const auto &[priority, entry] : ContentRegistry::Interface::getMenuItems()) { + if (!entry.enabledCallback()) + continue; + + std::vector names; + std::transform(entry.unlocalizedNames.begin(), entry.unlocalizedNames.end(), std::back_inserter(names), [](auto &name) { return LangEntry(name); }); + + if (auto combined = wolv::util::combineStrings(names, " -> "); hex::containsIgnoreCase(combined, input) && !combined.contains(ContentRegistry::Interface::impl::SeparatorValue) && !combined.contains(ContentRegistry::Interface::impl::SubMenuValue)) { + result.emplace_back(ContentRegistry::CommandPaletteCommands::QueryResult { + std::move(combined), + [entry](const auto&) { entry.callback(); } + }); + } + } + + return result; + }, + [](auto input) { + return hex::format("Menu Item: {}", input.data()); + }); + } } \ No newline at end of file diff --git a/plugins/builtin/source/content/views/view_command_palette.cpp b/plugins/builtin/source/content/views/view_command_palette.cpp index c36b4745b..fa2baef9e 100644 --- a/plugins/builtin/source/content/views/view_command_palette.cpp +++ b/plugins/builtin/source/content/views/view_command_palette.cpp @@ -122,6 +122,26 @@ namespace hex::plugin::builtin { } } + for (const auto &handler : ContentRegistry::CommandPaletteCommands::getHandlers()) { + const auto &[type, command, queryCallback, displayCallback] = handler; + + auto processedInput = input; + if (processedInput.starts_with(command)) + processedInput = processedInput.substr(command.length()); + + for (const auto &[description, callback] : queryCallback(processedInput)) { + if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) { + if (auto [match, value] = MatchCommand(input, command); match != MatchType::NoMatch) { + results.push_back({ hex::format("{} ({})", command, description), "", callback }); + } + } else if (type == ContentRegistry::CommandPaletteCommands::Type::KeywordCommand) { + if (auto [match, value] = MatchCommand(input, command + " "); match != MatchType::NoMatch) { + results.push_back({ hex::format("{} ({})", command, description), "", callback }); + } + } + } + } + return results; }