Skip to content

Commit

Permalink
Merge pull request #284 from AvaloniaUI/fix-syntax-issue
Browse files Browse the repository at this point in the history
Fixed lines not be highlighted sometimes
  • Loading branch information
danipen authored Oct 25, 2022
2 parents 8719db6 + 8c8c528 commit a083fb5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class TextMateColoringTransformer :
private TextView _textView;
private Action<Exception> _exceptionHandler;

private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;

Expand All @@ -46,6 +47,7 @@ public TextMateColoringTransformer(

public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;

Expand All @@ -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;
}
Expand Down Expand Up @@ -215,12 +218,15 @@ public void ModelTokensChanged(ModelTokensChangedEvent e)
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}

bool changedLinesAreNotVisible =
(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(() =>
{
Expand Down

0 comments on commit a083fb5

Please sign in to comment.