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

Implement more sane caret behavior #270

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
83 changes: 81 additions & 2 deletions ReClass.NET/Controls/HotSpotTextBox.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ReClassNET.Debugger;
using ReClassNET.UI;

namespace ReClassNET.Controls
Expand Down Expand Up @@ -54,6 +56,83 @@ protected override void OnVisibleChanged(EventArgs e)
}
}

protected override bool ProcessCmdKey(ref Message m, Keys keyData)
{
// Checks if we're on some address.
var selectionPredicate = (char c) =>
{
c = Char.ToLower(c);
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z');
};

if (keyData == (Keys.Control | Keys.Left))
{
if (SelectionStart > 0 && !string.IsNullOrEmpty(Text))
{
var atEnd = SelectionStart == Text.Length;
var selectionInText = Math.Min(SelectionStart, Text.Length - 1);
var currChar = () => Text[selectionInText - 1];
bool currMatchesPredicate = (atEnd && selectionPredicate(Text[selectionInText])) || selectionPredicate(currChar());

if (currMatchesPredicate)
{
while (selectionInText > 0 && selectionPredicate(currChar()))
{
selectionInText -= 1;
}
}
else
{
selectionInText -= 1;
while (selectionInText > 0 && !selectionPredicate(currChar()))
{
selectionInText -= 1;
}
}

selectionInText = Math.Max(selectionInText, 0);
SelectionStart = selectionInText;
SelectionLength = 0;

return true;
}
}
else if (keyData == (Keys.Control | Keys.Right))
{
var maxSelectionStart = Text.Length;
if (!string.IsNullOrEmpty(Text) && SelectionStart != maxSelectionStart)
{
var selectionInText = Math.Min(SelectionStart, Text.Length - 1);
var currChar = () => Text[selectionInText];
bool currMatchesPredicate = selectionPredicate(currChar());

if (currMatchesPredicate)
{
while (selectionInText < maxSelectionStart && selectionPredicate(currChar()))
{
selectionInText += 1;
}
}
else
{
selectionInText += 1;
while (selectionInText < maxSelectionStart && !selectionPredicate(currChar()))
{
selectionInText += 1;
}
}

selectionInText = Math.Min(selectionInText, maxSelectionStart);
SelectionStart = selectionInText;
SelectionLength = 0;

return true;
}
}

return base.ProcessCmdKey(ref m, keyData);
}

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
Expand Down Expand Up @@ -87,7 +166,7 @@ private void OnCommit()
Committed?.Invoke(this, new HotSpotTextBoxCommitEventArgs(currentHotSpot));
}

#endregion
#endregion Events

public void ShowOnHotSpot(HotSpot hotSpot)
{
Expand Down Expand Up @@ -127,4 +206,4 @@ public HotSpotTextBoxCommitEventArgs(HotSpot hotSpot)
HotSpot = hotSpot;
}
}
}
}