Browse Source

Fix #2257: Add global:: prefix, iff the containing namespace matches the type name.

pull/2308/head
Siegfried Pammer 4 years ago
parent
commit
f35074ebd8
  1. 41
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs
  2. 15
      ILSpy/Languages/CSharpHighlightingTokenWriter.cs

41
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -525,14 +525,13 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
// Handle top-level types // Handle top-level types
if (string.IsNullOrEmpty(genericType.Namespace)) if (string.IsNullOrEmpty(genericType.Namespace))
{ {
result.Target = new SimpleType("global"); result.Target = MakeGlobal();
if (AddResolveResultAnnotations && resolver != null)
result.Target.AddAnnotation(new NamespaceResolveResult(resolver.Compilation.RootNamespace));
result.IsDoubleColon = true; result.IsDoubleColon = true;
} }
else else
{ {
result.Target = ConvertNamespace(genericType.Namespace, out _); result.Target = ConvertNamespace(genericType.Namespace,
out _, genericType.Namespace == genericType.Name);
} }
} }
result.MemberName = genericType.Name; result.MemberName = genericType.Name;
@ -605,6 +604,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
} }
public AstType ConvertNamespace(string namespaceName, out NamespaceResolveResult nrr) public AstType ConvertNamespace(string namespaceName, out NamespaceResolveResult nrr)
{
return ConvertNamespace(namespaceName, out nrr, requiresGlobalPrefix: false);
}
AstType ConvertNamespace(string namespaceName, out NamespaceResolveResult nrr, bool requiresGlobalPrefix)
{ {
if (resolver != null) if (resolver != null)
{ {
@ -633,18 +637,27 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{ {
if (IsValidNamespace(namespaceName, out nrr)) if (IsValidNamespace(namespaceName, out nrr))
{ {
var ns = MakeSimpleType(namespaceName); AstType ns;
if (requiresGlobalPrefix)
{
ns = new MemberType {
Target = MakeGlobal(),
IsDoubleColon = true,
MemberName = namespaceName
};
}
else
{
ns = MakeSimpleType(namespaceName);
}
if (AddResolveResultAnnotations && nrr != null) if (AddResolveResultAnnotations && nrr != null)
ns.AddAnnotation(nrr); ns.AddAnnotation(nrr);
return ns; return ns;
} }
else else
{ {
var target = new SimpleType("global");
if (AddResolveResultAnnotations)
target.AddAnnotation(new NamespaceResolveResult(resolver.Compilation.RootNamespace));
var ns = new MemberType { var ns = new MemberType {
Target = target, Target = MakeGlobal(),
IsDoubleColon = true, IsDoubleColon = true,
MemberName = namespaceName MemberName = namespaceName
}; };
@ -661,7 +674,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{ {
string parentNamespace = namespaceName.Substring(0, pos); string parentNamespace = namespaceName.Substring(0, pos);
string localNamespace = namespaceName.Substring(pos + 1); string localNamespace = namespaceName.Substring(pos + 1);
var parentNS = ConvertNamespace(parentNamespace, out var parentNRR); var parentNS = ConvertNamespace(parentNamespace, out var parentNRR, requiresGlobalPrefix);
var ns = new MemberType { var ns = new MemberType {
Target = parentNS, Target = parentNS,
MemberName = localNamespace MemberName = localNamespace
@ -695,6 +708,14 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
return new SimpleType(name); return new SimpleType(name);
} }
SimpleType MakeGlobal()
{
var global = new SimpleType("global");
if (AddResolveResultAnnotations && resolver != null)
global.AddAnnotation(new NamespaceResolveResult(resolver.Compilation.RootNamespace));
return global;
}
static MemberType MakeMemberType(AstType target, string name) static MemberType MakeMemberType(AstType target, string name)
{ {
if (name == "_") if (name == "_")

15
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

@ -144,7 +144,6 @@ namespace ICSharpCode.ILSpy
case "for": case "for":
case "foreach": case "foreach":
case "lock": case "lock":
case "global":
case "await": case "await":
color = structureKeywordsColor; color = structureKeywordsColor;
break; break;
@ -343,8 +342,18 @@ namespace ICSharpCode.ILSpy
{ {
color = valueKeywordColor; color = valueKeywordColor;
} }
if (identifier.Name == "var" && identifier.Parent is AstType) if (identifier.Parent is AstType)
color = queryKeywordsColor; {
switch (identifier.Name)
{
case "var":
color = queryKeywordsColor;
break;
case "global":
color = structureKeywordsColor;
break;
}
}
switch (GetCurrentDefinition()) switch (GetCurrentDefinition())
{ {
case ITypeDefinition t: case ITypeDefinition t:

Loading…
Cancel
Save