Browse Source

Add workaround for NRefactory parser bug that was causing the semantic highlighting to crash.

pull/297/head
Daniel Grunwald 12 years ago
parent
commit
00113de78f
  1. 19
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpSemanticHighlighter.cs

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

@ -226,6 +226,9 @@ namespace CSharpBinding
if (cachedLine != null && cachedLine.IsValid && newVersion.CompareAge(cachedLine.OldVersion) == 0) { if (cachedLine != null && cachedLine.IsValid && newVersion.CompareAge(cachedLine.OldVersion) == 0) {
// the file hasn't changed since the cache was created, so just reuse the old highlighted line // the file hasn't changed since the cache was created, so just reuse the old highlighted line
#if DEBUG
cachedLine.HighlightedLine.ValidateInvariants();
#endif
return cachedLine.HighlightedLine; return cachedLine.HighlightedLine;
} }
} }
@ -263,6 +266,9 @@ namespace CSharpBinding
// If there's a cached version, adjust it to the latest document changes and return it. // If there's a cached version, adjust it to the latest document changes and return it.
// This avoids flickering when changing a line that contains semantic highlighting. // This avoids flickering when changing a line that contains semantic highlighting.
cachedLine.Update(newVersion); cachedLine.Update(newVersion);
#if DEBUG
cachedLine.HighlightedLine.ValidateInvariants();
#endif
return cachedLine.HighlightedLine; return cachedLine.HighlightedLine;
} else { } else {
return null; return null;
@ -295,6 +301,14 @@ namespace CSharpBinding
return line; return line;
} }
#if DEBUG
public override void VisitSyntaxTree(ICSharpCode.NRefactory.CSharp.SyntaxTree syntaxTree)
{
base.VisitSyntaxTree(syntaxTree);
line.ValidateInvariants();
}
#endif
HighlightingColor IHighlighter.DefaultTextColor { HighlightingColor IHighlighter.DefaultTextColor {
get { get {
return null; return null;
@ -336,8 +350,13 @@ namespace CSharpBinding
return; return;
if (start.Line <= lineNumber && end.Line >= lineNumber) { if (start.Line <= lineNumber && end.Line >= lineNumber) {
int lineStartOffset = line.DocumentLine.Offset; int lineStartOffset = line.DocumentLine.Offset;
int lineEndOffset = lineStartOffset + line.DocumentLine.Length;
int startOffset = lineStartOffset + (start.Line == lineNumber ? start.Column - 1 : 0); int startOffset = lineStartOffset + (start.Line == lineNumber ? start.Column - 1 : 0);
int endOffset = lineStartOffset + (end.Line == lineNumber ? end.Column - 1 : line.DocumentLine.Length); int endOffset = lineStartOffset + (end.Line == lineNumber ? end.Column - 1 : line.DocumentLine.Length);
// For some parser errors, the mcs parser produces grossly wrong locations (e.g. miscounting the number of newlines),
// so we need to coerce the offsets to valid values within the line
startOffset = startOffset.CoerceValue(lineStartOffset, lineEndOffset);
endOffset = endOffset.CoerceValue(lineStartOffset, lineEndOffset);
if (line.Sections.Count > 0) { if (line.Sections.Count > 0) {
HighlightedSection prevSection = line.Sections.Last(); HighlightedSection prevSection = line.Sections.Last();
if (startOffset < prevSection.Offset + prevSection.Length) { if (startOffset < prevSection.Offset + prevSection.Length) {

Loading…
Cancel
Save