Exemplo n.º 1
0
 private void ProcessMembers(SyntaxList <MemberDeclarationSyntax> members)
 {
     foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
     {
         this.ProcessUsingDirectives(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Usings);
         this.ProcessMembers(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
     }
 }
Exemplo n.º 2
0
            private static void ProcessNodeMembers(TreeTextSpan.Builder builder, SyntaxList <MemberDeclarationSyntax> members)
            {
                foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
                {
                    var childBuilder = builder.AddChild(namespaceDeclaration.FullSpan.Start);
                    childBuilder.SetEnd(namespaceDeclaration.FullSpan.End);

                    ProcessNodeMembers(childBuilder, ((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
                }
            }
        private static int CountNamespaces(SyntaxList <MemberDeclarationSyntax> members)
        {
            var result = 0;

            foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
            {
                result += 1 + CountNamespaces(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
            }

            return(result);
        }
        private static string DetermineIndentation(CompilationUnitSyntax compilationUnit, IndentationSettings indentationSettings, UsingDirectivesPlacement usingDirectivesPlacement)
        {
            string usingsIndentation;

            if (usingDirectivesPlacement == UsingDirectivesPlacement.InsideNamespace)
            {
                var rootNamespace    = compilationUnit.Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
                var indentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, rootNamespace);
                if (!rootNamespace.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration))
                {
                    indentationLevel++;
                }

                usingsIndentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationLevel);
            }
            else
            {
                usingsIndentation = string.Empty;
            }

            return(usingsIndentation);
        }
        private static SyntaxNode UpdateSyntaxRoot(MemberDeclarationSyntax memberDeclaration, ImmutableArray <OrderingTrait> elementOrder, SyntaxNode syntaxRoot, IndentationSettings indentationSettings)
        {
            var parentDeclaration = memberDeclaration.Parent;
            var memberToMove      = new MemberOrderHelper(memberDeclaration, elementOrder);

            if (parentDeclaration is TypeDeclarationSyntax)
            {
                return(HandleTypeDeclaration(memberToMove, (TypeDeclarationSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationSettings));
            }

            if (BaseNamespaceDeclarationSyntaxWrapper.IsInstance(parentDeclaration))
            {
                return(HandleBaseNamespaceDeclaration(memberToMove, (BaseNamespaceDeclarationSyntaxWrapper)parentDeclaration, elementOrder, syntaxRoot, indentationSettings));
            }

            if (parentDeclaration is CompilationUnitSyntax)
            {
                return(HandleCompilationUnitDeclaration(memberToMove, (CompilationUnitSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationSettings));
            }

            return(syntaxRoot);
        }
        private static SyntaxNode AddUsingsToNamespace(SyntaxNode newSyntaxRoot, UsingsSorter usingsHelper, string usingsIndentation, bool hasConditionalDirectives)
        {
            var rootNamespace         = (BaseNamespaceDeclarationSyntaxWrapper)((CompilationUnitSyntax)newSyntaxRoot).Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
            var withLeadingBlankLine  = rootNamespace.SyntaxNode.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration);
            var withTrailingBlankLine = hasConditionalDirectives || rootNamespace.Members.Any() || rootNamespace.Externs.Any();

            var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withLeadingBlankLine, withTrailingBlankLine, qualifyNames: false);

            groupedUsings = groupedUsings.AddRange(rootNamespace.Usings);

            var newRootNamespace = rootNamespace.WithUsings(groupedUsings);

            newSyntaxRoot = newSyntaxRoot.ReplaceNode(rootNamespace, newRootNamespace);

            return(newSyntaxRoot);
        }
        public override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            var document = context.Document;
            var root     = await document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            foreach (var diagnostic in context.Diagnostics)
            {
                var token        = root.FindToken(diagnostic.Location.SourceSpan.Start);
                var tokenText    = token.ValueText.TrimStart('_');
                var baseName     = char.ToUpper(tokenText[0]) + tokenText.Substring(1);
                var newName      = baseName;
                var memberSyntax = RenameHelper.GetParentDeclaration(token);

                if (BaseNamespaceDeclarationSyntaxWrapper.IsInstance(memberSyntax))
                {
                    // namespaces are not symbols. So we are just renaming the namespace
                    Task <Document> RenameNamespace(CancellationToken cancellationToken)
                    {
                        IdentifierNameSyntax identifierSyntax = (IdentifierNameSyntax)token.Parent;

                        var newIdentifierSyntax = identifierSyntax.WithIdentifier(SyntaxFactory.Identifier(newName));

                        var newRoot = root.ReplaceNode(identifierSyntax, newIdentifierSyntax);

                        return(Task.FromResult(context.Document.WithSyntaxRoot(newRoot)));
                    }

                    context.RegisterCodeFix(
                        CodeAction.Create(
                            string.Format(NamingResources.RenameToCodeFix, newName),
                            (Func <CancellationToken, Task <Document> >)RenameNamespace,
                            nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id),
                        diagnostic);
                }
                else if (memberSyntax != null)
                {
                    SemanticModel semanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);

                    var declaredSymbol = semanticModel.GetDeclaredSymbol(memberSyntax);
                    if (declaredSymbol == null)
                    {
                        continue;
                    }

                    bool usedSuffix = false;
                    if (declaredSymbol.Kind == SymbolKind.Field &&
                        declaredSymbol.ContainingType?.TypeKind != TypeKind.Enum &&
                        !await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
                    {
                        usedSuffix = true;
                        newName   += Suffix;
                    }

                    int index = 0;
                    while (!await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
                    {
                        usedSuffix = false;
                        index++;
                        newName = baseName + index;
                    }

                    context.RegisterCodeFix(
                        CodeAction.Create(
                            string.Format(NamingResources.RenameToCodeFix, newName),
                            cancellationToken => RenameHelper.RenameSymbolAsync(document, root, token, newName, cancellationToken),
                            nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id + "_" + usedSuffix + "_" + index),
                        diagnostic);
                }
            }
        }