From a598b2938a40095d3ad51a2e9547db9669a827cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Standa=20Luke=C5=A1?= Date: Thu, 19 Oct 2023 16:16:37 +0200 Subject: [PATCH] Fix whitespace padding width with tabs and Unicode combining characters 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. --- .../ViewCompiler/DefaultViewCompiler.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Framework/Framework/Compilation/ViewCompiler/DefaultViewCompiler.cs b/src/Framework/Framework/Compilation/ViewCompiler/DefaultViewCompiler.cs index 4507494343..5c67f77199 100644 --- a/src/Framework/Framework/Compilation/ViewCompiler/DefaultViewCompiler.cs +++ b/src/Framework/Framework/Compilation/ViewCompiler/DefaultViewCompiler.cs @@ -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; @@ -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 {