Enhance East Asian width handling and text input buffer functionality#335
Merged
Enhance East Asian width handling and text input buffer functionality#335
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves Unicode correctness in Sharprompt’s terminal rendering and interactive editing by switching width calculation and text input buffer operations to be grapheme-cluster aware (handling emoji ZWJ sequences, keycaps, modifiers, and regional indicator flags).
Changes:
- Updated
EastAsianWidth.GetWidthto iterate by text elements (grapheme clusters) and to treat certain Unicode categories as zero-width, with added emoji/sequence-aware width rules. - Refactored
TextInputBuffercursor movement and deletion/word operations to act on text elements instead of UTF-16 code units. - Added new tests covering complex emoji/grapheme scenarios for both width calculation and text buffer operations.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Sharprompt/Internal/TextInputBuffer.cs | Refactors editing/navigation logic to use grapheme-cluster boundaries tracked via StringInfo.ParseCombiningCharacters. |
| Sharprompt/Internal/EastAsianWidth.cs | Changes width calculation to be text-element based and adds special handling for emoji sequences/zero-width code points. |
| Sharprompt.Tests/TextInputBufferTests.cs | Adds test coverage for complex emoji grapheme clusters across editing and navigation operations. |
| Sharprompt.Tests/EastAsianWidthTests.cs | Adds tests for ZWJ, emoji sequences, keycaps, modifiers, and regional indicator flags. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
This pull request significantly improves how the code handles complex Unicode text elements, such as emojis and combined characters, in both width calculation and text input buffer operations. The changes ensure accurate cursor movement, deletion, and word navigation for grapheme clusters, including multi-codepoint emojis and regional indicators. Comprehensive tests were added to validate these behaviors.
Unicode and emoji handling improvements:
EastAsianWidth.GetWidthto correctly calculate string width for emojis, regional indicators, zero-width joiners, and other complex Unicode sequences, including handling zero-width and emoji-modifier codepoints. [1] [2]EastAsianWidthTests.csfor zero-width joiner, emoji sequences, and combined emojis to ensure correct width calculation.Text input buffer enhancements:
TextInputBufferto operate on Unicode text elements (grapheme clusters) rather than individual code units, improving correctness for insertion, deletion, backspacing, and cursor movement.Testing for complex text input scenarios:
TextInputBufferTests.csfor backspace, delete, move backward/forward, and word navigation with complex emojis and combined characters, ensuring robust handling of these cases. [1] [2] [3] [4] [5]