From 866b956680a25b648e8dc62fa909b0e03757944e Mon Sep 17 00:00:00 2001 From: paxcut <53811119+paxcut@users.noreply.github.com> Date: Sun, 15 Sep 2024 06:28:37 -0700 Subject: [PATCH] feat: Added per provider cursor position for the Pattern editor (#1861) ### Problem description Currently, the pattern editor does not remember where the cursor is located in each provider. For example, suppose you have 2 providers in your project, and you scrolled down to line 200 in the first pattern to make some changes and remembered that the code you want to insert is in the second provider. Then you switch to the second provider, look for the code and find it in line 235. Switch back to the first one, and you are at the beginning of the file. So you again look for the line to edit paste it to realize that it needs code a few lines before the place you found it. You switch to the second provider, and you are at the top again. This gets annoying very fast. ### Implementation description This PR ensures that, when you return to the pattern in the editor for any of the opened providers, the cursor will still be at the same place it was when you switched to a different one. Each provider pattern saves its cursor position and returns to it when you switch to that provider. It does that by creating a PerProvider variable and using it when providers are first opened to set it to the origin and when switching providers it first saves the position of the old provider and then loads and sets the saved position of the new provider. --------- Co-authored-by: Nik --- .../content/views/view_pattern_editor.hpp | 1 + .../source/content/views/view_pattern_editor.cpp | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/plugins/builtin/include/content/views/view_pattern_editor.hpp b/plugins/builtin/include/content/views/view_pattern_editor.hpp index db25fe728..da7afbaac 100644 --- a/plugins/builtin/include/content/views/view_pattern_editor.hpp +++ b/plugins/builtin/include/content/views/view_pattern_editor.hpp @@ -248,6 +248,7 @@ namespace hex::plugin::builtin { std::mutex m_logMutex; + PerProvider m_cursorPosition; PerProvider> m_lastEvaluationError; PerProvider> m_lastCompileError; PerProvider> m_callStack; diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 4435b9d44..1bac9ff0e 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -566,7 +566,7 @@ namespace hex::plugin::builtin { } if (m_hasUnevaluatedChanges && m_runningEvaluators == 0 && m_runningParsers == 0) { - if ((std::chrono::steady_clock::now() - m_lastEditorChangeTime) > std::chrono::seconds(1)) { + if ((std::chrono::steady_clock::now() - m_lastEditorChangeTime) > std::chrono::seconds(1LL)) { m_hasUnevaluatedChanges = false; auto code = m_textEditor.GetText(); @@ -1735,7 +1735,7 @@ namespace hex::plugin::builtin { *m_breakpointHit = true; m_resetDebuggerVariables = true; while (*m_breakpointHit) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(100LL)); } }); @@ -1758,7 +1758,7 @@ namespace hex::plugin::builtin { m_dangerousFunctionCalled = true; while (m_dangerousFunctionsAllowed == DangerousFunctionPerms::Ask) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(100LL)); } return m_dangerousFunctionsAllowed == DangerousFunctionPerms::Allow; @@ -1852,15 +1852,19 @@ namespace hex::plugin::builtin { m_envVarEntries->emplace_back(0, "", i128(0), EnvVarType::Integer); m_debuggerDrawer.get(provider) = std::make_unique(); + m_cursorPosition.get(provider) = TextEditor::Coordinates(0, 0); }); EventProviderChanged::subscribe(this, [this](prv::Provider *oldProvider, prv::Provider *newProvider) { - if (oldProvider != nullptr) + if (oldProvider != nullptr) { m_sourceCode.set(oldProvider, m_textEditor.GetText()); + m_cursorPosition.set(m_textEditor.GetCursorPosition(),oldProvider); + } - if (newProvider != nullptr) + if (newProvider != nullptr) { m_textEditor.SetText(m_sourceCode.get(newProvider)); - else + m_textEditor.SetCursorPosition(m_cursorPosition.get(newProvider)); + } else m_textEditor.SetText(""); });