Exemplo n.º 1
0
        private static Task <Document> RefactorAsync(
            Document document,
            XmlElementSyntax xmlElement,
            CancellationToken cancellationToken)
        {
            SyntaxList <XmlNodeSyntax> nodes = xmlElement.Content;

            (TextSpan span1, TextSpan span2, List <TextSpan> spans) = AddParagraphToDocumentationCommentAnalyzer.FindFixableSpan(nodes, stopOnFirstMatch: false, cancellationToken: cancellationToken);

            var textChanges = new List <TextChange>();

            string newLine = nodes
                             .OfType <XmlTextSyntax>()
                             .SelectMany(f => f.TextTokens)
                             .First(f => f.IsKind(SyntaxKind.XmlTextLiteralNewLineToken))
                             .ValueText;

            string indentation = xmlElement.GetIndentation(cancellationToken).ToString();

            string s = $"{newLine}{indentation}/// ";

            int prevEnd = -1;

            foreach (TextSpan span in spans)
            {
                cancellationToken.ThrowIfCancellationRequested();

                if (prevEnd != -1)
                {
                    textChanges.Add(new TextChange(TextSpan.FromBounds(prevEnd, span.Start), s));
                }

                SyntaxToken token    = xmlElement.FindToken(span.Start);
                SyntaxToken endToken = xmlElement.FindToken(span.End - 1);

                bool isMultiline = xmlElement.SyntaxTree.IsMultiLineSpan(span, cancellationToken);

                string text = "<para>";

                if (isMultiline)
                {
                    text += s;
                }

                int start  = token.SpanStart;
                int length = 0;

                if (token.IsKind(SyntaxKind.XmlTextLiteralToken) &&
                    token.ValueText[0] == ' ')
                {
                    if (prevEnd == -1)
                    {
                        start++;
                    }
                    else
                    {
                        length++;
                    }
                }

                textChanges.Add(new TextChange(new TextSpan(start, length), text));

                text = "";

                if (isMultiline)
                {
                    text += s;
                }

                text += "</para>";

                textChanges.Add(new TextChange(new TextSpan(span.End, 0), text));

                prevEnd = endToken.Span.End;
            }

            cancellationToken.ThrowIfCancellationRequested();

            return(document.WithTextChangesAsync(textChanges, cancellationToken));
        }