sys: Add highlighting provider function support, move pattern highlighting code out of hex editor

This commit is contained in:
WerWolv
2022-02-16 14:57:13 +01:00
parent b9030d7e47
commit 7117592f38
6 changed files with 83 additions and 30 deletions

View File

@@ -1,6 +1,8 @@
#include "content/views/view_hex_editor.hpp"
#include <hex/api/imhex_api.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/helpers/file.hpp>
@@ -9,7 +11,6 @@
#include <hex/helpers/project_file_handler.hpp>
#include <hex/helpers/loader_script_handler.hpp>
#include <hex/pattern_language/pattern_data.hpp>
#include <content/providers/file_provider.hpp>
@@ -97,7 +98,7 @@ namespace hex::plugin::builtin {
std::optional<color_t> highlightColor;
std::string highlightTooltip;
for (const auto &[id, highlight] : ImHexApi::HexEditor::getHighlights()) {
for (const auto &[id, highlight] : ImHexApi::HexEditor::impl::getHighlights()) {
auto &region = highlight.getRegion();
auto &color = highlight.getColor();
auto &tooltip = highlight.getTooltip();
@@ -108,13 +109,11 @@ namespace hex::plugin::builtin {
}
}
auto patterns = provider->getPatternLanguageRuntime().getPatterns();
for (const auto &pattern : patterns) {
auto child = pattern->getPattern(blockStartOffset + i);
if (child != nullptr) {
auto color = (child->getColor() & 0x00FFFFFF) | alpha;
highlightColor = highlightColor.has_value() ? ImAlphaBlendColors(color, highlightColor.value()) : color;
break;
for (const auto &[id, function] : ImHexApi::HexEditor::impl::getHighlightingFunctions()) {
auto highlight = function(blockStartOffset + i);
if (highlight.has_value()) {
highlightColor = highlightColor.has_value() ? ImAlphaBlendColors(highlight->getColor(), highlightColor.value()) : highlight->getColor();
highlightTooltip = highlight->getTooltip();
}
}

View File

@@ -222,6 +222,19 @@ namespace hex::plugin::builtin {
});
}
});
ImHexApi::HexEditor::addHighlightingProvider([](u64 address) -> std::optional<ImHexApi::HexEditor::Highlighting> {
auto patterns = ImHexApi::Provider::get()->getPatternLanguageRuntime().getPatterns();
for (const auto &pattern : patterns) {
auto child = pattern->getPattern(address);
if (child != nullptr) {
return ImHexApi::HexEditor::Highlighting(Region { address, 1 }, child->getColor(), child->getVariableName());
}
}
return std::nullopt;
});
}
ViewPatternEditor::~ViewPatternEditor() {