Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not leave trailing white space when formatting comments #546

Merged
merged 1 commit into from
Apr 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions CodeMaid.UnitTests/Formatting/XmlFormattingTests.cs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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