From 90e0aa83d87213e1471fac171d9064a9362c4762 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 11 Jan 2021 23:02:55 +0100 Subject: [PATCH] Added registry for command palette commands --- .../include/helpers/content_registry.hpp | 19 ++++++ .../libimhex/include/helpers/shared_data.hpp | 50 ++++++++------- .../source/helpers/content_registry.cpp | 11 ++++ source/views/view_command_palette.cpp | 62 ++++++++++++------- 4 files changed, 94 insertions(+), 48 deletions(-) diff --git a/plugins/libimhex/include/helpers/content_registry.hpp b/plugins/libimhex/include/helpers/content_registry.hpp index 78b01bac6..e78824b9a 100644 --- a/plugins/libimhex/include/helpers/content_registry.hpp +++ b/plugins/libimhex/include/helpers/content_registry.hpp @@ -39,6 +39,25 @@ namespace hex { static auto get(std::string_view name); }; + + struct CommandPaletteCommands { + CommandPaletteCommands() = delete; + + enum class Type : u32 { + SymbolCommand, + KeywordCommand + }; + + struct Entry { + Type type; + std::string command; + std::string description; + std::function callback; + }; + + static void add(Type type, std::string_view command, std::string_view description, const std::function &callback); + static std::vector getEntries(); + }; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/helpers/shared_data.hpp b/plugins/libimhex/include/helpers/shared_data.hpp index cae4b1289..a59e0cd4a 100644 --- a/plugins/libimhex/include/helpers/shared_data.hpp +++ b/plugins/libimhex/include/helpers/shared_data.hpp @@ -58,34 +58,35 @@ namespace hex { static nlohmann::json settingsJsonStorage; static std::map customEventsStorage; static u32 customEventsLastIdStorage = u32(Events::Events_BuiltinEnd) + 1; + static std::vector commandPaletteCommandsStorage; - this->imguiContext = ImGui::GetCurrentContext(); - this->eventHandlers = &eventHandlersStorage; - this->deferredCalls = &deferredCallsStorage; - this->currentProvider = ¤tProviderStorage; - this->settingsEntries = &settingsEntriesStorage; - this->sharedVariables = &sharedVariablesStorage; - this->settingsJson = &settingsJsonStorage; - this->customEvents = &customEventsStorage; - this->customEventsLastId = &customEventsLastIdStorage; - - this->windowPos = &windowPosStorage; - this->windowSize = &windowSizeStorage; + this->imguiContext = ImGui::GetCurrentContext(); + this->eventHandlers = &eventHandlersStorage; + this->deferredCalls = &deferredCallsStorage; + this->currentProvider = ¤tProviderStorage; + this->settingsEntries = &settingsEntriesStorage; + this->sharedVariables = &sharedVariablesStorage; + this->windowPos = &windowPosStorage; + this->windowSize = &windowSizeStorage; + this->settingsJson = &settingsJsonStorage; + this->customEvents = &customEventsStorage; + this->customEventsLastId = &customEventsLastIdStorage; + this->commandPaletteCommands = &commandPaletteCommandsStorage; } void initializeData(const SharedData &other) { - this->imguiContext = other.imguiContext; - this->eventHandlers = other.eventHandlers; - this->deferredCalls = other.deferredCalls; - this->currentProvider = other.currentProvider; - this->settingsEntries = other.settingsEntries; - this->sharedVariables = other.sharedVariables; - this->settingsJson = other.settingsJson; - this->customEvents = other.customEvents; - this->customEventsLastId = other.customEventsLastId; - - this->windowPos = other.windowPos; - this->windowSize = other.windowSize; + this->imguiContext = other.imguiContext; + this->eventHandlers = other.eventHandlers; + this->deferredCalls = other.deferredCalls; + this->currentProvider = other.currentProvider; + this->settingsEntries = other.settingsEntries; + this->sharedVariables = other.sharedVariables; + this->windowPos = other.windowPos; + this->windowSize = other.windowSize; + this->settingsJson = other.settingsJson; + this->customEvents = other.customEvents; + this->customEventsLastId = other.customEventsLastId; + this->commandPaletteCommands = other.commandPaletteCommands; } public: @@ -97,6 +98,7 @@ namespace hex { nlohmann::json *settingsJson; std::map *customEvents; u32 *customEventsLastId; + std::vector *commandPaletteCommands; ImVec2 *windowPos; ImVec2 *windowSize; diff --git a/plugins/libimhex/source/helpers/content_registry.cpp b/plugins/libimhex/source/helpers/content_registry.cpp index 868034e57..6b0d57d08 100644 --- a/plugins/libimhex/source/helpers/content_registry.cpp +++ b/plugins/libimhex/source/helpers/content_registry.cpp @@ -62,4 +62,15 @@ namespace hex { return customEvents[name.data()]; } + + /* Command Palette Commands */ + + void ContentRegistry::CommandPaletteCommands::add(ContentRegistry::CommandPaletteCommands::Type type, std::string_view command, std::string_view description, const std::function &callback) { + SharedData::get().commandPaletteCommands->push_back(ContentRegistry::CommandPaletteCommands::Entry{ type, command.data(), description.data(), callback }); + } + + std::vector ContentRegistry::CommandPaletteCommands::getEntries() { + return *SharedData::get().commandPaletteCommands; + } + } \ No newline at end of file diff --git a/source/views/view_command_palette.cpp b/source/views/view_command_palette.cpp index 67569f6b9..01222eb24 100644 --- a/source/views/view_command_palette.cpp +++ b/source/views/view_command_palette.cpp @@ -10,6 +10,27 @@ namespace hex { this->getWindowOpenState() = true; this->m_commandBuffer.resize(1024, 0x00); + + ContentRegistry::CommandPaletteCommands::add( + ContentRegistry::CommandPaletteCommands::Type::SymbolCommand, + "#", "Calculator", + [](std::string input) { + MathEvaluator evaluator; + evaluator.registerStandardVariables(); + evaluator.registerStandardFunctions(); + + std::optional result; + + try { + result = evaluator.evaluate(input); + } catch (std::runtime_error &e) {} + + if (result.has_value()) + return hex::format("#%s = %Lf", input.data(), result.value()); + else + return hex::format("#%s = ???", input.data()); + }); + this->m_lastResults = this->getCommandResults(""); } @@ -77,7 +98,7 @@ namespace hex { PerfectMatch }; - std::vector ViewCommandPalette::getCommandResults(std::string_view command) { + std::vector ViewCommandPalette::getCommandResults(std::string_view input) { constexpr auto matchCommand = [](std::string_view currCommand, std::string_view commandToMatch) -> std::pair { if (currCommand.empty()) { return { MatchType::InfoMatch, "" }; @@ -98,31 +119,24 @@ namespace hex { std::vector results; - if (auto [match, value] = matchCommand(command, "#"); match != MatchType::NoMatch) { - if (match != MatchType::PerfectMatch) - results.emplace_back("# (Calculator)"); - else { - MathEvaluator evaluator; - evaluator.registerStandardVariables(); - evaluator.registerStandardFunctions(); + for (const auto &[type, command, description, callback] : ContentRegistry::CommandPaletteCommands::getEntries()) { - auto result = evaluator.evaluate(std::string(value)); + if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) { + if (auto [match, value] = matchCommand(input, command); match != MatchType::NoMatch) { + if (match != MatchType::PerfectMatch) + results.emplace_back(command + " (" + description + ")"); + else + results.emplace_back(callback(input.substr(command.length()).data())); + } + } else if (type == ContentRegistry::CommandPaletteCommands::Type::KeywordCommand) { + if (auto [match, value] = matchCommand(input, command + " "); match != MatchType::NoMatch) { + if (match != MatchType::PerfectMatch) + results.emplace_back(command + " (" + description + ")"); + else + results.emplace_back(callback(input.substr(command.length() + 1).data())); + } + } - if (result.has_value()) - results.emplace_back(hex::format("#%s = %Lf", value.data(), result.value())); - else - results.emplace_back(hex::format("#%s = ???", value.data())); - } - } - if (auto [match, value] = matchCommand(command, "/find "); match != MatchType::NoMatch) { - if (match != MatchType::PerfectMatch) - results.emplace_back("/find (Find Command)"); - else { - results.emplace_back(hex::format("Command: Find \"%s\"", value.data())); - } - } - if (auto [match, value] = matchCommand(command, ">"); match != MatchType::NoMatch) { - results.emplace_back("> (Command)"); } return results;