Exemplo n.º 1
0
        /// <summary>
        /// Helper function to generate the preview in the options menu.
        /// </summary>
        public static string FormatXml(string text)
        {
            var xml       = XElement.Parse($"<doc>{text}</doc>");
            var line      = new CommentLineXml(xml);
            var regex     = CodeCommentHelper.GetCommentRegex(CodeLanguage.CSharp, false);
            var formatter = new CommentFormatter(line, "///", 4, regex);

            return(formatter.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Helper function to generate the preview in the options menu.
        /// </summary>
        public static string FormatXml(string text, CodeCommentOptions options)
        {
            var xml       = XElement.Parse(string.Format("<doc>{0}</doc>", text));
            var line      = new CommentLineXml(xml, options);
            var regex     = CodeCommentHelper.GetCommentRegex("CSharp", false);
            var formatter = new CommentFormatter(line, "///", options, regex);

            return(formatter.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Formats the comment.
        /// </summary>
        public TextPoint Format()
        {
            if (!IsValid)
            {
                throw new InvalidOperationException("Cannot format comment, the comment is not valid.");
            }

            var originalText = _startPoint.GetText(_endPoint);
            var matches      = _commentLineRegex.Matches(originalText).OfType <Match>().ToArray();

            var commentOptions = new CommentOptions
            {
                Prefix = matches.First(m => m.Success).Groups["prefix"].Value ?? string.Empty,
                Regex  = CodeCommentHelper.GetCommentRegex(_document.GetCodeLanguage(), false)
            };

            // Concatenate the comment lines without comment prefixes and see if the resulting bit
            // can be parsed as XML.
            ICommentLine line        = null;
            var          lineTexts   = matches.Select(m => m.Groups["line"].Value).ToArray();
            var          commentText = string.Join(Environment.NewLine, lineTexts);

            if (commentText.Contains('<'))
            {
                try
                {
                    var xml = XElement.Parse($"<doc>{commentText}</doc>");
                    line = new CommentLineXml(xml);
                }
                catch (System.Xml.XmlException)
                {
                    // If XML cannot be parsed, comment will be handled as a normal text comment.
                }
            }

            if (line == null)
            {
                line = new CommentLine(commentText);
            }

            var formatter = new CommentFormatter(
                line,
                _formatterOptions,
                commentOptions);

            if (!formatter.Equals(originalText))
            {
                var cursor = StartPoint.CreateEditPoint();
                cursor.Delete(EndPoint);
                cursor.Insert(formatter.ToString());
                _endPoint = cursor.CreateEditPoint();
            }

            return(EndPoint);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Helper function to generate the preview in the options menu.
        /// </summary>
        public static string FormatXml(string text, string prefix = "///")
        {
            var xml = XElement.Parse($"<doc>{text}</doc>");

            var formatter = new CommentFormatter(
                new CommentLineXml(xml),
                new FormatterOptions
            {
                IgnoreTokens = new[] { "TODO: " },
                TabSize      = 4
            },
                new CommentOptions
            {
                Prefix = prefix,
                Regex  = CodeCommentHelper.GetCommentRegex(CodeLanguage.CSharp, !string.IsNullOrWhiteSpace(prefix))
            });

            return(formatter.ToString());
        }
        /// <summary>
        /// Helper function to generate the preview in the options menu.
        /// </summary>
        public static string Format(string text, string prefix = null, Action <FormatterOptions> options = null)
        {
            var xml = XElement.Parse($"<doc>{text}</doc>");

            var formatterOptions = FormatterOptions
                                   .FromSettings(Properties.Settings.Default)
                                   .Set(o => o.IgnoreTokens = new[] { "TODO: " });

            options?.Invoke(formatterOptions);

            var commentOptions = new CommentOptions
            {
                Prefix = prefix,
                Regex  = CodeCommentHelper.GetCommentRegex(CodeLanguage.CSharp, !string.IsNullOrWhiteSpace(prefix))
            };

            var formatter = new CommentFormatter(
                new CommentLineXml(xml, formatterOptions),
                formatterOptions,
                commentOptions);

            return(formatter.ToString());
        }