private static int GetIndentationSteps(IndentationOptions indentationOptions, SyntaxTree syntaxTree, SyntaxTriviaList leadingTrivia)
        {
            var triviaSpan = syntaxTree.GetLineSpan(leadingTrivia.FullSpan);

            // There is no indentation when the leading trivia doesn't begin at the start of the line.
            if ((triviaSpan.StartLinePosition == triviaSpan.EndLinePosition) && (triviaSpan.StartLinePosition.Character > 0))
            {
                return(0);
            }

            var builder = StringBuilderPool.Allocate();

            foreach (SyntaxTrivia trivia in leadingTrivia.Reverse())
            {
                if (!trivia.IsKind(SyntaxKind.WhitespaceTrivia))
                {
                    break;
                }

                builder.Insert(0, trivia.ToFullString());
            }

            var tabSize          = indentationOptions.TabSize;
            var indentationCount = 0;

            for (var i = 0; i < builder.Length; i++)
            {
                indentationCount += builder[i] == '\t' ? tabSize - (indentationCount % tabSize) : 1;
            }

            StringBuilderPool.ReturnAndFree(builder);

            return((indentationCount + (indentationOptions.IndentationSize / 2)) / indentationOptions.IndentationSize);
        }
        /// <summary>
        /// Gets a whitespace trivia containing the proper amount of indentation for new lines in the query of which the given token is a part.
        /// </summary>
        /// <param name="indentationOptions">The indentation options to use.</param>
        /// <param name="token">A token within a query expression.</param>
        /// <returns>A whitespace trivia containing the proper amount of indentation.</returns>
        internal static SyntaxTrivia GetQueryIndentationTrivia(IndentationOptions indentationOptions, SyntaxToken token)
        {
            var currentNode = token.Parent;
            while (!currentNode.IsKind(SyntaxKind.QueryExpression))
            {
                currentNode = currentNode.Parent;
            }

            return GetQueryIndentationTrivia(indentationOptions, (QueryExpressionSyntax)currentNode);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets a whitespace trivia containing the proper amount of indentation for new lines in the query of which the given token is a part.
        /// </summary>
        /// <param name="indentationOptions">The indentation options to use.</param>
        /// <param name="token">A token within a query expression.</param>
        /// <returns>A whitespace trivia containing the proper amount of indentation.</returns>
        internal static SyntaxTrivia GetQueryIndentationTrivia(IndentationOptions indentationOptions, SyntaxToken token)
        {
            var currentNode = token.Parent;

            while (!currentNode.IsKind(SyntaxKind.QueryExpression))
            {
                currentNode = currentNode.Parent;
            }

            return(GetQueryIndentationTrivia(indentationOptions, (QueryExpressionSyntax)currentNode));
        }
        /// <summary>
        /// Gets a whitespace trivia containing the proper amount of indentation for new lines in the given query.
        /// </summary>
        /// <param name="indentationOptions">The indentation options to use.</param>
        /// <param name="queryExpression">The query expression to determine indentation from.</param>
        /// <returns>A whitespace trivia containing the proper amount of indentation.</returns>
        internal static SyntaxTrivia GetQueryIndentationTrivia(IndentationOptions indentationOptions, QueryExpressionSyntax queryExpression)
        {
            var firstTokenOnTextLine = IndentationHelper.GetFirstTokenOnTextLine(queryExpression.FromClause.FromKeyword);
            var indentationSteps = IndentationHelper.GetIndentationSteps(indentationOptions, firstTokenOnTextLine);

            // add an extra indentation step when the first from clause is not properly indented yet
            if (!firstTokenOnTextLine.IsKind(SyntaxKind.OpenParenToken) && (firstTokenOnTextLine != queryExpression.FromClause.FromKeyword))
            {
                indentationSteps++;
            }

            return IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, indentationSteps);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets a whitespace trivia containing the proper amount of indentation for new lines in the given query.
        /// </summary>
        /// <param name="indentationOptions">The indentation options to use.</param>
        /// <param name="queryExpression">The query expression to determine indentation from.</param>
        /// <returns>A whitespace trivia containing the proper amount of indentation.</returns>
        internal static SyntaxTrivia GetQueryIndentationTrivia(IndentationOptions indentationOptions, QueryExpressionSyntax queryExpression)
        {
            var firstTokenOnTextLine = IndentationHelper.GetFirstTokenOnTextLine(queryExpression.FromClause.FromKeyword);
            var indentationSteps     = IndentationHelper.GetIndentationSteps(indentationOptions, firstTokenOnTextLine);

            // add an extra indentation step when the first from clause is not properly indented yet
            if (!firstTokenOnTextLine.IsKind(SyntaxKind.OpenParenToken) && (firstTokenOnTextLine != queryExpression.FromClause.FromKeyword))
            {
                indentationSteps++;
            }

            return(IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, indentationSteps));
        }
        /// <summary>
        /// Generate a new indentation string.
        /// </summary>
        /// <param name="indentationOptions">The indentation options to use.</param>
        /// <param name="indentationSteps">The number of indentation steps.</param>
        /// <returns>A string containing the amount of whitespace needed for the given indentation steps.</returns>
        public static string GenerateIndentationString(IndentationOptions indentationOptions, int indentationSteps)
        {
            string result;
            var indentationCount = indentationSteps * indentationOptions.IndentationSize;
            if (indentationOptions.UseTabs)
            {
                var tabCount = indentationCount / indentationOptions.TabSize;
                var spaceCount = indentationCount % indentationOptions.TabSize;
                result = new string('\t', tabCount) + new string(' ', spaceCount);
            }
            else
            {
                result = new string(' ', indentationCount);
            }

            return result;
        }
        /// <summary>
        /// Generate a new indentation string.
        /// </summary>
        /// <param name="indentationOptions">The indentation options to use.</param>
        /// <param name="indentationSteps">The number of indentation steps.</param>
        /// <returns>A string containing the amount of whitespace needed for the given indentation steps.</returns>
        public static string GenerateIndentationString(IndentationOptions indentationOptions, int indentationSteps)
        {
            string result;
            var    indentationCount = indentationSteps * indentationOptions.IndentationSize;

            if (indentationOptions.UseTabs)
            {
                var tabCount   = indentationCount / indentationOptions.TabSize;
                var spaceCount = indentationCount % indentationOptions.TabSize;
                result = new string('\t', tabCount) + new string(' ', spaceCount);
            }
            else
            {
                result = new string(' ', indentationCount);
            }

            return(result);
        }
        private static int GetIndentationSteps(IndentationOptions indentationOptions, SyntaxTree syntaxTree, SyntaxTriviaList leadingTrivia)
        {
            var triviaSpan = syntaxTree.GetLineSpan(leadingTrivia.FullSpan);

            // There is no indentation when the leading trivia doesn't begin at the start of the line.
            if ((triviaSpan.StartLinePosition == triviaSpan.EndLinePosition) && (triviaSpan.StartLinePosition.Character > 0))
            {
                return 0;
            }

            var builder = StringBuilderPool.Allocate();

            foreach (SyntaxTrivia trivia in leadingTrivia.Reverse())
            {
                if (!trivia.IsKind(SyntaxKind.WhitespaceTrivia))
                {
                    break;
                }

                builder.Insert(0, trivia.ToFullString());
            }

            var tabSize = indentationOptions.TabSize;
            var indentationCount = 0;
            for (var i = 0; i < builder.Length; i++)
            {
                indentationCount += builder[i] == '\t' ? tabSize - (indentationCount % tabSize) : 1;
            }

            StringBuilderPool.ReturnAndFree(builder);

            return (indentationCount + (indentationOptions.IndentationSize / 2)) / indentationOptions.IndentationSize;
        }
 /// <summary>
 /// Generates a whitespace trivia with the requested indentation.
 /// </summary>
 /// <param name="indentationOptions">The indentation options to use.</param>
 /// <param name="indentationSteps">The amount of indentation steps.</param>
 /// <returns>A <see cref="SyntaxTrivia"/> containing the indentation whitespace.</returns>
 public static SyntaxTrivia GenerateWhitespaceTrivia(IndentationOptions indentationOptions, int indentationSteps)
 {
     return SyntaxFactory.Whitespace(GenerateIndentationString(indentationOptions, indentationSteps));
 }
 /// <summary>
 /// Gets the number of steps that the given token is indented.
 /// </summary>
 /// <param name="indentationOptions">The indentation options to use.</param>
 /// <param name="token">The token to inspect.</param>
 /// <returns>The number of steps that the token is indented.</returns>
 public static int GetIndentationSteps(IndentationOptions indentationOptions, SyntaxToken token)
 {
     return GetIndentationSteps(indentationOptions, token.SyntaxTree, token.LeadingTrivia);
 }
 /// <summary>
 /// Gets the number of steps that the given node is indented.
 /// </summary>
 /// <param name="indentationOptions">The indentation options to use.</param>
 /// <param name="node">The node to inspect.</param>
 /// <returns>The number of steps that the node is indented.</returns>
 public static int GetIndentationSteps(IndentationOptions indentationOptions, SyntaxNode node)
 {
     return GetIndentationSteps(indentationOptions, node.SyntaxTree, node.GetLeadingTrivia());
 }
 /// <summary>
 /// Generates a whitespace trivia with the requested indentation.
 /// </summary>
 /// <param name="indentationOptions">The indentation options to use.</param>
 /// <param name="indentationSteps">The amount of indentation steps.</param>
 /// <returns>A <see cref="SyntaxTrivia"/> containing the indentation whitespace.</returns>
 public static SyntaxTrivia GenerateWhitespaceTrivia(IndentationOptions indentationOptions, int indentationSteps)
 {
     return(SyntaxFactory.Whitespace(GenerateIndentationString(indentationOptions, indentationSteps)));
 }
 /// <summary>
 /// Gets the number of steps that the given token is indented.
 /// </summary>
 /// <param name="indentationOptions">The indentation options to use.</param>
 /// <param name="token">The token to inspect.</param>
 /// <returns>The number of steps that the token is indented.</returns>
 public static int GetIndentationSteps(IndentationOptions indentationOptions, SyntaxToken token)
 {
     return(GetIndentationSteps(indentationOptions, token.SyntaxTree, token.LeadingTrivia));
 }
 /// <summary>
 /// Gets the number of steps that the given node is indented.
 /// </summary>
 /// <param name="indentationOptions">The indentation options to use.</param>
 /// <param name="node">The node to inspect.</param>
 /// <returns>The number of steps that the node is indented.</returns>
 public static int GetIndentationSteps(IndentationOptions indentationOptions, SyntaxNode node)
 {
     return(GetIndentationSteps(indentationOptions, node.SyntaxTree, node.GetLeadingTrivia()));
 }