Skip to content

Commit

Permalink
ui: Properly clear highlighting cache when switching provider
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Mar 13, 2022
1 parent 97bfb40 commit 74ef9ec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
6 changes: 6 additions & 0 deletions plugins/builtin/include/content/views/view_pattern_editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ namespace hex::plugin::builtin {
u32 m_selectedPatternFile = 0;
bool m_runAutomatically = false;

bool m_lastEvaluationProcessed = true;
bool m_lastEvaluationResult = false;
std::optional<pl::PatternLanguageError> m_lastEvaluationError;
std::vector<std::pair<pl::LogConsole::Level, std::string>> m_lastEvaluationLog;
std::map<std::string, pl::Token::Literal> m_lastEvaluationOutVars;

std::atomic<u32> m_runningEvaluators = 0;
std::atomic<u32> m_runningParsers = 0;

Expand Down
5 changes: 5 additions & 0 deletions plugins/builtin/source/content/views/view_hex_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ namespace hex::plugin::builtin {
EventManager::unsubscribe<RequestOpenWindow>(this);
EventManager::unsubscribe<EventSettingsChanged>(this);
EventManager::unsubscribe<EventHighlightingChanged>(this);
EventManager::unsubscribe<EventProviderChanged>(this);
}

void ViewHexEditor::drawContent() {
Expand Down Expand Up @@ -962,6 +963,10 @@ namespace hex::plugin::builtin {
EventManager::subscribe<EventHighlightingChanged>(this, [this] {
this->m_highlights.clear();
});

EventManager::subscribe<EventProviderChanged>(this, [](auto, auto) {
EventManager::post<EventHighlightingChanged>();
});
}

void ViewHexEditor::registerShortcuts() {
Expand Down
48 changes: 27 additions & 21 deletions plugins/builtin/source/content/views/view_pattern_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ namespace hex::plugin::builtin {
View::discardNavigationRequests();
}
ImGui::End();

if (!this->m_lastEvaluationProcessed) {
this->m_console = this->m_lastEvaluationLog;

if (!this->m_lastEvaluationResult) {
TextEditor::ErrorMarkers errorMarkers = {
{this->m_lastEvaluationError->getLineNumber(), this->m_lastEvaluationError->what()}
};
this->m_textEditor.SetErrorMarkers(errorMarkers);
} else {
for (auto &[name, variable] : this->m_patternVariables) {
if (variable.outVariable && this->m_lastEvaluationOutVars.contains(name))
variable.value = this->m_lastEvaluationOutVars.at(name);
}

EventManager::post<EventHighlightingChanged>();
}

this->m_lastEvaluationProcessed = true;
}
}

void ViewPatternEditor::drawConsole(ImVec2 size) {
Expand Down Expand Up @@ -657,30 +677,16 @@ namespace hex::plugin::builtin {
auto provider = ImHexApi::Provider::get();
auto &runtime = provider->getPatternLanguageRuntime();

auto result = runtime.executeString(provider, code, envVars, inVariables);
if (!result) {
auto error = runtime.getError();
if (error) {
TextEditor::ErrorMarkers errorMarkers = {
{error->getLineNumber(), error->what()}
};
this->m_textEditor.SetErrorMarkers(errorMarkers);
}
}

this->m_console = runtime.getConsoleLog();

auto outVariables = runtime.getOutVariables();
for (auto &[name, variable] : this->m_patternVariables) {
if (variable.outVariable && outVariables.contains(name))
variable.value = outVariables.at(name);
}

if (result) {
EventManager::post<EventHighlightingChanged>();
this->m_lastEvaluationResult = runtime.executeString(provider, code, envVars, inVariables);
if (!this->m_lastEvaluationResult) {
this->m_lastEvaluationError = runtime.getError();
}

this->m_lastEvaluationLog = runtime.getConsoleLog();
this->m_lastEvaluationOutVars = runtime.getOutVariables();
this->m_runningEvaluators--;

this->m_lastEvaluationProcessed = false;
}).detach();
}

Expand Down

0 comments on commit 74ef9ec

Please sign in to comment.