예제 #1
0
		private void AddAccessorNode(AccessorListSyntax accessorList, SyntaxKind filter)
		{
			var accessor = accessorList.Accessors.FirstOrDefault(x => x.IsKind(filter));
			if (accessor != null)
			{
				_members.Add(accessor);
			}
		}
예제 #2
0
        protected override CombinedSyntaxNode[] QueryTargetNodesFunc()
        {
            // this.BaseNode.Parent = SyntaxList<AccessorDeclarationSyntax>
            // this.BaseNode.Parent.Parent = AccessorListSyntax
            // this.BaseNode.Parent.Parent.Parent = PropertyDeclarationSyntax
            AccessorListSyntax parent = this.BaseNode?.Parent
                                        as AccessorListSyntax ?? throw new InvalidOperationException(
                                                  $"Unable to query the parent node for {nameof(AccessorDeclarationState)}");

            return(parent.Accessors.Select(x => new CombinedSyntaxNode(x)).ToArray());
        }
예제 #3
0
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IndexerDeclarationSyntax indexerDeclaration)
        {
            if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseExpressionBodiedMember) &&
                context.SupportsCSharp6)
            {
                AccessorListSyntax accessorList = indexerDeclaration.AccessorList;

                if (accessorList != null &&
                    context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(accessorList))
                {
                    AccessorDeclarationSyntax accessor = indexerDeclaration
                                                         .AccessorList?
                                                         .Accessors
                                                         .SingleOrDefault(shouldThrow: false);

                    if (accessor?.AttributeLists.Any() == false &&
                        accessor.IsKind(SyntaxKind.GetAccessorDeclaration) &&
                        accessor.Body != null &&
                        (UseExpressionBodiedMemberAnalysis.GetReturnExpression(accessor.Body) != null))
                    {
                        context.RegisterRefactoring(
                            UseExpressionBodiedMemberRefactoring.Title,
                            ct => UseExpressionBodiedMemberRefactoring.RefactorAsync(context.Document, indexerDeclaration, ct),
                            RefactoringIdentifiers.UseExpressionBodiedMember);
                    }
                }
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberAbstract) &&
                indexerDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakeIndexerAbstractRefactoring.ComputeRefactoring(context, indexerDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.MakeMemberVirtual) &&
                indexerDeclaration.HeaderSpan().Contains(context.Span))
            {
                MakeIndexerVirtualRefactoring.ComputeRefactoring(context, indexerDeclaration);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.CopyDocumentationCommentFromBaseMember) &&
                indexerDeclaration.HeaderSpan().Contains(context.Span))
            {
                await CopyDocumentationCommentFromBaseMemberRefactoring.ComputeRefactoringAsync(context, indexerDeclaration).ConfigureAwait(false);
            }

            if (context.IsRefactoringEnabled(RefactoringIdentifiers.AddMemberToInterface) &&
                context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(indexerDeclaration.ThisKeyword))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                AddMemberToInterfaceRefactoring.ComputeRefactoring(context, indexerDeclaration, semanticModel);
            }
        }
예제 #4
0
        private static AccessorDeclarationSyntax Accessor(this AccessorListSyntax accessorList, SyntaxKind kind)
        {
            if (accessorList == null)
            {
                throw new ArgumentNullException(nameof(accessorList));
            }

            return(accessorList
                   .Accessors
                   .FirstOrDefault(accessor => accessor.IsKind(kind)));
        }
예제 #5
0
        private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, AccessorListSyntax accessorList, ExpressionSyntax expression)
        {
            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UseExpressionBodiedMember, accessorList);

            if (expression.Parent is ReturnStatementSyntax returnStatement)
            {
                DiagnosticHelpers.ReportToken(context, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut, returnStatement.ReturnKeyword);
            }

            CSharpDiagnosticHelpers.ReportBraces(context, DiagnosticDescriptors.UseExpressionBodiedMemberFadeOut, accessorList);
        }
        private AccessorListSyntax UpdateAccessorList(AccessorListSyntax accessorList)
        {
            if (accessorList == null)
            {
                var getter = SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                             .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
                return(SyntaxFactory.AccessorList(SyntaxFactory.List(Enumerable.Repeat(getter, 1))));
            }

            return(accessorList.WithAccessors(SyntaxFactory.List(GetAccessors(accessorList.Accessors))));
        }
        private static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
        {
            var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;

            if (propertyDeclaration.ContainsDirectives)
            {
                return;
            }

            if (propertyDeclaration.ContainsDiagnostics)
            {
                return;
            }

            if (propertyDeclaration.AttributeLists.Any())
            {
                return;
            }

            if (!CheckModifiers(propertyDeclaration.Modifiers))
            {
                return;
            }

            if (propertyDeclaration.HasDocumentationComment())
            {
                return;
            }

            if (!propertyDeclaration.DescendantTrivia(propertyDeclaration.Span).All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                return;
            }

            AccessorListSyntax accessorList = propertyDeclaration.AccessorList;

            if (accessorList == null)
            {
                return;
            }

            foreach (AccessorDeclarationSyntax accessor in accessorList.Accessors)
            {
                if (!IsFixable(propertyDeclaration, accessor, context.SemanticModel, context.CancellationToken))
                {
                    return;
                }
            }

            DiagnosticHelpers.ReportDiagnostic(context,
                                               DiagnosticDescriptors.RemoveRedundantOverridingMember,
                                               propertyDeclaration,
                                               CSharpFacts.GetTitle(propertyDeclaration));
        }
예제 #8
0
                public override SyntaxNode VisitAccessorList(AccessorListSyntax node)
                {
                    if (node != this.ContainerOfStatementsOrFieldToReplace)
                    {
                        return(base.VisitAccessorList(node));
                    }

                    var newAccessors = VisitList(ReplaceAccessors(node.Accessors));

                    return(node.WithAccessors(newAccessors));
                }
예제 #9
0
        private static AccessorListSyntax CreateAccessorList(BlockSyntax block, bool singleline)
        {
            AccessorListSyntax accessorList = AccessorList(GetAccessorDeclaration(block));

            if (singleline)
            {
                accessorList = accessorList.RemoveWhitespace();
            }

            return(accessorList);
        }
예제 #10
0
        public static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
        {
            if (context.Node.ContainsDiagnostics)
            {
                return;
            }

            var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;

            EqualsValueClauseSyntax initializer = propertyDeclaration.Initializer;

            if (initializer == null)
            {
                return;
            }

            if (initializer.SpanOrLeadingTriviaContainsDirectives())
            {
                return;
            }

            ExpressionSyntax value = initializer.Value;

            if (value == null)
            {
                return;
            }

            AccessorListSyntax accessorList = propertyDeclaration.AccessorList;

            if (accessorList == null)
            {
                return;
            }

            if (accessorList.Accessors.Any(f => !f.IsAutoImplemented()))
            {
                return;
            }

            ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(propertyDeclaration.Type, context.CancellationToken);

            if (typeSymbol?.IsErrorType() != false)
            {
                return;
            }

            if (!context.SemanticModel.IsDefaultValue(typeSymbol, value, context.CancellationToken))
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveRedundantAutoPropertyInitialization, value);
        }
예제 #11
0
 public static BasePropertyDeclarationSyntax WithAccessorList(this BasePropertyDeclarationSyntax node, AccessorListSyntax accessorList)
 {
     switch (node.Kind())
     {
         case SyntaxKind.IndexerDeclaration:
             return ((IndexerDeclarationSyntax)node).WithAccessorList(accessorList);
         case SyntaxKind.PropertyDeclaration:
             return (((PropertyDeclarationSyntax)node)).WithAccessorList(accessorList);
     }
     throw new NotImplementedException();
 }
        protected static BlockSyntax GetBodyFromSingleGetAccessor(AccessorListSyntax accessorList)
        {
            if (accessorList != null &&
                accessorList.Accessors.Count == 1 &&
                accessorList.Accessors[0].AttributeLists.Count == 0 &&
                accessorList.Accessors[0].IsKind(SyntaxKind.GetAccessorDeclaration))
            {
                return(accessorList.Accessors[0].Body);
            }

            return(null);
        }
예제 #13
0
        private static bool CanRefactor(AccessorListSyntax accessorList)
        {
            if (accessorList?.Accessors.Count == 1)
            {
                AccessorDeclarationSyntax accessor = accessorList.Accessors[0];

                return(accessor.IsKind(SyntaxKind.GetAccessorDeclaration) &&
                       CanRefactor(accessor.Body));
            }

            return(false);
        }
        public static ExpressionSyntax GetReturnExpression(AccessorListSyntax accessorList)
        {
            AccessorDeclarationSyntax accessor = accessorList?.Accessors.SingleOrDefault(shouldThrow: false);

            if (accessor?.Kind() == SyntaxKind.GetAccessorDeclaration &&
                !accessor.AttributeLists.Any())
            {
                return(GetReturnExpression(accessor.Body));
            }

            return(default(ExpressionSyntax));
        }
예제 #15
0
        protected static AccessorDeclarationSyntax GetSingleGetAccessor(AccessorListSyntax accessorList)
        {
            if (accessorList != null &&
                accessorList.Accessors.Count == 1 &&
                accessorList.Accessors[0].AttributeLists.Count == 0 &&
                accessorList.Accessors[0].IsKind(SyntaxKind.GetAccessorDeclaration))
            {
                return accessorList.Accessors[0];
            }

            return null;
        }
        public static AccessorListSyntax ExpandAccessorList(AccessorListSyntax accessorList)
        {
            IEnumerable <AccessorDeclarationSyntax> accessors = accessorList
                                                                .Accessors
                                                                .Select(f => f.WithBody(Block()).WithoutSemicolonToken());

            AccessorListSyntax newAccessorList = AccessorList(List(accessors));

            return(newAccessorList
                   .RemoveWhitespaceOrEndOfLineTrivia()
                   .WithCloseBraceToken(newAccessorList.CloseBraceToken.WithLeadingTrivia(NewLine())));
        }
        private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(List(CreateAccessors(propertyDeclaration)));

            accessorList = Remover.RemoveWhitespaceOrEndOfLine(accessorList)
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(CSharpFactory.NewLineTrivia()));

            return(propertyDeclaration
                   .WithoutInitializer()
                   .WithoutSemicolonToken()
                   .WithAccessorList(accessorList));
        }
        private static bool TryHandleAccessorList(SyntaxNodeAnalysisContext context, AccessorListSyntax accessorList)
        {
            if (accessorList == null)
            {
                return false;
            }

            var accessors = accessorList.Accessors;
            if (accessors.Count != 1)
            {
                return false;
            }

            var accessor = accessors[0];
            if (!accessor.IsKind(SyntaxKind.GetAccessorDeclaration))
            {
                return false;
            }

            if (accessor.AttributeLists.Count != 0)
            {
                return false;
            }

            var accessorBody = accessor.Body;
            if (accessorBody == null)
            {
                return false;
            }

            var statements = accessorBody.Statements;
            if (statements.Count == 0)
            {
                return false;
            }

            var returnStatement = statements[0] as ReturnStatementSyntax;
            if (returnStatement == null ||
                returnStatement.Expression == null)
            {
                return false;
            }

            FadeOutToken(context, accessorList.OpenBraceToken);
            FadeOutToken(context, accessor.Keyword);
            FadeOutToken(context, accessorBody.OpenBraceToken);
            FadeOutToken(context, returnStatement.ReturnKeyword);
            FadeOutToken(context, accessorBody.CloseBraceToken);
            FadeOutToken(context, accessorList.CloseBraceToken);

            return true;
        }
예제 #19
0
        public static Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            AccessorListSyntax accessorList = AccessorList();

            if (propertyDeclaration.ExpressionBody != null)
            {
                accessorList = accessorList
                               .AddAccessors(AutoGetAccessorDeclaration());
            }
            else
            {
                AccessorDeclarationSyntax getter = propertyDeclaration.Getter();
                if (getter != null)
                {
                    if (SyntaxAccessibility.GetExplicitAccessibility(getter) == Accessibility.Private)
                    {
                        getter = SyntaxAccessibility.WithExplicitAccessibility(getter, Accessibility.Protected);
                    }

                    accessorList = accessorList.AddAccessors(getter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(SemicolonToken()));
                }

                AccessorDeclarationSyntax setter = propertyDeclaration.Setter();
                if (setter != null)
                {
                    if (SyntaxAccessibility.GetExplicitAccessibility(setter) == Accessibility.Private)
                    {
                        setter = SyntaxAccessibility.WithExplicitAccessibility(setter, Accessibility.Protected);
                    }

                    accessorList = accessorList.AddAccessors(setter
                                                             .WithBody(null)
                                                             .WithSemicolonToken(SemicolonToken()));
                }
            }

            PropertyDeclarationSyntax newNode = propertyDeclaration
                                                .WithExpressionBody(null)
                                                .WithSemicolonToken(default(SyntaxToken))
                                                .WithAccessorList(accessorList)
                                                .InsertModifier(SyntaxKind.AbstractKeyword)
                                                .RemoveModifier(SyntaxKind.VirtualKeyword)
                                                .WithTriviaFrom(propertyDeclaration)
                                                .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(propertyDeclaration, newNode, cancellationToken));
        }
        public static PropertyDeclarationSyntax CreateAutoProperty(PropertyDeclarationSyntax property, EqualsValueClauseSyntax initializer)
        {
            AccessorListSyntax accessorList = property.AccessorList;

            if (accessorList != null)
            {
                SyntaxList <AccessorDeclarationSyntax> newAccessors = accessorList
                                                                      .Accessors
                                                                      .Select(accessor =>
                {
                    accessor = accessor.Update(
                        attributeLists: accessor.AttributeLists,
                        modifiers: accessor.Modifiers,
                        keyword: accessor.Keyword,
                        body: default(BlockSyntax),
                        expressionBody: default(ArrowExpressionClauseSyntax),
                        semicolonToken: SemicolonToken());

                    return(accessor.WithTriviaFrom(accessor));
                })
                                                                      .ToSyntaxList();

                accessorList = accessorList.WithAccessors(newAccessors);
            }
            else
            {
                accessorList = AccessorList(AutoGetAccessorDeclaration())
                               .WithTriviaFrom(property.ExpressionBody);
            }

            if (accessorList
                .DescendantTrivia()
                .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
            {
                accessorList = accessorList.RemoveWhitespace();
            }

            PropertyDeclarationSyntax newProperty = property.Update(
                attributeLists: property.AttributeLists,
                modifiers: property.Modifiers,
                type: property.Type,
                explicitInterfaceSpecifier: property.ExplicitInterfaceSpecifier,
                identifier: property.Identifier.WithTrailingTrivia(Space),
                accessorList: accessorList,
                expressionBody: default(ArrowExpressionClauseSyntax),
                initializer: initializer,
                semicolonToken: (initializer != null) ? SemicolonToken() : default(SyntaxToken));

            return(newProperty
                   .WithTriviaFrom(property)
                   .WithFormatterAnnotation());
        }
예제 #21
0
        public static Task <Document> RefactorAsync(
            Document document,
            PropertyDeclarationSyntax propertyDeclaration,
            CancellationToken cancellationToken = default)
        {
            AccessorListSyntax accessorList = AccessorList(List(ExpandProperty()));

            accessorList = accessorList
                           .RemoveWhitespace()
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));

            PropertyDeclarationSyntax newPropertyDeclaration = propertyDeclaration
                                                               .WithInitializer(null)
                                                               .WithSemicolonToken(default(SyntaxToken))
                                                               .WithAccessorList(accessorList);

            newPropertyDeclaration = newPropertyDeclaration
                                     .WithModifiers(newPropertyDeclaration.Modifiers.Replace(SyntaxKind.AbstractKeyword, SyntaxKind.VirtualKeyword))
                                     .WithTriviaFrom(propertyDeclaration)
                                     .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(propertyDeclaration, newPropertyDeclaration, cancellationToken));

            IEnumerable <AccessorDeclarationSyntax> ExpandProperty()
            {
                foreach (AccessorDeclarationSyntax accessor in propertyDeclaration.AccessorList.Accessors)
                {
                    if (accessor.IsKind(SyntaxKind.GetAccessorDeclaration))
                    {
                        ExpressionSyntax value = propertyDeclaration.Initializer?.Value;

                        if (value != null)
                        {
                            yield return(accessor
                                         .WithBody(Block(ReturnStatement(value)))
                                         .WithSemicolonToken(default(SyntaxToken)));

                            continue;
                        }
                    }

                    BlockSyntax body = Block(
                        OpenBraceToken(),
                        List <StatementSyntax>(),
                        CloseBraceToken().WithNavigationAnnotation());

                    yield return(accessor
                                 .WithBody(body)
                                 .WithSemicolonToken(default(SyntaxToken)));
                }
            }
        }
예제 #22
0
        private static PropertyDeclarationSyntax ExpandProperty(PropertyDeclarationSyntax propertyDeclaration)
        {
            AccessorListSyntax accessorList = AccessorList(List(CreateAccessors(propertyDeclaration)));

            accessorList = accessorList
                           .RemoveWhitespace()
                           .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));

            return(propertyDeclaration
                   .WithInitializer(null)
                   .WithSemicolonToken(default(SyntaxToken))
                   .WithAccessorList(accessorList));
        }
        public static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
        {
            var propertyDeclaration = (PropertyDeclarationSyntax)context.Node;

            AccessorListSyntax accessorList = propertyDeclaration.AccessorList;

            if (accessorList == null)
            {
                return;
            }

            Analyze(context, propertyDeclaration, accessorList.OpenBraceToken, accessorList.CloseBraceToken);
        }
예제 #24
0
        private static ArrowExpressionClauseSyntax TryGetExpressionBody(
            AccessorListSyntax accessorList, ParseOptions options)
        {
            if (accessorList.Accessors.Count == 1)
            {
                var accessor = accessorList.Accessors[0];
                if (accessor.IsKind(SyntaxKind.GetAccessorDeclaration))
                {
                    return(TryGetExpressionBody(accessor, options));
                }
            }

            return(null);
        }
        private static AccessorDeclarationSyntax GetAccessor(this AccessorListSyntax accessorList, SyntaxKind accessorKind)
        {
            SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

            for (int i = 0; i < accessors.Count; i++)
            {
                if (accessors[i].IsKind(accessorKind))
                {
                    return(accessors[i]);
                }
            }

            return(null);
        }
        static SyntaxToken GetSemicolon(AccessorListSyntax accessorList)
        {
            var semicolon = ((ReturnStatementSyntax)accessorList.Accessors[0].Body.Statements[0]).SemicolonToken;

            var trivia = semicolon.TrailingTrivia.AsEnumerable();

            trivia = trivia.Where(t => !t.IsKind(SyntaxKind.EndOfLineTrivia));

            var closeBraceTrivia = accessorList.CloseBraceToken.TrailingTrivia.AsEnumerable();

            trivia = trivia.Concat(closeBraceTrivia);

            return(semicolon.WithTrailingTrivia(trivia));
        }
        private static AccessorListSyntax CreateAccessorList(ExpressionSyntax expression)
        {
            BlockSyntax block = CreateBlock(expression);

            AccessorListSyntax accessorList = AccessorList(Getter(block));

            if (expression.IsSingleLine())
            {
                accessorList = Remover.RemoveWhitespaceOrEndOfLine(accessorList)
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLineTrivia()));
            }

            return(accessorList);
        }
예제 #28
0
 public static PropertyDeclarationSyntax PropertyDeclaration(SyntaxList<AttributeListSyntax> attributeLists, SyntaxTokenList modifiers, TypeSyntax type, ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier, SyntaxToken identifier, AccessorListSyntax accessorList, ArrowExpressionClauseSyntax expressionBody, EqualsValueClauseSyntax initializer, SyntaxToken semicolonToken)
 {
     return PropertyDeclaration(
         attributeLists,
         modifiers,
         refKeyword: default(SyntaxToken),
         type: type,
         explicitInterfaceSpecifier: explicitInterfaceSpecifier, 
         identifier: identifier, 
         accessorList: accessorList, 
         expressionBody: expressionBody, 
         initializer: initializer, 
         semicolonToken: semicolonToken);
 }
예제 #29
0
        public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            VisitAttributeLists(node.AttributeLists);

            if (!ShouldVisit)
            {
                return;
            }

            TypeSyntax type = node.Type;

            if (type != null)
            {
                VisitType(type);
            }

            if (!ShouldVisit)
            {
                return;
            }

            EqualsValueClauseSyntax initializer = node.Initializer;

            if (initializer != null)
            {
                VisitEqualsValueClause(initializer);
            }

            if (!ShouldVisit)
            {
                return;
            }

            AccessorListSyntax accessorList = node.AccessorList;

            if (accessorList != null)
            {
                VisitAccessorList(accessorList);
            }
            else
            {
                ArrowExpressionClauseSyntax expressionBody = node.ExpressionBody;

                if (expressionBody != null)
                {
                    VisitArrowExpressionClause(expressionBody);
                }
            }
        }
예제 #30
0
        public override void VisitIndexerDeclaration(IndexerDeclarationSyntax node)
        {
            VisitAttributeLists(node.AttributeLists);

            if (!ShouldVisit)
            {
                return;
            }

            TypeSyntax type = node.Type;

            if (type != null)
            {
                VisitType(type);
            }

            if (!ShouldVisit)
            {
                return;
            }

            BracketedParameterListSyntax parameterList = node.ParameterList;

            if (node != null)
            {
                VisitBracketedParameterList(parameterList);
            }

            if (!ShouldVisit)
            {
                return;
            }

            AccessorListSyntax accessorList = node.AccessorList;

            if (accessorList != null)
            {
                VisitAccessorList(accessorList);
            }
            else
            {
                ArrowExpressionClauseSyntax expressionBody = node.ExpressionBody;

                if (expressionBody != null)
                {
                    VisitArrowExpressionClause(expressionBody);
                }
            }
        }
        private static Task <Document> AddNewLineBeforeAccessorOfFullPropertyAsync(
            AccessorDeclarationSyntax accessorDeclaration,
            Document document,
            CancellationToken cancellationToken)
        {
            AccessorDeclarationSyntax newAccessorDeclaration = accessorDeclaration.AppendEndOfLineToLeadingTrivia();

            var accessorList = (AccessorListSyntax)accessorDeclaration.Parent;

            AccessorListSyntax newAccessorList = accessorList
                                                 .WithAccessors(accessorList.Accessors.Replace(accessorDeclaration, newAccessorDeclaration))
                                                 .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(accessorList, newAccessorList, cancellationToken));
        }
        private static Task <Document> RemoveNewLinesFromAccessorListAsync(
            Document document,
            AccessorListSyntax accessorList,
            CancellationToken cancellationToken)
        {
            TextSpan span = TextSpan.FromBounds(
                accessorList.GetFirstToken().GetPreviousToken().Span.End,
                accessorList.CloseBraceToken.SpanStart);

            SyntaxNode newNode = accessorList.Parent
                                 .RemoveWhitespace(span)
                                 .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(accessorList.Parent, newNode, cancellationToken));
        }
예제 #33
0
        private static AccessorListSyntax CreateAccessorList(ExpressionSyntax expression, SyntaxToken semicolon)
        {
            BlockSyntax block = CreateBlock(expression, semicolon);

            AccessorListSyntax accessorList = AccessorList(GetAccessorDeclaration(block));

            if (expression.IsSingleLine())
            {
                accessorList = accessorList
                               .RemoveWhitespace()
                               .WithCloseBraceToken(accessorList.CloseBraceToken.WithLeadingTrivia(NewLine()));
            }

            return(accessorList);
        }
        private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, AccessorListSyntax accessorList, ExpressionSyntax expression)
        {
            context.ReportDiagnostic(
                DiagnosticDescriptors.UseExpressionBodiedMember,
                accessorList);

            SyntaxNode parent = expression.Parent;

            if (parent.IsKind(SyntaxKind.ReturnStatement))
            {
                context.ReportToken(FadeOutDescriptor, ((ReturnStatementSyntax)parent).ReturnKeyword);
            }

            context.ReportBraces(FadeOutDescriptor, accessorList);
        }
예제 #35
0
 public static IndexerDeclarationSyntax IndexerDeclaration(
     SyntaxList<AttributeListSyntax> attributeLists,
     SyntaxTokenList modifiers,
     TypeSyntax type,
     ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier,
     BracketedParameterListSyntax parameterList,
     AccessorListSyntax accessorList)
 {
     return SyntaxFactory.IndexerDeclaration(
         attributeLists: attributeLists,
         modifiers: modifiers,
         type: type,
         explicitInterfaceSpecifier: explicitInterfaceSpecifier,
         parameterList: parameterList,
         accessorList: accessorList,
         expressionBody: default(ArrowExpressionClauseSyntax));
 }
예제 #36
0
 public static IndexerDeclarationSyntax IndexerDeclaration(
     SyntaxList<AttributeListSyntax> attributeLists, 
     SyntaxTokenList modifiers, 
     TypeSyntax type, 
     ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier,
     SyntaxToken thisKeyword, 
     BracketedParameterListSyntax parameterList, 
     AccessorListSyntax accessorList, 
     ArrowExpressionClauseSyntax expressionBody, 
     SyntaxToken semicolonToken)
 {
     return SyntaxFactory.IndexerDeclaration(
         attributeLists: attributeLists,
         modifiers: modifiers,
         refKeyword: default(SyntaxToken),
         type: type,
         explicitInterfaceSpecifier: explicitInterfaceSpecifier,
         thisKeyword: thisKeyword,
         parameterList: parameterList,
         accessorList: accessorList,
         expressionBody: expressionBody,
         semicolonToken: semicolonToken);
 }
예제 #37
0
        public void VisitAccessorList(AccessorListSyntax node)
        {
            if (node == null)
                throw new ArgumentNullException("node");

            node.Validate();

            bool isSimple =
                _writer.Configuration.LineBreaksAndWrapping.Other.PlaceAbstractAutoPropertyIndexerEventDeclarationOnSingleLine &&
                node.Accessors.All(p =>
                    (!HasExtras(p) || _writer.Configuration.LineBreaksAndWrapping.Other.PlaceSingleLineAccessorAttributeOnSameLine) &&
                    p.Body == null
                );

            if (!isSimple)
            {
                isSimple =
                    _writer.Configuration.LineBreaksAndWrapping.Other.PlaceSimplePropertyIndexerEventDeclarationOnSingleLine &&
                    node.Accessors.All(p =>
                        (!HasExtras(p) || _writer.Configuration.LineBreaksAndWrapping.Other.PlaceSingleLineAccessorAttributeOnSameLine) &&
                        IsSimpleBody(p.Body)
                    );
            }

            if (isSimple)
            {
                _writer.WriteSpace();
                _writer.WriteSyntax(Syntax.OpenBrace);
                _writer.WriteSpace();
            }
            else
            {
                _writer.BeginBlock();
            }

            bool hadOne = false;

            foreach (var accessor in node.Accessors)
            {
                if (hadOne && isSimple)
                    _writer.WriteSpace();
                else
                    hadOne = true;

                accessor.Accept(this);
            }

            if (isSimple)
            {
                _writer.WriteSpace();
                _writer.WriteSyntax(Syntax.CloseBrace);
                _writer.WriteLine();
            }
            else
            {
                _writer.EndBlock();
            }
        }
예제 #38
0
        public static SyntaxNode TryGetEffectiveGetterBody(ArrowExpressionClauseSyntax propertyBody, AccessorListSyntax accessorList)
        {
            if (propertyBody != null)
            {
                return propertyBody.Expression;
            }

            return accessorList?.Accessors.Where(a => a.IsKind(SyntaxKind.GetAccessorDeclaration)).FirstOrDefault()?.Body;
        }
 private static ExpressionSyntax GetExpression(AccessorListSyntax accessorList)
 {
     return ((ReturnStatementSyntax)accessorList.Accessors[0].Body.Statements[0]).Expression;
 }
        private static AccessorDeclarationSyntax GetNewAccessor(AccessorListSyntax accessorList, AccessorDeclarationSyntax firstAccessor, AccessorDeclarationSyntax secondAccessor)
        {
            var newLeadingTrivia = GetLeadingTriviaWithoutLeadingBlankLines(secondAccessor);
            if (AccessorsAreOnTheSameLine(firstAccessor, secondAccessor))
            {
                var leadingWhitespace = firstAccessor.GetLeadingTrivia().Where(x => x.IsKind(SyntaxKind.WhitespaceTrivia));
                newLeadingTrivia = SyntaxFactory.TriviaList(TriviaHelper.MergeTriviaLists(newLeadingTrivia, SyntaxTriviaList.Empty.AddRange(leadingWhitespace)));
            }

            var newAccessor = accessorList.Accessors[1]
                .WithBody(secondAccessor.Body)
                .WithLeadingTrivia(newLeadingTrivia);

            return newAccessor;
        }
예제 #41
0
        private AccessorListSyntax UpdateAccessorList(AccessorListSyntax accessorListSyntax, TypeSyntax type)
        {
            var specialType = _typeTranslation.FirstOrDefault(tt => PrettyTypeName(tt.ActualType) == type.ToFullString().Replace("global::", ""));

            if (specialType != null)
            {
                return accessorListSyntax.WithAccessors(
                    SyntaxFactory.List<AccessorDeclarationSyntax>(
                        accessorListSyntax.Accessors.Select(a =>
                            a.WithBody(UpdateMethodBody(
                                a.Body,
                                type,
                                SyntaxFactory.ParameterList(
                                    SyntaxFactory.SeparatedList<ParameterSyntax>( new [] { SyntaxFactory.Parameter(SyntaxFactory.ParseToken("value")).WithType(type) }))))))
                    );
            }

            return accessorListSyntax;
        }
예제 #42
0
        private static ArrowExpressionClauseSyntax TryGetExpressionBody(
            AccessorListSyntax accessorList, ParseOptions options)
        {
            if (accessorList.Accessors.Count == 1)
            {
                var accessor = accessorList.Accessors[0];
                if (accessor.IsKind(SyntaxKind.GetAccessorDeclaration))
                {
                    return TryGetExpressionBody(accessor, options);
                }
            }

            return null;
        }
        private static SyntaxToken GetSemicolon(AccessorListSyntax accessorList)
        {
            var semicolon = ((ReturnStatementSyntax)accessorList.Accessors[0].Body.Statements[0]).SemicolonToken;

            var trivia = semicolon.TrailingTrivia.AsEnumerable();
            trivia = trivia.Where(t => !t.IsKind(SyntaxKind.EndOfLineTrivia));

            // Append trailing trivia from the closing brace.
            var closeBraceTrivia = accessorList.CloseBraceToken.TrailingTrivia.AsEnumerable();
            trivia = trivia.Concat(closeBraceTrivia);

            return semicolon.WithTrailingTrivia(trivia);
        }
        private static ExpressionSyntax GetExpression(AccessorListSyntax accessorList, out SyntaxTriviaList leadingTrivia)
        {
            var returnStatement = (ReturnStatementSyntax)accessorList.Accessors[0].Body.Statements[0];
            leadingTrivia = returnStatement.GetLeadingTrivia();

            // TODO: Concatenate any trivia between the return keyword and the expression?

            return returnStatement.Expression;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitAccessorList(AccessorListSyntax node)
 {
     this.OnNodeVisited(node, this.type.IsInstanceOfType(node));
     base.VisitAccessorList(node);
 }
예제 #46
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitAccessorList(AccessorListSyntax node)
 {
     this.OnNodeVisited(node);
     if (!this.traverseRootOnly) base.VisitAccessorList(node);
 }
예제 #47
0
 public IndexerDeclarationSyntax Update(SyntaxList<AttributeListSyntax> attributeLists, SyntaxTokenList modifiers, TypeSyntax type, ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier, SyntaxToken thisKeyword, BracketedParameterListSyntax parameterList, AccessorListSyntax accessorList, ArrowExpressionClauseSyntax expressionBody, SyntaxToken semicolonToken)
 {
     return Update(attributeLists, modifiers, this.RefKeyword, type, explicitInterfaceSpecifier, thisKeyword, parameterList, accessorList, expressionBody, semicolonToken);
 }
예제 #48
0
 public static IndexerDeclarationSyntax Update(
     this IndexerDeclarationSyntax syntax,
     SyntaxList<AttributeListSyntax> attributeLists,
     SyntaxTokenList modifiers,
     TypeSyntax type,
     ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier,
     SyntaxToken thisKeyword,
     BracketedParameterListSyntax parameterList,
     AccessorListSyntax accessorList)
 {
     return syntax.Update(
         attributeLists,
         modifiers,
         default(SyntaxToken),
         type,
         explicitInterfaceSpecifier,
         thisKeyword,
         parameterList,
         accessorList,
         default(ArrowExpressionClauseSyntax),
         default(SyntaxToken));
 }
예제 #49
0
 public PropertyDeclarationSyntax Update(SyntaxList<AttributeListSyntax> attributeLists, SyntaxTokenList modifiers, TypeSyntax type, ExplicitInterfaceSpecifierSyntax explicitInterfaceSpecifier, SyntaxToken identifier, AccessorListSyntax accessorList, ArrowExpressionClauseSyntax expressionBody, EqualsValueClauseSyntax initializer, SyntaxToken semicolonToken)
 {
     return Update(attributeLists, modifiers, this.RefKeyword, type, explicitInterfaceSpecifier, identifier, accessorList, expressionBody, initializer, semicolonToken);
 }
 public AccessorListTranslation(AccessorListSyntax syntax, SyntaxTranslation parent) : base(syntax, parent)
 {
     Accessors = syntax.Accessors.Get<AccessorDeclarationSyntax, AccessorDeclarationTranslation>(this);
 }
        private static AccessorDeclarationSyntax GetNewAccessor(AccessorListSyntax accessorList, AccessorDeclarationSyntax firstAccessor, AccessorDeclarationSyntax secondAccessor)
        {
            var newLeadingTrivia = GetLeadingTriviaWithoutLeadingBlankLines(secondAccessor);
            if (AccessorsAreOnTheSameLine(firstAccessor, secondAccessor))
            {
                var leadingWhitespace = firstAccessor.GetLeadingTrivia().Where(x => x.IsKind(SyntaxKind.WhitespaceTrivia)).ToList();
                newLeadingTrivia = SyntaxFactory.TriviaList(TriviaHelper.MergeTriviaLists(newLeadingTrivia, leadingWhitespace));
            }

            var newAccessor = accessorList.Accessors[1]
                .WithBody(secondAccessor.Body)
                .WithLeadingTrivia(newLeadingTrivia);

            if (secondAccessor.GetFirstToken().HasLeadingBlankLines())
            {
                newAccessor.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed);
            }

            return newAccessor;
        }