コード例 #1
0
        protected override int GetDeclarationIndex(SyntaxList <MemberDeclarationSyntax> members)
        {
            int index = members.LastIndexOf(SyntaxKind.PropertyDeclaration);

            if (index == -1)
            {
                index = members.LastIndexOf(SyntaxKind.ConstructorDeclaration);
            }

            return(index + 1);
        }
コード例 #2
0
        public static async Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            bool prefixIdentifierWithUnderscore = true,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            string fieldName = TextUtility.ToCamelCase(
                propertyDeclaration.Identifier.ValueText,
                prefixWithUnderscore: prefixIdentifierWithUnderscore);

            FieldDeclarationSyntax fieldDeclaration = CreateBackingField(propertyDeclaration, fieldName)
                                                      .WithFormatterAnnotation();

            PropertyDeclarationSyntax newPropertyDeclaration = ExpandPropertyAndAddBackingField(propertyDeclaration, fieldName)
                                                               .WithTriviaFrom(propertyDeclaration)
                                                               .WithFormatterAnnotation();

            var parentMember = (MemberDeclarationSyntax)propertyDeclaration.Parent;
            SyntaxList <MemberDeclarationSyntax> members = parentMember.GetMembers();

            int propertyIndex = members.IndexOf(propertyDeclaration);

            if (propertyDeclaration.IsReadOnlyAutoProperty())
            {
                SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                IEnumerable <ReferencedSymbol> referencedSymbols = await SymbolFinder.FindReferencesAsync(
                    semanticModel.GetDeclaredSymbol(propertyDeclaration, cancellationToken),
                    document.Project.Solution,
                    cancellationToken).ConfigureAwait(false);

                ImmutableArray <IdentifierNameSyntax> identifierNames = SyntaxUtility
                                                                        .FindNodes <IdentifierNameSyntax>(oldRoot, referencedSymbols)
                                                                        .ToImmutableArray();

                var rewriter = new IdentifierNameSyntaxRewriter(identifierNames, Identifier(fieldName));

                var newParentMember = (MemberDeclarationSyntax)rewriter.Visit(parentMember);

                members = newParentMember.GetMembers();
            }

            int indexOfLastField = members.LastIndexOf(f => f.IsKind(SyntaxKind.FieldDeclaration));

            SyntaxList <MemberDeclarationSyntax> newMembers = members
                                                              .Replace(members[propertyIndex], newPropertyDeclaration)
                                                              .Insert(indexOfLastField + 1, fieldDeclaration);

            SyntaxNode newRoot = oldRoot.ReplaceNode(parentMember, parentMember.SetMembers(newMembers));

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #3
0
        protected static int DetermineFieldInsertPosition(
            SyntaxList <MemberDeclarationSyntax> oldMembers,
            SyntaxList <MemberDeclarationSyntax> newMembers)
        {
            // 1) Place the constant after the last field.
            //
            // 2) If there is no field, place it after the last constant
            //
            // 3) If the first change is before either of those, then place before the first
            // change
            //
            // 4) Otherwise, place it at the start.
            var index          = 0;
            var lastFieldIndex = oldMembers.LastIndexOf(member => member is FieldDeclarationSyntax);

            if (lastFieldIndex >= 0)
            {
                index = lastFieldIndex + 1;
            }
            else
            {
                var lastConstantIndex = oldMembers.LastIndexOf(IsConstantField);
                if (lastConstantIndex >= 0)
                {
                    index = lastConstantIndex + 1;
                }
            }

            var firstChangeIndex = DetermineFirstChange(oldMembers, newMembers);

            if (firstChangeIndex >= 0)
            {
                index = Math.Min(index, firstChangeIndex);
            }

            return(index);
        }
コード例 #4
0
        private static int FindIndex(ClassDeclarationSyntax classDeclaration)
        {
            SyntaxList <MemberDeclarationSyntax> members = classDeclaration.Members;

            int index = members.LastIndexOf(SyntaxKind.ConstructorDeclaration);

            if (index == -1)
            {
                return(members
                       .TakeWhile(f => f.IsKind(SyntaxKind.FieldDeclaration))
                       .Count());
            }

            return(index + 1);
        }
コード例 #5
0
        protected override int GetDeclarationIndex(SyntaxList <MemberDeclarationSyntax> members)
        {
            int index = members.LastIndexOf(SyntaxKind.FieldDeclaration);

            if (index != -1)
            {
                return(index + 1);
            }

            index = members.IndexOf(SyntaxKind.ConstructorDeclaration);

            if (index != -1)
            {
                return(index);
            }

            return(0);
        }
コード例 #6
0
        public static async Task <Document> RefactorAsync(
            Document document,
            EventFieldDeclarationSyntax eventFieldDeclaration,
            IEventSymbol eventSymbol,
            ITypeSymbol eventArgsSymbol,
            bool supportsCSharp6,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var containingMember = (MemberDeclarationSyntax)eventFieldDeclaration.Parent;

            SyntaxList <MemberDeclarationSyntax> members = containingMember.GetMembers();

            MethodDeclarationSyntax method = CreateOnEventMethod(eventSymbol, eventArgsSymbol, supportsCSharp6);

            int index = members.LastIndexOf(SyntaxKind.MethodDeclaration);

            SyntaxNode newRoot = root.InsertNodesAfter(
                (index == -1) ? eventFieldDeclaration : members[index],
                new SyntaxNode[] { method.WithFormatterAnnotation() });

            return(document.WithSyntaxRoot(newRoot));
        }
コード例 #7
0
 public override int LastIndexOf(Func <TNode, bool> predicate)
 => SyntaxList.LastIndexOf(node => predicate(SyntaxWrapper.Wrap(node)));
コード例 #8
0
 public override int LastIndexOf(TNode node)
 => SyntaxList.LastIndexOf(SyntaxWrapper.Unwrap(node));
コード例 #9
0
        public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
        {
            Diagnostic diagnostic = context.Diagnostics[0];

            if (!Settings.IsEnabled(diagnostic.Id, CodeFixIdentifiers.RemoveUnreachableCode))
            {
                return;
            }

            SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);

            if (!TryFindFirstAncestorOrSelf(root, context.Span, out StatementSyntax statement))
            {
                return;
            }

            Debug.Assert(context.Span.Start == statement.SpanStart, statement.ToString());

            if (context.Span.Start != statement.SpanStart)
            {
                return;
            }

            CodeAction codeAction = CreateCodeActionForIfElse(context.Document, diagnostic, statement.Parent);

            if (codeAction != null)
            {
                context.RegisterCodeFix(codeAction, diagnostic);
                return;
            }

            StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(statement);

            if (statementsInfo.Success)
            {
                codeAction = CodeAction.Create(
                    Title,
                    cancellationToken =>
                {
                    SyntaxList <StatementSyntax> statements = statementsInfo.Statements;

                    int index = statements.IndexOf(statement);

                    if (index == statements.Count - 1)
                    {
                        return(context.Document.RemoveStatementAsync(statement, cancellationToken));
                    }
                    else
                    {
                        int lastIndex = statements.LastIndexOf(f => !f.IsKind(SyntaxKind.LocalFunctionStatement));

                        SyntaxList <StatementSyntax> nodes = RemoveRange(statements, index, lastIndex - index + 1, f => !f.IsKind(SyntaxKind.LocalFunctionStatement));

                        return(context.Document.ReplaceStatementsAsync(
                                   statementsInfo,
                                   nodes,
                                   cancellationToken));
                    }
                },
                    base.GetEquivalenceKey(diagnostic));

                context.RegisterCodeFix(codeAction, diagnostic);
            }
        }
コード例 #10
0
 public static int LastIndexOf <TNode>(this SyntaxList <TNode> list, SyntaxKind kind) where TNode : SyntaxNode
 {
     return(list.LastIndexOf(f => f.IsKind(kind)));
 }