Ejemplo n.º 1
0
        public static string AdjustIndentForXmlDocExteriorTrivia(
            this string triviaText,
            bool forceIndentation,
            int indentation,
            int indentationDelta,
            bool useTab,
            int tabSize)
        {
            var isEmptyString = false;
            var builder       = StringBuilderPool.Allocate();

            var trimmedTriviaText      = triviaText.TrimEnd(s_trimChars);
            var nonWhitespaceCharIndex = GetFirstNonWhitespaceIndexInString(triviaText);

            if (nonWhitespaceCharIndex == -1)
            {
                isEmptyString          = true;
                nonWhitespaceCharIndex = triviaText.Length;
            }

            var newIndentation = GetNewIndentationForComments(triviaText, nonWhitespaceCharIndex, forceIndentation, indentation, indentationDelta, tabSize);

            builder.AppendIndentationString(newIndentation, useTab, tabSize);
            if (!isEmptyString)
            {
                builder.Append(triviaText, nonWhitespaceCharIndex, triviaText.Length - nonWhitespaceCharIndex);
            }

            return(StringBuilderPool.ReturnAndFree(builder));
        }
Ejemplo n.º 2
0
        private string GetWhitespaceString(LineColumn lineColumn, LineColumnDelta delta)
        {
            var sb = StringBuilderPool.Allocate();

            for (int i = 0; i < delta.Lines; i++)
            {
                sb.AppendLine();
            }

            if (delta.Spaces == 0)
            {
                return(StringBuilderPool.ReturnAndFree(sb));
            }

            var useTabs = this.OptionSet.GetOption(FormattingOptions.UseTabs, this.Language);
            var tabSize = this.OptionSet.GetOption(FormattingOptions.TabSize, this.Language);

            // space indicates indentation
            if (delta.Lines > 0 || lineColumn.Column == 0)
            {
                sb.Append(delta.Spaces.CreateIndentationString(useTabs, tabSize));
                return(StringBuilderPool.ReturnAndFree(sb));
            }

            var useTabOnlyForIndentation = this.OptionSet.GetOption(FormattingOptions.UseTabOnlyForIndentation, this.Language);

            // space indicates space between two noisy trivia or tokens
            sb.Append(GetSpacesOrTabs(lineColumn.Column, delta.Spaces, useTabs && !useTabOnlyForIndentation, tabSize));
            return(StringBuilderPool.ReturnAndFree(sb));
        }
Ejemplo n.º 3
0
        private string GetWhitespaceString(LineColumn lineColumn, LineColumnDelta delta)
        {
            var sb = StringBuilderPool.Allocate();

            var newLine = this.OptionSet.GetOption(FormattingOptions.NewLine, this.Language);

            for (int i = 0; i < delta.Lines; i++)
            {
                sb.Append(newLine);
            }

            if (delta.Spaces == 0)
            {
                return(StringBuilderPool.ReturnAndFree(sb));
            }

            var useTabs = this.OptionSet.GetOption(FormattingOptions.UseTabs, this.Language);
            var tabSize = this.OptionSet.GetOption(FormattingOptions.TabSize, this.Language);

            // space indicates indentation
            if (delta.Lines > 0 || lineColumn.Column == 0)
            {
                sb.AppendIndentationString(delta.Spaces, useTabs, tabSize);
                return(StringBuilderPool.ReturnAndFree(sb));
            }

            // space indicates space between two noisy trivia or tokens
            sb.Append(' ', repeatCount: delta.Spaces);
            return(StringBuilderPool.ReturnAndFree(sb));
        }
Ejemplo n.º 4
0
            public override string GetTextBetween(SyntaxToken token1, SyntaxToken token2)
            {
                var builder = StringBuilderPool.Allocate();

                CommonFormattingHelpers.AppendTextBetween(token1, token2, builder);

                return(StringBuilderPool.ReturnAndFree(builder));
            }
Ejemplo n.º 5
0
        public static string ReindentStartOfXmlDocumentationComment(
            this string triviaText,
            bool forceIndentation,
            int indentation,
            int indentationDelta,
            bool useTab,
            int tabSize,
            string newLine
            )
        {
            var builder = StringBuilderPool.Allocate();

            // split xml doc comments into lines
            var lines = triviaText.Split('\n');

            Contract.ThrowIfFalse(lines.Length > 0);

            // add first line and append new line iff it is not a single line xml doc comment
            builder.Append(lines[0].Trim(s_trimChars));
            if (0 < lines.Length - 1)
            {
                builder.Append(newLine);
            }

            // add rest of xml doc comments
            for (var i = 1; i < lines.Length; i++)
            {
                var line = lines[i].TrimEnd(s_trimChars);
                var nonWhitespaceCharIndex = GetFirstNonWhitespaceIndexInString(line);
                if (nonWhitespaceCharIndex >= 0)
                {
                    var newIndentation = GetNewIndentationForComments(
                        line,
                        nonWhitespaceCharIndex,
                        forceIndentation,
                        indentation,
                        indentationDelta,
                        tabSize
                        );
                    builder.AppendIndentationString(newIndentation, useTab, tabSize);
                    builder.Append(
                        line,
                        nonWhitespaceCharIndex,
                        line.Length - nonWhitespaceCharIndex
                        );
                }

                if (i < lines.Length - 1)
                {
                    builder.Append(newLine);
                }
            }

            return(StringBuilderPool.ReturnAndFree(builder));
        }
            private string CreateString(TriviaDataWithList triviaData, CancellationToken cancellationToken)
            {
                // create string from given trivia data
                var sb = StringBuilderPool.Allocate();

                foreach (var trivia in triviaData.GetTriviaList(cancellationToken))
                {
                    sb.Append(trivia.ToFullString());
                }

                return(StringBuilderPool.ReturnAndFree(sb));
            }
            private string CreateString(string newLine)
            {
                if (this.SecondTokenIsFirstTokenOnLine)
                {
                    var builder = StringBuilderPool.Allocate();
                    for (var i = 0; i < this.LineBreaks; i++)
                    {
                        builder.Append(newLine);
                    }

                    builder.AppendIndentationString(this.Spaces, this.OptionSet.GetOption(FormattingOptions.UseTabs, this.Language), this.OptionSet.GetOption(FormattingOptions.TabSize, this.Language));
                    return(StringBuilderPool.ReturnAndFree(builder));
                }

                // space case. always use space
                return(new string(' ', this.Spaces));
            }