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
hlm.RegisterHighlighting("HTML", new[] { ".htm", ".html" }, "HTML-Mode.xshd"); hlm.RegisterHighlighting("HTML", new[] { ".htm", ".html" }, "HTML-Mode.xshd");
hlm.RegisterHighlighting("ASP/XHTML", new[] { ".asp", ".aspx", ".asax", ".asmx" }, "ASPX.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("Boo", new[] { ".boo" }, "Boo.xshd");
hlm.RegisterHighlighting("Coco", new[] { ".atg" }, "Coco-Mode.xshd"); hlm.RegisterHighlighting("Coco", new[] { ".atg" }, "Coco-Mode.xshd");
hlm.RegisterHighlighting("C++", new[] { ".c", ".h", ".cc", ".cpp" , ".hpp" }, "CPP-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 @@
<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"> <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> <RuleSet>
<Span foreground="Green" multiline="true"> <Span color="Comment" multiline="true">
<Begin>&lt;!--</Begin> <Begin>&lt;!--</Begin>
<End>--&gt;</End> <End>--&gt;</End>
</Span> </Span>
<Span foreground="Blue" multiline="true"> <Span color="CData" multiline="true">
<Begin>&lt;!\[CDATA\[</Begin> <Begin>&lt;!\[CDATA\[</Begin>
<End>]]&gt;</End> <End>]]&gt;</End>
</Span> </Span>
<Span foreground="Blue" multiline="true"> <Span color="DocType" multiline="true">
<Begin>&lt;!DOCTYPE</Begin> <Begin>&lt;!DOCTYPE</Begin>
<End>&gt;</End> <End>&gt;</End>
</Span> </Span>
<Span foreground="Blue" multiline="true"> <Span color="XmlDecl" multiline="true">
<Begin>&lt;\?</Begin> <Begin>&lt;\?</Begin>
<End>\?&gt;</End> <End>\?&gt;</End>
</Span> </Span>
<Span foreground="DarkMagenta" multiline="true"> <Span color="XmlTag" multiline="true">
<Begin>&lt;</Begin> <Begin>&lt;</Begin>
<End>&gt;</End> <End>&gt;</End>
<RuleSet> <RuleSet>
<Span foreground="Blue" multiline="true"> <Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
<Begin>"</Begin> <Begin>"</Begin>
<End>"</End> <End>"</End>
</Span> </Span>
<Span foreground="Blue" multiline="true"> <Span color="AttributeValue" multiline="true" ruleSet="EntitySet">
<Begin>'</Begin> <Begin>'</Begin>
<End>'</End> <End>'</End>
</Span> </Span>
<Rule foreground="Red">[\d\w_\-\.]+(?=(\s*=))</Rule> <Rule color="AttributeName">[\d\w_\-\.]+(?=(\s*=))</Rule>
<Rule foreground="Blue">=</Rule> <Rule color="AttributeValue">=</Rule>
<Rule foreground="DarkMagenta">/</Rule>
</RuleSet> </RuleSet>
</Span> </Span>
<Span foreground="Blue"> <Import ruleSet="EntitySet"/>
<Begin>&amp;</Begin> </RuleSet>
<End>;</End>
</Span> <RuleSet name="EntitySet">
<Rule color="Entity">
&amp;
[\w\d\#]+
;
</Rule>
<Rule color="BrokenEntity">
&amp;
[\w\d\#]*
#missing ;
</Rule>
</RuleSet> </RuleSet>
</SyntaxDefinition> </SyntaxDefinition>

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

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

Loading…
Cancel
Save