Browse Source

Add semantic highlighting for properties and events

Also add a fallback mechanism for colors: if a color definition is empty, another one can be used instead.
pull/2906/head
Lucas Trzesniewski 2 years ago
parent
commit
39cb275456
  1. 2
      ICSharpCode.Decompiler/Output/TextTokenWriter.cs
  2. 93
      ILSpy/Languages/CSharpHighlightingTokenWriter.cs
  3. 9
      ILSpy/TextView/CSharp-Mode.xshd
  4. 2
      ILSpy/Themes/Theme.RSharpDark.xaml
  5. 2
      ILSpy/Themes/Theme.RSharpLight.xaml
  6. 2
      ILSpy/Themes/Theme.VSCodeDarkPlus.xaml
  7. 2
      ILSpy/Themes/Theme.VSCodeLightPlus.xaml

2
ICSharpCode.Decompiler/Output/TextTokenWriter.cs

@ -457,7 +457,7 @@ namespace ICSharpCode.Decompiler
{ {
if (node is EntityDeclaration && !(node.Parent is LocalFunctionDeclarationStatement)) if (node is EntityDeclaration && !(node.Parent is LocalFunctionDeclarationStatement))
return true; return true;
if (node is VariableInitializer && node.Parent is FieldDeclaration) if (node is VariableInitializer && node.Parent is FieldDeclaration or EventDeclaration)
{ {
node = node.Parent; node = node.Parent;
return true; return true;

93
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

@ -55,9 +55,12 @@ namespace ICSharpCode.ILSpy
HighlightingColor methodCallColor; HighlightingColor methodCallColor;
HighlightingColor methodDeclarationColor; HighlightingColor methodDeclarationColor;
HighlightingColor fieldDeclarationColor; HighlightingColor fieldDeclarationColor;
HighlightingColor fieldAccessColor; HighlightingColor fieldAccessColor;
HighlightingColor propertyDeclarationColor;
HighlightingColor propertyAccessColor;
HighlightingColor eventDeclarationColor;
HighlightingColor eventAccessColor;
HighlightingColor valueKeywordColor; HighlightingColor valueKeywordColor;
HighlightingColor thisKeywordColor; HighlightingColor thisKeywordColor;
@ -74,39 +77,49 @@ namespace ICSharpCode.ILSpy
this.locatable = locatable; this.locatable = locatable;
this.textOutput = textOutput; this.textOutput = textOutput;
this.visibilityKeywordsColor = highlighting.GetNamedColor("Visibility"); this.visibilityKeywordsColor = GetColor("Visibility") ?? GetColor("Keywords");
this.namespaceKeywordsColor = highlighting.GetNamedColor("NamespaceKeywords"); this.namespaceKeywordsColor = GetColor("NamespaceKeywords") ?? GetColor("Keywords");
this.structureKeywordsColor = highlighting.GetNamedColor("Keywords"); this.structureKeywordsColor = GetColor("Keywords");
this.gotoKeywordsColor = highlighting.GetNamedColor("GotoKeywords"); this.gotoKeywordsColor = GetColor("GotoKeywords") ?? GetColor("Keywords");
this.queryKeywordsColor = highlighting.GetNamedColor("QueryKeywords"); this.queryKeywordsColor = GetColor("QueryKeywords") ?? GetColor("Keywords");
this.exceptionKeywordsColor = highlighting.GetNamedColor("ExceptionKeywords"); this.exceptionKeywordsColor = GetColor("ExceptionKeywords") ?? GetColor("Keywords");
this.checkedKeywordColor = highlighting.GetNamedColor("CheckedKeyword"); this.checkedKeywordColor = GetColor("CheckedKeyword") ?? GetColor("Keywords");
this.unsafeKeywordsColor = highlighting.GetNamedColor("UnsafeKeywords"); this.unsafeKeywordsColor = GetColor("UnsafeKeywords") ?? GetColor("Keywords");
this.valueTypeKeywordsColor = highlighting.GetNamedColor("ValueTypeKeywords"); this.valueTypeKeywordsColor = GetColor("ValueTypeKeywords") ?? GetColor("Keywords");
this.referenceTypeKeywordsColor = highlighting.GetNamedColor("ReferenceTypeKeywords"); this.referenceTypeKeywordsColor = GetColor("ReferenceTypeKeywords") ?? GetColor("Keywords");
this.operatorKeywordsColor = highlighting.GetNamedColor("OperatorKeywords"); this.operatorKeywordsColor = GetColor("OperatorKeywords") ?? GetColor("Keywords");
this.parameterModifierColor = highlighting.GetNamedColor("ParameterModifiers"); this.parameterModifierColor = GetColor("ParameterModifiers") ?? GetColor("Keywords");
this.modifiersColor = highlighting.GetNamedColor("Modifiers"); this.modifiersColor = GetColor("Modifiers") ?? GetColor("Keywords");
this.accessorKeywordsColor = highlighting.GetNamedColor("GetSetAddRemove"); this.accessorKeywordsColor = GetColor("GetSetAddRemove") ?? GetColor("Keywords");
this.referenceTypeColor = highlighting.GetNamedColor("ReferenceTypes"); this.referenceTypeColor = GetColor("ReferenceTypes") ?? GetColor("Types");
this.valueTypeColor = highlighting.GetNamedColor("ValueTypes"); this.valueTypeColor = GetColor("ValueTypes") ?? GetColor("Types");
this.interfaceTypeColor = highlighting.GetNamedColor("InterfaceTypes"); this.interfaceTypeColor = GetColor("InterfaceTypes") ?? GetColor("Types");
this.enumerationTypeColor = highlighting.GetNamedColor("EnumTypes"); this.enumerationTypeColor = GetColor("EnumTypes") ?? GetColor("Types");
this.typeParameterTypeColor = highlighting.GetNamedColor("TypeParameters"); this.typeParameterTypeColor = GetColor("TypeParameters") ?? GetColor("Types");
this.delegateTypeColor = highlighting.GetNamedColor("DelegateTypes"); this.delegateTypeColor = GetColor("DelegateTypes") ?? GetColor("Types");
this.methodDeclarationColor = this.methodCallColor = highlighting.GetNamedColor("MethodCall"); this.methodDeclarationColor = GetColor("MethodDeclaration") ?? GetColor("MethodCall");
//this.eventDeclarationColor = this.eventAccessColor = defaultTextColor; this.methodCallColor = GetColor("MethodCall") ?? GetColor("MethodDeclaration");
//this.propertyDeclarationColor = this.propertyAccessColor = defaultTextColor; this.fieldDeclarationColor = GetColor("FieldDeclaration") ?? GetColor("FieldAccess");
this.fieldDeclarationColor = this.fieldAccessColor = highlighting.GetNamedColor("FieldAccess"); this.fieldAccessColor = GetColor("FieldAccess") ?? GetColor("FieldDeclaration");
this.propertyDeclarationColor = GetColor("PropertyDeclaration") ?? GetColor("PropertyAccess");
this.propertyAccessColor = GetColor("PropertyAccess") ?? GetColor("PropertyDeclaration");
this.eventDeclarationColor = GetColor("EventDeclaration") ?? GetColor("EventAccess");
this.eventAccessColor = GetColor("EventAccess") ?? GetColor("EventDeclaration");
//this.variableDeclarationColor = this.variableAccessColor = defaultTextColor; //this.variableDeclarationColor = this.variableAccessColor = defaultTextColor;
//this.parameterDeclarationColor = this.parameterAccessColor = defaultTextColor; //this.parameterDeclarationColor = this.parameterAccessColor = defaultTextColor;
this.valueKeywordColor = highlighting.GetNamedColor("NullOrValueKeywords"); this.valueKeywordColor = GetColor("NullOrValueKeywords") ?? GetColor("Keywords");
this.thisKeywordColor = highlighting.GetNamedColor("ThisOrBaseReference"); this.thisKeywordColor = GetColor("ThisOrBaseReference") ?? GetColor("Keywords");
this.trueKeywordColor = highlighting.GetNamedColor("TrueFalse"); this.trueKeywordColor = GetColor("TrueFalse") ?? GetColor("Keywords");
this.typeKeywordsColor = highlighting.GetNamedColor("TypeKeywords"); this.typeKeywordsColor = GetColor("TypeKeywords") ?? GetColor("Keywords");
this.attributeKeywordsColor = highlighting.GetNamedColor("AttributeKeywords"); this.attributeKeywordsColor = GetColor("AttributeKeywords") ?? GetColor("Keywords");
//this.externAliasKeywordColor = ...; //this.externAliasKeywordColor = ...;
HighlightingColor GetColor(string colorName)
{
var color = highlighting.GetNamedColor(colorName);
return color is not { Foreground: null, Background: null, FontFamily: null, FontWeight: null, FontSize: null, FontStyle: null, Strikethrough: null, Underline: null } ? color : null;
}
} }
public override void WriteKeyword(Role role, string keyword) public override void WriteKeyword(Role role, string keyword)
@ -380,12 +393,18 @@ namespace ICSharpCode.ILSpy
break; break;
} }
break; break;
case IMethod m: case IMethod:
color = methodDeclarationColor; color = methodDeclarationColor;
break; break;
case IField f: case IField:
color = fieldDeclarationColor; color = fieldDeclarationColor;
break; break;
case IProperty:
color = propertyDeclarationColor;
break;
case IEvent:
color = eventDeclarationColor;
break;
} }
switch (GetCurrentMemberReference()) switch (GetCurrentMemberReference())
{ {
@ -409,12 +428,18 @@ namespace ICSharpCode.ILSpy
break; break;
} }
break; break;
case IMethod m: case IMethod:
color = methodCallColor; color = methodCallColor;
break; break;
case IField f: case IField:
color = fieldAccessColor; color = fieldAccessColor;
break; break;
case IProperty:
color = propertyAccessColor;
break;
case IEvent:
color = eventAccessColor;
break;
} }
if (color != null) if (color != null)
{ {

9
ILSpy/TextView/CSharp-Mode.xshd

@ -33,14 +33,23 @@
<Color name="AttributeKeywords" foreground="Navy" exampleText="[assembly: AssemblyVersion(&quot;1.0.0.*&quot;)]" /> <Color name="AttributeKeywords" foreground="Navy" exampleText="[assembly: AssemblyVersion(&quot;1.0.0.*&quot;)]" />
<!-- Colors used for semantic highlighting --> <!-- Colors used for semantic highlighting -->
<Color name="Types" exampleText="System.#{#Uri#}# uri;"/>
<Color name="ReferenceTypes" foreground="#004085" exampleText="System.#{#Uri#}# uri;"/> <Color name="ReferenceTypes" foreground="#004085" exampleText="System.#{#Uri#}# uri;"/>
<Color name="InterfaceTypes" foreground="#004085" exampleText="System.#{#IDisposable#}# obj;"/> <Color name="InterfaceTypes" foreground="#004085" exampleText="System.#{#IDisposable#}# obj;"/>
<Color name="TypeParameters" foreground="#004085" exampleText="class MyList&lt;#{#T#}#&gt; { }"/> <Color name="TypeParameters" foreground="#004085" exampleText="class MyList&lt;#{#T#}#&gt; { }"/>
<Color name="DelegateTypes" foreground="#004085" exampleText="System.#{#Action#}#; action;"/> <Color name="DelegateTypes" foreground="#004085" exampleText="System.#{#Action#}#; action;"/>
<Color name="ValueTypes" fontWeight="bold" foreground="#004085" exampleText="System.#{#DateTime#}# date;"/> <Color name="ValueTypes" fontWeight="bold" foreground="#004085" exampleText="System.#{#DateTime#}# date;"/>
<Color name="EnumTypes" fontWeight="bold" foreground="#004085" exampleText="System.#{#ConsoleKey#}# key;"/> <Color name="EnumTypes" fontWeight="bold" foreground="#004085" exampleText="System.#{#ConsoleKey#}# key;"/>
<Color name="MethodDeclaration" exampleText="override string #{#ToString#}#() { }"/>
<Color name="MethodCall" foreground="MidnightBlue" fontWeight="bold" exampleText="o.#{#ToString#}#();"/> <Color name="MethodCall" foreground="MidnightBlue" fontWeight="bold" exampleText="o.#{#ToString#}#();"/>
<Color name="FieldDeclaration" exampleText="private int #{#name#}#;"/>
<Color name="FieldAccess" fontStyle="italic" exampleText="return this.#{#name#}#;"/> <Color name="FieldAccess" fontStyle="italic" exampleText="return this.#{#name#}#;"/>
<Color name="PropertyDeclaration" exampleText="private int #{#name#}# { get; set; }"/>
<Color name="PropertyAccess" exampleText="return this.#{#name#}#;"/>
<Color name="EventDeclaration" exampleText="private event Action #{#name#}#;"/>
<Color name="EventAccess" exampleText="this.#{#name#}#?.Invoke();"/>
<Color name="InactiveCode" foreground="Gray" exampleText="#{#Deactivated by #if#}#"/> <Color name="InactiveCode" foreground="Gray" exampleText="#{#Deactivated by #if#}#"/>
<Color name="SemanticError" foreground="DarkRed" exampleText="o.#{#MissingMethod#}#()"/> <Color name="SemanticError" foreground="DarkRed" exampleText="o.#{#MissingMethod#}#()"/>

2
ILSpy/Themes/Theme.RSharpDark.xaml

@ -70,6 +70,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#ADB0E6" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#ADB0E6" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#00FFFF" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#00FFFF" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#C4ADE6" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#C4ADE6" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.PropertyAccess" Foreground="#C4ADE6" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.EventAccess" Foreground="#DDA0DD" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#A9A9A9" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#A9A9A9" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#FF3333" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#FF3333" />

2
ILSpy/Themes/Theme.RSharpLight.xaml

@ -70,6 +70,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#3F008F" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#3F008F" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#008B8B" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#008B8B" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#660E7A" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#660E7A" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.PropertyAccess" Foreground="#660E7A" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.EventAccess" Foreground="#FF00FF" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#A9A9A9" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#A9A9A9" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#FF0000" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#FF0000" />

2
ILSpy/Themes/Theme.VSCodeDarkPlus.xaml

@ -75,6 +75,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#4EC9B0" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#4EC9B0" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#DCDCAA" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#DCDCAA" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#9CDCFE" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#9CDCFE" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.PropertyAccess" Foreground="#9CDCFE" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.EventAccess" Foreground="#9CDCFE" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#A6A6A6" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#A6A6A6" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#f44747" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#f44747" />

2
ILSpy/Themes/Theme.VSCodeLightPlus.xaml

@ -75,6 +75,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#267f99" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#267f99" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#795E26" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#795E26" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#001080" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.FieldAccess" Foreground="#001080" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.PropertyAccess" Foreground="#001080" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.EventAccess" Foreground="#001080" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#767676" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.InactiveCode" Foreground="#767676" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#cd3131" /> <themes:SyntaxColor x:Key="SyntaxColor.C#.SemanticError" Foreground="#cd3131" />

Loading…
Cancel
Save