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

Fix magically added slashes #647

Merged
merged 2 commits into from
Apr 19, 2019
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
35 changes: 31 additions & 4 deletions CodeMaid.UnitTests/Formatting/FormatWithPrefixTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SteveCadwallader.CodeMaid.Properties;
using System;

namespace SteveCadwallader.CodeMaid.UnitTests.Formatting
{
Expand Down Expand Up @@ -30,8 +30,35 @@ public void SimpleFormatWithPrefixTests_KeepsPrefix()
[TestCategory("Formatting UnitTests")]
public void SimpleFormatWithPrefixTests_TrimsTrailingSpace()
{
var input = "// ";
var expected = "//";
var input = "// Trailing space ";
var expected = "// Trailing space";
CommentFormatHelper.AssertEqualAfterFormat(input, expected, "//");
}

[TestMethod]
[TestCategory("Formatting UnitTests")]
public void SimpleFormatWithPrefixTests_TrimsTrailingLines()
{
var input =
"// Comment with some trailing lines" + Environment.NewLine +
"//" + Environment.NewLine +
"//";
var expected =
"// Comment with some trailing lines";
CommentFormatHelper.AssertEqualAfterFormat(input, expected, "//");
}

[TestMethod]
[TestCategory("Formatting UnitTests")]
public void SimpleFormatWithPrefixTests_TrimsLeadingLines()
{
var input =
"//" + Environment.NewLine +
"//" + Environment.NewLine +
"// Comment with some leading lines";
var expected =
"// Comment with some leading lines";

CommentFormatHelper.AssertEqualAfterFormat(input, expected, "//");
}

Expand Down
8 changes: 8 additions & 0 deletions CodeMaid/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; Top-most EditorConfig file
root = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!


[*.{cs,vb}]
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
121 changes: 71 additions & 50 deletions CodeMaid/Model/Comments/CommentFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ internal class CommentFormatter : IEquatable<string>
private readonly FormatterOptions _formatterOptions;
private int _commentPrefixLength;
private int _currentPosition;
private bool _isAfterCommentPrefix;
private int _indentAmount;
private bool _isFirstWord;
private bool _isIndented;
private bool _isPrefixWritten;

public CommentFormatter(ICommentLine line, FormatterOptions formatterOptions, CommentOptions commentOptions)
{
Expand All @@ -29,7 +30,9 @@ public CommentFormatter(ICommentLine line, FormatterOptions formatterOptions, Co
_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
Expand All @@ -52,7 +55,6 @@ public CommentFormatter(ICommentLine line, FormatterOptions formatterOptions, Co
else
{
// Normal comment line has no child-lines and can be processed normally.
NewLine();
Format(line);
}
}
Expand All @@ -62,19 +64,6 @@ public bool Equals(string other)
return string.Equals(ToString(), other);
}

public void Indent(int amount)
{
if (!_isIndented)
{
if (amount > 0)
{
Append(string.Empty.PadLeft(amount));
}
_isIndented = true;
_isFirstWord = true;
}
}

public override string ToString()
{
return _builder.ToString().TrimEnd();
Expand Down Expand Up @@ -103,27 +92,60 @@ private void Append(char value)
Append(value.ToString());
}

private void Append(string value)
/// <summary>
/// Append value to current line. When line is still empty, this first writes the comment
/// prefix, indenting and initial spacer before appending the given value. Empty values are
/// ignored, but the comment prefix will be added if required.
/// </summary>
/// <param name="value">The string to append to the writer.</param>
/// <param name="noIdenting">
/// <c>true</c> if value should not be indented, eg for literal content.
/// </param>
private void Append(string value, bool noIdenting = false)
{
if (!_isPrefixWritten)
{
_builder.Append(_commentOptions.Prefix);
_currentPosition += _commentPrefixLength;
_isPrefixWritten = true;
_isIndented = false;
}

if (string.IsNullOrEmpty(value))
{
return;
}
if (_isAfterCommentPrefix)

if (!_isIndented)
{
_builder.Append(CodeCommentHelper.Spacer);
_isAfterCommentPrefix = false;
if (!noIdenting)
{
// Empty prefix also means no initial spacing.
if (_commentPrefixLength > 0)
{
_builder.Append(CodeCommentHelper.Spacer);
_currentPosition += 1;
}

if (_indentAmount > 0)
{
_builder.Append(string.Empty.PadLeft(_indentAmount));
_currentPosition += _indentAmount;
}

_isIndented = true;
}
}

_builder.Append(value);
_currentPosition += WordLength(value);
_isFirstWord = false;
}

/// <summary>
/// Parse a code comment line into a string and write it to the buffer.
/// Parse a comment line into individual words and write it to the buffer.
/// </summary>
/// <param name="line">The comment line.</param>
/// <param name="indentAmount">The amount of indenting for the content of this tag.</param>
/// <param name="xmlTagLength">
/// The length of the enclosing XML tags, this is needed to calculate the line length for
/// single line XML comments.
Expand All @@ -134,11 +156,11 @@ private void Append(string value)
/// <returns>
/// <c>true</c> if line fitted on single line, <c>false</c> if it wrapped on multiple lines.
/// </returns>
private bool Format(ICommentLine line, int indentAmount = 0, int xmlTagLength = 0, bool xmlSpaceParentTagContent = false)
private bool Format(ICommentLine line, int xmlTagLength = 0, bool xmlSpaceParentTagContent = false)
{
if (line is CommentLineXml xml)
{
return FormatXml(xml, indentAmount);
return FormatXml(xml);
}

if (line.Content == null)
Expand Down Expand Up @@ -181,7 +203,7 @@ private bool Format(ICommentLine line, int indentAmount = 0, int xmlTagLength =
if (!forceBreak && matchCount == 1 && matches[0].Words.Any())
{
// Calculate the length of the first line.
var firstLineLength = _commentPrefixLength + xmlTagLength + matches[0].Length + indentAmount;
var firstLineLength = _commentPrefixLength + xmlTagLength + matches[0].Length + _indentAmount;

// If set to skip wrapping on the last word, the last word's length does not matter.
if (_formatterOptions.SkipWrapOnLastWord)
Expand Down Expand Up @@ -216,8 +238,6 @@ private bool Format(ICommentLine line, int indentAmount = 0, int xmlTagLength =
NewLine();
fittedOnLine = false;
}

Indent(indentAmount);
}

if (match.IsList)
Expand All @@ -231,7 +251,6 @@ private bool Format(ICommentLine line, int indentAmount = 0, int xmlTagLength =

if (!match.IsEmpty)
{
Indent(indentAmount);
var wordCount = match.Words.Count - 1;

for (int i = 0; i <= wordCount; i++)
Expand All @@ -258,7 +277,6 @@ private bool Format(ICommentLine line, int indentAmount = 0, int xmlTagLength =
if (wrap)
{
NewLine();
Indent(indentAmount);
fittedOnLine = false;

// If linewrap is on a list item, add extra spacing to align the text
Expand All @@ -282,12 +300,10 @@ private bool Format(ICommentLine line, int indentAmount = 0, int xmlTagLength =
}
else
{
// Line without words, create a blank line.
if (!_isFirstWord)
{
NewLine();
}
// Line without words, create a blank line. First end the current line.
NewLine();

// And then force a newline creating an empty one.
NewLine(true);
fittedOnLine = false;
}
Expand All @@ -300,7 +316,7 @@ private bool Format(ICommentLine line, int indentAmount = 0, int xmlTagLength =
/// Returns <c>true</c> if the line requests a break afterwards (did not fit on a single
/// line), otherwise <c>false</c>.
/// </returns>
private bool FormatXml(CommentLineXml xml, int indentAmount)
private bool FormatXml(CommentLineXml xml)
{
var isLiteralContent = !string.IsNullOrEmpty(xml.Content);
var split = xml.TagOptions.Split;
Expand All @@ -322,7 +338,6 @@ private bool FormatXml(CommentLineXml xml, int indentAmount)
NewLine();
}

Indent(indentAmount);
Append(xml.TagOptions.KeepTogether ? CodeCommentHelper.FakeToSpace(xml.OpenTag) : xml.OpenTag);

// Self closing tags have no content, skip all further logic and just output.
Expand All @@ -346,16 +361,17 @@ private bool FormatXml(CommentLineXml xml, int indentAmount)
}

// Increase the indenting.
indentAmount += xml.TagOptions.Indent;
_indentAmount += xml.TagOptions.Indent;

if (isLiteralContent)
{
// If the literal content of an XML tag is set, output that content without formatting.
var literals = xml.Content.Trim('\r', '\n').TrimEnd('\r', '\n', '\t', ' ').Split('\n');
for (int i = 0; i < literals.Length; i++)
{
if (i > 0) NewLine(true);
Append(literals[i].TrimEnd());
if (i > 0)
NewLine(true);
Append(literals[i].TrimEnd(), true);
}
}
else
Expand All @@ -365,19 +381,18 @@ private bool FormatXml(CommentLineXml xml, int indentAmount)

foreach (var line in xml.Lines)
{
if (!Format(line, indentAmount, xmlTagLength, xml.TagOptions.SpaceContent))
if (!Format(line, xmlTagLength, xml.TagOptions.SpaceContent))
split |= XmlTagNewLine.BeforeClose | XmlTagNewLine.AfterClose;
}
}

// Remove the indenting.
indentAmount -= xml.TagOptions.Indent;
_indentAmount -= xml.TagOptions.Indent;

// If opening tag was on own line, do the same for the closing tag.
if (split.HasFlag(XmlTagNewLine.BeforeClose))
{
NewLine();
Indent(indentAmount);
}
else if (xml.TagOptions.SpaceContent)
{
Expand All @@ -388,7 +403,7 @@ private bool FormatXml(CommentLineXml xml, int indentAmount)

if (split.HasFlag(XmlTagNewLine.AfterClose))
{
if (!xml.IsLast)
//if (!xml.IsLast)
{
NewLine();
}
Expand All @@ -399,21 +414,27 @@ private bool FormatXml(CommentLineXml xml, int indentAmount)
return true;
}

/// <summary>
/// Appends a new line to the buffer, unless buffer already is on an empty new line.
/// </summary>
/// <param name="force">
/// If <c>true</c>, creates a new line even if the current line is empty.
/// </param>
private void NewLine(bool force = false)
{
if (_isFirstWord && force)
{
Append(string.Empty);
}

if (!_isFirstWord || force)
{
_builder.AppendLine();
_currentPosition = 0;
}

_builder.Append(_commentOptions.Prefix);
_currentPosition += _commentPrefixLength;
_isFirstWord = true;
_isIndented = false;

// Cannot simply be true, because an empty prefix also means no initial spacing.
_isAfterCommentPrefix = _commentPrefixLength > 0;
_isPrefixWritten = false;
_isFirstWord = true;
}
}

/// <summary>
Expand Down