Skip to content

Commit

Permalink
feat: Added per provider cursor position for the Pattern editor (#1861)
Browse files Browse the repository at this point in the history
### 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 <werwolv98@gmail.com>
  • Loading branch information
paxcut and WerWolv committed Sep 15, 2024
1 parent d88252c commit 866b956
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ namespace hex::plugin::builtin {

std::mutex m_logMutex;

PerProvider<TextEditor::Coordinates> m_cursorPosition;
PerProvider<std::optional<pl::core::err::PatternLanguageError>> m_lastEvaluationError;
PerProvider<std::vector<pl::core::err::CompileError>> m_lastCompileError;
PerProvider<std::vector<const pl::core::ast::ASTNode*>> m_callStack;
Expand Down
16 changes: 10 additions & 6 deletions plugins/builtin/source/content/views/view_pattern_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
});

Expand All @@ -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;
Expand Down Expand Up @@ -1852,15 +1852,19 @@ namespace hex::plugin::builtin {
m_envVarEntries->emplace_back(0, "", i128(0), EnvVarType::Integer);

m_debuggerDrawer.get(provider) = std::make_unique<ui::PatternDrawer>();
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("");
});

Expand Down

0 comments on commit 866b956

Please sign in to comment.