Example #1
0
        public async Task <Document> RefactorAsync(
            Document document,
            TNode node,
            CancellationToken cancellationToken)
        {
            MemberDeclarationSyntax memberDeclaration = GetMemberDeclaration(node);

            DocumentationCommentTriviaSyntax comment = memberDeclaration.GetSingleLineDocumentationComment();

            SeparatedSyntaxList <TNode> typeParameters = GetContainingList(node);

            List <ElementInfo <TNode> > infos = GetElementInfos(comment, typeParameters);

            string newTrivia = GetNewTrivia(comment, infos);

            SyntaxTriviaList triviaList = SyntaxFactory.ParseLeadingTrivia(newTrivia);

            if (triviaList.Any())
            {
                SyntaxTrivia firstTrivia = triviaList.First();

                if (firstTrivia.HasStructure)
                {
                    var newComment = firstTrivia.GetStructure() as DocumentationCommentTriviaSyntax;

                    if (newComment != null)
                    {
                        newComment = newComment.WithFormatterAnnotation();

                        return(await document.ReplaceNodeAsync(comment, newComment, cancellationToken).ConfigureAwait(false));
                    }
                }
            }

            Debug.Assert(false, "");

            return(document);
        }
Example #2
0
        private static SyntaxTrivia GetDocumentationCommentTrivia(ISymbol symbol, SemanticModel semanticModel, int position, CancellationToken cancellationToken = default(CancellationToken))
        {
            string xml = symbol.GetDocumentationCommentXml(cancellationToken: cancellationToken);

            if (string.IsNullOrEmpty(xml))
            {
                return(default(SyntaxTrivia));
            }

            string innerXml = GetInnerXml(xml);

            Debug.Assert(innerXml != null, xml);

            if (innerXml == null)
            {
                return(default(SyntaxTrivia));
            }

            string innerXmlWithSlashes = AddSlashes(innerXml.TrimEnd());

            SyntaxTriviaList leadingTrivia = ParseLeadingTrivia(innerXmlWithSlashes);

            if (leadingTrivia.Count != 1)
            {
                return(default(SyntaxTrivia));
            }

            SyntaxTrivia trivia = leadingTrivia.First();

            if (trivia.Kind() != SyntaxKind.SingleLineDocumentationCommentTrivia)
            {
                return(default(SyntaxTrivia));
            }

            if (!trivia.HasStructure)
            {
                return(default(SyntaxTrivia));
            }

            SyntaxNode structure = trivia.GetStructure();

            if (structure.Kind() != SyntaxKind.SingleLineDocumentationCommentTrivia)
            {
                return(default(SyntaxTrivia));
            }

            var commentTrivia = (DocumentationCommentTriviaSyntax)structure;

            var rewriter = new DocumentationCommentTriviaRewriter(position, semanticModel);

            // Remove T: from cref attribute and replace `1 with {T}
            commentTrivia = (DocumentationCommentTriviaSyntax)rewriter.VisitDocumentationCommentTrivia(commentTrivia);

            // Remove <filterpriority> element
            commentTrivia = RemoveFilterPriorityElement(commentTrivia);

            string text = commentTrivia.ToFullString();

            // Remove /// from empty lines
            text = _commentedEmptyLineRegex.Replace(text, "");

            leadingTrivia = ParseLeadingTrivia(text);

            if (leadingTrivia.Count == 1)
            {
                return(leadingTrivia.First());
            }

            return(default(SyntaxTrivia));
        }