Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto evaluate doesn't work with undo. #2106

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/third_party/imgui/ColorTextEditor/include/TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class TextEditor

void Render(const char* aTitle, const ImVec2& aSize = ImVec2(), bool aBorder = false);
void SetText(const std::string& aText);
void JumpToLine(int line);
void JumpToLine(int line=-1);
void JumpToCoords(const Coordinates &coords);
std::string GetText() const;
bool isEmpty() const {
Expand Down Expand Up @@ -352,6 +352,7 @@ class TextEditor
void SetReadOnly(bool aValue);
bool IsReadOnly() const { return mReadOnly; }
bool IsTextChanged() const { return mTextChanged; }
void SetTextChanged(bool aValue=false) { mTextChanged = aValue; }
bool IsCursorPositionChanged() const { return mCursorPositionChanged; }
bool IsBreakpointsChanged() const { return mBreakPointsChanged; }
void ClearBreakpointsChanged() { mBreakPointsChanged = false; }
Expand Down Expand Up @@ -578,7 +579,7 @@ class TextEditor
void HandleKeyboardInputs();
void HandleMouseInputs();
void RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPos, const ImVec2 &textEditorSize);

void SetFocus();
float mLineSpacing = 1.0F;
Lines mLines;
EditorState mState = {};
Expand Down
28 changes: 19 additions & 9 deletions lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,16 @@ inline void TextUnformattedColoredAt(const ImVec2 &pos, const ImU32 &color, cons
ImGui::PopStyleColor();
}

void TextEditor::SetFocus() {
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = mFocusAtCoords;
mSelectionMode = SelectionMode::Normal;
SetSelection(mInteractiveStart, mInteractiveEnd, mSelectionMode);
ResetCursorBlinkTime();
EnsureCursorVisible();
ImGui::SetKeyboardFocusHere(-1);
mUpdateFocus = false;
}

void TextEditor::RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPos, const ImVec2 &textEditorSize) {
/* Compute mCharAdvance regarding scaled font size (Ctrl + mouse wheel)*/
const float fontSize = ImGui::GetFont()->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, -1.0f, "#", nullptr, nullptr).x;
Expand Down Expand Up @@ -1068,6 +1078,10 @@ void TextEditor::RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPo
auto prevColor = line.empty() ? mPalette[(int)PaletteIndex::Default] : GetGlyphColor(line[0]);
ImVec2 bufferOffset;

if (mUpdateFocus && mFocusAtCoords == Coordinates(lineNo, 0)) {
SetFocus();
}

for (int i = 0; i < line.size();) {
auto &glyph = line[i];
auto color = GetGlyphColor(glyph);
Expand Down Expand Up @@ -1106,13 +1120,7 @@ void TextEditor::RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPo
prevColor = color;

if (mUpdateFocus && mFocusAtCoords == Coordinates(lineNo, i)) {
mState.mCursorPosition = mInteractiveStart = mInteractiveEnd = mFocusAtCoords;
mSelectionMode = SelectionMode::Normal;
SetSelection(mInteractiveStart, mInteractiveEnd, mSelectionMode);
ResetCursorBlinkTime();
EnsureCursorVisible();
ImGui::SetKeyboardFocusHere(-1);
mUpdateFocus = false;
SetFocus();
}

if (glyph.mChar == '\t') {
Expand Down Expand Up @@ -1206,7 +1214,6 @@ void TextEditor::RenderText(const char *aTitle, const ImVec2 &lineNumbersStartPo

void TextEditor::Render(const char *aTitle, const ImVec2 &aSize, bool aBorder) {
mWithinRender = true;
mTextChanged = false;
mCursorPositionChanged = false;

auto scrollBg = ImGui::GetStyleColorVec4(ImGuiCol_ScrollbarBg);
Expand Down Expand Up @@ -1634,7 +1641,10 @@ void TextEditor::DeleteSelection() {
}

void TextEditor::JumpToLine(int line) {
auto newPos = Coordinates(line, 0);
auto newPos = mState.mCursorPosition;
if (line != -1) {
newPos = Coordinates(line , 0);
}
JumpToCoords(newPos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ namespace hex::plugin::builtin {
bool m_replaceMode = false;
bool m_openFindReplacePopUp = false;
bool m_openGotoLinePopUp = false;
bool m_patternEvaluating = false;
std::map<std::fs::path, std::string> m_patternNames;

ImRect m_textEditorHoverBox;
Expand Down
10 changes: 8 additions & 2 deletions plugins/builtin/source/content/views/view_pattern_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,14 @@ namespace hex::plugin::builtin {
}
}

if (m_textEditor.IsTextChanged()) {
if (m_textEditor.IsTextChanged() && !m_hasUnevaluatedChanges) {
m_hasUnevaluatedChanges = true;
m_lastEditorChangeTime = std::chrono::steady_clock::now();
ImHexApi::Provider::markDirty();
}

if (m_hasUnevaluatedChanges && m_runningEvaluators == 0 && m_runningParsers == 0) {
if ((std::chrono::steady_clock::now() - m_lastEditorChangeTime) > std::chrono::seconds(1LL)) {
m_hasUnevaluatedChanges = false;

auto code = m_textEditor.GetText();
EventPatternEditorChanged::post(code);
Expand All @@ -598,6 +597,8 @@ namespace hex::plugin::builtin {
if (m_runAutomatically)
m_triggerAutoEvaluate = true;
});
m_hasUnevaluatedChanges = false;
m_textEditor.SetTextChanged();
}
}

Expand Down Expand Up @@ -1088,6 +1089,10 @@ namespace hex::plugin::builtin {
ImGui::PopFont();

ImGui::SetCursorPosY(ImGui::GetCursorPosY() + ImGui::GetStyle().FramePadding.y + 1_scaled);
if (m_patternEvaluating && m_runningEvaluators == 0) {
m_patternEvaluating = false;
m_textEditor.JumpToLine();
}
}

void ViewPatternEditor::drawEnvVars(ImVec2 size, std::list<EnvVar> &envVars) {
Expand Down Expand Up @@ -1899,6 +1904,7 @@ namespace hex::plugin::builtin {

m_accessHistory = {};
m_accessHistoryIndex = 0;
m_patternEvaluating = true;

EventHighlightingChanged::post();

Expand Down
Loading