|
|
@ -32,8 +32,6 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms |
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class IntroduceUsingDeclarations : IAstTransform |
|
|
|
public class IntroduceUsingDeclarations : IAstTransform |
|
|
|
{ |
|
|
|
{ |
|
|
|
public bool FullyQualifyAmbiguousTypeNames = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Run(AstNode rootNode, TransformContext context) |
|
|
|
public void Run(AstNode rootNode, TransformContext context) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// First determine all the namespaces that need to be imported:
|
|
|
|
// First determine all the namespaces that need to be imported:
|
|
|
@ -42,26 +40,28 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms |
|
|
|
|
|
|
|
|
|
|
|
var usingScope = new UsingScope(); |
|
|
|
var usingScope = new UsingScope(); |
|
|
|
|
|
|
|
|
|
|
|
var insertionPoint = rootNode.Children.LastOrDefault(n => n is PreProcessorDirective p && p.Type == PreProcessorDirectiveType.Define); |
|
|
|
if (context.Settings.UsingDeclarations) { |
|
|
|
|
|
|
|
var insertionPoint = rootNode.Children.LastOrDefault(n => n is PreProcessorDirective p && p.Type == PreProcessorDirectiveType.Define); |
|
|
|
// Now add using declarations for those namespaces:
|
|
|
|
|
|
|
|
foreach (string ns in requiredImports.ImportedNamespaces.OrderByDescending(n => n)) { |
|
|
|
// Now add using declarations for those namespaces:
|
|
|
|
// we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards
|
|
|
|
foreach (string ns in requiredImports.ImportedNamespaces.OrderByDescending(n => n)) { |
|
|
|
// (always inserting at the start of the list)
|
|
|
|
// we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards
|
|
|
|
string[] parts = ns.Split('.'); |
|
|
|
// (always inserting at the start of the list)
|
|
|
|
AstType nsType = new SimpleType(parts[0]); |
|
|
|
string[] parts = ns.Split('.'); |
|
|
|
for (int i = 1; i < parts.Length; i++) { |
|
|
|
AstType nsType = new SimpleType(parts[0]); |
|
|
|
nsType = new MemberType { Target = nsType, MemberName = parts[i] }; |
|
|
|
for (int i = 1; i < parts.Length; i++) { |
|
|
|
} |
|
|
|
nsType = new MemberType { Target = nsType, MemberName = parts[i] }; |
|
|
|
if (FullyQualifyAmbiguousTypeNames) { |
|
|
|
} |
|
|
|
var reference = nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) as TypeOrNamespaceReference; |
|
|
|
if (context.Settings.FullyQualifyAmbiguousTypeNames) { |
|
|
|
if (reference != null) |
|
|
|
var reference = nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) as TypeOrNamespaceReference; |
|
|
|
usingScope.Usings.Add(reference); |
|
|
|
if (reference != null) |
|
|
|
|
|
|
|
usingScope.Usings.Add(reference); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
rootNode.InsertChildAfter(insertionPoint, new UsingDeclaration { Import = nsType }, SyntaxTree.MemberRole); |
|
|
|
} |
|
|
|
} |
|
|
|
rootNode.InsertChildAfter(insertionPoint, new UsingDeclaration { Import = nsType }, SyntaxTree.MemberRole); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!FullyQualifyAmbiguousTypeNames) |
|
|
|
if (!context.Settings.FullyQualifyAmbiguousTypeNames) |
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
// verify that the SimpleTypes refer to the correct type (no ambiguities)
|
|
|
|
// verify that the SimpleTypes refer to the correct type (no ambiguities)
|
|
|
|