-
Notifications
You must be signed in to change notification settings - Fork 0
Add Memento pattern source generator for classes, structs, and records #88
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
Merged
+2,148
−1
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5552645
Initial plan
Copilot 290c13d
Add Memento pattern source generator with comprehensive tests
Copilot b42d982
Add comprehensive Memento pattern demos and documentation
Copilot bb0a281
Address code review feedback
Copilot 879e1df
ci: bump gitversion version
JerrettDavis 63ab37e
Address PR review feedback
Copilot 2fafc85
Address additional PR review feedback
Copilot 8641aca
Fix code quality issues in MementoGenerator
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
266 changes: 266 additions & 0 deletions
266
src/PatternKit.Examples/Generators/Memento/EditorStateDemo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| using PatternKit.Generators; | ||
|
|
||
| namespace PatternKit.Examples.Generators.Memento; | ||
|
|
||
| /// <summary> | ||
| /// Demonstrates the Memento pattern source generator with a text editor scenario. | ||
| /// Shows snapshot capture, restore, and undo/redo functionality using generated code. | ||
| /// </summary> | ||
| public static class EditorStateDemo | ||
| { | ||
| /// <summary> | ||
| /// Immutable editor state using record class with generated memento support. | ||
| /// The [Memento] attribute generates: | ||
| /// - EditorStateMemento struct for capturing snapshots | ||
| /// - EditorStateHistory class for undo/redo management | ||
| /// </summary> | ||
| [Memento(GenerateCaretaker = true, Capacity = 100, SkipDuplicates = true)] | ||
| public partial record class EditorState(string Text, int Cursor, int SelectionLength) | ||
| { | ||
| public bool HasSelection => SelectionLength > 0; | ||
|
|
||
| public int SelectionStart => Cursor; | ||
|
|
||
| public int SelectionEnd => Cursor + SelectionLength; | ||
|
|
||
| /// <summary> | ||
| /// Creates an initial empty state. | ||
| /// </summary> | ||
| public static EditorState Empty() => new("", 0, 0); | ||
|
|
||
| /// <summary> | ||
| /// Inserts text at the cursor position (or replaces selection). | ||
| /// Returns a new state with the text inserted. | ||
| /// </summary> | ||
| public EditorState Insert(string text) | ||
| { | ||
| if (string.IsNullOrEmpty(text)) | ||
| return this; | ||
|
|
||
| string newText; | ||
| int newCursor; | ||
|
|
||
| if (HasSelection) | ||
| { | ||
| // Replace selection | ||
| newText = Text.Remove(SelectionStart, SelectionLength).Insert(SelectionStart, text); | ||
| newCursor = SelectionStart + text.Length; | ||
| } | ||
| else | ||
| { | ||
| // Insert at cursor | ||
| newText = Text.Insert(Cursor, text); | ||
| newCursor = Cursor + text.Length; | ||
| } | ||
|
|
||
| return this with { Text = newText, Cursor = newCursor, SelectionLength = 0 }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Moves the cursor to a new position. | ||
| /// </summary> | ||
| public EditorState MoveCursor(int position) | ||
| { | ||
| var newCursor = Math.Clamp(position, 0, Text.Length); | ||
| return this with { Cursor = newCursor, SelectionLength = 0 }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Selects text from start position with the given length. | ||
| /// </summary> | ||
| public EditorState Select(int start, int length) | ||
| { | ||
| start = Math.Clamp(start, 0, Text.Length); | ||
| var end = Math.Clamp(start + length, 0, Text.Length); | ||
| var selLength = end - start; | ||
| return this with { Cursor = start, SelectionLength = selLength }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Deletes the selection or one character before the cursor. | ||
| /// </summary> | ||
| public EditorState Backspace() | ||
| { | ||
| if (HasSelection) | ||
| { | ||
| var newText = Text.Remove(SelectionStart, SelectionLength); | ||
| return this with { Text = newText, Cursor = SelectionStart, SelectionLength = 0 }; | ||
| } | ||
|
|
||
| if (Cursor == 0) | ||
| return this; | ||
|
|
||
| var newText2 = Text.Remove(Cursor - 1, 1); | ||
| return this with { Text = newText2, Cursor = Cursor - 1, SelectionLength = 0 }; | ||
| } | ||
|
|
||
| public override string ToString() => HasSelection | ||
| ? $"Text='{Text}' Cursor={Cursor} Sel=[{SelectionStart},{SelectionEnd})" | ||
| : $"Text='{Text}' Cursor={Cursor}"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Text editor using the generated caretaker for undo/redo. | ||
| /// </summary> | ||
| public sealed class TextEditor | ||
| { | ||
| // The generated EditorStateHistory class manages undo/redo | ||
| private readonly EditorStateHistory _history; | ||
|
|
||
| public TextEditor() | ||
| { | ||
| _history = new EditorStateHistory(EditorState.Empty()); | ||
| } | ||
|
|
||
| public EditorState Current => _history.Current; | ||
|
|
||
| public bool CanUndo => _history.CanUndo; | ||
|
|
||
| public bool CanRedo => _history.CanRedo; | ||
|
|
||
| public int HistoryCount => _history.Count; | ||
|
|
||
| /// <summary> | ||
| /// Applies an editing operation and captures it in history. | ||
| /// </summary> | ||
| public void Apply(Func<EditorState, EditorState> operation) | ||
| { | ||
| var newState = operation(Current); | ||
| _history.Capture(newState); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Undoes the last operation. | ||
| /// </summary> | ||
| public bool Undo() | ||
| { | ||
| return _history.Undo(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Redoes the last undone operation. | ||
| /// </summary> | ||
| public bool Redo() | ||
| { | ||
| return _history.Redo(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Clears all history and resets to empty state. | ||
| /// </summary> | ||
| public void Clear() | ||
| { | ||
| _history.Clear(EditorState.Empty()); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Runs a demonstration of the text editor with undo/redo. | ||
| /// </summary> | ||
| public static List<string> Run() | ||
| { | ||
| var log = new List<string>(); | ||
| var editor = new TextEditor(); | ||
|
|
||
| void LogState(string action) | ||
| { | ||
| log.Add($"{action}: {editor.Current}"); | ||
| } | ||
|
|
||
| // Initial state | ||
| LogState("Initial"); | ||
|
|
||
| // Type "Hello" | ||
| editor.Apply(s => s.Insert("Hello")); | ||
| LogState("Insert 'Hello'"); | ||
|
|
||
| // Type " world" | ||
| editor.Apply(s => s.Insert(" world")); | ||
| LogState("Insert ' world'"); | ||
|
|
||
| // Move cursor to position 5 (after "Hello") | ||
| editor.Apply(s => s.MoveCursor(5)); | ||
| LogState("Move cursor to 5"); | ||
|
|
||
| // Insert " brave new" | ||
| editor.Apply(s => s.Insert(" brave new")); | ||
| LogState("Insert ' brave new'"); | ||
|
|
||
| // Select "Hello" (0-5) | ||
| editor.Apply(s => s.Select(0, 5)); | ||
| LogState("Select 'Hello'"); | ||
|
|
||
| // Replace with "Hi" | ||
| editor.Apply(s => s.Insert("Hi")); | ||
| LogState("Replace with 'Hi'"); | ||
|
|
||
| // Undo (restore "Hello brave new world" with selection) | ||
| if (editor.Undo()) | ||
| { | ||
| LogState("Undo"); | ||
| } | ||
|
|
||
| // Undo (restore no selection) | ||
| if (editor.Undo()) | ||
| { | ||
| LogState("Undo"); | ||
| } | ||
|
|
||
| // Undo (restore "Hello world") | ||
| if (editor.Undo()) | ||
| { | ||
| LogState("Undo"); | ||
| } | ||
|
|
||
| // Redo | ||
| if (editor.Redo()) | ||
| { | ||
| LogState("Redo"); | ||
| } | ||
|
|
||
| // Create divergent branch: make a new edit | ||
| editor.Apply(s => s.MoveCursor(s.Text.Length)); | ||
| LogState("Move to end (divergent)"); | ||
|
|
||
| editor.Apply(s => s.Insert("!!!")); | ||
| LogState("Insert '!!!' (clears redo)"); | ||
|
|
||
| // Try to redo (should fail - redo history was truncated) | ||
| if (!editor.Redo()) | ||
| { | ||
| log.Add("Redo failed (as expected - forward history was truncated)"); | ||
| } | ||
|
|
||
| log.Add($"Final: {editor.Current}"); | ||
| log.Add($"CanUndo: {editor.CanUndo}, CanRedo: {editor.CanRedo}"); | ||
| log.Add($"History count: {editor.HistoryCount}"); | ||
|
|
||
| return log; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Demonstrates manual memento capture/restore without the caretaker. | ||
| /// </summary> | ||
| public static List<string> RunManualSnapshot() | ||
| { | ||
| var log = new List<string>(); | ||
|
|
||
| var state1 = new EditorState("Hello", 5, 0); | ||
| log.Add($"State1: {state1}"); | ||
|
|
||
| // Manually capture a memento | ||
| var memento = EditorStateMemento.Capture(in state1); | ||
| log.Add($"Captured memento: Version={memento.MementoVersion}"); | ||
|
|
||
| // Modify state | ||
| var state2 = state1.Insert(" world"); | ||
| log.Add($"State2: {state2}"); | ||
|
|
||
| // Restore from memento | ||
| var restored = memento.RestoreNew(); | ||
| log.Add($"Restored: {restored}"); | ||
| log.Add($"Restored equals State1: {restored == state1}"); | ||
|
|
||
| return log; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.