/// <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);
            IBlock    containingBlock = element.GetContainingNode <IBlock>(true);

            if (containingBlock != null)
            {
                //// CSharpFormatterHelper.FormatterInstance.Format(containingBlock);
                ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)CSharpLanguage.Instance.LanguageService().CodeFormatter;
                codeFormatter.Format(containingBlock);

                new LayoutRules().CurlyBracketsForMultiLineStatementsMustNotShareLine(containingBlock);
            }
            else
            {
                IFieldDeclaration fieldDeclarationNode = element.GetContainingNode <IFieldDeclaration>(true);
                if (fieldDeclarationNode != null)
                {
                    //// CSharpFormatterHelper.FormatterInstance.Format(fieldDeclarationNode);
                    ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)CSharpLanguage.Instance.LanguageService().CodeFormatter;
                    codeFormatter.Format(fieldDeclarationNode);

                    new LayoutRules().CurlyBracketsForMultiLineStatementsMustNotShareLine(fieldDeclarationNode);
                }
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The execute inner.
        /// </summary>
        /// <param name="solution">
        /// The solution.
        /// </param>
        /// <param name="textControl">
        /// The text control.
        /// </param>
        public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl)
        {
            IDeclaration declaration = Utils.GetDeclarationClosestToTextControl(solution, textControl);

            if (declaration != null)
            {
                string rulesNamespace = this.Rule.Namespace;

                string ruleText = string.Format("{0}:{1}", this.Rule.CheckId, this.Rule.Name);

                IContextBoundSettingsStore settingsStore = PsiSourceFileExtensions.GetSettingsStore(null, solution);

                string justificationText = settingsStore.GetValue((StyleCopOptionsSettingsKey key) => key.SuppressStyleCopAttributeJustificationText);

                IAttributesOwnerDeclaration attributesOwnerDeclaration = declaration as IAttributesOwnerDeclaration;

                if (attributesOwnerDeclaration != null)
                {
                    CSharpElementFactory factory = CSharpElementFactory.GetInstance(declaration.GetPsiModule());

                    ITypeElement typeElement = Utils.GetTypeElement(declaration, "System.Diagnostics.CodeAnalysis.SuppressMessageAttribute");

                    IAttribute attribute = factory.CreateAttribute(typeElement);

                    ICSharpArgument newArg1 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), rulesNamespace), null);

                    ICSharpArgument newArg2 = attribute.AddArgumentAfter(Utils.CreateConstructorArgumentValueExpression(declaration.GetPsiModule(), ruleText), newArg1);

                    attribute.AddArgumentAfter(Utils.CreateArgumentValueExpression(declaration.GetPsiModule(), "Justification = \"" + justificationText + "\""), newArg2);

                    attributesOwnerDeclaration.AddAttributeAfter(attribute, null);

                    IFile file = declaration.GetContainingFile();
                    if (file != null)
                    {
                        LanguageService languageService = CSharpLanguage.Instance.LanguageService();
                        if (languageService != null)
                        {
                            ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)languageService.CodeFormatter;

                            if (codeFormatter != null)
                            {
                                codeFormatter.FormatFile(file, CodeFormatProfile.DEFAULT, NullProgressIndicator.Instance);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Moves the IStartRegion specified inside the next open curly bracket and moves the corresponding end region inside too.
        /// </summary>
        /// <param name="startRegionNode">
        /// The node to move.
        /// </param>
        public static void MoveRegionInsideNextOpenCurlyBracket(IStartRegion startRegionNode)
        {
            using (WriteLockCookie.Create(true))
            {
                ITokenNode newLocationTokenNode = Utils.GetFirstNonWhitespaceTokenToRight(startRegionNode.Message);

                // if its a start region there is probably a corresponding end region
                // find it, and move it inside the block
                // find the position to delete from
                ITokenNode startOfTokensToDelete = Utils.GetFirstNewLineTokenToLeft(startRegionNode.NumberSign);
                ITokenNode endOfTokensToDelete   = Utils.GetFirstNewLineTokenToRight(startRegionNode.Message);
                ITokenNode startOfTokensToFormat = startOfTokensToDelete.GetPrevToken();

                IEndRegion   endRegionNode  = startRegionNode.EndRegion;
                IStartRegion newStartRegion = startRegionNode.Copy(null);
                ITokenNode   firstNonWhitespaceAfterBracket = Utils.GetFirstNonWhitespaceTokenToRight(newLocationTokenNode);

                LowLevelModificationUtil.AddChildBefore(firstNonWhitespaceAfterBracket, new[] { newStartRegion });

                newStartRegion.InsertNewLineAfter();

                LowLevelModificationUtil.DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete);
                IStartRegion endOfTokensToFormat = newStartRegion;

                if (endRegionNode != null)
                {
                    startOfTokensToDelete = Utils.GetFirstNewLineTokenToLeft(endRegionNode.NumberSign);
                    endOfTokensToDelete   = Utils.GetFirstNewLineTokenToRight(endRegionNode.NumberSign);

                    IEndRegion newEndRegionNode = endRegionNode.Copy(null);
                    ITokenNode newLineToken     = Utils.GetFirstNonWhitespaceTokenToLeft(endRegionNode.NumberSign);
                    LowLevelModificationUtil.AddChildBefore(newLineToken, new[] { newEndRegionNode });

                    newEndRegionNode.InsertNewLineAfter();

                    LowLevelModificationUtil.DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete);
                    endOfTokensToFormat = (IStartRegion)newLineToken;
                }

                ////CSharpFormatterHelper.FormatterInstance.Format(startOfTokensToFormat, endOfTokensToFormat);
                ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)CSharpLanguage.Instance.LanguageService().CodeFormatter;
                codeFormatter.Format(startOfTokensToFormat, endOfTokensToFormat);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Moves the comment token specified after the next available non whitespace char (normally an open curly bracket).
        /// </summary>
        /// <param name="commentTokenNode">
        /// The comment token to move.
        /// </param>
        public static void MoveCommentInsideNextOpenCurlyBracket(ITokenNode commentTokenNode)
        {
            using (WriteLockCookie.Create(true))
            {
                // move comment inside block curly bracket here
                // we copy it, then insert it and then delete the copied one
                ITokenNode startOfTokensToDelete = Utils.GetFirstNonWhitespaceTokenToLeft(commentTokenNode).GetNextToken();
                ITokenNode endOfTokensToDelete   = Utils.GetFirstNewLineTokenToRight(commentTokenNode);
                ITokenNode startOfTokensToFormat = startOfTokensToDelete.GetPrevToken();

                ITokenNode openCurlyBracketTokenNode = Utils.GetFirstNonWhitespaceTokenToRight(commentTokenNode);
                ITokenNode newCommentTokenNode       = commentTokenNode.Copy(null);
                ITokenNode tokenNodeToInsertAfter    = Utils.GetFirstNewLineTokenToRight(openCurlyBracketTokenNode);
                LowLevelModificationUtil.AddChildAfter(tokenNodeToInsertAfter, new[] { newCommentTokenNode });
                LowLevelModificationUtil.AddChildAfter(newCommentTokenNode, newCommentTokenNode.InsertNewLineAfter());

                DeleteChildRange(startOfTokensToDelete, endOfTokensToDelete);
                ITokenNode endOfTokensToFormat = newCommentTokenNode;

                ////CSharpFormatterHelper.FormatterInstance.Format(startOfTokensToFormat, endOfTokensToFormat);
                ICSharpCodeFormatter codeFormatter = (ICSharpCodeFormatter)CSharpLanguage.Instance.LanguageService().CodeFormatter;
                codeFormatter.Format(startOfTokensToFormat, endOfTokensToFormat);
            }
        }
Ejemplo n.º 6
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);
                }
            }
        }