Browse Source

allow colors from referenced rulesets to be customized

pull/27/merge
Siegfried Pammer 13 years ago
parent
commit
edd9edc3ba
  1. 8
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/ExpressionHighlightRenderer.cs
  2. 78
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Options/HighlightingOptions.xaml.cs
  3. 15
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Options/NamedColorHighlightingItem.cs
  4. 11
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingManager.cs
  5. 11
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/IHighlightingDefinition.cs
  6. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/CSharp-Mode.xshd
  7. 8
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/ModeV2.xsd
  8. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/VBNET-Mode.xshd
  9. 8
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/XmlDoc.xshd
  10. 12
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/V2Loader.cs
  11. 15
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs
  12. 38
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/XshdProperty.cs
  13. 1
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj

8
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/ExpressionHighlightRenderer.cs

@ -22,8 +22,8 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -22,8 +22,8 @@ namespace ICSharpCode.AvalonEdit.AddIn
Pen borderPen;
Brush backgroundBrush;
TextView textView;
readonly Color borderColor = Color.FromArgb(52, 30, 130, 255); //Color.FromArgb(180, 70, 230, 70))
readonly Color fillColor = Color.FromArgb(22, 30, 130, 255); //Color.FromArgb(40, 60, 255, 60)
public readonly Color DefaultBorderColor = Color.FromArgb(52, 30, 130, 255); //Color.FromArgb(180, 70, 230, 70))
public readonly Color DefaultFillColor = Color.FromArgb(22, 30, 130, 255); //Color.FromArgb(40, 60, 255, 60)
readonly int borderThickness = 1;
readonly int cornerRadius = 1;
@ -45,8 +45,8 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -45,8 +45,8 @@ namespace ICSharpCode.AvalonEdit.AddIn
if (textView == null)
throw new ArgumentNullException("textView");
this.textView = textView;
this.borderPen = new Pen(new SolidColorBrush(borderColor), borderThickness);
this.backgroundBrush = new SolidColorBrush(fillColor);
this.borderPen = new Pen(new SolidColorBrush(DefaultBorderColor), borderThickness);
this.backgroundBrush = new SolidColorBrush(DefaultFillColor);
this.borderPen.Freeze();
this.backgroundBrush.Freeze();
this.textView.BackgroundRenderers.Add(this);

78
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Options/HighlightingOptions.xaml.cs

@ -171,7 +171,6 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -171,7 +171,6 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
from name in typeof(HighlightingManager).Assembly.GetManifestResourceNames().AsParallel()
where name.StartsWith(typeof(HighlightingManager).Namespace + ".Resources.", StringComparison.OrdinalIgnoreCase)
&& name.EndsWith(".xshd", StringComparison.OrdinalIgnoreCase)
&& !name.EndsWith("XmlDoc.xshd", StringComparison.OrdinalIgnoreCase)
select LoadBuiltinXshd(name)
).Concat(
ICSharpCode.Core.AddInTree.BuildItems<AddInTreeSyntaxMode>(SyntaxModeDoozer.Path, null, false).AsParallel()
@ -187,7 +186,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -187,7 +186,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
languageComboBox.Items.Clear();
languageComboBox.Items.Add(new XshdSyntaxDefinition { Name = "All languages" });
foreach (XshdSyntaxDefinition def in allSyntaxDefinitions)
foreach (XshdSyntaxDefinition def in allSyntaxDefinitions.Where(d => !d.Name.Equals("XmlDoc", StringComparison.OrdinalIgnoreCase)))
languageComboBox.Items.Add(def);
if (allSyntaxDefinitions.Count > 0)
languageComboBox.SelectedIndex = 0;
@ -210,7 +209,9 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -210,7 +209,9 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
if (def == null) {
throw new InvalidOperationException("Expected that all XSHDs are registered in default highlighting manager; but highlighting definition was not found");
} else {
foreach (XshdColor namedColor in xshd.Elements.OfType<XshdColor>()) {
var visitor = new ColorVisitor(allSyntaxDefinitions);
xshd.AcceptElements(visitor);
foreach (XshdColor namedColor in visitor.foundColors) {
if (namedColor.ExampleText != null) {
IHighlightingItem item = new NamedColorHighlightingItem(defaultText, namedColor) { ParentDefinition = def };
item = new CustomizedHighlightingItem(customizationList, item, xshd.Name);
@ -225,6 +226,60 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -225,6 +226,60 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
}
}
class ColorVisitor : IXshdVisitor
{
internal readonly List<XshdColor> foundColors = new List<XshdColor>();
readonly HashSet<XshdSyntaxDefinition> visitedDefinitons = new HashSet<XshdSyntaxDefinition>();
IList<XshdSyntaxDefinition> allSyntaxDefinitions;
public ColorVisitor(IList<XshdSyntaxDefinition> allSyntaxDefinitions)
{
this.allSyntaxDefinitions = allSyntaxDefinitions;
}
public object VisitRuleSet(XshdRuleSet ruleSet)
{
ruleSet.AcceptElements(this);
return null;
}
public object VisitColor(XshdColor color)
{
foundColors.Add(color);
return null;
}
public object VisitKeywords(XshdKeywords keywords)
{
return keywords.ColorReference.AcceptVisitor(this);
}
public object VisitSpan(XshdSpan span)
{
if (span.RuleSetReference.InlineElement != null)
return span.RuleSetReference.AcceptVisitor(this);
XshdSyntaxDefinition definition = allSyntaxDefinitions.SingleOrDefault(def => def.Name == span.RuleSetReference.ReferencedDefinition);
if (definition != null && visitedDefinitons.Add(definition))
foundColors.AddRange(definition.Elements.OfType<XshdColor>());
return null;
}
public object VisitImport(XshdImport import)
{
if (import.RuleSetReference.InlineElement != null)
return import.RuleSetReference.AcceptVisitor(this);
XshdSyntaxDefinition definition = allSyntaxDefinitions.SingleOrDefault(def => def.Name == import.RuleSetReference.ReferencedDefinition);
if (definition != null && visitedDefinitons.Add(definition))
foundColors.AddRange(definition.Elements.OfType<XshdColor>());
return null;
}
public object VisitRule(XshdRule rule)
{
return rule.ColorReference.AcceptVisitor(this);
}
}
void CreateDefaultEntries(string language, out IHighlightingItem defaultText, IList<IHighlightingItem> items)
{
// Create entry for "default text/background"
@ -597,16 +652,15 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -597,16 +652,15 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
static readonly MultiDictionary<string, string> mapping = new MultiDictionary<string, string>(StringComparer.Ordinal) {
{ "Brace Matching (Rectangle)", BracketHighlightRenderer.BracketHighlight },
{ "Collapsible Text", FoldingTextMarkers },
{ "Comment", "XML.Comment" },
{ "Comment", "VBNET.Comment" },
{ "Comment", "C#.Comment" },
{ "Compiler Error", ErrorPainter.ErrorColorName },
{ "CSS Comment", "CSS.Comment" },
{ "CSS Keyword", "" },
{ "CSS Property Name", "" },
{ "CSS Property Value", "" },
{ "CSS Selector", "" },
{ "CSS String Value", "" },
{ "CSS Property Name", "CSS.Property" },
{ "CSS Property Value", "CSS.Value" },
{ "CSS Selector", "CSS.Selector" },
{ "CSS String Value", "CSS.String" },
{ "Excluded Code", "" },
{ "HTML Attribute Value", "" },
{ "HTML Attribute", "" },
@ -687,10 +741,10 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -687,10 +741,10 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
{ "XAML Name", "" },
{ "XAML Text", "" },
{ "XML Attribute Quotes", "" },
{ "XML Attribute Value", "XML." },
{ "XML Attribute", "" },
{ "XML CData Section", "" },
{ "XML Comment", "" },
{ "XML Attribute Value", "XML.AttributeValue" },
{ "XML Attribute", "XML.AttributeName" },
{ "XML CData Section", "XML.CData" },
{ "XML Comment", "XML.Comment" },
{ "XML Delimiter", "" },
{ "XML Doc Comment", "" },
{ "XML Doc Tag", "" },

15
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Options/NamedColorHighlightingItem.cs

@ -2,13 +2,15 @@ @@ -2,13 +2,15 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using ICSharpCode.Core;
namespace ICSharpCode.AvalonEdit.AddIn.Options
{
@ -127,7 +129,16 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -127,7 +129,16 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
public void ShowExample(TextArea exampleTextArea)
{
exampleTextArea.Document.Text = color.ExampleText;
exampleTextArea.Document.Text = StringParser.Parse(color.ExampleText, GetXshdProperties().ToArray());
}
IEnumerable<StringTagPair> GetXshdProperties()
{
IHighlightingDefinition2 def = ParentDefinition as IHighlightingDefinition2;
if (def == null)
yield break;
foreach (var p in def.Properties)
yield return new StringTagPair(p.Key, p.Value);
}
event System.ComponentModel.PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged { add {} remove {} }

11
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingManager.cs

@ -20,7 +20,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -20,7 +20,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// </remarks>
public class HighlightingManager : IHighlightingDefinitionReferenceResolver
{
sealed class DelayLoadedHighlightingDefinition : IHighlightingDefinition
sealed class DelayLoadedHighlightingDefinition : IHighlightingDefinition2
{
readonly object lockObj = new object();
readonly string name;
@ -104,6 +104,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -104,6 +104,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting
{
return this.Name;
}
public IDictionary<string, string> Properties {
get {
var def = GetDefinition() as IHighlightingDefinition2;
if (def != null)
return def.Properties;
return null;
}
}
}
readonly object lockObj = new object();

11
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/IHighlightingDefinition.cs

@ -40,4 +40,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -40,4 +40,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// </summary>
IEnumerable<HighlightingColor> NamedHighlightingColors { get; }
}
/// <summary>
/// Extension of IHighlightingDefinition to avoid breaking changes in the API.
/// </summary>
public interface IHighlightingDefinition2 : IHighlightingDefinition
{
/// <summary>
/// Gets the list of properties.
/// </summary>
IDictionary<string, string> Properties { get; }
}
}

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

@ -27,6 +27,8 @@ @@ -27,6 +27,8 @@
<Color name="TrueFalse" fontWeight="bold" foreground="DarkCyan" exampleText="b = false; a = true;" />
<Color name="TypeKeywords" fontWeight="bold" foreground="DarkCyan" exampleText="if (x is int) { a = x as int; type = typeof(int); size = sizeof(int); c = new object(); }"/>
<Property name="DocCommentMarker" value="///" />
<RuleSet name="CommentMarkerSet">
<Keywords fontWeight="bold" foreground="Red">
<Word>TODO</Word>

8
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/ModeV2.xsd

@ -50,6 +50,13 @@ @@ -50,6 +50,13 @@
</xsd:complexType>
</xsd:element>
<xsd:element name="Property">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="value" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
<!-- Regular expression -->
<xsd:simpleType name="regex">
<xsd:restriction base="xsd:string"/>
@ -135,6 +142,7 @@ @@ -135,6 +142,7 @@
<xsd:element name="SyntaxDefinition">
<xsd:complexType>
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element ref="Property"/>
<xsd:element ref="Color"/>
<xsd:element ref="RuleSet"/>
</xsd:choice>

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

@ -18,6 +18,8 @@ @@ -18,6 +18,8 @@
<Color name="FunctionKeywords" foreground="Blue" exampleText="CInt(a)" />
<Color name="ContextKeywords" foreground="Blue" exampleText="Declare Unicode Sub SomeMethod" />
<Property name="DocCommentMarker" value="'''" />
<RuleSet ignoreCase="true">
<Span color="String">
<Begin>"</Begin>

8
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/XmlDoc.xshd

@ -1,9 +1,9 @@ @@ -1,9 +1,9 @@
<?xml version="1.0"?>
<SyntaxDefinition name="XmlDoc" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="XmlString" foreground="Silver" fontWeight="bold" />
<Color name="DocComment" foreground="Gray" />
<Color name="XmlPunctuation" fontWeight="bold" />
<Color name="KnownDocTags" fontWeight="bold" />
<Color name="XmlString" foreground="Silver" fontWeight="bold" exampleText="${DocCommentMarker} &lt;exception cref=&quot;System.Exception&quot; /&gt;" />
<Color name="DocComment" foreground="Gray" exampleText="${DocCommentMarker} &lt;exception cref=&quot;System.Exception&quot; /&gt;" />
<Color name="XmlPunctuation" fontWeight="bold" exampleText="${DocCommentMarker} &lt;exception cref=&quot;System.Exception&quot; /&gt;" />
<Color name="KnownDocTags" fontWeight="bold" exampleText="${DocCommentMarker} &lt;exception cref=&quot;System.Exception&quot; /&gt;" />
<RuleSet name="DocCommentSet">
<Span color="DocComment">

12
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/V2Loader.cs

@ -64,6 +64,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -64,6 +64,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
case "RuleSet":
c.Add(ParseRuleSet(reader));
break;
case "Property":
c.Add(ParseProperty(reader));
break;
case "Color":
c.Add(ParseNamedColor(reader));
break;
@ -85,6 +88,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -85,6 +88,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
}
}
static XshdElement ParseProperty(XmlReader reader)
{
XshdProperty property = new XshdProperty();
SetPosition(property, reader);
property.Name = reader.GetAttribute("name");
property.Value = reader.GetAttribute("value");
return property;
}
static XshdRuleSet ParseRuleSet(XmlReader reader)
{
XshdRuleSet ruleSet = new XshdRuleSet();

15
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs

@ -5,15 +5,15 @@ using System; @@ -5,15 +5,15 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using ICSharpCode.AvalonEdit.Utils;
namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
{
[Serializable]
sealed class XmlHighlightingDefinition : IHighlightingDefinition
sealed class XmlHighlightingDefinition : IHighlightingDefinition2
{
public string Name { get; private set; }
@ -37,6 +37,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -37,6 +37,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
throw new HighlightingDefinitionInvalidException("Could not find main RuleSet.");
// Translate elements within the rulesets (resolving references and processing imports)
xshd.AcceptElements(new TranslateElementVisitor(this, rnev.ruleSets, resolver));
foreach (var p in xshd.Elements.OfType<XshdProperty>())
propDict.Add(p.Name, p.Value);
}
#region RegisterNamedElements
@ -358,6 +361,8 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -358,6 +361,8 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
Dictionary<string, HighlightingRuleSet> ruleSetDict = new Dictionary<string, HighlightingRuleSet>();
Dictionary<string, HighlightingColor> colorDict = new Dictionary<string, HighlightingColor>();
[OptionalField]
Dictionary<string, string> propDict = new Dictionary<string, string>();
public HighlightingRuleSet MainRuleSet { get; private set; }
@ -391,5 +396,11 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -391,5 +396,11 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
{
return this.Name;
}
public IDictionary<string, string> Properties {
get {
return propDict;
}
}
}
}

38
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/XshdProperty.cs

@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
{
/// <summary>
/// A property in an Xshd file.
/// </summary>
[Serializable]
public class XshdProperty : XshdElement
{
/// <summary>
/// Gets/sets the name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets/sets the value.
/// </summary>
public string Value { get; set; }
/// <summary>
/// Creates a new XshdColor instance.
/// </summary>
public XshdProperty()
{
}
/// <inheritdoc/>
public override object AcceptVisitor(IXshdVisitor visitor)
{
return null;
// return visitor.VisitProperty(this);
}
}
}

1
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/ICSharpCode.AvalonEdit.csproj

@ -212,6 +212,7 @@ @@ -212,6 +212,7 @@
<Compile Include="Highlighting\Xshd\XmlHighlightingDefinition.cs" />
<Compile Include="Highlighting\Xshd\XshdColor.cs" />
<Compile Include="Highlighting\Xshd\XshdImport.cs" />
<Compile Include="Highlighting\Xshd\XshdProperty.cs" />
<Compile Include="Highlighting\Xshd\XshdReference.cs" />
<Compile Include="Highlighting\Xshd\XshdElement.cs" />
<Compile Include="Highlighting\Xshd\XshdKeywords.cs" />

Loading…
Cancel
Save