Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed lines not be highlighted sometimes #284

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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