From 228bb5e4f05dbf2dc7251b104ece41e7163d35ae Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 26 Sep 2023 13:20:54 +0200 Subject: [PATCH] Pass a dictionary with the color map instead an interface --- .../TextMateColoringTransformer.cs | 15 ++-------- .../TextTransformation.cs | 29 +++++++++++-------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs b/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs index 37e9920..b1df490 100644 --- a/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs +++ b/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs @@ -17,8 +17,7 @@ namespace AvaloniaEdit.TextMate { public class TextMateColoringTransformer : GenericLineTransformer, - IModelTokensChangedListener, - ForegroundTextTransformation.IColorMap + IModelTokensChangedListener { private Theme _theme; private IGrammar _grammar; @@ -77,6 +76,7 @@ namespace AvaloniaEdit.TextMate public void Dispose() { _textView.VisualLinesChanged -= TextView_VisualLinesChanged; + _brushes.Clear(); } public void SetTheme(Theme theme) @@ -105,15 +105,6 @@ namespace AvaloniaEdit.TextMate } } - IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId) - { - if (_brushes == null) - return null; - - _brushes.TryGetValue(colorId, out IBrush result); - return result; - } - protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context) { try @@ -190,7 +181,7 @@ namespace AvaloniaEdit.TextMate if (transformations[i] == null) transformations[i] = new ForegroundTextTransformation(); - transformations[i].ColorMap = this; + transformations[i].ColorMap = _brushes; transformations[i].ExceptionHandler = _exceptionHandler; transformations[i].StartOffset = lineOffset + startIndex; transformations[i].EndOffset = lineOffset + endIndex; diff --git a/src/AvaloniaEdit.TextMate/TextTransformation.cs b/src/AvaloniaEdit.TextMate/TextTransformation.cs index d3a9b8f..268ddfa 100644 --- a/src/AvaloniaEdit.TextMate/TextTransformation.cs +++ b/src/AvaloniaEdit.TextMate/TextTransformation.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using AvaloniaEdit.Document; @@ -13,12 +14,7 @@ namespace AvaloniaEdit.TextMate public class ForegroundTextTransformation : TextTransformation { - public interface IColorMap - { - AM.IBrush GetBrush(int color); - } - - public IColorMap ColorMap { get; set; } + public Dictionary ColorMap { get; set; } public Action ExceptionHandler { get; set; } public int ForegroundColor { get; set; } public int BackgroundColor { get; set; } @@ -47,13 +43,13 @@ namespace AvaloniaEdit.TextMate } transformer.SetTextStyle(line, formattedOffset, endOffset - line.Offset - formattedOffset, - ColorMap.GetBrush(ForegroundColor), - ColorMap.GetBrush(BackgroundColor), - GetFontStyle(), - GetFontWeight(), - IsUnderline()); + GetBrush(ForegroundColor), + GetBrush(BackgroundColor), + GetFontStyle(), + GetFontWeight(), + IsUnderline()); } - catch(Exception ex) + catch (Exception ex) { ExceptionHandler?.Invoke(ex); } @@ -85,5 +81,14 @@ namespace AvaloniaEdit.TextMate return false; } + + AM.IBrush GetBrush(int colorId) + { + if (ColorMap == null) + return null; + + ColorMap.TryGetValue(colorId, out AM.IBrush result); + return result; + } } } \ No newline at end of file