Conversation
Use String.visualWidth (backed by StringInfo.LengthInTextElements) instead of String.length when advancing the column counter in WriterModel.update. Combining characters attach to a preceding base character and have no visual width; counting them as separate code units caused the formatter to over-estimate line length and make wrong line-break decisions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…acking-v2-d609b15d49dde407
Contributor
|
/repo-assist format the code |
Contributor
Author
|
✗ Repo Assist encountered failed, see workflow run. |
…acking-v2-d609b15d49dde407
4 tasks
…acking-v2-d609b15d49dde407
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 PR was created by Repo Assist, an automated AI assistant.
Summary
Fixes #2945 — Unicode strings containing combining characters (e.g. diacritics like U+036E, U+0312, U+036B) caused incorrect column tracking in the formatter's
WriterModel, leading to wrong line-break decisions.Root Cause
WriterModel.updateadvanced the column counter usingString.length s, which counts UTF-16 code units. Combining characters have no visual advance — they attach to the preceding base character. A string like"l\u036e\u0312\u036b"hasString.length = 4but visually occupies only 1 column. This caused Fantomas to over-estimate line width and make wrong line-break decisions.Fix
String.visualWidthinUtils.fs/Utils.fsi, backed bySystem.Globalization.StringInfo.LengthInTextElements, which counts grapheme clusters rather than UTF-16 code units.WriterModel.update(Write/WriteTriviacases inContext.fs) to useString.visualWidthinstead ofString.length.StringInfoon every token write (which is the common case for typical F# source code).Trade-offs
The fast path means no overhead for ASCII tokens (the common case).
StringInfois only allocated for tokens with non-ASCII characters.Test Status
dotnet build fantomas.sln— ✅ Build succeededdotnet test src/Fantomas.Core.Tests/— ✅ 2838 passed, 0 failed, 11 skippedTwo new tests added:
String.visualWidth counts grapheme clusters not UTF-16 code units, 2945(UtilsTests.fs)string with Unicode combining characters should not affect formatting decisions, 2945(StringTests.fs)