Browse Source

Improve XML highlighting.

Fixed bug in XSHD loader when XSHD <Import> refers to a ruleset defined later in the same file.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5398 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Daniel Grunwald 16 years ago
parent
commit
6f3e1f87de
  1. 1
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/Resources.cs
  2. 48
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/XML-Mode.xshd
  3. 50
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Xshd/XmlHighlightingDefinition.cs

1
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/Resources.cs

@ -31,6 +31,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -31,6 +31,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
hlm.RegisterHighlighting("HTML", new[] { ".htm", ".html" }, "HTML-Mode.xshd");
hlm.RegisterHighlighting("ASP/XHTML", new[] { ".asp", ".aspx", ".asax", ".asmx" }, "ASPX.xshd");
//hlm.RegisterHighlighting("Batch", new[] { ".bat", ".cmd" }, "BAT-Mode.xshd");
hlm.RegisterHighlighting("Boo", new[] { ".boo" }, "Boo.xshd");
hlm.RegisterHighlighting("Coco", new[] { ".atg" }, "Coco-Mode.xshd");
hlm.RegisterHighlighting("C++", new[] { ".c", ".h", ".cc", ".cpp" , ".hpp" }, "CPP-Mode.xshd");

48
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/Resources/XML-Mode.xshd

@ -1,41 +1,61 @@ @@ -1,41 +1,61 @@
<SyntaxDefinition name="XML" extensions=".xml;.xsl;.xslt;.xsd;.manifest;.config;.addin;.xshd;.wxs;.wxi;.wxl;.proj;.csproj;.vbproj;.ilproj;.booproj;.build;.xfrm;.targets;.xaml;.xpt;.xft;.map;.wsdl;.disco" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color foreground="Green" name="Comment" />
<Color foreground="Blue" name="CData" />
<Color foreground="Blue" name="DocType" />
<Color foreground="Blue" name="XmlDecl" />
<Color foreground="DarkMagenta" name="XmlTag" />
<Color foreground="Red" name="AttributeName" />
<Color foreground="Blue" name="AttributeValue" />
<Color foreground="DarkRed" name="Entity" />
<Color foreground="Olive" name="BrokenEntity" />
<RuleSet>
<Span foreground="Green" multiline="true">
<Span color="Comment" multiline="true">
<Begin>&lt;!--</Begin>
<End>--&gt;</End>
</Span>
<Span foreground="Blue" multiline="true">
<Span color="CData" multiline="true">
<Begin>&lt;!\[CDATA\[</Begin>
<End>]]&gt;</End>
</Span>
<Span foreground="Blue" multiline="true">
<Span color="DocType" multiline="true">
<Begin>&lt;!DOCTYPE</Begin>
<End>&gt;</End>
</Span>
<Span foreground="Blue" multiline="true">
<Span color="XmlDecl" multiline="true">
<Begin>&lt;\?</Begin>
<End>\?&gt;</End>
</Span>
<Span foreground="DarkMagenta" multiline="true">
<Span color="XmlTag" multiline="true">
<Begin>&lt;</Begin>
<End>&gt;</End>
<RuleSet>
<Span foreground="Blue" multiline="true">
<Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
<Begin>"</Begin>
<End>"</End>
</Span>
<Span foreground="Blue" multiline="true">
<Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
<Begin>'</Begin>
<End>'</End>
</Span>
<Rule foreground="Red">[\d\w_\-\.]+(?=(\s*=))</Rule>
<Rule foreground="Blue">=</Rule>
<Rule foreground="DarkMagenta">/</Rule>
<Rule color="AttributeName">[\d\w_\-\.]+(?=(\s*=))</Rule>
<Rule color="AttributeValue">=</Rule>
</RuleSet>
</Span>
<Span foreground="Blue">
<Begin>&amp;</Begin>
<End>;</End>
</Span>
<Import ruleSet="EntitySet"/>
</RuleSet>
<RuleSet name="EntitySet">
<Rule color="Entity">
&amp;
[\w\d\#]+
;
</Rule>
<Rule color="BrokenEntity">
&amp;
[\w\d\#]*
#missing ;
</Rule>
</RuleSet>
</SyntaxDefinition>

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

@ -24,8 +24,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -24,8 +24,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
public XmlHighlightingDefinition(XshdSyntaxDefinition xshd, IHighlightingDefinitionReferenceResolver resolver)
{
this.Name = xshd.Name;
xshd.AcceptElements(new RegisterNamedElementsVisitor(this));
TranslateElementVisitor translateVisitor = new TranslateElementVisitor(this, resolver);
var rnev = new RegisterNamedElementsVisitor(this);
xshd.AcceptElements(rnev);
TranslateElementVisitor translateVisitor = new TranslateElementVisitor(this, rnev.ruleSets, resolver);
foreach (XshdElement element in xshd.Elements) {
HighlightingRuleSet rs = element.AcceptVisitor(translateVisitor) as HighlightingRuleSet;
XshdRuleSet xrs = element as XshdRuleSet;
@ -44,6 +45,8 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -44,6 +45,8 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
sealed class RegisterNamedElementsVisitor : IXshdVisitor
{
XmlHighlightingDefinition def;
internal readonly Dictionary<XshdRuleSet, HighlightingRuleSet> ruleSets
= new Dictionary<XshdRuleSet, HighlightingRuleSet>();
public RegisterNamedElementsVisitor(XmlHighlightingDefinition def)
{
@ -52,13 +55,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -52,13 +55,15 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
public object VisitRuleSet(XshdRuleSet ruleSet)
{
HighlightingRuleSet hrs = new HighlightingRuleSet();
ruleSets.Add(ruleSet, hrs);
if (ruleSet.Name != null) {
if (ruleSet.Name.Length == 0)
throw Error(ruleSet, "Name must not be the empty string");
if (def.ruleSetDict.ContainsKey(ruleSet.Name))
throw Error(ruleSet, "Duplicate rule set name '" + ruleSet.Name + "'.");
def.ruleSetDict.Add(ruleSet.Name, new HighlightingRuleSet());
def.ruleSetDict.Add(ruleSet.Name, hrs);
}
ruleSet.AcceptElements(this);
return null;
@ -105,25 +110,34 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -105,25 +110,34 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
#region TranslateElements
sealed class TranslateElementVisitor : IXshdVisitor
{
XmlHighlightingDefinition def;
IHighlightingDefinitionReferenceResolver resolver;
readonly XmlHighlightingDefinition def;
readonly Dictionary<XshdRuleSet, HighlightingRuleSet> ruleSetDict;
readonly Dictionary<HighlightingRuleSet, XshdRuleSet> reverseRuleSetDict;
readonly IHighlightingDefinitionReferenceResolver resolver;
HashSet<XshdRuleSet> processingStartedRuleSets = new HashSet<XshdRuleSet>();
HashSet<XshdRuleSet> processedRuleSets = new HashSet<XshdRuleSet>();
bool ignoreCase;
public TranslateElementVisitor(XmlHighlightingDefinition def, IHighlightingDefinitionReferenceResolver resolver)
public TranslateElementVisitor(XmlHighlightingDefinition def, Dictionary<XshdRuleSet, HighlightingRuleSet> ruleSetDict, IHighlightingDefinitionReferenceResolver resolver)
{
Debug.Assert(def != null);
Debug.Assert(ruleSetDict != null);
this.def = def;
this.ruleSetDict = ruleSetDict;
this.resolver = resolver;
reverseRuleSetDict = new Dictionary<HighlightingRuleSet, XshdRuleSet>();
foreach (var pair in ruleSetDict) {
reverseRuleSetDict.Add(pair.Value, pair.Key);
}
}
bool ignoreCase;
public object VisitRuleSet(XshdRuleSet ruleSet)
{
HighlightingRuleSet rs;
if (ruleSet.Name != null)
rs = def.ruleSetDict[ruleSet.Name];
else
rs = new HighlightingRuleSet();
HighlightingRuleSet rs = ruleSetDict[ruleSet];
if (processedRuleSets.Contains(ruleSet))
return rs;
if (!processingStartedRuleSets.Add(ruleSet))
throw Error(ruleSet, "RuleSet cannot be processed because it contains cyclic <Import>");
bool oldIgnoreCase = ignoreCase;
if (ruleSet.IgnoreCase != null)
@ -150,6 +164,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -150,6 +164,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
}
ignoreCase = oldIgnoreCase;
processedRuleSets.Add(ruleSet);
return rs;
}
@ -301,7 +316,14 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd @@ -301,7 +316,14 @@ namespace ICSharpCode.AvalonEdit.Highlighting.Xshd
public object VisitImport(XshdImport import)
{
return GetRuleSet(import, import.RuleSetReference);
HighlightingRuleSet hrs = GetRuleSet(import, import.RuleSetReference);
XshdRuleSet inputRuleSet;
if (reverseRuleSetDict.TryGetValue(hrs, out inputRuleSet)) {
// ensure the ruleset is processed before importing its members
if (VisitRuleSet(inputRuleSet) != hrs)
Debug.Fail("this shouldn't happen");
}
return hrs;
}
public object VisitRule(XshdRule rule)

Loading…
Cancel
Save