Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 0b78a71

Browse files
committed
Fix #455: C# CaretReferenceHighlighter highlights incorrect offsets before it is being refreshed
1 parent 0e4c8b8 commit 0b78a71

1 file changed

Lines changed: 37 additions & 10 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/Src/CaretReferenceHighlightRenderer.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public CaretReferenceHighlightRenderer(ITextEditor editor)
7171
timer.Tick += (sender, e) => ResolveAtCaret();
7272

7373
editor.Caret.LocationChanged += CaretLocationChanged;
74+
editor.Document.ChangeCompleted += DocumentChanged;
7475
textView.VisualLinesChanged += VisualLinesChanged;
7576
SD.ParserService.ParseInformationUpdated += ParseInformationUpdated;
7677
SD.ParserService.LoadSolutionProjectsThread.Finished += LoadSolutionProjectsThreadFinished;
@@ -79,18 +80,17 @@ public CaretReferenceHighlightRenderer(ITextEditor editor)
7980

8081
public void Dispose()
8182
{
83+
timer.Stop();
8284
this.textView.BackgroundRenderers.Remove(this);
8385
editor.Caret.LocationChanged -= CaretLocationChanged;
86+
editor.Document.ChangeCompleted -= DocumentChanged;
87+
textView.VisualLinesChanged -= VisualLinesChanged;
88+
SD.ParserService.ParseInformationUpdated -= ParseInformationUpdated;
89+
SD.ParserService.LoadSolutionProjectsThread.Finished -= LoadSolutionProjectsThreadFinished;
8490
}
8591

8692
public void Draw(TextView textView, DrawingContext drawingContext)
8793
{
88-
var codeEditorOptions = editor.Options as ICodeEditorOptions;
89-
if ((codeEditorOptions != null) && !codeEditorOptions.HighlightSymbol) {
90-
// User has disabled highlighting of symbols
91-
return;
92-
}
93-
9494
if (currentReferences == null) {
9595
if (textView.VisualLines.Count == 0)
9696
return;
@@ -130,21 +130,48 @@ void ParseInformationUpdated(object sender, ParseInformationEventArgs e)
130130
return;
131131
currentReferences = null;
132132
textView.InvalidateLayer(KnownLayer.Selection);
133-
134133
}
135134

136135
void LoadSolutionProjectsThreadFinished(object sender, EventArgs e)
137136
{
138-
currentReferences = null;
139-
textView.InvalidateLayer(KnownLayer.Selection);
137+
StartTimer();
140138
}
141139

142140
void CaretLocationChanged(object sender, EventArgs e)
141+
{
142+
if (currentReferences != null) {
143+
int caretOffset = editor.Caret.Offset;
144+
if (!currentReferences.Any(r => r.Offset <= caretOffset && caretOffset <= r.EndOffset)) {
145+
// If the caret moved outside any highlighted identifier, immediately clear the highlight
146+
// as the caret is not on the same symbol as before
147+
SetCurrentSymbol(null);
148+
}
149+
}
150+
StartTimer();
151+
}
152+
153+
void DocumentChanged(object sender, EventArgs e)
154+
{
155+
// If the document has changed, the current symbol likely also has changed (most edits are at the caret position),
156+
// so immediately clear the highlighting.
157+
SetCurrentSymbol(null);
158+
StartTimer();
159+
}
160+
161+
void StartTimer()
143162
{
144163
if (caretMovementTokenSource != null)
145164
caretMovementTokenSource.Cancel();
146165
timer.Stop();
147-
timer.Start();
166+
167+
var codeEditorOptions = editor.Options as ICodeEditorOptions;
168+
if (codeEditorOptions == null || codeEditorOptions.HighlightSymbol) {
169+
// If symbol highlighting is enabled
170+
timer.Start();
171+
} else {
172+
// Clear highlighting if its disabled
173+
SetCurrentSymbol(null);
174+
}
148175
}
149176

150177
async void ResolveAtCaret()

0 commit comments

Comments
 (0)