Skip to content

Commit

Permalink
Do not leave trailing white space when formatting comments
Browse files Browse the repository at this point in the history
Fixes codecadwallader#360.

Using the setting to add whitespace between XML tags and content would leave a trailing whitespace in cases when the containing XML tags are on their own line.
  • Loading branch information
w5l committed Apr 26, 2018
1 parent 18d7a74 commit 8c1cc89
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
47 changes: 47 additions & 0 deletions CodeMaid.UnitTests/Formatting/XmlFormattingTests.cs
Expand Up @@ -43,6 +43,53 @@ public void XmlFormattingTests_AddSpaceToTagContent()
CommentFormatHelper.AssertEqualAfterFormat(input, expected);
}

[TestMethod]
[TestCategory("Formatting UnitTests")]
public void XmlFormattingTests_AddSpaceToTagContentWithSelfClosingTag()
{
var input = "<summary><see/></summary>";
var expected = "<summary> <see/> </summary>";

Settings.Default.Formatting_CommentXmlSplitSummaryTagToMultipleLines = false;
Settings.Default.Formatting_CommentXmlSpaceTags = true;

CommentFormatHelper.AssertEqualAfterFormat(input, expected);
}

[TestMethod]
[TestCategory("Formatting UnitTests")]
public void XmlFormattingTests_AddSpaceToTagContentWithSelfClosingTagMultiline()
{
var input = "<summary><see/></summary>";
var expected =
"<summary>" + Environment.NewLine +
"<see/>" + Environment.NewLine +
"</summary>";

Settings.Default.Formatting_CommentXmlSplitSummaryTagToMultipleLines = true;
Settings.Default.Formatting_CommentXmlSpaceTags = true;

CommentFormatHelper.AssertEqualAfterFormat(input, expected);
}

[TestMethod]
[TestCategory("Formatting UnitTests")]
public void XmlFormattingTests_AddSpaceToTagContentShouldLeaveNoTrailingWhitespace()
{
var input = "<summary>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</summary>";
var expected =
"<summary>" + Environment.NewLine +
"Lorem ipsum dolor sit amet," + Environment.NewLine +
"consectetur adipiscing elit." + Environment.NewLine +
"</summary>";

Settings.Default.Formatting_CommentWrapColumn = 30;
Settings.Default.Formatting_CommentXmlSplitSummaryTagToMultipleLines = true;
Settings.Default.Formatting_CommentXmlSpaceTags = true;

CommentFormatHelper.AssertEqualAfterFormat(input, expected);
}

[TestMethod]
[TestCategory("Formatting UnitTests")]
public void XmlFormattingTests_AllRootLevelTagsOnNewLine()
Expand Down
20 changes: 10 additions & 10 deletions CodeMaid/Model/Comments/CommentFormatter.cs
Expand Up @@ -216,10 +216,6 @@ private bool Parse(ICommentLine line, int indentLevel = 0, int xmlTagLength = 0)
{
NewLine();
}
else if (!_isFirstWord && Settings.Default.Formatting_CommentXmlSpaceTags)
{
Append(CodeCommentHelper.Spacer);
}

// Always consider the word after the opening tag as the first word to prevent an
// extra space before.
Expand Down Expand Up @@ -305,11 +301,6 @@ private bool Parse(ICommentLine line, int indentLevel = 0, int xmlTagLength = 0)
}
}

if (!forceBreak && Settings.Default.Formatting_CommentXmlSpaceTags)
{
Append(CodeCommentHelper.Spacer);
}

if (_currentPosition == 0 || _currentPosition > _commentPrefixLength && forceBreak)
{
// This comment fitted on a single line.
Expand Down Expand Up @@ -366,6 +357,11 @@ private bool ParseXml(CommentLineXml line, int indentLevel = 0)
// If true the tag should be alone on it's own line.
tagOnOwnLine |= isLiteralContent;

if (!tagOnOwnLine && Settings.Default.Formatting_CommentXmlSpaceTags)
{
Append(CodeCommentHelper.Spacer);
}

// If the literal content of an XML tag is set, output that content without formatting.
if (isLiteralContent)
{
Expand Down Expand Up @@ -412,9 +408,13 @@ private bool ParseXml(CommentLineXml line, int indentLevel = 0)
if (tagOnOwnLine && !_isFirstWord)
{
NewLine();
Indent(indentLevel);
}
else if (Settings.Default.Formatting_CommentXmlSpaceTags)
{
Append(CodeCommentHelper.Spacer);
}

Indent(indentLevel);
Append(line.CloseTag);

return tagOnOwnLine || CommentLineXml.SingleLineElementNames.Contains(line.TagName, StringComparer.OrdinalIgnoreCase);
Expand Down

0 comments on commit 8c1cc89

Please sign in to comment.