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

93
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

@ -55,9 +55,12 @@ namespace ICSharpCode.ILSpy @@ -55,9 +55,12 @@ namespace ICSharpCode.ILSpy
HighlightingColor methodCallColor;
HighlightingColor methodDeclarationColor;
HighlightingColor fieldDeclarationColor;
HighlightingColor fieldAccessColor;
HighlightingColor propertyDeclarationColor;
HighlightingColor propertyAccessColor;
HighlightingColor eventDeclarationColor;
HighlightingColor eventAccessColor;
HighlightingColor valueKeywordColor;
HighlightingColor thisKeywordColor;
@ -74,39 +77,49 @@ namespace ICSharpCode.ILSpy @@ -74,39 +77,49 @@ namespace ICSharpCode.ILSpy
this.locatable = locatable;
this.textOutput = textOutput;
this.visibilityKeywordsColor = highlighting.GetNamedColor("Visibility");
this.namespaceKeywordsColor = highlighting.GetNamedColor("NamespaceKeywords");
this.structureKeywordsColor = highlighting.GetNamedColor("Keywords");
this.gotoKeywordsColor = highlighting.GetNamedColor("GotoKeywords");
this.queryKeywordsColor = highlighting.GetNamedColor("QueryKeywords");
this.exceptionKeywordsColor = highlighting.GetNamedColor("ExceptionKeywords");
this.checkedKeywordColor = highlighting.GetNamedColor("CheckedKeyword");
this.unsafeKeywordsColor = highlighting.GetNamedColor("UnsafeKeywords");
this.valueTypeKeywordsColor = highlighting.GetNamedColor("ValueTypeKeywords");
this.referenceTypeKeywordsColor = highlighting.GetNamedColor("ReferenceTypeKeywords");
this.operatorKeywordsColor = highlighting.GetNamedColor("OperatorKeywords");
this.parameterModifierColor = highlighting.GetNamedColor("ParameterModifiers");
this.modifiersColor = highlighting.GetNamedColor("Modifiers");
this.accessorKeywordsColor = highlighting.GetNamedColor("GetSetAddRemove");
this.visibilityKeywordsColor = GetColor("Visibility") ?? GetColor("Keywords");
this.namespaceKeywordsColor = GetColor("NamespaceKeywords") ?? GetColor("Keywords");
this.structureKeywordsColor = GetColor("Keywords");
this.gotoKeywordsColor = GetColor("GotoKeywords") ?? GetColor("Keywords");
this.queryKeywordsColor = GetColor("QueryKeywords") ?? GetColor("Keywords");
this.exceptionKeywordsColor = GetColor("ExceptionKeywords") ?? GetColor("Keywords");
this.checkedKeywordColor = GetColor("CheckedKeyword") ?? GetColor("Keywords");
this.unsafeKeywordsColor = GetColor("UnsafeKeywords") ?? GetColor("Keywords");
this.valueTypeKeywordsColor = GetColor("ValueTypeKeywords") ?? GetColor("Keywords");
this.referenceTypeKeywordsColor = GetColor("ReferenceTypeKeywords") ?? GetColor("Keywords");
this.operatorKeywordsColor = GetColor("OperatorKeywords") ?? GetColor("Keywords");
this.parameterModifierColor = GetColor("ParameterModifiers") ?? GetColor("Keywords");
this.modifiersColor = GetColor("Modifiers") ?? GetColor("Keywords");
this.accessorKeywordsColor = GetColor("GetSetAddRemove") ?? GetColor("Keywords");
this.referenceTypeColor = highlighting.GetNamedColor("ReferenceTypes");
this.valueTypeColor = highlighting.GetNamedColor("ValueTypes");
this.interfaceTypeColor = highlighting.GetNamedColor("InterfaceTypes");
this.enumerationTypeColor = highlighting.GetNamedColor("EnumTypes");
this.typeParameterTypeColor = highlighting.GetNamedColor("TypeParameters");
this.delegateTypeColor = highlighting.GetNamedColor("DelegateTypes");
this.methodDeclarationColor = this.methodCallColor = highlighting.GetNamedColor("MethodCall");
//this.eventDeclarationColor = this.eventAccessColor = defaultTextColor;
//this.propertyDeclarationColor = this.propertyAccessColor = defaultTextColor;
this.fieldDeclarationColor = this.fieldAccessColor = highlighting.GetNamedColor("FieldAccess");
this.referenceTypeColor = GetColor("ReferenceTypes") ?? GetColor("Types");
this.valueTypeColor = GetColor("ValueTypes") ?? GetColor("Types");
this.interfaceTypeColor = GetColor("InterfaceTypes") ?? GetColor("Types");
this.enumerationTypeColor = GetColor("EnumTypes") ?? GetColor("Types");
this.typeParameterTypeColor = GetColor("TypeParameters") ?? GetColor("Types");
this.delegateTypeColor = GetColor("DelegateTypes") ?? GetColor("Types");
this.methodDeclarationColor = GetColor("MethodDeclaration") ?? GetColor("MethodCall");
this.methodCallColor = GetColor("MethodCall") ?? GetColor("MethodDeclaration");
this.fieldDeclarationColor = GetColor("FieldDeclaration") ?? GetColor("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.parameterDeclarationColor = this.parameterAccessColor = defaultTextColor;
this.valueKeywordColor = highlighting.GetNamedColor("NullOrValueKeywords");
this.thisKeywordColor = highlighting.GetNamedColor("ThisOrBaseReference");
this.trueKeywordColor = highlighting.GetNamedColor("TrueFalse");
this.typeKeywordsColor = highlighting.GetNamedColor("TypeKeywords");
this.attributeKeywordsColor = highlighting.GetNamedColor("AttributeKeywords");
this.valueKeywordColor = GetColor("NullOrValueKeywords") ?? GetColor("Keywords");
this.thisKeywordColor = GetColor("ThisOrBaseReference") ?? GetColor("Keywords");
this.trueKeywordColor = GetColor("TrueFalse") ?? GetColor("Keywords");
this.typeKeywordsColor = GetColor("TypeKeywords") ?? GetColor("Keywords");
this.attributeKeywordsColor = GetColor("AttributeKeywords") ?? GetColor("Keywords");
//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)
@ -380,12 +393,18 @@ namespace ICSharpCode.ILSpy @@ -380,12 +393,18 @@ namespace ICSharpCode.ILSpy
break;
}
break;
case IMethod m:
case IMethod:
color = methodDeclarationColor;
break;
case IField f:
case IField:
color = fieldDeclarationColor;
break;
case IProperty:
color = propertyDeclarationColor;
break;
case IEvent:
color = eventDeclarationColor;
break;
}
switch (GetCurrentMemberReference())
{
@ -409,12 +428,18 @@ namespace ICSharpCode.ILSpy @@ -409,12 +428,18 @@ namespace ICSharpCode.ILSpy
break;
}
break;
case IMethod m:
case IMethod:
color = methodCallColor;
break;
case IField f:
case IField:
color = fieldAccessColor;
break;
case IProperty:
color = propertyAccessColor;
break;
case IEvent:
color = eventAccessColor;
break;
}
if (color != null)
{

9
ILSpy/TextView/CSharp-Mode.xshd

@ -33,14 +33,23 @@ @@ -33,14 +33,23 @@
<Color name="AttributeKeywords" foreground="Navy" exampleText="[assembly: AssemblyVersion(&quot;1.0.0.*&quot;)]" />
<!-- Colors used for semantic highlighting -->
<Color name="Types" exampleText="System.#{#Uri#}# uri;"/>
<Color name="ReferenceTypes" foreground="#004085" exampleText="System.#{#Uri#}# uri;"/>
<Color name="InterfaceTypes" foreground="#004085" exampleText="System.#{#IDisposable#}# obj;"/>
<Color name="TypeParameters" foreground="#004085" exampleText="class MyList&lt;#{#T#}#&gt; { }"/>
<Color name="DelegateTypes" foreground="#004085" exampleText="System.#{#Action#}#; action;"/>
<Color name="ValueTypes" fontWeight="bold" foreground="#004085" exampleText="System.#{#DateTime#}# date;"/>
<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="FieldDeclaration" exampleText="private int #{#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="SemanticError" foreground="DarkRed" exampleText="o.#{#MissingMethod#}#()"/>

2
ILSpy/Themes/Theme.RSharpDark.xaml

@ -70,6 +70,8 @@ @@ -70,6 +70,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#ADB0E6" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#00FFFF" />
<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#.SemanticError" Foreground="#FF3333" />

2
ILSpy/Themes/Theme.RSharpLight.xaml

@ -70,6 +70,8 @@ @@ -70,6 +70,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#3F008F" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#008B8B" />
<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#.SemanticError" Foreground="#FF0000" />

2
ILSpy/Themes/Theme.VSCodeDarkPlus.xaml

@ -75,6 +75,8 @@ @@ -75,6 +75,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#4EC9B0" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#DCDCAA" />
<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#.SemanticError" Foreground="#f44747" />

2
ILSpy/Themes/Theme.VSCodeLightPlus.xaml

@ -75,6 +75,8 @@ @@ -75,6 +75,8 @@
<themes:SyntaxColor x:Key="SyntaxColor.C#.EnumTypes" Foreground="#267f99" />
<themes:SyntaxColor x:Key="SyntaxColor.C#.MethodCall" Foreground="#795E26" />
<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#.SemanticError" Foreground="#cd3131" />

Loading…
Cancel
Save