Beispiel #1
0
        private void CheckNameAndInsertText(
            ITextView textView,
            ITextBuffer subjectBuffer,
            SnapshotPoint position,
            XmlElementStartTagSyntax startTag,
            int?finalCaretPosition,
            string formatString
            )
        {
            if (startTag == null)
            {
                return;
            }

            var elementName = startTag.Name.LocalName.ValueText;

            if (elementName.Length > 0)
            {
                var parentElement = startTag.Parent as XmlElementSyntax;
                if (parentElement.EndTag.Name.LocalName.ValueText != elementName)
                {
                    InsertTextAndMoveCaret(
                        textView,
                        subjectBuffer,
                        position,
                        string.Format(formatString, elementName),
                        finalCaretPosition
                        );
                }
            }
        }
        protected override List <Func <string> > GetExpectedCommentForTag(XmlElementStartTagSyntax startTag)
        {
            var tag = startTag.Name.LocalName.ValueText;

            if ("summary".Equals(tag))
            {
                // Pattern: The $(decomposed-name).
                // Pattern: The $(name).
                // Pattern: The $(name) $(decomposed-element-type-name).
                // Pattern: The $(decomposed-name) $(decomposed-element-type-name).
                // Pattern: $(element-type-name) $(name).
                var decomposed            = StyleCopDecompose(Name);
                var elemType              = GetCodeElementType();
                var decomposedElementType = StyleCopDecompose(elemType);
                var list = new List <Func <string> >
                {
                    () => string.Format("The {0}.", decomposed),
                    () => string.Format("The {0}.", Name),
                    () => string.Format("The {0} {1}.", decomposed, decomposedElementType),
                    () => string.Format("The {0} {1}.", Name, decomposedElementType),
                    () => string.Format("{0} {1}.", elemType, Name)
                };
                return(list);
            }

            Info("Unexpected tag {0} in comment.", tag);

            return(new List <Func <string> >());
        }
        protected override List <Func <string> > GetExpectedCommentForTag(XmlElementStartTagSyntax startTag)
        {
            var tag = startTag.Name.LocalName.ValueText;

            if ("summary".Equals(tag))
            {
                return(GetPredictedIndexerSummaryText());
            }

            if ("value".Equals(tag))
            {
                return(GetPredictedIndexerValueText());
            }

            if ("param".Equals(tag))
            {
                return(ExpectedCommentForParamTag(startTag));
            }

            if ("returns".Equals(tag))
            {
                return(GetPredictedIndexerReturnsText());
            }

            Info("Unexpected tag {0} in indexer comment.", tag);

            return(new List <Func <string> >());
        }
        private static void AnalyzeSingleLineDocumentationCommentTrivia(SyntaxNodeAnalysisContext context)
        {
            var documentationComment = (DocumentationCommentTriviaSyntax)context.Node;

            XmlElementSyntax summaryElement = documentationComment.SummaryElement();

            if (summaryElement != null)
            {
                XmlElementStartTagSyntax startTag = summaryElement?.StartTag;

                if (startTag?.IsMissing == false)
                {
                    XmlElementEndTagSyntax endTag = summaryElement.EndTag;

                    if (endTag?.IsMissing == false &&
                        startTag.GetSpanEndLine() < endTag.GetSpanStartLine())
                    {
                        Match match = _regex.Match(
                            summaryElement.ToString(),
                            startTag.Span.End - summaryElement.SpanStart,
                            endTag.SpanStart - startTag.Span.End);

                        if (match.Success)
                        {
                            DiagnosticHelpers.ReportDiagnostic(context,
                                                               DiagnosticDescriptors.FormatDocumentationSummaryOnSingleLine,
                                                               summaryElement);
                        }
                    }
                }
            }
        }
        public static string GetAttributeValue(XmlElementSyntax element, string attributeName)
        {
            XmlElementStartTagSyntax startTag = element.StartTag;

            if (startTag != null)
            {
                foreach (XmlAttributeSyntax attribute in startTag.Attributes)
                {
                    if (attribute.IsKind(SyntaxKind.XmlNameAttribute))
                    {
                        var nameAttribute = (XmlNameAttributeSyntax)attribute;

                        if (nameAttribute.Name?.LocalName.ValueText == attributeName)
                        {
                            IdentifierNameSyntax identifierName = nameAttribute.Identifier;

                            if (identifierName != null)
                            {
                                return(identifierName.Identifier.ValueText);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Beispiel #6
0
        public static void Analyze(SyntaxNodeAnalysisContext context, DocumentationCommentTriviaSyntax documentationComment)
        {
            XmlElementSyntax summaryElement = FormatSummaryRefactoring.GetSummaryElement(documentationComment);

            if (summaryElement != null)
            {
                XmlElementStartTagSyntax startTag = summaryElement?.StartTag;

                if (startTag?.IsMissing == false)
                {
                    XmlElementEndTagSyntax endTag = summaryElement.EndTag;

                    if (endTag?.IsMissing == false &&
                        startTag.GetSpanEndLine() < endTag.GetSpanStartLine())
                    {
                        Match match = FormatSummaryRefactoring.Regex.Match(
                            summaryElement.ToString(),
                            startTag.Span.End - summaryElement.Span.Start,
                            endTag.Span.Start - startTag.Span.End);

                        if (match.Success)
                        {
                            context.ReportDiagnostic(
                                DiagnosticDescriptors.FormatDocumentationSummaryOnSingleLine,
                                summaryElement.GetLocation());
                        }
                    }
                }
            }
        }
        private static bool ContainsException(DocumentationCommentTriviaSyntax comment, ITypeSymbol exceptionSymbol, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            foreach (XmlElementSyntax xmlElement in comment.Exceptions())
            {
                XmlElementStartTagSyntax startTag = xmlElement.StartTag;

                if (startTag != null)
                {
                    foreach (XmlAttributeSyntax xmlAttribute in startTag.Attributes)
                    {
                        if (xmlAttribute.IsKind(SyntaxKind.XmlCrefAttribute))
                        {
                            var xmlCrefAttribute = (XmlCrefAttributeSyntax)xmlAttribute;

                            CrefSyntax cref = xmlCrefAttribute.Cref;

                            if (cref != null)
                            {
                                ISymbol symbol = semanticModel.GetSymbolInfo(cref, cancellationToken).Symbol;

                                if (exceptionSymbol.Equals(symbol))
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
        protected override List <Func <string> > GetExpectedCommentForTag(XmlElementStartTagSyntax startTag)
        {
            var tag = startTag.Name.LocalName.ValueText;

            if ("summary".Equals(tag))
            {
                // Pattern: The $(name) interface.
                // Pattern: The $(decomposed-name) interface.
                // Pattern: The $(name).
                // Pattern: The $(decomposed-name).
                var stylecopChoppedName = Name.Substring(1);
                var list = new List <Func <string> >
                {
                    () => string.Format("The {0} interface.", stylecopChoppedName),
                    () =>
                    string.Format(
                        "The {0} interface.",
                        StyleCopDecompose(stylecopChoppedName)),
                    () => string.Format("The {0}.", stylecopChoppedName),
                    () => string.Format("The {0}.", StyleCopDecompose(stylecopChoppedName)),
                    () => string.Format("The {0}.", Name),
                    () => string.Format("The {0}.", StyleCopDecompose(Name))
                };
                return(list);
            }

            Info("Unexpected tag {0} in interface comment.", tag);
            return(new List <Func <string> >());
        }
        private static void Analyze(SyntaxNodeAnalysisContext context)
        {
            XmlElementStartTagSyntax elementStartTag = context.Node as XmlElementStartTagSyntax;

            if (elementStartTag.Parent.Parent.Kind() != SyntaxKind.SingleLineDocumentationCommentTrivia)
            {
                return;
            }

            if (elementStartTag.Name.LocalName.Text != @"summary")
            {
                return;
            }

            string content = (elementStartTag.Parent as XmlElementSyntax).Content.ToString();

            content = content.Replace('/', ' ');
            if (!string.IsNullOrWhiteSpace(content))
            {
                return;
            }

            Diagnostic diagnostic = Diagnostic.Create(Rule, elementStartTag.GetLocation());

            context.ReportDiagnostic(diagnostic);
        }
        private static async Task <Document> FormatSummaryOnSingleLineAsync(
            Document document,
            DocumentationCommentTriviaSyntax documentationComment,
            CancellationToken cancellationToken)
        {
            SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);

            XmlElementSyntax summaryElement = documentationComment.SummaryElement();

            XmlElementStartTagSyntax startTag = summaryElement.StartTag;
            XmlElementEndTagSyntax   endTag   = summaryElement.EndTag;

            Match match = _formatSummaryOnSingleLineRegex.Match(
                summaryElement.ToString(),
                startTag.Span.End - summaryElement.SpanStart,
                endTag.SpanStart - startTag.Span.End);

            var textChange = new TextChange(
                new TextSpan(startTag.Span.End, match.Length),
                match.Groups[1].Value);

            SourceText newSourceText = sourceText.WithChanges(textChange);

            return(document.WithText(newSourceText));
        }
Beispiel #11
0
        private void FormatElementStartTag(XmlElementStartTagSyntax node)
        {
            if (node.ContainsSkippedText)
            {
                AddWord(node.Span);
            }
            else
            {
                AddWord(node.LessThanToken.Span);

                EnqueueTrailingTriviaChange(node.LessThanToken, string.Empty);

                EnqueueLeadingTriviaChange(node.Name, string.Empty);

                FormatTagName(node.Name);

                EnqueueTrailingTriviaChange(node.Name, string.Empty);

                foreach (var attribute in node.Attributes)
                {
                    EnqueueLeadingTriviaChange(attribute, " ");

                    FormatTagAttribute(attribute);

                    EnqueueTrailingTriviaChange(attribute, string.Empty);
                }

                EnqueueLeadingTriviaChange(node.GreaterThanToken, string.Empty);

                AddWord(node.GreaterThanToken.Span);
            }
        }
Beispiel #12
0
        private static bool ContainsException(XmlElementSyntax xmlElement, INamedTypeSymbol exceptionSymbol, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            XmlElementStartTagSyntax startTag = xmlElement.StartTag;

            return(startTag != null &&
                   ContainsException(startTag.Attributes, exceptionSymbol, semanticModel, cancellationToken));
        }
Beispiel #13
0
        public override void VisitXmlElementStartTag(XmlElementStartTagSyntax node)
        {
            node.Name?.Accept(this);
            foreach (XmlAttributeSyntax attribute in node.Attributes)
            {
                attribute.Accept(this);
            }

            base.VisitXmlElementStartTag(node);
        }
Beispiel #14
0
        public override Evaluation VisitXmlElementStartTag(XmlElementStartTagSyntax node)
        {
            node.Name?.Accept <Evaluation>(this);
            foreach (XmlAttributeSyntax attribute in node.Attributes)
            {
                attribute.Accept <Evaluation>(this);
            }

            return(base.VisitXmlElementStartTag(node));
        }
            public override SyntaxNode VisitXmlElementStartTag(XmlElementStartTagSyntax node)
            {
                if (!IsValueName(node.Name))
                {
                    return(base.VisitXmlElementStartTag(node));
                }

                return(node.ReplaceNode(node.Name, ConvertToParam(node.Name))
                       .AddAttributes(SyntaxFactory.XmlNameAttribute("value")));
            }
Beispiel #16
0
        /// <summary>
        /// Creates summary element syntax.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns>A XmlElementSyntax.</returns>
        private static XmlElementSyntax CreateSummaryElementSyntax(string content)
        {
            XmlNameSyntax            xmlName         = SyntaxFactory.XmlName(SyntaxFactory.Identifier(DocumentationHeaderHelper.Summary));
            XmlElementStartTagSyntax summaryStartTag = SyntaxFactory.XmlElementStartTag(xmlName);
            XmlElementEndTagSyntax   summaryEndTag   = SyntaxFactory.XmlElementEndTag(xmlName);

            return(SyntaxFactory.XmlElement(
                       summaryStartTag,
                       SyntaxFactory.SingletonList <XmlNodeSyntax>(CreateSummaryTextSyntax(content)),
                       summaryEndTag));
        }
        bool HasMatchingEndTag(XmlElementStartTagSyntax parentStartTag)
        {
            var parentElement = parentStartTag?.Parent as XmlElementSyntax;

            if (parentElement == null)
            {
                return(false);
            }
            var endTag = parentElement.EndTag;

            return(endTag != null && !endTag.IsMissing && endTag.Name.LocalName.ValueText == parentStartTag.Name.LocalName.ValueText);
        }
        private void ClassifyXmlElementStartTag(XmlElementStartTagSyntax node)
        {
            AddXmlClassification(node.LessThanToken, ClassificationTypeNames.XmlDocCommentDelimiter);
            ClassifyXmlName(node.Name);

            foreach (var attribute in node.Attributes)
            {
                ClassifyXmlAttribute(attribute);
            }

            AddXmlClassification(node.GreaterThanToken, ClassificationTypeNames.XmlDocCommentDelimiter);
        }
Beispiel #19
0
 public new void AddChildren()
 {
     base.AddChildren();
     Kind               = Node.Kind();
     _startTag          = ((XmlElementSyntax)Node).StartTag;
     _startTagIsChanged = false;
     _content           = ((XmlElementSyntax)Node).Content;
     _contentIsChanged  = false;
     _contentCount      = _content.Count;
     _endTag            = ((XmlElementSyntax)Node).EndTag;
     _endTagIsChanged   = false;
 }
Beispiel #20
0
        /// <summary>
        /// Creates return element syntax.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns>A XmlElementSyntax.</returns>
        private static XmlElementSyntax CreateReturnElementSyntax(string content)
        {
            XmlNameSyntax xmlName = SyntaxFactory.XmlName("returns");
            /// <returns>[0]xxx[1]</returns>[2]

            XmlElementStartTagSyntax startTag = SyntaxFactory.XmlElementStartTag(xmlName);

            XmlTextSyntax contentText = SyntaxFactory.XmlText(content);

            XmlElementEndTagSyntax endTag = SyntaxFactory.XmlElementEndTag(xmlName);

            return(SyntaxFactory.XmlElement(startTag, SyntaxFactory.SingletonList <XmlNodeSyntax>(contentText), endTag));
        }
        bool HasUnmatchedIdenticalParent(XmlElementStartTagSyntax parentStartTag)
        {
            if (parentStartTag.Parent.Parent is XmlElementSyntax grandParentElement)
            {
                if (grandParentElement.StartTag.Name.LocalName.ValueText == parentStartTag.Name.LocalName.ValueText)
                {
                    if (HasMatchingEndTag(grandParentElement.StartTag))
                    {
                        return(HasUnmatchedIdenticalParent(grandParentElement.StartTag));
                    }
                    return(true);
                }
            }

            return(false);
        }
        private static bool HasMatchingEndTag(XmlElementStartTagSyntax parentStartTag)
        {
            if (parentStartTag == null)
            {
                return(false);
            }

            if (parentStartTag.Parent is not XmlElementSyntax parentElement)
            {
                return(false);
            }

            var endTag = parentElement.EndTag;

            return(endTag != null && !endTag.IsMissing && endTag.Name.LocalName.ValueText == parentStartTag.Name.LocalName.ValueText);
        }
Beispiel #23
0
        public override void VisitXmlElementStartTag(XmlElementStartTagSyntax node)
        {
            if (!PreVisit(node))
            {
                return;
            }

            node.Name?.Accept(this);
            foreach (XmlAttributeSyntax attribute in node.Attributes)
            {
                attribute.Accept(this);
            }

            base.VisitXmlElementStartTag(node);

            PostVisit(node);
        }
Beispiel #24
0
        private void AddXmlElementItems(List <CompletionItem> items, XmlElementStartTagSyntax startTag)
        {
            var xmlElementName = startTag.Name.LocalName.ValueText;

            if (xmlElementName == ListElementName)
            {
                items.AddRange(GetListItems());
            }
            else if (xmlElementName == ListHeaderElementName)
            {
                items.AddRange(GetListHeaderItems());
            }
            else if (xmlElementName == ItemElementName)
            {
                items.AddRange(GetItemTagItems());
            }
        }
Beispiel #25
0
        /// <summary>
        /// Creates parameter element syntax.
        /// </summary>
        /// <param name="parameterName">The parameter name.</param>
        /// <param name="parameterContent">The parameter content.</param>
        /// <returns>A XmlElementSyntax.</returns>
        private static XmlElementSyntax CreateParameterElementSyntax(string parameterName, string parameterContent)
        {
            XmlNameSyntax paramName = SyntaxFactory.XmlName("param");

            /// <param name="parameterName">[0][1]</param>[2]

            // [0] -- param start tag with attribute
            XmlNameAttributeSyntax   paramAttribute = SyntaxFactory.XmlNameAttribute(parameterName);
            XmlElementStartTagSyntax startTag       = SyntaxFactory.XmlElementStartTag(paramName, SyntaxFactory.SingletonList <XmlAttributeSyntax>(paramAttribute));

            // [1] -- content
            XmlTextSyntax content = SyntaxFactory.XmlText(parameterContent);

            // [2] -- end tag
            XmlElementEndTagSyntax endTag = SyntaxFactory.XmlElementEndTag(paramName);

            return(SyntaxFactory.XmlElement(startTag, SyntaxFactory.SingletonList <SyntaxNode>(content), endTag));
        }
Beispiel #26
0
        protected override List <Func <string> > GetExpectedCommentForTag(XmlElementStartTagSyntax startTag)
        {
            var tag = startTag.Name.LocalName.ValueText;

            if ("summary".Equals(tag))
            {
                return(GetPredictedPropertySummaryText());
            }

            if ("value".Equals(tag))
            {
                return(GetPredictedPropertyValueText());
            }

            Info("Unexpected tag {0} in property comment.", tag);

            return(new List <Func <string> >());
        }
Beispiel #27
0
        private static Task <Document> FormatSummaryOnSingleLineAsync(
            Document document,
            DocumentationCommentTriviaSyntax documentationComment,
            CancellationToken cancellationToken)
        {
            XmlElementSyntax summaryElement = documentationComment.SummaryElement();

            XmlElementStartTagSyntax startTag = summaryElement.StartTag;
            XmlElementEndTagSyntax   endTag   = summaryElement.EndTag;

            Match match = _formatSummaryOnSingleLineRegex.Match(
                summaryElement.ToString(),
                startTag.Span.End - summaryElement.SpanStart,
                endTag.SpanStart - startTag.Span.End);

            return(document.WithTextChangeAsync(
                       new TextSpan(startTag.Span.End, match.Length),
                       match.Groups[1].Value,
                       cancellationToken));
        }
        private List <Func <string> > ExpectedCommentForParamTag(XmlElementStartTagSyntax startTag)
        {
            string paramNameFromAttribute = "";

            foreach (var a in startTag.Attributes)
            {
                var attrName  = a.Name.LocalName.ValueText;
                var attrValue = string.Join("", a.TextTokens.Select(t => t.ValueText));
                if ("name".Equals(attrName))
                {
                    paramNameFromAttribute = attrValue;
                }
                else
                {
                    // Unexpected attribute.
                    return(new List <Func <string> >());
                }
            }

            // Sanity check for attribute value.
            var paramExists =
                It.ParameterList.Parameters.Any(
                    ps =>
                    string.Equals(
                        ps.Identifier.ValueText,
                        paramNameFromAttribute,
                        StringComparison.InvariantCultureIgnoreCase));

            if (!paramExists)
            {
                Info("! The indexer does not have a parameter named {0}.", paramNameFromAttribute);
            }

            var decomposedParamName = StyleCopDecompose(paramNameFromAttribute);

            return(new List <Func <string> >
            {
                () => string.Format("The {0}.", decomposedParamName),
                () => string.Format("The {0}.", paramNameFromAttribute)
            });
        }
Beispiel #29
0
        protected override List <Func <string> > GetExpectedCommentForTag(XmlElementStartTagSyntax startTag)
        {
            string tag = startTag.Name.LocalName.ValueText;

            if ("summary".Equals(tag))
            {
                return(ExpectedCommentForSummaryTag());
            }

            if ("param".Equals(tag))
            {
                return(ExpectedCommentForParamTag(startTag));
            }

            if ("returns".Equals(tag))
            {
                return(ExpectedCommentForReturnsTag());
            }

            return(new List <Func <string> >());
        }
        void CheckNameAndInsertText(XmlElementStartTagSyntax startTag, int?finalCaretPosition, string formatString)
        {
            if (startTag == null)
            {
                return;
            }

            var elementName = startTag.Name.LocalName.ValueText;

            if (elementName.Length > 0)
            {
                var parentElement = startTag.Parent as XmlElementSyntax;
                if (parentElement.EndTag.Name.LocalName.ValueText != elementName)
                {
                    Editor.InsertAtCaret(string.Format(formatString, elementName));
                    if (finalCaretPosition.HasValue)
                    {
                        Editor.CaretOffset = finalCaretPosition.Value;
                    }
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitXmlElementStartTag(XmlElementStartTagSyntax node)
 {
     this.OnNodeVisited(node);
     if (!this.traverseRootOnly) base.VisitXmlElementStartTag(node);
 }
        private bool HasUnmatchedIdenticalParent(XmlElementStartTagSyntax parentStartTag)
        {
            var grandParentElement = parentStartTag.Parent.Parent as XmlElementSyntax;
            if (grandParentElement != null)
            {
                if (grandParentElement.StartTag.Name.LocalName.ValueText == parentStartTag.Name.LocalName.ValueText)
                {
                    if (HasMatchingEndTag(grandParentElement.StartTag))
                    {
                        return HasUnmatchedIdenticalParent(grandParentElement.StartTag);
                    }

                    return true;
                }
            }

            return false;
        }
        private void CheckNameAndInsertText(ITextView textView, ITextBuffer subjectBuffer, SnapshotPoint position, XmlElementStartTagSyntax startTag, int? finalCaretPosition, string formatString)
        {
            if (startTag == null)
            {
                return;
            }

            var elementName = startTag.Name.LocalName.ValueText;

            if (elementName.Length > 0)
            {
                var parentElement = startTag.Parent as XmlElementSyntax;
                if (parentElement.EndTag.Name.LocalName.ValueText != elementName)
                {
                    InsertTextAndMoveCaret(textView, subjectBuffer, position, string.Format(formatString, elementName), finalCaretPosition);
                }
            }
        }
        private bool HasMatchingEndTag(XmlElementStartTagSyntax parentStartTag)
        {
            if (parentStartTag == null)
            {
                return false;
            }

            var parentElement = parentStartTag.Parent as XmlElementSyntax;
            if (parentElement == null)
            {
                return false;
            }

            var endTag = parentElement.EndTag;
            return endTag != null && !endTag.IsMissing && endTag.Name.LocalName.ValueText == parentStartTag.Name.LocalName.ValueText;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitXmlElementStartTag(XmlElementStartTagSyntax node)
 {
     this.OnNodeVisited(node, this.type.IsInstanceOfType(node));
     base.VisitXmlElementStartTag(node);
 }
        private void ClassifyXmlElementStartTag(XmlElementStartTagSyntax node)
        {
            AddXmlClassification(node.LessThanToken, ClassificationTypeNames.XmlDocCommentDelimiter);
            ClassifyXmlName(node.Name);

            foreach (var attribute in node.Attributes)
            {
                ClassifyXmlAttribute(attribute);
            }

            AddXmlClassification(node.GreaterThanToken, ClassificationTypeNames.XmlDocCommentDelimiter);
        }