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

Commit 00113de

Browse files
committed
Add workaround for NRefactory parser bug that was causing the semantic highlighting to crash.
1 parent 840f3f5 commit 00113de

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ public HighlightedLine HighlightLine(int lineNumber)
226226

227227
if (cachedLine != null && cachedLine.IsValid && newVersion.CompareAge(cachedLine.OldVersion) == 0) {
228228
// the file hasn't changed since the cache was created, so just reuse the old highlighted line
229+
#if DEBUG
230+
cachedLine.HighlightedLine.ValidateInvariants();
231+
#endif
229232
return cachedLine.HighlightedLine;
230233
}
231234
}
@@ -263,6 +266,9 @@ HighlightedLine DoHighlightLine(int lineNumber, IDocumentLine documentLine, Cach
263266
// If there's a cached version, adjust it to the latest document changes and return it.
264267
// This avoids flickering when changing a line that contains semantic highlighting.
265268
cachedLine.Update(newVersion);
269+
#if DEBUG
270+
cachedLine.HighlightedLine.ValidateInvariants();
271+
#endif
266272
return cachedLine.HighlightedLine;
267273
} else {
268274
return null;
@@ -295,6 +301,14 @@ HighlightedLine DoHighlightLine(int lineNumber, IDocumentLine documentLine, Cach
295301
return line;
296302
}
297303

304+
#if DEBUG
305+
public override void VisitSyntaxTree(ICSharpCode.NRefactory.CSharp.SyntaxTree syntaxTree)
306+
{
307+
base.VisitSyntaxTree(syntaxTree);
308+
line.ValidateInvariants();
309+
}
310+
#endif
311+
298312
HighlightingColor IHighlighter.DefaultTextColor {
299313
get {
300314
return null;
@@ -336,8 +350,13 @@ protected override void Colorize(TextLocation start, TextLocation end, Highlight
336350
return;
337351
if (start.Line <= lineNumber && end.Line >= lineNumber) {
338352
int lineStartOffset = line.DocumentLine.Offset;
353+
int lineEndOffset = lineStartOffset + line.DocumentLine.Length;
339354
int startOffset = lineStartOffset + (start.Line == lineNumber ? start.Column - 1 : 0);
340355
int endOffset = lineStartOffset + (end.Line == lineNumber ? end.Column - 1 : line.DocumentLine.Length);
356+
// For some parser errors, the mcs parser produces grossly wrong locations (e.g. miscounting the number of newlines),
357+
// so we need to coerce the offsets to valid values within the line
358+
startOffset = startOffset.CoerceValue(lineStartOffset, lineEndOffset);
359+
endOffset = endOffset.CoerceValue(lineStartOffset, lineEndOffset);
341360
if (line.Sections.Count > 0) {
342361
HighlightedSection prevSection = line.Sections.Last();
343362
if (startOffset < prevSection.Offset + prevSection.Length) {

0 commit comments

Comments
 (0)