ui: Properly clear highlighting cache when switching provider

Fixes #471
This commit is contained in:
WerWolv 2022-03-13 17:33:27 +01:00
parent 97bfb4004b
commit 74ef9ece30
3 changed files with 38 additions and 21 deletions

View File

@ -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;

View File

@ -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() {
@ -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() {

View File

@ -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) {
@ -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();
}