Beispiel #1
0
        public void TestReturnsElement()
        {
            string expected =
                "<returns>\r\n"
                + "/// Returns.\r\n"
                + "/// </returns>";

            Assert.Equal(expected, XmlSyntaxFactory.ReturnsElement("\r\n", XmlSyntaxFactory.Text("Returns.")).ToFullString());
        }
Beispiel #2
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))));
        }