Esempio n. 1
0
            protected override AdjustNewLinesOperation GetAdjustNewLinesOperationBetweenMembersAndUsings(
                SyntaxToken token1,
                SyntaxToken token2
                )
            {
                var previousToken = token1;
                var currentToken  = token2;

                // We are not between members or usings if the last token wasn't the end of a statement or if the current token
                // is the end of a scope.
                if (
                    (
                        previousToken.Kind() != SyntaxKind.SemicolonToken &&
                        previousToken.Kind() != SyntaxKind.CloseBraceToken
                    ) ||
                    currentToken.Kind() == SyntaxKind.CloseBraceToken
                    )
                {
                    return(null);
                }

                SyntaxNode previousMember = FormattingRangeHelper.GetEnclosingMember(previousToken);
                SyntaxNode nextMember     = FormattingRangeHelper.GetEnclosingMember(currentToken);

                // Is the previous statement an using directive? If so, treat it like a member to add
                // the right number of lines.
                if (
                    previousToken.Kind() == SyntaxKind.SemicolonToken &&
                    previousToken.Parent.Kind() == SyntaxKind.UsingDirective
                    )
                {
                    previousMember = previousToken.Parent;
                }

                if (previousMember == null || nextMember == null || previousMember == nextMember)
                {
                    return(null);
                }

                // If we have two members of the same kind, we won't insert a blank line
                if (previousMember.Kind() == nextMember.Kind())
                {
                    return(FormattingOperations.CreateAdjustNewLinesOperation(
                               1,
                               AdjustNewLinesOption.ForceLines
                               ));
                }

                // Force a blank line between the two nodes by counting the number of lines of
                // trivia and adding one to it.
                var triviaList = token1.TrailingTrivia.Concat(token2.LeadingTrivia);

                return(FormattingOperations.CreateAdjustNewLinesOperation(
                           GetNumberOfLines(triviaList) + 1,
                           AdjustNewLinesOption.ForceLines
                           ));
            }
        private AdjustNewLinesOperation GetAdjustNewLinesOperationBetweenMembers(SyntaxToken previousToken, SyntaxToken currentToken)
        {
            if (!FormattingRangeHelper.InBetweenTwoMembers(previousToken, currentToken))
            {
                return(null);
            }

            var previousMember = FormattingRangeHelper.GetEnclosingMember(previousToken);
            var nextMember     = FormattingRangeHelper.GetEnclosingMember(currentToken);

            if (previousMember == null || nextMember == null)
            {
                return(null);
            }

            // see whether first non whitespace trivia after before the current member is a comment or not
            var triviaList = currentToken.LeadingTrivia;
            var firstNonWhitespaceTrivia = triviaList.FirstOrDefault(trivia => !IsWhitespace(trivia));

            if (firstNonWhitespaceTrivia.IsRegularOrDocComment())
            {
                // the first one is a comment, add two more lines than existing number of lines
                var numberOfLines = GetNumberOfLines(triviaList);
                var numberOfLinesBeforeComment = GetNumberOfLines(triviaList.Take(triviaList.IndexOf(firstNonWhitespaceTrivia)));
                var addedLines = (numberOfLinesBeforeComment < 1) ? 2 : 1;
                return(CreateAdjustNewLinesOperation(numberOfLines + addedLines, AdjustNewLinesOption.ForceLines));
            }

            // If we have two members of the same kind, we won't insert a blank line if both members
            // have any content (e.g. accessors bodies, non-empty method bodies, etc.).
            if (previousMember.Kind() == nextMember.Kind())
            {
                // Easy cases:
                if (previousMember.Kind() == SyntaxKind.FieldDeclaration ||
                    previousMember.Kind() == SyntaxKind.EventFieldDeclaration)
                {
                    // Ensure that fields and events are each declared on a separate line.
                    return(CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.ForceLines));
                }

                // Don't insert a blank line between properties, indexers or events with no accessors
                if (previousMember is BasePropertyDeclarationSyntax previousProperty)
                {
                    var nextProperty = (BasePropertyDeclarationSyntax)nextMember;

                    if (previousProperty?.AccessorList?.Accessors.All(a => a.Body == null) == true &&
                        nextProperty?.AccessorList?.Accessors.All(a => a.Body == null) == true)
                    {
                        return(CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.PreserveLines));
                    }
                }

                // Don't insert a blank line between methods with no bodies
                if (previousMember is BaseMethodDeclarationSyntax previousMethod)
                {
                    var nextMethod = (BaseMethodDeclarationSyntax)nextMember;

                    if (previousMethod.Body == null &&
                        nextMethod.Body == null)
                    {
                        return(CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.PreserveLines));
                    }
                }
            }

            return(FormattingOperations.CreateAdjustNewLinesOperation(2 /* +1 for member itself and +1 for a blank line*/, AdjustNewLinesOption.ForceLines));
        }