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

15
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

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

Loading…
Cancel
Save