public void TestEmptySummaryElement()
        {
            string expected =
                "<summary>\r\n"
                + "///\r\n"
                + "/// </summary>";

            Assert.Equal(expected, XmlSyntaxFactory.SummaryElement("\r\n").ToFullString());
        }
        public void TestSummaryElement()
        {
            string expected =
                "<summary>\r\n"
                + "/// Summary.\r\n"
                + "/// </summary>";

            Assert.Equal(expected, XmlSyntaxFactory.SummaryElement("\r\n", XmlSyntaxFactory.Text("Summary.")).ToFullString());
        }
Exemple #3
0
        private static Task <Document> GetConstructorOrDestructorDocumentationTransformedDocumentAsync(Document document, SyntaxNode root, BaseMethodDeclarationSyntax declaration, CancellationToken cancellationToken)
        {
            SyntaxTriviaList leadingTrivia = declaration.GetLeadingTrivia();
            int insertionIndex             = GetInsertionIndex(ref leadingTrivia);

            string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);

            var documentationNodes = new List <XmlNodeSyntax>();

            var typeDeclaration        = declaration.FirstAncestorOrSelf <BaseTypeDeclarationSyntax>();
            var standardText           = SA1642SA1643CodeFixProvider.GenerateStandardText(document, declaration, typeDeclaration, cancellationToken);
            var standardTextSyntaxList = SA1642SA1643CodeFixProvider.BuildStandardTextSyntaxList(typeDeclaration, newLineText, standardText[0], standardText[1]);

            // Remove the empty line generated by build standard text, as this is not needed with constructing a new summary element.
            standardTextSyntaxList = standardTextSyntaxList.RemoveAt(0);

            documentationNodes.Add(XmlSyntaxFactory.SummaryElement(newLineText, standardTextSyntaxList));

            if (declaration.ParameterList != null)
            {
                foreach (var parameter in declaration.ParameterList.Parameters)
                {
                    documentationNodes.Add(XmlSyntaxFactory.NewLine(newLineText));
                    documentationNodes.Add(XmlSyntaxFactory.ParamElement(parameter.Identifier.ValueText));
                }
            }

            var documentationComment =
                XmlSyntaxFactory.DocumentationComment(
                    newLineText,
                    documentationNodes.ToArray());
            var trivia = SyntaxFactory.Trivia(documentationComment);

            SyntaxTriviaList newLeadingTrivia = leadingTrivia.Insert(insertionIndex, trivia);
            SyntaxNode       newElement       = declaration.WithLeadingTrivia(newLeadingTrivia);

            return(Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(declaration, newElement))));
        }
Exemple #4
0
        private static async Task <Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
        {
            SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var firstComment = root.FindTrivia(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
            var parentToken  = firstComment.Token;

            var lines = new List <string>();

            for (int i = 0; i < parentToken.LeadingTrivia.Count; i++)
            {
                if (!parentToken.LeadingTrivia[i].IsKind(SyntaxKind.SingleLineCommentTrivia) &&
                    !parentToken.LeadingTrivia[i].IsKind(SyntaxKind.MultiLineCommentTrivia))
                {
                    continue;
                }

                if (!diagnostic.Location.SourceSpan.Contains(parentToken.LeadingTrivia[i].Span))
                {
                    continue;
                }

                if (parentToken.LeadingTrivia[i].IsKind(SyntaxKind.SingleLineCommentTrivia))
                {
                    lines.Add(parentToken.LeadingTrivia[i].ToString().Substring(2));
                }
                else
                {
                    var commentText    = parentToken.LeadingTrivia[i].ToString();
                    var normalizedText = commentText.Substring(1, commentText.Length - 3)
                                         .Replace("\r\n", "\n").Replace('\r', '\n');
                    foreach (var line in normalizedText.Split('\n'))
                    {
                        if (Regex.IsMatch(line, "^\\s*\\*"))
                        {
                            lines.Add(line.Substring(line.IndexOf('*') + 1));
                        }
                        else
                        {
                            lines.Add(line);
                        }
                    }

                    lines[lines.Count - 1] = lines[lines.Count - 1].TrimEnd();
                }
            }

            int firstContentLine = lines.FindIndex(line => !string.IsNullOrWhiteSpace(line));

            if (firstContentLine >= 0)
            {
                lines.RemoveRange(0, firstContentLine);
                int lastContentLine = lines.FindLastIndex(line => !string.IsNullOrWhiteSpace(line));
                lines.RemoveRange(lastContentLine + 1, lines.Count - lastContentLine - 1);
            }

            if (lines.All(line => line.Length == 0 || line.StartsWith(" ")))
            {
                for (int i = 0; i < lines.Count; i++)
                {
                    if (lines[i].Length == 0)
                    {
                        continue;
                    }

                    lines[i] = lines[i].Substring(1);
                }
            }

            var nodes = new List <XmlNodeSyntax>(lines.Select(line => XmlSyntaxFactory.Text(line)));

            for (int i = nodes.Count - 1; i > 0; i--)
            {
                nodes.Insert(i, XmlSyntaxFactory.NewLine(Environment.NewLine));
            }

            var summary = XmlSyntaxFactory.SummaryElement(Environment.NewLine, nodes.ToArray());

            var leadingTrivia  = SyntaxFactory.TriviaList(parentToken.LeadingTrivia.TakeWhile(trivia => !trivia.Equals(firstComment)));
            var newParentToken = parentToken.WithLeadingTrivia(leadingTrivia.Add(SyntaxFactory.Trivia(XmlSyntaxFactory.DocumentationComment(Environment.NewLine, summary))));

            var newRoot = root.ReplaceToken(parentToken, newParentToken);

            return(document.WithSyntaxRoot(root.ReplaceToken(parentToken, newParentToken)));
        }
Exemple #5
0
        private static Task <Document> GetMethodDocumentationTransformedDocumentAsync(Document document, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
        {
            SyntaxTriviaList leadingTrivia = methodDeclaration.GetLeadingTrivia();
            int insertionIndex             = GetInsertionIndex(ref leadingTrivia);

            string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);

            var documentationNodes = new List <XmlNodeSyntax>();

            documentationNodes.Add(XmlSyntaxFactory.SummaryElement(newLineText));

            if (methodDeclaration.TypeParameterList != null)
            {
                foreach (var typeParameter in methodDeclaration.TypeParameterList.Parameters)
                {
                    documentationNodes.Add(XmlSyntaxFactory.NewLine(newLineText));
                    documentationNodes.Add(XmlSyntaxFactory.TypeParamElement(typeParameter.Identifier.ValueText));
                }
            }

            if (methodDeclaration.ParameterList != null)
            {
                foreach (var parameter in methodDeclaration.ParameterList.Parameters)
                {
                    documentationNodes.Add(XmlSyntaxFactory.NewLine(newLineText));
                    documentationNodes.Add(XmlSyntaxFactory.ParamElement(parameter.Identifier.ValueText));
                }
            }

            TypeSyntax typeName;

            var typeSymbol = semanticModel.GetSymbolInfo(methodDeclaration.ReturnType, cancellationToken).Symbol as INamedTypeSymbol;

            if (typeSymbol.IsGenericType)
            {
                typeName = SyntaxFactory.ParseTypeName("global::System.Threading.Tasks.Task<TResult>");
            }
            else
            {
                typeName = SyntaxFactory.ParseTypeName("global::System.Threading.Tasks.Task");
            }

            XmlNodeSyntax[] returnContent =
            {
                XmlSyntaxFactory.Text(DocumentationResources.TaskReturnElementFirstPart),
                XmlSyntaxFactory.SeeElement(SyntaxFactory.TypeCref(typeName)).WithAdditionalAnnotations(Simplifier.Annotation),
                XmlSyntaxFactory.Text(DocumentationResources.TaskReturnElementSecondPart),
            };

            documentationNodes.Add(XmlSyntaxFactory.NewLine(newLineText));
            documentationNodes.Add(XmlSyntaxFactory.ReturnsElement(returnContent));

            var documentationComment =
                XmlSyntaxFactory.DocumentationComment(
                    newLineText,
                    documentationNodes.ToArray());
            var trivia = SyntaxFactory.Trivia(documentationComment);

            SyntaxTriviaList newLeadingTrivia = leadingTrivia.Insert(insertionIndex, trivia);
            SyntaxNode       newElement       = methodDeclaration.WithLeadingTrivia(newLeadingTrivia);

            return(Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(methodDeclaration, newElement))));
        }