Skip to content

Commit aa8d4a2

Browse files
committed
feature: show line-endings when toggle Show hidden symbols in text diff view (#2458)
The text diff view only shows the output of `git diff` command. And this commit makes it possible to see the line-endings in the `git diff` command output. Remember when the git configure `core.autocrlf` is `true`, all line-endings of file that committed to the repo will be replaced with `\n`. To see line-ending changes, users must turn off `Ignore CR at end-of-line in diff` settings from `Preferences` dialog. Signed-off-by: leo <longshuang@msn.cn>
1 parent 004bea9 commit aa8d4a2

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

‎src/Views/TextDiffView.axaml.cs‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ public void Draw(TextView textView, DrawingContext drawingContext)
221221
var lines = _presenter.GetLines();
222222
var width = textView.Bounds.Width;
223223
var pixelHeight = PixelSnapHelpers.GetPixelSize(textView).Height;
224+
var typeface = textView.CreateTypeface();
225+
var lineEndingBrush = new SolidColorBrush(Colors.Gray, 0.8);
226+
224227
foreach (var line in textView.VisualLines)
225228
{
226229
if (line.IsDisposed || line.FirstDocumentLine == null || line.FirstDocumentLine.IsDeleted)
@@ -232,8 +235,9 @@ public void Draw(TextView textView, DrawingContext drawingContext)
232235

233236
var info = lines[index - 1];
234237

238+
var lastTextLine = line.TextLines[^1];
235239
var startY = line.GetTextLineVisualYPosition(line.TextLines[0], VisualYPosition.LineTop) - textView.VerticalOffset;
236-
var endY = line.GetTextLineVisualYPosition(line.TextLines[^1], VisualYPosition.LineBottom) - textView.VerticalOffset;
240+
var endY = line.GetTextLineVisualYPosition(lastTextLine, VisualYPosition.LineBottom) - textView.VerticalOffset;
237241

238242
var bg = GetBrushByLineType(info.Type);
239243
if (bg != null)
@@ -281,14 +285,30 @@ public void Draw(TextView textView, DrawingContext drawingContext)
281285

282286
if (info.NoNewLineEndOfFile)
283287
{
284-
var lastTextLine = line.TextLines[^1];
285288
var radius = Math.Min(6, (lastTextLine.Height - 4) * 0.5);
286289
var pen = new Pen(Brushes.Red, 1.5);
287290
var indicatorX = lastTextLine.WidthIncludingTrailingWhitespace - textView.HorizontalOffset + radius + 4;
288291
var indicatorY = line.GetTextLineVisualYPosition(lastTextLine, VisualYPosition.TextMiddle) - textView.VerticalOffset + 0.5;
289292
drawingContext.DrawEllipse(null, pen, new Point(indicatorX, indicatorY), radius, radius);
290293
drawingContext.DrawLine(pen, new Point(indicatorX - radius + 3, indicatorY), new Point(indicatorX + radius - 3, indicatorY));
291294
}
295+
else if (_presenter.ShowHiddenSymbols &&
296+
(info.Type == Models.TextDiffLineType.Normal ||
297+
info.Type == Models.TextDiffLineType.Added ||
298+
info.Type == Models.TextDiffLineType.Deleted))
299+
{
300+
var indicatorX = lastTextLine.WidthIncludingTrailingWhitespace - textView.HorizontalOffset + 2;
301+
var indicatorY = line.GetTextLineVisualYPosition(lastTextLine, VisualYPosition.TextMiddle) - textView.VerticalOffset;
302+
var lineEnding = info.RawContent.Length != 0 && info.RawContent[^1] == '\r' ? "\\r\\n" : "\\n";
303+
var indicator = new FormattedText(
304+
lineEnding,
305+
CultureInfo.CurrentCulture,
306+
FlowDirection.LeftToRight,
307+
typeface,
308+
_presenter.FontSize,
309+
lineEndingBrush);
310+
drawingContext.DrawText(indicator, new Point(indicatorX, indicatorY - (indicator.Height * 0.5)));
311+
}
292312

293313
if (changeBlock == null)
294314
continue;

0 commit comments

Comments
 (0)