/// <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); }
public CommentFormatter(ICommentLine line, FormatterOptions formatterOptions, CommentOptions commentOptions) { _formatterOptions = formatterOptions; _commentOptions = commentOptions; _builder = new StringBuilder(); _currentPosition = 0; _isFirstWord = true; _isIndented = false; _commentPrefixLength = WordLength(commentOptions.Prefix); // Special handling for the root XML line, it should not output it's surrounding xml // tags, only it's child lines. var xml = line as CommentLineXml; if (xml != null) { // On the content of the root, fix the optional alignment of param tags. This is not // important if all tags will be broken onto seperate lines anyway. if (!Settings.Default.Formatting_CommentXmlSplitAllTags && Settings.Default.Formatting_CommentXmlAlignParamTags) { var paramPhrases = xml.Lines.OfType <CommentLineXml>().Where(p => string.Equals(p.TagName, "param", StringComparison.OrdinalIgnoreCase)); if (paramPhrases.Count() > 1) { var longestParam = paramPhrases.Max(p => p.OpenTag.Length); foreach (var phrase in paramPhrases) { phrase.OpenTag = phrase.OpenTag.PadRight(longestParam); } } } // Process all the lines inside the root XML line. foreach (var l in xml.Lines) { NewLine(); Parse(l); } } else { // Normal comment line has no child-lines and can be processed normally. NewLine(); Parse(line); } }
/// <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()); }
public CommentFormatter(ICommentLine line, FormatterOptions formatterOptions, CommentOptions commentOptions) { _formatterOptions = formatterOptions; _commentOptions = commentOptions; _builder = new StringBuilder(); _currentPosition = 0; _isFirstWord = true; _isPrefixWritten = false; _isIndented = false; _indentAmount = 0; _commentPrefixLength = WordLength(commentOptions.Prefix); // Special handling for the root XML line, it should not output it's surrounding xml // tags, only it's child lines. if (line is CommentLineXml xml) { // On the content of the root, fix the optional alignment of param tags. if (_formatterOptions.Xml.AlignParamTags) { AlignParamTags(xml); } // Process all the lines inside the root XML line. foreach (var l in xml.Lines) { NewLine(); Format(l); } } else { // Normal comment line has no child-lines and can be processed normally. Format(line); } }