Optimize TextEditorModel's InvalidLineRange from class to struct, add comprehensive test coverage, fix exception handling for TokenizeViewPort#577
Open
udlose wants to merge 4 commits intoAvaloniaUI:masterfrom
Conversation
Added extensive new unit tests to TextEditorModelTests.cs, covering line counting, content, insert/remove/replace operations, batch updates, disposal idempotency, event handler resilience, exception handling, InvalidLineRange logic, DocumentSnapshot consistency, and edge cases (including CRLF/mixed endings and viewport/tokenization logic). Tests use Avalonia headless infrastructure and reflection helpers for thorough coverage. Existing tests were reorganized and some replaced with more comprehensive versions. This greatly improves confidence in TextEditorModel's correctness and robustness.
- Add thread-safe disposal checks using Volatile.Read/Write and _isDisposed - Change InvalidLineRange from class to readonly struct to reduce allocations - Refactor invalid range merging to return new struct instead of mutating - Early-exit event handlers if disposed to prevent invalid operations - Refactor TokenizeViewPort to use Dispatcher.UIThread.Post and extract core logic for testability - Improve XML documentation and code clarity - Use C# 8 index-from-end operator (^) for cleaner list access - Add ThrowIfDisposed for consistent disposal checks - Update property/field declarations for consistency
Refactored LinkElementGenerator and MailLinkElementGenerator to use C# source-generated regular expressions via the [GeneratedRegex] attribute. This replaces static Regex field initializations, improving performance and reducing allocations. Public API and logic remain unchanged.
danipen
requested changes
Apr 6, 2026
Collaborator
There was a problem hiding this comment.
Would be possible to avoid using reflection here for testing?
I think it's worth using InternalsVisibleTo or just making internal methods XXXForTesting rather than using reflection what is quite bad in terms of maintainability.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Classification
Code cleanup and test enhancement to improve resource management, thread-safety, and test coverage for
TextEditorModel.PR Summary
Refactored
TextEditorModelfor safer disposal and better testability, and significantly expanded unit tests for edge cases and internal behaviors.TextEditorModel.cs: Added thread-safe disposal, convertedInvalidLineRangeto a struct, improved event handler safety, and refactored tokenization logic.ReflectionTestHelper.cs: Introduced a utility for invoking private/internal members in tests.TextEditorModelTests.cs: Added comprehensive tests for disposal, event handling, edge cases, and internal logic using the new reflection helper.Test Coverage