Example #1
0
            public override void AddIndentBlockOperations(List <IndentBlockOperation> list, SyntaxNode node, OptionSet optionSet, NextAction <IndentBlockOperation> nextOperation)
            {
                // these nodes should be from syntax tree from ITextSnapshot.
                Contract.Requires(node.SyntaxTree != null);
                Contract.Requires(node.SyntaxTree.GetText() != null);

                nextOperation.Invoke(list);

                ReplaceCaseIndentationRules(list, node);

                if (node is BaseParameterListSyntax ||
                    node is TypeArgumentListSyntax ||
                    node is TypeParameterListSyntax ||
                    node.IsKind(SyntaxKind.Interpolation))
                {
                    AddIndentBlockOperations(list, node);
                    return;
                }

                var argument = node as BaseArgumentListSyntax;

                if (argument != null &&
                    argument.Parent.Kind() != SyntaxKind.ThisConstructorInitializer &&
                    !IsBracketedArgumentListMissingBrackets(argument as BracketedArgumentListSyntax))
                {
                    AddIndentBlockOperations(list, argument);
                    return;
                }

                // only valid if the user has started to actually type a constructor initializer
                var constructorInitializer = node as ConstructorInitializerSyntax;

                if (constructorInitializer != null &&
                    constructorInitializer.ArgumentList.OpenParenToken.Kind() != SyntaxKind.None &&
                    !constructorInitializer.ThisOrBaseKeyword.IsMissing)
                {
                    var text = node.SyntaxTree.GetText();

                    // 3 different cases
                    // first case : this or base is the first token on line
                    // second case : colon is the first token on line
                    var colonIsFirstTokenOnLine      = !constructorInitializer.ColonToken.IsMissing && constructorInitializer.ColonToken.IsFirstTokenOnLine(text);
                    var thisOrBaseIsFirstTokenOnLine = !constructorInitializer.ThisOrBaseKeyword.IsMissing && constructorInitializer.ThisOrBaseKeyword.IsFirstTokenOnLine(text);

                    if (colonIsFirstTokenOnLine || thisOrBaseIsFirstTokenOnLine)
                    {
                        list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                                     constructorInitializer.ThisOrBaseKeyword,
                                     constructorInitializer.ArgumentList.OpenParenToken.GetNextToken(includeZeroWidth: true),
                                     constructorInitializer.ArgumentList.CloseParenToken.GetPreviousToken(includeZeroWidth: true),
                                     indentationDelta: 1,
                                     option: IndentBlockOption.RelativePosition));
                    }
                    else
                    {
                        // third case : none of them are the first token on the line
                        AddIndentBlockOperations(list, constructorInitializer.ArgumentList);
                    }
                }
            }
Example #2
0
 protected void SetAlignmentBlockOperation(
     List <IndentBlockOperation> list,
     SyntaxToken baseToken,
     SyntaxToken startToken,
     SyntaxToken endToken,
     IndentBlockOption option = IndentBlockOption.RelativePosition)
 {
     list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(baseToken, startToken, endToken, indentationDelta: 0, option: option));
 }
Example #3
0
            private static void AddIndentBlockOperations(List <IndentBlockOperation> list, SyntaxNode node)
            {
                // only add indent block operation if the base token is the first token on line
                var text      = node.SyntaxTree.GetText();
                var baseToken = node.Parent.GetFirstToken(includeZeroWidth: true);

                list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                             baseToken,
                             node.GetFirstToken(includeZeroWidth: true).GetNextToken(includeZeroWidth: true),
                             node.GetLastToken(includeZeroWidth: true).GetPreviousToken(includeZeroWidth: true),
                             indentationDelta: 1,
                             option: IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
            }
Example #4
0
        private void AddChangeSignatureIndentOperation(List <IndentBlockOperation> list, SyntaxNode node)
        {
            if (node.Parent != null)
            {
                var baseToken  = node.Parent.GetFirstToken();
                var startToken = node.GetFirstToken();
                var endToken   = node.GetLastToken();
                var span       = CommonFormattingHelpers.GetSpanIncludingTrailingAndLeadingTriviaOfAdjacentTokens(startToken, endToken);
                span = TextSpan.FromBounds(Math.Max(baseToken.Span.End, span.Start), span.End);

                list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(baseToken, startToken, endToken, span, indentationDelta: 1, option: IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
            }
        }
                /// <summary>
                /// Align all the wrapped parts of the expression with the token after 'return'.
                /// That way we get:
                ///
                /// return a == obj.a &amp;&amp;
                ///        b == obj.b &amp;&amp;
                ///        ...
                /// </summary>
                public override void AddIndentBlockOperations(
                    List <IndentBlockOperation> list, SyntaxNode node, OptionSet optionSet, NextAction <IndentBlockOperation> nextOperation)
                {
                    if (_syntaxFacts.IsReturnStatement(node))
                    {
                        var expr = _syntaxFacts.GetExpressionOfReturnStatement(node);
                        if (expr?.ChildNodesAndTokens().Count > 1)
                        {
                            list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                                         expr.GetFirstToken(),
                                         expr.GetFirstToken().GetNextToken(),
                                         node.GetLastToken(),
                                         indentationDelta: 0,
                                         option: IndentBlockOption.RelativePosition));

                            return;
                        }
                    }

                    nextOperation.Invoke(list);
                }
Example #6
0
        public override void AddIndentBlockOperations(
            List <IndentBlockOperation> list, SyntaxNode node, OptionSet optionSet, NextAction <IndentBlockOperation> nextOperation)
        {
            if (node.HasAnnotation(SpecializedFormattingAnnotation) &&
                node is ConditionalExpressionSyntax conditional)
            {
                var statement = conditional.FirstAncestorOrSelf <StatementSyntax>();
                if (statement != null)
                {
                    var baseToken = statement.GetFirstToken();

                    // we want to indent the ? and : in one level from the containing statement.
                    list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                                 baseToken, conditional.QuestionToken, conditional.WhenTrue.GetLastToken(),
                                 indentationDelta: 1, IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
                    list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
                                 baseToken, conditional.ColonToken, conditional.WhenFalse.GetLastToken(),
                                 indentationDelta: 1, IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
                    return;
                }
            }

            nextOperation.Invoke(list);
        }