/// <summary> /// Do not place regions within elements. /// </summary> /// <param name="node"> /// The node to process. /// </param> private void DoNotPlaceRegionsWithinElements(ITreeNode node) { for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.PP_START_REGION) { IStartRegionNode startRegionNode = tokenNode.GetContainingElement <IStartRegionNode>(true); if (startRegionNode != null) { if (startRegionNode.Parent is IBlock) { // we're in a block so remove the end and start region IEndRegionNode endRegionNode = startRegionNode.EndRegion; using (WriteLockCookie.Create(true)) { LowLevelModificationUtil.DeleteChild(endRegionNode); LowLevelModificationUtil.DeleteChild(startRegionNode); } } } } } if (currentNode.FirstChild != null) { this.DoNotPlaceRegionsWithinElements(currentNode.FirstChild); } } }
/// <summary> /// Block statements must not contain embedded regions. /// </summary> /// <param name="node"> /// The node to process. /// </param> private void BlockStatementsMustNotContainEmbeddedRegions(ITreeNode node) { for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.LBRACE) { ITokenNode previousTokenNode = Utils.GetFirstNonWhitespaceTokenToLeft(tokenNode); ITokenNode previousTokenNode2 = previousTokenNode.GetPrevToken(); if (previousTokenNode.GetTokenType() == CSharpTokenType.PP_MESSAGE && previousTokenNode2.GetTokenType() == CSharpTokenType.PP_START_REGION) { IStartRegionNode startRegionNode = previousTokenNode.GetContainingElement <IStartRegionNode>(true); if (startRegionNode != null) { MoveRegionInsideNextOpenCurlyBracket(startRegionNode); } } } } if (currentNode.FirstChild != null) { this.BlockStatementsMustNotContainEmbeddedRegions(currentNode.FirstChild); } } }
/// <summary> /// Block statements must not contain embedded comments. /// </summary> /// <param name="node"> /// The node to process. /// </param> private void BlockStatementsMustNotContainEmbeddedComments(ITreeNode node) { for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.LBRACE) { ITokenNode previousTokenNode = Utils.GetFirstNonWhitespaceTokenToLeft(tokenNode); if (previousTokenNode.GetTokenType() == CSharpTokenType.END_OF_LINE_COMMENT) { ICommentNode commentNode = previousTokenNode.GetContainingElement <ICommentNode>(true); MoveCommentInsideNextOpenCurlyBracket(commentNode); } } } if (currentNode.FirstChild != null) { this.BlockStatementsMustNotContainEmbeddedComments(currentNode.FirstChild); } } }
/// <summary> /// Curly brackets for multi line statements must not share line. /// </summary> /// <param name="node"> /// The node. /// </param> public void CurlyBracketsForMultiLineStatementsMustNotShareLine(ITreeNode node) { int offsetColumn = 0; for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.LBRACE) { // check to see if this LBRACE { is on a line with another non whitespace token if (tokenNode.Parent is ICreationExpressionInitializerNode) { ICreationExpressionInitializerNode creationExpressionInitializerNode = tokenNode.Parent as ICreationExpressionInitializerNode; if (creationExpressionInitializerNode != null) { ITokenNode leftBrace = creationExpressionInitializerNode.LBrace; ITokenNode rightBrace = creationExpressionInitializerNode.RBrace; ICreationExpressionNode creationExpressionNode = tokenNode.GetContainingElement <ICreationExpressionNode>(true); if (creationExpressionNode != null) { ITreeNode first = creationExpressionNode.FirstChild; ITreeNode last = creationExpressionNode.LastChild; if (Utils.HasLineBreakBetween(first, last)) { if (Utils.TokenHasNonWhitespaceTokenToRightOnSameLine(leftBrace)) { // We'll be 4-1 honest. offsetColumn = Utils.GetOffsetToStartOfLine(leftBrace); ITreeNode newLine = leftBrace.InsertNewLineAfter(); Utils.InsertWhitespaceAfter(newLine, offsetColumn + 3); } if (rightBrace != null) { // check to see if this RBRACE { is on a line with another non whitespace token if (Utils.TokenHasNonWhitespaceTokenToLeftOnSameLine(rightBrace)) { ITreeNode newLine = rightBrace.InsertNewLineBefore(); Utils.InsertWhitespaceAfter(newLine, offsetColumn); } } } } } } } } if (currentNode.FirstChild != null) { this.CurlyBracketsForMultiLineStatementsMustNotShareLine(currentNode.FirstChild); } } }
/// <summary> /// Replace empty strings with string dot empty. /// </summary> /// <param name="node"> /// The node to process. /// </param> private void ReplaceEmptyStringsWithStringDotEmpty(ITreeNode node) { for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.STRING_LITERAL) { IAttribute attribute = tokenNode.GetContainingElement <IAttribute>(true); ISwitchLabelStatement switchLabelStatement = tokenNode.GetContainingElement <ISwitchLabelStatement>(true); IConstantDeclaration constantDeclaration = tokenNode.GetContainingElement <IConstantDeclaration>(true); // Not for attributes switch labels or constant declarations if (attribute == null && switchLabelStatement == null && constantDeclaration == null) { string text = currentNode.GetText(); if (text == "\"\"" || text == "@\"\"") { const string NewText = "string.Empty"; ITokenNode newLiteral = (ITokenNode) CSharpTokenType.STRING_LITERAL.Create(new JB::JetBrains.Text.StringBuffer(NewText), new TreeOffset(0), new TreeOffset(NewText.Length)); using (WriteLockCookie.Create(true)) { LowLevelModificationUtil.ReplaceChildRange(currentNode, currentNode, new ITreeNode[] { newLiteral }); } } } } } if (currentNode.FirstChild != null) { this.ReplaceEmptyStringsWithStringDotEmpty(currentNode.FirstChild); } } }
/// <summary>Analyzes the specified statement.</summary> /// <param name="node">The node.</param> /// <returns>Returns the suggestion base.</returns> public SuggestionBase[] Analyze(ITokenNode node) { var type = node.GetTokenType(); if (!type.IsStringLiteral) { return null; } if (node.Language.Name != "CSHARP") { return null; } if (node.GetText() != "\"\"") { return null; } var parent = node.Parent; if (parent == null) { return null; } if (parent.Parent is ISwitchLabelStatement) { return null; } var attribute = node.GetContainingElement(typeof(IAttribute), true) as IAttribute; if (attribute != null) { return null; } var suggestions = new List<SuggestionBase> { new StringEmptySuggestion(this.solution, node) }; return suggestions.ToArray(); }
private void CodeMustNotContainEmptyRegions(ITreeNode node) { for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling) { if (currentNode is ITokenNode) { ITokenNode tokenNode = currentNode as ITokenNode; if (tokenNode.GetTokenType() == CSharpTokenType.PP_START_REGION) { IStartRegionNode startRegionNode = tokenNode.GetContainingElement <IStartRegionNode>(true); IEndRegionNode endRegion = startRegionNode.EndRegion; if (endRegion != null) { ITokenNode previousTokenNode = Utils.GetFirstNonWhitespaceTokenToLeft(endRegion.NumberSign); IStartRegionNode a = previousTokenNode.GetContainingElement <IStartRegionNode>(true); if (a != null && a.Equals(startRegionNode)) { using (WriteLockCookie.Create(true)) { ModificationUtil.DeleteChild(startRegionNode); ModificationUtil.DeleteChild(endRegion); } } } } } if (currentNode.FirstChild != null) { this.CodeMustNotContainEmptyRegions(currentNode.FirstChild); } } }