示例#1
0
        /// <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 ICreationExpressionInitializer)
                        {
                            ICreationExpressionInitializer creationExpressionInitializerNode = tokenNode.Parent as ICreationExpressionInitializer;
                            if (creationExpressionInitializerNode != null)
                            {
                                ITokenNode          leftBrace              = creationExpressionInitializerNode.LBrace;
                                ITokenNode          rightBrace             = creationExpressionInitializerNode.RBrace;
                                ICreationExpression creationExpressionNode = tokenNode.GetContainingNode <ICreationExpression>(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);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Replace empty strings with string dot empty.
        /// </summary>
        /// <param name="node">
        /// The node to process.
        /// </param>
        public static 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().IsStringLiteral)
                    {
                        IAttribute                   attribute            = tokenNode.GetContainingNode <IAttribute>(true);
                        ISwitchLabelStatement        switchLabelStatement = tokenNode.GetContainingNode <ISwitchLabelStatement>(true);
                        IConstantDeclaration         constantDeclaration  = tokenNode.GetContainingNode <IConstantDeclaration>(true);
                        IRegularParameterDeclaration parameterDeclaration = tokenNode.GetContainingNode <IRegularParameterDeclaration>(true);

                        // Not for attributes, switch labels, constant declarations, or parameter declarations
                        if (attribute == null && switchLabelStatement == null && constantDeclaration == null && parameterDeclaration == null)
                        {
                            string text = currentNode.GetText();
                            if (text == "\"\"" || text == "@\"\"")
                            {
                                const string NewText    = "string.Empty";
                                ITokenNode   newLiteral =
                                    (ITokenNode)
                                    CSharpTokenType.STRING_LITERAL_REGULAR.Create(new 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)
                {
                    ReadabilityRules.ReplaceEmptyStringsWithStringDotEmpty(currentNode.FirstChild);
                }
            }
        }
示例#3
0
        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)
                    {
                        IStartRegion startRegionNode = tokenNode.GetContainingNode <IStartRegion>(true);

                        IEndRegion endRegion = startRegionNode.EndRegion;

                        if (endRegion != null)
                        {
                            ITokenNode previousTokenNode = Utils.GetFirstNonWhitespaceTokenToLeft(endRegion.NumberSign);

                            IStartRegion a = previousTokenNode.GetContainingNode <IStartRegion>(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);
                }
            }
        }
        public static void SpellCheck(IDocument document, ITokenNode token, ISpellChecker spellChecker,
                                      ISolution solution, DefaultHighlightingConsumer consumer, IContextBoundSettingsStore settingsStore, StringSettings settings)
        {
            if (spellChecker == null)
            {
                return;
            }

            string buffer    = unescape(token.GetText());
            ILexer wordLexer = new WordLexer(buffer);

            wordLexer.Start();
            while (wordLexer.TokenType != null)
            {
                string tokenText = wordLexer.GetCurrTokenText();
                if (SpellCheckUtil.ShouldSpellCheck(tokenText, settings.CompiledWordsToIgnore) &&
                    !spellChecker.TestWord(tokenText, true))
                {
                    IClassMemberDeclaration containingElement =
                        token.GetContainingNode <IClassMemberDeclaration>(false);
                    if (containingElement == null ||
                        !IdentifierResolver.IsIdentifier(containingElement, solution, tokenText))
                    {
                        CamelHumpLexer camelHumpLexer = new CamelHumpLexer(buffer, wordLexer.TokenStart, wordLexer.TokenEnd);
                        foreach (LexerToken humpToken in camelHumpLexer)
                        {
                            if (SpellCheckUtil.ShouldSpellCheck(humpToken.Value, settings.CompiledWordsToIgnore) &&
                                !spellChecker.TestWord(humpToken.Value, true))
                            {
                                //int start = token.GetTreeStartOffset().Offset + wordLexer.TokenStart;
                                //int end = start + tokenText.Length;

                                //TextRange range = new TextRange(start, end);
                                //DocumentRange documentRange = new DocumentRange(document, range);

                                DocumentRange documentRange =
                                    token.GetContainingFile().TranslateRangeForHighlighting(token.GetTreeTextRange());
                                documentRange = documentRange.ExtendLeft(-wordLexer.TokenStart);
                                documentRange = documentRange.ExtendRight(-1 * (documentRange.GetText().Length - tokenText.Length));

                                TextRange textRange = new TextRange(humpToken.Start - wordLexer.TokenStart,
                                                                    humpToken.End - wordLexer.TokenStart);

                                //string word = document.GetText(range);
                                string word = documentRange.GetText();
                                consumer.AddHighlighting(
                                    new StringSpellCheckHighlighting(
                                        word,
                                        documentRange,
                                        humpToken.Value,
                                        textRange,
                                        solution,
                                        spellChecker,
                                        settingsStore),
                                    documentRange);
                                break;
                            }
                        }
                    }
                }
                wordLexer.Advance();
            }
        }