From d6b770d206209fc66fa5afb279deba284dabe193 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 25 Oct 2022 13:19:48 +0200 Subject: [PATCH 1/2] Fixed lines not be highlighted sometimes --- src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs b/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs index 0c720df4..a4fb0d11 100644 --- a/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs +++ b/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs @@ -27,6 +27,7 @@ public class TextMateColoringTransformer : private TextView _textView; private Action _exceptionHandler; + private volatile bool _areVisualLinesValid = false; private volatile int _firstVisibleLineIndex = -1; private volatile int _lastVisibleLineIndex = -1; @@ -46,6 +47,7 @@ public TextMateColoringTransformer( public void SetModel(TextDocument document, TMModel model) { + _areVisualLinesValid = false; _document = document; _model = model; @@ -62,6 +64,7 @@ private void TextView_VisualLinesChanged(object sender, EventArgs e) if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0) return; + _areVisualLinesValid = true; _firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1; _lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1; } @@ -216,8 +219,9 @@ public void ModelTokensChanged(ModelTokensChangedEvent e) } bool changedLinesAreNotVisible = - (firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) || - (firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex); + _areVisualLinesValid && + ((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) || + (firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex)); if (changedLinesAreNotVisible) return; From 71d83c4cb3cd98db80a3dd5e00a42d3965af8cc6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 25 Oct 2022 16:21:43 +0200 Subject: [PATCH 2/2] Make code more readable --- .../TextMateColoringTransformer.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs b/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs index a4fb0d11..37e9920f 100644 --- a/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs +++ b/src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs @@ -218,13 +218,15 @@ public void ModelTokensChanged(ModelTokensChangedEvent e) lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex); } - bool changedLinesAreNotVisible = - _areVisualLinesValid && - ((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) || - (firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex)); + if (_areVisualLinesValid) + { + bool changedLinesAreNotVisible = + ((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) || + (firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex)); - if (changedLinesAreNotVisible) - return; + if (changedLinesAreNotVisible) + return; + } Dispatcher.UIThread.Post(() => {