Browse Source

C# syntax highlighting: Don't colorize punctuation.

AvalonEdit: don't create separate TextRuns when syntax highlighting applies a color-less span.
4.1
Daniel Grunwald 14 years ago
parent
commit
56f38f8524
  1. 6
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedInlineBuilder.cs
  2. 14
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColorizer.cs
  3. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/CSharp-Mode.xshd

6
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedInlineBuilder.cs

@ -84,6 +84,12 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -84,6 +84,12 @@ namespace ICSharpCode.AvalonEdit.Highlighting
{
if (color == null)
throw new ArgumentNullException("color");
if (color.Foreground == null && color.FontStyle == null && color.FontWeight == null) {
// Optimization: don't split the HighlightingState when we're not changing
// any property. For example, the "Punctuation" color in C# is
// empty by default.
return;
}
int startIndex = GetIndexForOffset(offset);
int endIndex = GetIndexForOffset(offset + length);
for (int i = startIndex; i < endIndex; i++) {

14
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColorizer.cs

@ -137,6 +137,8 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -137,6 +137,8 @@ namespace ICSharpCode.AvalonEdit.Highlighting
HighlightedLine hl = highlighter.HighlightLine(lineNumberBeingColorized);
lineNumberBeingColorized = 0;
foreach (HighlightedSection section in hl.Sections) {
if (IsEmptyColor(section.Color))
continue;
ChangeLinePart(section.Offset, section.Offset + section.Length,
visualLineElement => ApplyColorToElement(visualLineElement, section.Color));
}
@ -144,6 +146,18 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -144,6 +146,18 @@ namespace ICSharpCode.AvalonEdit.Highlighting
this.lastColorizedLine = line;
}
/// <summary>
/// Gets whether the color is empty (has no effect on a VisualLineTextElement).
/// For example, the C# "Punctuation" is an empty color.
/// </summary>
bool IsEmptyColor(HighlightingColor color)
{
if (color == null)
return true;
return color.Background == null && color.Foreground == null
&& color.FontStyle == null && color.FontWeight == null;
}
/// <summary>
/// Applies a highlighting color to a visual line element.
/// </summary>

2
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/CSharp-Mode.xshd

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
<Color name="String" foreground="Blue" exampleText="string text = &quot;Hello, World!&quot;"/>
<Color name="Char" foreground="Magenta" exampleText="char linefeed = '\n';"/>
<Color name="Preprocessor" foreground="Green" exampleText="#region Title" />
<Color name="Punctuation" foreground="DarkGreen" exampleText="a(b.c);" />
<Color name="Punctuation" exampleText="a(b.c);" />
<Color name="ValueTypes" fontWeight="bold" foreground="Red" exampleText="bool b = true;" />
<Color name="ReferenceTypes" foreground="Red" exampleText="object o;" />
<Color name="MethodCall" foreground="MidnightBlue" fontWeight="bold" exampleText="o.ToString();"/>

Loading…
Cancel
Save