Skip to content

Commit

Permalink
fix: update undo system to fix multi-tab undo
Browse files Browse the repository at this point in the history
  • Loading branch information
PocketMiner82 committed Apr 20, 2024
1 parent fa71218 commit 89ba8ef
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions pseudocodeIde/PseudocodeIDEForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ private void UserUpdatedText()
{
UpdateUndoStack(false);
}

TryHandleContentUpdate();
}

/// <summary>
Expand All @@ -268,26 +270,27 @@ private void UserChangedSelection()
HighlightWord(codeTextBox.SelectedText);
RemovePreviousTabSelectionIndicators();

TryHandleSelectionChange();
if (_lastCursorPosition != codeTextBox.SelectionStart && codeTextBox.Text != _undoStack.Peek().Code)
{
UpdateUndoStack(true);
}
}

/// <summary>
/// Forcefully updates the undoStack, when the cursor moved more than 1 since last check or the user selected something
/// </summary>
private void TryHandleSelectionChange()
private void TryHandleContentUpdate()
{
// ignore when we already undid something
if (_redoStack.Count != 0)
{
_lastCursorPosition = codeTextBox.SelectionStart;
return;
}

if (codeTextBox.SelectionEnd - codeTextBox.SelectionStart != 0 || Math.Abs(_lastCursorPosition - codeTextBox.SelectionStart) > 1)
{
UpdateUndoStack(true);
}
_lastCursorPosition = codeTextBox.SelectionStart;
}

private void CodeTextBox_UpdateUI(object sender, UpdateUIEventArgs e)
Expand All @@ -301,18 +304,13 @@ private void CodeTextBox_UpdateUI(object sender, UpdateUIEventArgs e)
UserChangedSelection();
break;
}
_lastCursorPosition = codeTextBox.SelectionStart;
}

private void CodeTextBox_TextChanged(object sender, EventArgs e)
{
// when the code is modified, the code is no longer saved in the file
SetFileNotSaved();

// only update undo stack if next event not ignored
if (!IgnoreTextChange)
{
TryHandleSelectionChange();
}

// Did the number of characters in the line number display change?
int maxLineNumberCharLength = codeTextBox.Lines.Count.ToString().Length;
Expand Down Expand Up @@ -346,17 +344,14 @@ private void CodeTextBox_KeyDown(object sender, KeyEventArgs e)
&& (Control.ModifierKeys == Keys.Control || Control.ModifierKeys == (Keys.Control | Keys.Shift)))
{
e.SuppressKeyPress = true;
return;
}

// deleting always updates undo stack
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
else if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
IgnoreTextChange = true;
UpdateUndoStack(true);
TryHandleSelectionChange();
CodeTextBox_UpdateUI(null, new UpdateUIEventArgs(UpdateChange.Content));
e.SuppressKeyPress = false;
return;
}
// find replace: find previous
else if (e.Shift && e.KeyCode == Keys.F3)
Expand Down Expand Up @@ -407,7 +402,7 @@ public void AddTabSelectionIndicators(List<(int selectionStart, int selectionEnd
codeTextBox.IndicatorFillRange(selectionStart, selectionEnd - selectionStart);
}

TryHandleSelectionChange();
TryHandleContentUpdate();
}

/// <summary>
Expand Down Expand Up @@ -889,7 +884,10 @@ private void UndoToolStripMenuItem_Click(object sender, EventArgs e)
{
IgnoreTextChange = true;
UpdateCodeTextBox(_undoStack.Peek());
} while (_undoStack.Count > 1 && currentText.Equals(_undoStack.Pop()));
} while (_undoStack.Count > 1 && currentText == _undoStack.Pop().Code);

// the undo stack always has the current text in it
_undoStack.Push(new UndoPoint(codeTextBox.Text, codeTextBox.SelectionStart, codeTextBox.SelectionEnd));
}
}

Expand Down Expand Up @@ -921,7 +919,7 @@ private void UpdateCodeTextBox(UndoPoint point)
codeTextBox.Text = point.Code;
codeTextBox.SelectionStart = point.SelectionStart;
codeTextBox.SelectionEnd = point.SelectionEnd;
UserChangedSelection();
TryHandleContentUpdate();
}

// ---------------------------------------------
Expand Down

0 comments on commit 89ba8ef

Please sign in to comment.