/// <summary> /// Executes the cleanup rules. /// </summary> /// <param name="options"> /// The options. /// </param> /// <param name="file"> /// The file to process. /// </param> public void Execute(ReadabilityOptions options, ICSharpFile file) { StyleCopTrace.In(options, file); bool dontPrefixCallsWithBaseUnlessLocalImplementationExists = options.SA1100DoNotPrefixCallsWithBaseUnlessLocalImplementationExists; bool codeMustNotContainEmptyStatements = options.SA1106CodeMustNotContainEmptyStatements; bool blockStatementsMustNotContainEmbeddedComments = options.SA1108BlockStatementsMustNotContainEmbeddedComments; bool blockStatementsMustNotContainEmbeddedRegions = options.SA1109BlockStatementsMustNotContainEmbeddedRegions; bool commentsMustContainText = options.SA1120CommentsMustContainText; bool useBuiltInTypeAlias = options.SA1121UseBuiltInTypeAlias; bool useStringEmptyForEmptyStrings = options.SA1122UseStringEmptyForEmptyStrings; bool dontPlaceRegionsWithinElements = options.SA1123DoNotPlaceRegionsWithinElements; bool codeMustNotContainEmptyRegions = options.SA1124CodeMustNotContainEmptyRegions; if (dontPlaceRegionsWithinElements) { this.DoNotPlaceRegionsWithinElements(file.ToTreeNode().FirstChild); } if (blockStatementsMustNotContainEmbeddedComments) { this.BlockStatementsMustNotContainEmbeddedComments(file.ToTreeNode().FirstChild); } if (blockStatementsMustNotContainEmbeddedRegions) { this.BlockStatementsMustNotContainEmbeddedRegions(file.ToTreeNode().FirstChild); } if (codeMustNotContainEmptyStatements) { this.CodeMustNotContainEmptyStatements(file.ToTreeNode().FirstChild); } if (codeMustNotContainEmptyRegions) { this.CodeMustNotContainEmptyRegions(file.ToTreeNode().FirstChild); } if (useStringEmptyForEmptyStrings) { this.ReplaceEmptyStringsWithStringDotEmpty(file.ToTreeNode().FirstChild); } if (useBuiltInTypeAlias) { SwapToBuiltInTypeAlias(file.ToTreeNode().FirstChild); } if (commentsMustContainText) { RemoveEmptyComments(file.ToTreeNode().FirstChild); } if (dontPrefixCallsWithBaseUnlessLocalImplementationExists) { this.DoNotPrefixCallsWithBaseUnlessLocalImplementationExists(file.ToTreeNode().FirstChild); } StyleCopTrace.Out(); }
/// <summary> /// The Execute method. /// </summary> /// <param name="options"> /// The options. /// </param> /// <param name="file"> /// The file. /// </param> public void Execute(LayoutOptions options, ICSharpFile file) { StyleCopTrace.In(options, file); Param.RequireNotNull(options, "options"); Param.RequireNotNull(file, "file"); bool curlyBracketsForMultiLineStatementsMustNotShareLine = options.SA1500CurlyBracketsForMultiLineStatementsMustNotShareLine; bool openingCurlyBracketsMustNotBePrecededByBlankLine = options.SA1509OpeningCurlyBracketsMustNotBePrecededByBlankLine; bool chainedStatementBlocksMustNotBePrecededByBlankLine = options.SA1510ChainedStatementBlocksMustNotBePrecededByBlankLine; bool whileDoFooterMustNotBePrecededByBlankLine = options.SA1511WhileDoFooterMustNotBePrecededByBlankLine; bool singleLineCommentsMustNotBeFollowedByBlankLine = options.SA1512SingleLineCommentsMustNotBeFollowedByBlankLine; bool closingCurlyBracketMustBeFollowedByBlankLine = options.SA1513ClosingCurlyBracketMustBeFollowedByBlankLine; bool elementDocumentationHeadersMustBePrecededByBlankLine = options.SA1514ElementDocumentationHeaderMustBePrecededByBlankLine; bool singleLineCommentsMustBeProceededByBlankLine = options.SA1515SingleLineCommentMustBeProceededByBlankLine; if (singleLineCommentsMustBeProceededByBlankLine) { this.CommentsMustBePreceededByBlankLine(file.ToTreeNode().FirstChild); } if (singleLineCommentsMustNotBeFollowedByBlankLine) { this.CommentsMustNotBeFollowedByBlankLine(file.ToTreeNode().FirstChild); } if (closingCurlyBracketMustBeFollowedByBlankLine) { ClosingCurlyBracketMustBeFollowedByBlankLine(file.ToTreeNode().FirstChild); } if (whileDoFooterMustNotBePrecededByBlankLine) { this.WhileDoFooterMustNotBePrecededByBlankLine(file.ToTreeNode().FirstChild); } if (chainedStatementBlocksMustNotBePrecededByBlankLine) { this.ChainedStatementBlocksMustNotBePrecededByBlankLine(file.ToTreeNode().FirstChild); } if (openingCurlyBracketsMustNotBePrecededByBlankLine) { this.OpeningCurlyBracketsMustNotBePrecededByBlankLine(file.ToTreeNode().FirstChild); } if (elementDocumentationHeadersMustBePrecededByBlankLine) { this.ElementDocumentationHeadersMustBePrecededByBlankLine(file.ToTreeNode().FirstChild); } if (curlyBracketsForMultiLineStatementsMustNotShareLine) { this.CurlyBracketsForMultiLineStatementsMustNotShareLine(file.ToTreeNode().FirstChild); } StyleCopTrace.Out(); }
/// <summary> /// The Execute method. /// </summary> /// <param name="options"> /// The options. /// </param> /// <param name="file"> /// The file. /// </param> public void Execute(MaintainabilityOptions options, ICSharpFile file) { StyleCopTrace.In(options, file); bool statementMustNotUseUnnecessaryParenthesis = options.SA1119StatementMustNotUseUnnecessaryParenthesis; if (statementMustNotUseUnnecessaryParenthesis) { RemoveUnnecessaryParenthesisFromStatements(file.ToTreeNode().FirstChild); } StyleCopTrace.Out(); }
/// <summary> /// Implement the Execute method. /// </summary> /// <param name="options"> /// The options. /// </param> /// <param name="file"> /// The file to use. /// </param> public void Execute(SpacingOptions options, ICSharpFile file) { StyleCopTrace.In(options, file); Param.RequireNotNull(options, "options"); Param.RequireNotNull(file, "file"); bool commasMustBeSpacedCorrectly = options.SA1001CommasMustBeSpacedCorrectly; bool singleLineCommentsMustBeginWithSingleSpace = options.SA1005SingleLineCommentsMustBeginWithSingleSpace; bool preprocessorKeywordsMustNotBePrecededBySpace = options.SA1006PreprocessorKeywordsMustNotBePrecededBySpace; bool negativeSignsMustBeSpacedCorrectly = options.SA1021NegativeSignsMustBeSpacedCorrectly; bool positiveSignsMustBeSpacedCorrectly = options.SA1022PositiveSignsMustBeSpacedCorrectly; bool codeMustNotContainMultipleWhitespaceInARow = options.SA1025CodeMustNotContainMultipleWhitespaceInARow; if (codeMustNotContainMultipleWhitespaceInARow) { this.CodeMustNotContainMultipleWhitespaceInARow(file.ToTreeNode().FirstChild); } if (commasMustBeSpacedCorrectly) { this.CommasMustBeSpacedCorrectly(file.ToTreeNode().FirstChild); } if (singleLineCommentsMustBeginWithSingleSpace) { this.SingleLineCommentsMustBeginWithSingleSpace(file.ToTreeNode().FirstChild); } if (preprocessorKeywordsMustNotBePrecededBySpace) { this.PreprocessorKeywordsMustNotBePrecededBySpace(file.ToTreeNode().FirstChild); } if (negativeSignsMustBeSpacedCorrectly) { this.NegativeAndPositiveSignsMustBeSpacedCorrectly(file.ToTreeNode().FirstChild, CSharpTokenType.MINUS); } if (positiveSignsMustBeSpacedCorrectly) { this.NegativeAndPositiveSignsMustBeSpacedCorrectly(file.ToTreeNode().FirstChild, CSharpTokenType.PLUS); } StyleCopTrace.Out(); }
private static void SwapFileHeaderNode(ICSharpFile file, string newHeader) { ITreeRange existingHeaderRange = Utils.GetFileHeaderTreeRange(file); using (WriteLockCookie.Create(file.IsPhysical())) { ICommentNode newCommentNode; if (existingHeaderRange.IsEmpty) { // existing header missing so add on a new line for our new header newHeader += Environment.NewLine; IWhitespaceNode node = file.ToTreeNode().FirstChild as IWhitespaceNode; bool insertNewLine = true; while (node != null) { if (node.IsNewLine) { insertNewLine = false; break; } node = node.NextSibling as IWhitespaceNode; } if (insertNewLine) { newHeader += Environment.NewLine; } newCommentNode = (ICommentNode) CSharpTokenType.END_OF_LINE_COMMENT.Create(new JB::JetBrains.Text.StringBuffer(newHeader), new TreeOffset(0), new TreeOffset(newHeader.Length)); LowLevelModificationUtil.AddChildBefore(file.ToTreeNode().FirstChild, new ITreeNode[] { newCommentNode }); } else { ITokenNode lastToken = (ITokenNode)existingHeaderRange.Last; ITokenNode nextToken = lastToken.GetNextToken(); if (nextToken != null) { ITokenNode nextNextToken = nextToken.GetNextToken(); if (nextNextToken != null) { ITokenNode nextNextNextToken = nextNextToken.GetNextToken(); if (!nextToken.IsNewLine() || !nextNextToken.IsNewLine()) { newHeader += Environment.NewLine; } if (nextNextNextToken.GetTokenType() == CSharpTokenType.PP_SHARP && nextToken.IsNewLine() && nextNextToken.IsNewLine()) { newHeader += Environment.NewLine; } newCommentNode = (ICommentNode) CSharpTokenType.END_OF_LINE_COMMENT.Create( new JB::JetBrains.Text.StringBuffer(newHeader), new TreeOffset(0), new TreeOffset(newHeader.Length)); LowLevelModificationUtil.ReplaceChildRange(existingHeaderRange.First, existingHeaderRange.Last, new ITreeNode[] { newCommentNode }); } } } } }
/// <summary> /// Execute comments processing for declarations. /// </summary> /// <param name="options"> /// The <see cref="OrderingOptions"/> to use. /// </param> /// <param name="file"> /// The <see cref="ICSharpFile"/> to use. /// </param> public void Execute(DocumentationOptions options, ICSharpFile file) { StyleCopTrace.In(options, file); Param.RequireNotNull(options, "options"); Param.RequireNotNull(file, "file"); foreach (ICSharpNamespaceDeclaration namespaceDeclaration in file.NamespaceDeclarations) { this.ProcessCSharpTypeDeclarations(options, file, namespaceDeclaration.TypeDeclarations); } this.ProcessCSharpTypeDeclarations(options, file, file.TypeDeclarations); bool fixSingleLineCommentsOption = options.SA1626SingleLineCommentsMustNotUseDocumentationStyleSlashes; if (fixSingleLineCommentsOption) { this.SwapDocCommentsToSingleLineComments(file.ToTreeNode().FirstChild); } this.UpdateFileHeader(options, file); StyleCopTrace.Out(); }
/// <summary> /// Gets the file header of the provided file. /// </summary> /// <param name="file"> /// The file to get the header for. /// </param> /// <returns> /// The ITreeRange of the header or TreeRange.Empty if there is no header. /// </returns> public static ITreeRange GetFileHeaderTreeRange(ICSharpFile file) { ITreeNode node = file.ToTreeNode().FirstChild; while ((node is ITokenNode) && node.IsWhitespace()) { node = node.NextSibling; } if (node is ICommentNode) { ITreeNode start = node; ICommentNode lastComment = node as ICommentNode; while ((node = node.NextSibling) != null) { if (!(node is ITokenNode) || !node.IsWhitespace()) { if (node is ICSharpCommentNode) { ICSharpCommentNode n = node as ICSharpCommentNode; if (n.CommentType == CommentType.END_OF_LINE_COMMENT) { lastComment = (ICommentNode)node; } } else { if (lastComment == null || !(start is ICommentNode)) { break; } return new TreeRange(start, lastComment); } } } } return TreeRange.Empty; }