Comment line implementation.
Inheritance: CodeElement, ICommentElement
Ejemplo n.º 1
0
        public void CreateTextAndXmlTest()
        {
            CommentElement commentLine = new CommentElement("Comment here", CommentType.XmlLine);

            //
            // Verify default property values
            //
            Assert.AreEqual(CommentType.XmlLine, commentLine.Type, "Unexpected value for IsXmlComment.");
            Assert.AreEqual("Comment here", commentLine.Text, "Unexpected value for Text.");
        }
Ejemplo n.º 2
0
        public void GetAttributeTypeTest()
        {
            FieldElement fieldElement = new FieldElement();
            fieldElement.Name = "TestField";
            fieldElement.Access = CodeAccess.Protected;
            fieldElement.Type = "int";

            string attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, fieldElement);
            Assert.AreEqual("int", attribute, "Unexpected attribute.");

            TypeElement typeElement = new TypeElement();
            typeElement.Type = TypeElementType.Interface;

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, typeElement);
            Assert.AreEqual("Interface", attribute, "Unexpected attribute.");

            CommentElement commentElement = new CommentElement(CommentType.Block);

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, commentElement);
            Assert.AreEqual("Block", attribute, "Unexpected attribute.");

            UsingElement usingElement = new UsingElement();
            usingElement.Name = "MySystem";
            usingElement.Redefine = "System";

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, usingElement);
            Assert.AreEqual("Alias", attribute, "Unexpected attribute.");

            ConditionDirectiveElement conditionElement = new ConditionDirectiveElement();

            attribute = ElementUtilities.GetAttribute(ElementAttributeType.Type, conditionElement);
            Assert.AreEqual(string.Empty, attribute, "Unexpected attribute.");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses a comment line.
        /// </summary>
        /// <returns>Comment element.</returns>
        private CommentElement ParseCommentLine()
        {
            CommentElement commentLine;

            StringBuilder commentTextBuilder = new StringBuilder(DefaultBlockLength);

            CommentType commentType = CommentType.Line;
            if (NextChar == VBSymbol.BeginComment)
            {
                TryReadChar();
                if (NextChar == VBSymbol.BeginComment)
                {
                    commentType = CommentType.XmlLine;
                    TryReadChar();
                }
                else
                {
                    commentTextBuilder.Append(VBSymbol.BeginComment);
                }
            }

            commentTextBuilder.Append(ReadLine());
            commentLine = new CommentElement(commentTextBuilder.ToString(), commentType);
            return commentLine;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Parses a field.
        /// </summary>
        /// <param name="wordList">The word list.</param>
        /// <param name="access">The field access.</param>
        /// <param name="memberAttributes">The member attributes.</param>
        /// <param name="untypedAssignment">Whether or not the field is untyped.</param>
        /// <returns>A field code element.</returns>
        private FieldElement ParseField(
            StringCollection wordList,
            CodeAccess access,
            MemberModifiers memberAttributes,
            bool untypedAssignment)
        {
            FieldElement field = new FieldElement();

            StringBuilder nameBuilder = new StringBuilder(DefaultWordLength);

            foreach (string word in wordList)
            {
                string trimmedWord = word.Trim(' ', VBSymbol.AliasSeparator);

                string upperWord = trimmedWord.ToUpperInvariant();
                if ((!VBKeyword.IsVBKeyword(trimmedWord) ||
                    upperWord == VBKeyword.Custom.ToUpperInvariant() ||
                    upperWord == VBKeyword.Ansi.ToUpperInvariant() ||
                    upperWord == VBKeyword.Unicode.ToUpperInvariant() ||
                    upperWord == VBKeyword.Auto.ToUpperInvariant()) &&
                    trimmedWord.Length > 0)
                {
                    nameBuilder.Append(trimmedWord);
                    nameBuilder.Append(VBSymbol.AliasSeparator);
                    nameBuilder.Append(' ');
                }
            }

            field.Name = nameBuilder.ToString().TrimEnd(VBSymbol.AliasSeparator, ' ');

            EatWhiteSpace();

            if (!untypedAssignment)
            {
                string returnType = CaptureTypeName();
                if (returnType.ToUpperInvariant() == VBKeyword.New.ToUpperInvariant())
                {
                    EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab);
                    field.InitialValue = VBKeyword.New + " " + ReadCodeLine().Trim();
                }
                else
                {
                    field.Type = returnType;
                }
            }

            field.Access = access;
            field.MemberModifiers = memberAttributes;

            EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab);

            bool isAssignment = NextChar == VBSymbol.Assignment || untypedAssignment;
            if (isAssignment)
            {
                if (!untypedAssignment)
                {
                    EatChar(VBSymbol.Assignment);
                }
                string initialValue = ParseInitialValue();

                field.InitialValue = initialValue;
            }

            EatWhiteSpace(WhiteSpaceTypes.SpaceAndTab);

            if (NextChar == VBSymbol.BeginComment)
            {
                EatChar(VBSymbol.BeginComment);

                string commentText = ReadLine().Trim();
                if (commentText.Length > 0)
                {
                    CommentElement comment = new CommentElement(commentText);
                    field.TrailingComment = comment;
                }
            }

            return field;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Processes a region element.
        /// </summary>
        /// <param name="element">Region code element.</param>
        public void VisitRegionElement(RegionElement element)
        {
            RegionStyle regionStyle = _configuration.Formatting.Regions.Style;
            if (regionStyle == RegionStyle.Default)
            {
                // Use the default region style
                regionStyle = RegionStyle.Directive;
            }

            if (regionStyle == RegionStyle.NoDirective ||
                !element.DirectivesEnabled)
            {
                CodeWriter.WriteVisitElements(element.Children, Writer, this);
            }
            else
            {
                if (regionStyle == RegionStyle.Directive)
                {
                    WriteRegionBeginDirective(element);
                }
                else if (regionStyle == RegionStyle.CommentDirective)
                {
                    CommentElement commentDirective = new CommentElement(
                        string.Format(
                        CultureInfo.InvariantCulture,
                        Configuration.Formatting.Regions.CommentDirectiveBeginFormat,
                        element.Name).TrimEnd());
                    VisitCommentElement(commentDirective);
                }

                Writer.WriteLine();

                WriteChildren(element);

                if (element.Children.Count > 0)
                {
                    Writer.WriteLine();
                }

                if (regionStyle == RegionStyle.Directive)
                {
                    WriteRegionEndDirective(element);
                }
                else if (regionStyle == RegionStyle.CommentDirective)
                {
                    string regionName = string.Empty;
                    if (Configuration.Formatting.Regions.EndRegionNameEnabled)
                    {
                        regionName = element.Name;
                    }

                    CommentElement commentDirective = new CommentElement(
                        string.Format(
                        CultureInfo.InvariantCulture,
                        Configuration.Formatting.Regions.CommentDirectiveEndFormat,
                        regionName).TrimEnd());
                    VisitCommentElement(commentDirective);
                }
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Processes a comment element.
 /// </summary>
 /// <param name="element">Comment code element.</param>
 public abstract void VisitCommentElement(CommentElement element);
Ejemplo n.º 7
0
        /// <summary>
        /// Writes a comment line.
        /// </summary>
        /// <param name="comment">The comment.</param>
        public override void VisitCommentElement(CommentElement comment)
        {
            if (comment.Type == CommentType.Block)
            {
                throw new InvalidOperationException("Block comments are not supported by VB.");
            }
            else
            {
                StringBuilder builder = new StringBuilder(DefaultBlockLength);

                if (comment.Type == CommentType.XmlLine)
                {
                    builder.Append("'''");
                }
                else
                {
                    builder.Append("'");
                }

                builder.Append(FormatCommentText(comment));
                WriteIndented(builder.ToString());
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets text from a comment directive, if present.
        /// </summary>
        /// <param name="comment">Comment element containing the directive.</param>
        /// <param name="pattern">Regular expression pattern.</param>
        /// <param name="group">Group key for the text to retrieve.</param>
        /// <returns>Text for the comment directive.</returns>
        protected string GetCommentDirectiveText(CommentElement comment, string pattern, string group)
        {
            string text = null;

            if (comment != null && comment.Type == CommentType.Line && !string.IsNullOrEmpty(pattern))
            {
                Regex regex = null;
                if (!_regexCache.TryGetValue(pattern, out regex))
                {
                    regex = new Regex(pattern, RegexOptions.IgnoreCase);
                    _regexCache.Add(pattern, regex);
                }

                string commentText = comment.Text.Trim();

                Match match = regex.Match(commentText);

                if (match != null && match.Length > 0)
                {
                    Group textGroup = match.Groups[group];
                    if (textGroup != null)
                    {
                        text = textGroup.Value.Trim();
                    }
                }
            }

            return text;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Writes a comment line.
        /// </summary>
        /// <param name="comment">Comment code element.</param>
        public override void VisitCommentElement(CommentElement comment)
        {
            StringBuilder builder = new StringBuilder(DefaultBlockLength);

            if (comment.Type == CommentType.Block)
            {
                builder.Append("/*");
                builder.Append(FormatCommentText(comment));
                builder.Append("*/");

                WriteTextBlock(builder.ToString());
            }
            else
            {
                if (comment.Type == CommentType.XmlLine)
                {
                    builder.Append("///");
                }
                else
                {
                    builder.Append("//");
                }

                builder.Append(FormatCommentText(comment));
                WriteIndented(builder.ToString());
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Parses a comment line.
        /// </summary>
        /// <returns>Comment code element.</returns>
        private CommentElement ParseCommentLine()
        {
            CommentElement commentLine;
            TryReadChar();

            CommentType commentType = CommentType.Line;
            if (NextChar == CSharpSymbol.BeginComment)
            {
                commentType = CommentType.XmlLine;
                TryReadChar();
            }

            string commentText = ReadLine();
            commentLine = new CommentElement(commentText, commentType);
            return commentLine;
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Actual implementation of the clone test.
 /// </summary>
 /// <param name="original">The original.</param>
 /// <param name="clone">The clone.</param>
 private void DoVerifyClone(CommentElement original, CommentElement clone)
 {
     Assert.AreEqual(original.Text, clone.Text, "Text property was not copied correctly.");
     Assert.AreEqual(original.Type, clone.Type, "IsXmlComment was not copied correctly.");
 }
Ejemplo n.º 12
0
 public void ToStringTest()
 {
     CommentElement commentLine = new CommentElement("This is some text.");
     string str = commentLine.ToString();
     Assert.AreEqual("This is some text.", str, "Unexpected string representation.");
 }