Skip to content

Commit

Permalink
Port CodeCleanup bug fixes from CodePlex
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjayGuntur committed Jan 29, 2017
1 parent 9c98a78 commit be4a85e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/StyleCop.ReSharper/CodeCleanup/Rules/LayoutRules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ private static void CommentsMustBePreceededByBlankLine(ITreeNode node)
continue;
}

if (siblingMinus3 is ISwitchLabelStatement)
{
// if we're the start of a switch block then don't insert a new line.
continue;
}

if (siblingMinus3Token != null && siblingMinus3Token.GetTokenType() == CSharpTokenType.LBRACE)
{
// if we're the start of a code block then don't insert a new line.
Expand Down Expand Up @@ -423,7 +429,7 @@ private static void CommentsMustNotBeFollowedByBlankLine(ITreeNode node)
{
ITokenNode nextNextNextToken = Utils.GetFirstNonWhitespaceTokenToRight(nextNextToken);

if (nextNextToken.IsNewLine() && !(nextNextNextToken is ICommentNode))
if (nextNextToken.IsNewLine() && !(nextNextNextToken is ICommentNode) && !Utils.IsCommentInFileHeader(currentNode))
{
ITreeNode rightNode = currentNode.FindFormattingRangeToRight();
Utils.RemoveNewLineBefore(rightNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static void RemoveEmptyComments(ITreeNode node)
ICommentNode commentNode = currentNode as ICommentNode;
if (commentNode != null && !(commentNode is IDocCommentNode))
{
if (commentNode.CommentText.Trim() == string.Empty)
if (commentNode.CommentText.Trim() == string.Empty && !Utils.IsCommentInFileHeader(currentNode))
{
ITokenNode leftToken = Utils.GetFirstNewLineTokenToLeft((ITokenNode)currentNode);

Expand Down
18 changes: 18 additions & 0 deletions src/StyleCop.ReSharper/Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,24 @@ public static ITreeNode InsertWhitespaceAfter(ITreeNode currentNode, int count)
return leafElement;
}

public static bool IsCommentInFileHeader(ITreeNode comment)
{
for (ITreeNode currentNode = comment; currentNode != null; currentNode = currentNode.PrevSibling)
{
if (currentNode is ICommentNode || currentNode is IWhitespaceNode || currentNode is IPreprocessorDirective)
{
continue;
}

if (currentNode.NodeType != null)
{
return false;
}
}

return true;
}

/// <summary>
/// Indicates whether the type of the constructor passed in is a struct.
/// </summary>
Expand Down

0 comments on commit be4a85e

Please sign in to comment.