Beispiel #1
0
 public void WriteUsingStatements()
 {
     if (UsingNamespaces.Any())
     {
         AppendLineIfNeeded();
         foreach (var usingNamespace in UsingNamespaces.OrderBy(usingNamespace => usingNamespace))
         {
             WriteIndentation();
             Write($"using {usingNamespace};");
             WriteNewLine();
         }
         appendLine = true;
     }
 }
        private List <BaseTypeInfo> GetBaseTypeInfos(IEnumerable <SimpleBaseTypeSyntax> baseTypes, string defaultNamespace)
        {
            var candidatesNamespaces = new List <string>();

            candidatesNamespaces.Add(defaultNamespace);

            while (defaultNamespace.Contains("."))
            {
                defaultNamespace = defaultNamespace.Substring(0, defaultNamespace.LastIndexOf("."));
                candidatesNamespaces.Add(defaultNamespace);
            }

            var firstUsingIndex = -1;

            if (UsingNamespaces.Any())
            {
                var results = UsingNamespaces.Select(x => x.Namespace).ToList();
                candidatesNamespaces.AddRange(results);
                firstUsingIndex = candidatesNamespaces.IndexOf(results.FirstOrDefault());
            }

            var items = new List <BaseTypeInfo>();

            foreach (var baseType in baseTypes)
            {
                var hasGlobal = false;
                var typeName  = baseType.ToString();

                if (UsingNamespaces.Any())
                {
                    foreach (var check in UsingNamespaces)
                    {
                        if (!string.IsNullOrEmpty(check.Alternate) && typeName.Contains($"{check.Alternate}"))
                        {
                            typeName = typeName.Replace(check.Alternate, check.Namespace);
                        }

                        // 名前空間エイリアス修飾子(::演算子)の場合、:: が残る場合は . に置換
                        if (typeName.Contains("global::"))
                        {
                            hasGlobal = true;
                            typeName  = typeName.Replace("global::", string.Empty);
                        }

                        if (typeName.Contains("::"))
                        {
                            typeName = typeName.Replace("::", ".");
                        }
                    }
                }

                var candidatesDefineFullNames = new List <string>();

                if (firstUsingIndex == -1)
                {
                    if (typeName.Contains("."))
                    {
                        candidatesDefineFullNames.Add(typeName);
                    }

                    for (var i = 0; i < candidatesNamespaces.Count; i++)
                    {
                        candidatesDefineFullNames.Add($"{candidatesNamespaces[i]}.{typeName}");
                    }

                    if (!typeName.Contains("."))
                    {
                        candidatesDefineFullNames.Add(typeName);
                    }
                }
                else
                {
                    if (typeName.Contains("."))
                    {
                        candidatesDefineFullNames.Add(typeName);
                    }

                    if (!hasGlobal)
                    {
                        for (var i = 0; i < firstUsingIndex; i++)
                        {
                            candidatesDefineFullNames.Add($"{candidatesNamespaces[i]}.{typeName}");
                        }
                    }

                    if (!typeName.Contains("."))
                    {
                        candidatesDefineFullNames.Insert(firstUsingIndex, typeName);
                    }

                    for (var i = firstUsingIndex; i < candidatesNamespaces.Count; i++)
                    {
                        candidatesDefineFullNames.Add($"{candidatesNamespaces[i]}.{typeName}");
                    }
                }



                var genericParts = new List <string>();

                // IEnumerable<int>, IEnumerable<IEnumerable<int>>, Dictionary<int, int>
                // Dictionary<int, Dictionary<int, int>>
                var node = baseType.ChildNodes().FirstOrDefault();
                if (node is GenericNameSyntax)
                {
                    var listNode     = node.ChildNodes().FirstOrDefault(); // TypeArgumentListSyntax
                    var genericTypes = listNode.ChildNodes();              // PredefinedTypeSyntax, GenericNameSyntax, ...

                    foreach (var genericType in genericTypes)
                    {
                        genericParts.Add(genericType.ToString());
                    }
                }

                var startLength = baseType.Span.Start;
                var endLength   = baseType.Span.End;

                items.Add(new BaseTypeInfo
                {
                    CandidatesNamespaces      = candidatesNamespaces,
                    CandidatesDefineFullNames = candidatesDefineFullNames,
                    BaseType        = typeName,
                    DisplayBaseType = RemoveNamespace(typeName),
                    GenericParts    = genericParts,
                    SourceFile      = SourceFile,
                    StartLength     = startLength,
                    EndLength       = endLength,
                });
            }

            return(items);
        }