Skip to content

Commit

Permalink
Fix whitespace padding width with tabs and Unicode combining characters
Browse files Browse the repository at this point in the history
Tabs are replaced by tabs to preserve the string width,
all other characters are replaced by a single space.
Moreover we now count the unicode graphemes instead of UTF16 units.
It still has the problem that some characters (Emoji, CJK) are wider even
in monospace fonts, but that's not that easy to determine.
  • Loading branch information
exyi committed Oct 19, 2023
1 parent 6d1a350 commit a598b29
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using DotVVM.Framework.Compilation.ControlTree;
using DotVVM.Framework.Compilation.ControlTree.Resolved;
Expand Down Expand Up @@ -148,10 +149,19 @@ public override string ToString()
""
);
string error;
if (ContextLine is {})
if (ContextLine is {} contextLine)
{
var errorHighlight = new string(' ', CharPosition ?? 1) + new string('^', HighlightLength);
error = $"{fileLocation}: Dotvvm Compilation Warning\n{ContextLine}\n{errorHighlight} {Message}";
var graphemeIndices = StringInfo.ParseCombiningCharacters(contextLine.Substring(0, Math.Min(contextLine.Length, CharPosition ?? 0)));
var padding = string.Concat(
graphemeIndices.Select(
startIndex => contextLine[startIndex] switch {
'\t' => "\t",
_ => " "
}
)
);
var errorHighlight = padding + new string('^', HighlightLength);
error = $"{fileLocation}: Dotvvm Compilation Warning\n{contextLine}\n{errorHighlight} {Message}";
}
else
{
Expand Down

0 comments on commit a598b29

Please sign in to comment.