Esempio n. 1
0
        /// <summary>
        /// Formats the current line using the settings as defined in ReSharper &gt; Option &gt; Languages &gt; Common &gt; Code Style Sharing.
        /// </summary>
        /// <param name="solution">
        /// Current Solution.
        /// </param>
        /// <param name="textControl">
        /// Current Text Control.
        /// </param>
        public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl)
        {
            JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine> line = Utils.GetLineNumberForTextControl(textControl);
            ITreeNode       element           = Utils.GetElementAtCaret(solution, textControl);
            IUsingDirective containingElement = element.GetContainingNode <IUsingDirective>(true);

            Utils.FormatLines(element.Language, solution, textControl.Document, line.Minus1(), line.Plus1());

            if (containingElement != null)
            {
                new SpacingRules().EqualsMustBeSpacedCorrectly(containingElement);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the QuickFix with all the available BulbItems that can fix the current
        /// StyleCop Violation.
        /// </summary>
        protected override void InitialiseBulbItems()
        {
            JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine> line =
                (JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine>) this.Highlighting.LineNumber;

            string target = this.Highlighting.DocumentRange.Document.GetLineText(line.Minus1());

            target = target.Contains("++") ? "++" : "--";

            this.BulbItems = new List <IBulbAction>
            {
                new FormatLineBulbItem
                {
                    DocumentRange = this.Highlighting.DocumentRange,
                    Description   = "Fix Spacing : " + this.Highlighting.ToolTip,
                    LineNumber    = this.Highlighting.LineNumber,
                    Target        = target
                }
            };
        }
Esempio n. 3
0
        private static void HandleElement(ITextControl editor, ITreeNode element, int offset)
        {
            string stringToInsert = Clipboard.GetText();

            if (string.IsNullOrEmpty(stringToInsert))
            {
                return;
            }

            IDocCommentNode docCommentNode = element as IDocCommentNode;

            if (docCommentNode != null)
            {
                JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine> currentLineNumber =
                    editor.Document.GetCoordsByOffset(editor.Caret.Offset()).Line;
                string currentLine = editor.Document.GetLineText(currentLineNumber);
                int    index       = currentLine.IndexOf("///", StringComparison.Ordinal);
                if (index < 0)
                {
                    return;
                }
                string prefix = currentLine.Substring(0, index);

                if (ShallEscape(docCommentNode, editor.Caret.Offset()) &&
                    JetBrains.UI.RichText.RichTextBlockToHtml.HtmlEncode(stringToInsert) != stringToInsert &&
                    MessageBox.ShowYesNo("Do you want the text to be escaped?"))
                {
                    stringToInsert = JetBrains.UI.RichText.RichTextBlockToHtml.HtmlEncode(stringToInsert);
                }

                stringToInsert = stringToInsert.Replace("\n", "\n" + prefix + "///");
            }

            ITokenNode token = element as ITokenNode;

            if (token != null)
            {
                if (token.GetTokenType() == CSharpTokenType.STRING_LITERAL &&
                    offset < token.GetTreeTextRange().EndOffset.Offset)
                {
                    string text = token.GetText();
                    if (text.StartsWith("@") && offset > token.GetTreeTextRange().StartOffset.Offset + 1)
                    {
                        stringToInsert = stringToInsert.Replace("\"", "\"\"");
                    }
                    else if (!text.StartsWith("@"))
                    {
                        stringToInsert = stringToInsert.
                                         Replace("\\", "\\\\").
                                         Replace("\a", "\\a").
                                         Replace("\b", "\\b").
                                         Replace("\f", "\\f").
                                         Replace("\n", "\\n").
                                         Replace("\r", "\\r").
                                         Replace("\t", "\\t").
                                         Replace("\v", "\\v").
                                         Replace("\'", "\\'").
                                         Replace("\"", "\\\"");
                    }
                }
            }

            editor.Document.InsertText(editor.Caret.Offset(), stringToInsert);
        }
Esempio n. 4
0
        /// <summary>
        /// Closing curly bracket must be followed by blank line.
        /// </summary>
        /// <param name="node">
        /// The node to use.
        /// </param>
        public static void ClosingCurlyBracketMustBeFollowedByBlankLine(ITreeNode node)
        {
            // Closing curly brackets must be followed by a newline unless they are closing an object initializer or
            // followed by one of the endtokens defined here.
            // catch
            // finally
            // else
            // rbrace
            // dowhile
            // preprocessor directives
            List <TokenNodeType> tokensThatFollowClosingCurlyBracketWithoutNewLine = new List <TokenNodeType>
            {
                CSharpTokenType.RBRACE,
                CSharpTokenType.DO_KEYWORD,
                CSharpTokenType.ELSE_KEYWORD,
                CSharpTokenType.CATCH_KEYWORD,
                CSharpTokenType.FINALLY_KEYWORD
            };

            List <TokenNodeType> objectInitializerFollowers = new List <TokenNodeType>
            {
                CSharpTokenType.AS_KEYWORD,
                CSharpTokenType.IS_KEYWORD,
                CSharpTokenType.COMMA,
                CSharpTokenType.SEMICOLON,
                CSharpTokenType.DOT,
                CSharpTokenType.QUEST,
                CSharpTokenType.COLON,
                CSharpTokenType.RPARENTH,
                CSharpTokenType.EQEQ,
                CSharpTokenType.GE,
                CSharpTokenType.GT,
                CSharpTokenType.LE,
                CSharpTokenType.LT,
                CSharpTokenType.NE,
                CSharpTokenType.MINUS,
                CSharpTokenType.PLUS,
                CSharpTokenType.DIV,
                CSharpTokenType.ASTERISK,
                CSharpTokenType.PERC,
                CSharpTokenType.MINUSMINUS,
                CSharpTokenType.PLUSPLUS
            };

            for (ITreeNode currentNode = node; currentNode != null; currentNode = currentNode.NextSibling)
            {
                if (currentNode is ITokenNode)
                {
                    ITokenNode tokenNode = currentNode as ITokenNode;

                    if (tokenNode.GetTokenType() == CSharpTokenType.RBRACE)
                    {
                        IBlock blockNode = tokenNode.Parent as IBlock;

                        if (blockNode != null)
                        {
                            JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine> lineNumberForLBrace = Utils.GetLineNumberForElement(blockNode.LBrace);
                            JetBrains.Util.dataStructures.TypedIntrinsics.Int32 <DocLine> lineNumberForRBrace = Utils.GetLineNumberForElement(blockNode.RBrace);

                            if (lineNumberForLBrace != lineNumberForRBrace)
                            {
                                ITokenNode currentToken = tokenNode.GetNextToken();

                                int newLineCount = 0;
                                while (currentToken != null)
                                {
                                    if (currentToken.IsWhitespace())
                                    {
                                        if (currentToken.IsNewLine())
                                        {
                                            newLineCount++;
                                            if (newLineCount == 2)
                                            {
                                                // if we get 2 new lines we've already got a blank line after the closing curly bracket so jog on.
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if ((!tokensThatFollowClosingCurlyBracketWithoutNewLine.Contains(currentToken.GetTokenType()) &&
                                             !objectInitializerFollowers.Contains(currentToken.GetTokenType())) ||
                                            (objectInitializerFollowers.Contains(currentToken.GetTokenType()) && newLineCount == 1))
                                        {
                                            tokenNode.GetNextToken().InsertNewLineBefore();
                                        }

                                        break;
                                    }

                                    currentToken = currentToken.GetNextToken();
                                }
                            }
                        }
                    }
                }

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