Example #1
0
        /// <summary>
        /// The execute transaction inner.
        /// </summary>
        /// <param name="solution">
        /// The solution.
        /// </param>
        /// <param name="textControl">
        /// The text control.
        /// </param>
        public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl)
        {
            ITreeNode    element     = Utils.GetElementAtCaret(solution, textControl);
            ICommentNode commentNode = element.GetContainingNode <ICommentNode>(true);

            ReadabilityRules.MoveCommentInsideNextOpenCurlyBracket(commentNode);
        }
Example #2
0
        /// <summary>
        /// The execute transaction inner.
        /// </summary>
        /// <param name="solution">
        /// The solution.
        /// </param>
        /// <param name="textControl">
        /// The text control.
        /// </param>
        public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl)
        {
            ITreeNode    element     = Utils.GetElementAtCaret(solution, textControl);
            ICommentNode commentNode = element.GetContainingNode <ICommentNode>(true);

            ReadabilityRules.RemoveEmptyComments(commentNode);
        }
Example #3
0
        /// <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.GetContainingNode <ICommentNode>(true);
                            MoveCommentInsideNextOpenCurlyBracket(commentNode);
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    this.BlockStatementsMustNotContainEmbeddedComments(currentNode.FirstChild);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Comments must be preceded by blank line.
        /// </summary>
        /// <param name="node">
        /// The node.
        /// </param>
        private void CommentsMustBePreceededByBlankLine(ITreeNode node)
        {
            ITreeNode siblingMinus2;
            ITreeNode siblingMinus1;
            ITreeNode siblingMinus3;

            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ICommentNode && !(currentNode is IDocCommentNode))
                {
                    if (Utils.IsFirstNodeOnLine(currentNode) && !(currentNode.Parent is ICSharpFile))
                    {
                        siblingMinus1 = currentNode.PrevSibling;

                        if (siblingMinus1 != null)
                        {
                            siblingMinus2 = siblingMinus1.PrevSibling;

                            if (siblingMinus2 != null)
                            {
                                siblingMinus3 = siblingMinus2.PrevSibling;

                                ITokenNode      siblingMinus3Token          = siblingMinus3 as ITokenNode;
                                IWhitespaceNode siblingMinus2WhitespaceNode = siblingMinus2 as IWhitespaceNode;
                                IWhitespaceNode siblingMinus3WhitespaceNode = siblingMinus3 as IWhitespaceNode;

                                ICommentNode siblingMinus3CommentNode = siblingMinus3 as ICommentNode;
                                if (siblingMinus3CommentNode != null)
                                {
                                    // if the previous sibling is a comment then it doesn't need a new line.
                                    continue;
                                }

                                if (siblingMinus3Token != null && siblingMinus3Token.GetTokenType() == TokenType.LBRACE)
                                {
                                    // if we're the start of a code block then don't insert a new line.
                                    continue;
                                }

                                if (siblingMinus2WhitespaceNode == null || siblingMinus3WhitespaceNode == null || !siblingMinus2WhitespaceNode.IsNewLine ||
                                    !siblingMinus3WhitespaceNode.IsNewLine)
                                {
                                    currentNode.InsertNewLineBefore();

                                    ////CSharpFormatterHelper.FormatterInstance.Format(currentNode.Parent);
                                    ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)CSharpLanguage.Instance.LanguageService().CodeFormatter;
                                    codeFormatter.Format(currentNode.Parent);
                                }
                            }
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    this.CommentsMustBePreceededByBlankLine(currentNode.FirstChild);
                }
            }
        }
 internal LocalSuppressionWarning(
     [NotNull] string message,
     [NotNull] ICommentNode comment,
     [NonNegativeValue] int leadingWhitespaceCharacterCount,
     bool justOnce) : base(message)
 {
     this.comment = comment;
     this.leadingWhitespaceCharacterCount = leadingWhitespaceCharacterCount;
     this.justOnce = justOnce;
 }
 internal LocalSuppressionHighlighting(
     [NotNull] string message,
     [NotNull] ICommentNode comment,
     int leadingWhitespaceCharacterCount,
     bool justOnce) : base(message)
 {
     this.comment = comment;
     this.leadingWhitespaceCharacterCount = leadingWhitespaceCharacterCount;
     this.justOnce = justOnce;
 }
Example #7
0
        /// <summary>
        /// Remove empty comments.
        /// </summary>
        /// <param name="node">
        /// The node to process.
        /// </param>
        public static void RemoveEmptyComments(ITreeNode node)
        {
            // we don't remove empty lines from Element Doc Comments in here
            // the DeclarationHeader types take care of that.
            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ITokenNode)
                {
                    ICommentNode commentNode = currentNode as ICommentNode;
                    if (commentNode != null && !(commentNode is IDocCommentNode))
                    {
                        if (commentNode.CommentText.Trim() == string.Empty)
                        {
                            ITokenNode leftToken = Utils.GetFirstNewLineTokenToLeft((ITokenNode)currentNode);

                            ITokenNode rightToken = Utils.GetFirstNewLineTokenToRight((ITokenNode)currentNode);

                            if (leftToken == null)
                            {
                                leftToken = (ITokenNode)currentNode;
                            }
                            else
                            {
                                leftToken = leftToken.GetNextToken();
                            }

                            if (rightToken == null)
                            {
                                rightToken = (ITokenNode)currentNode;
                            }
                            else
                            {
                                currentNode = rightToken.GetNextToken();
                            }

                            using (WriteLockCookie.Create(true))
                            {
                                LowLevelModificationUtil.DeleteChildRange(leftToken, rightToken);
                            }
                        }
                    }
                }

                if (currentNode != null && currentNode.FirstChild != null)
                {
                    RemoveEmptyComments(currentNode.FirstChild);
                }
            }
        }
Example #8
0
        /// <summary>
        /// Single line comments must begin with single space.
        /// </summary>
        /// <param name="node">
        /// The node to use.
        /// </param>
        public static void SingleLineCommentsMustBeginWithSingleSpace(ITreeNode node)
        {
            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ICommentNode && !(currentNode is IDocCommentNode))
                {
                    ICommentNode commentNode = currentNode as ICommentNode;

                    if (commentNode.GetTokenType() == CSharpTokenType.END_OF_LINE_COMMENT && !(commentNode.Parent is ICSharpFile))
                    {
                        string originalCommentText = commentNode.CommentText;

                        // This check is to exclude comments starting with ////
                        if (!originalCommentText.StartsWith("//"))
                        {
                            int originalCommentTextLength = originalCommentText.Length;

                            string trimmedCommentText       = originalCommentText.TrimStart(' ');
                            int    trimmedCommentTextLength = trimmedCommentText.Length;

                            if (trimmedCommentTextLength != originalCommentTextLength - 1)
                            {
                                using (WriteLockCookie.Create(true))
                                {
                                    string       newText        = string.Format("// {0}", trimmedCommentText);
                                    ICommentNode newCommentNode =
                                        (ICommentNode)
                                        CSharpTokenType.END_OF_LINE_COMMENT.Create(
                                            new JetBrains.Text.StringBuffer(newText), new TreeOffset(0), new TreeOffset(newText.Length));
                                    LowLevelModificationUtil.ReplaceChildRange(currentNode, currentNode, new ITreeNode[] { newCommentNode });

                                    currentNode = newCommentNode;
                                }
                            }
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    SingleLineCommentsMustBeginWithSingleSpace(currentNode.FirstChild);
                }
            }
        }
Example #9
0
        /// <summary>
        /// Comments must not be followed by blank line.
        /// </summary>
        /// <param name="node">
        /// The node.
        /// </param>
        private void CommentsMustNotBeFollowedByBlankLine(ITreeNode node)
        {
            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ICommentNode && !(currentNode is IDocCommentNode))
                {
                    if (Utils.IsFirstNodeOnLine(currentNode))
                    {
                        ICommentNode tokenNode = currentNode as ICommentNode;

                        ITokenNode nextToken = tokenNode.GetNextToken();

                        if (nextToken != null && nextToken.IsNewLine())
                        {
                            ITokenNode nextNextToken = nextToken.GetNextToken();

                            if (nextNextToken != null)
                            {
                                ITokenNode nextNextNextToken = Utils.GetFirstNonWhitespaceTokenToRight(nextNextToken);

                                if (nextNextToken.IsNewLine() && !(nextNextNextToken is ICommentNode))
                                {
                                    ITreeNode rightNode = currentNode.FindFormattingRangeToRight();
                                    Utils.RemoveNewLineBefore(rightNode);
                                }
                            }
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    this.CommentsMustNotBeFollowedByBlankLine(currentNode.FirstChild);
                }
            }
        }
Example #10
0
        /// <summary>
        /// Comments must be preceded by blank line.
        /// </summary>
        /// <param name="node">
        /// The node.
        /// </param>
        private static void CommentsMustBePreceededByBlankLine(ITreeNode node)
        {
            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ICommentNode && !(currentNode is IDocCommentNode))
                {
                    if (Utils.IsFirstNodeOnLine(currentNode) && !(currentNode.Parent is ICSharpFile))
                    {
                        ITreeNode siblingMinus1 = currentNode.PrevSibling;

                        if (siblingMinus1 != null)
                        {
                            ITreeNode siblingMinus2 = siblingMinus1.PrevSibling;

                            if (siblingMinus2 != null)
                            {
                                ITreeNode siblingMinus3 = siblingMinus2.PrevSibling;

                                ITokenNode      siblingMinus3Token          = siblingMinus3 as ITokenNode;
                                IWhitespaceNode siblingMinus2WhitespaceNode = siblingMinus2 as IWhitespaceNode;
                                IWhitespaceNode siblingMinus3WhitespaceNode = siblingMinus3 as IWhitespaceNode;

                                ICommentNode siblingMinus3CommentNode = siblingMinus3 as ICommentNode;
                                if (siblingMinus3CommentNode != null)
                                {
                                    // if the previous sibling is a comment then it doesn't need a new line.
                                    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.
                                    continue;
                                }

                                if (siblingMinus2WhitespaceNode == null || siblingMinus3WhitespaceNode == null || !siblingMinus2WhitespaceNode.IsNewLine ||
                                    !(siblingMinus3WhitespaceNode.IsNewLine || siblingMinus3WhitespaceNode.IsWhitespace()))
                                {
                                    currentNode.InsertNewLineBefore();

                                    ////CSharpFormatterHelper.FormatterInstance.Format(currentNode.Parent);
                                    LanguageService languageService = CSharpLanguage.Instance.LanguageService();
                                    if (languageService != null)
                                    {
                                        ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)languageService.CodeFormatter;
                                        if (codeFormatter != null)
                                        {
                                            codeFormatter.Format(currentNode.Parent);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (currentNode.FirstChild != null)
                {
                    CommentsMustBePreceededByBlankLine(currentNode.FirstChild);
                }
            }
        }