Example #1
0
        private static void ApplyRenameHotspots(
            [NotNull] LiveTemplatesManager liveTemplatesManager, [NotNull] ITextControl textControl,
            [NotNull] IForeachStatement statement, [NotNull] IList <string> namesCollection,
            [CanBeNull] IReferenceExpression extraReference = null)
        {
            var variableDeclaration = statement.IteratorDeclaration;
            var endSelectionRange   = new TextRange(textControl.Caret.Offset());

            var suggestTypeName = new MacroCallExpressionNew(new SuggestVariableTypeMacroDef());
            var typeNameInfo    = new HotspotInfo(
                new TemplateField("type", suggestTypeName, 0),
                variableDeclaration.VarKeyword.GetDocumentRange());

            var nameRanges = new LocalList <DocumentRange>();

            nameRanges.Add(variableDeclaration.NameIdentifier.GetDocumentRange());

            if (extraReference != null)
            {
                var documentRange = extraReference.GetDocumentRange();
                nameRanges.Add(documentRange);
                endSelectionRange = new TextRange(documentRange.TextRange.EndOffset);
            }

            var variableNameInfo = new HotspotInfo(
                new TemplateField("name", new NameSuggestionsExpression(namesCollection), 0),
                nameRanges.ToArray());

            var session = liveTemplatesManager.CreateHotspotSessionAtopExistingText(
                statement.GetSolution(), endSelectionRange, textControl,
                LiveTemplatesManager.EscapeAction.LeaveTextAndCaret, typeNameInfo, variableNameInfo);

            session.Execute();
        }
 public virtual void VisitForeachStatement <TTypeReference, TExpression, TStatement>(
     IForeachStatement <TTypeReference, TExpression, TStatement> foreachStatement)
     where TTypeReference : ITypeReference
     where TExpression : IExpression
     where TStatement : IStatement
 {
     Visit(foreachStatement);
 }
 public static void VisitForeachStatementChildren <TTypeReference, TExpression, TStatement>(
     IForeachStatement <TTypeReference, TExpression, TStatement> foreachStatement,
     IGenericStatementVisitor visitor)
     where TTypeReference : ITypeReference
     where TExpression : IExpression
     where TStatement : IStatement
 {
     VisitIfNotNull(foreachStatement.Statement, visitor);
 }
 public override void VisitForeachStatement <TTypeReference, TExpression, TStatement>(
     IForeachStatement <TTypeReference, TExpression, TStatement> foreachStatement)
 {
     Steps.Add(new WriteForeachKeyword());
     Steps.Add(new WriteWhitespace());
     Steps.Add(new WriteStartParenthesis());
     Steps.Add(new WriteChildNode <TTypeReference>(foreachStatement.Type));
     Steps.Add(new WriteWhitespace());
     Steps.Add(new WriteName(foreachStatement.VariableName));
     Steps.Add(new WriteWhitespace());
     Steps.Add(new WriteInKeyword());
     Steps.Add(new WriteWhitespace());
     Steps.Add(new WriteExpression <TExpression>(foreachStatement.Iterator));
     Steps.Add(new WriteEndParenthesis());
     Steps.AddIndentedStatementSteps(foreachStatement.Statement);
 }
Example #5
0
        public override void VisitForeachStatement(IForeachStatement stmt, IList <IStatement> body)
        {
            if (IsTargetMatch(stmt, CompletionCase.EmptyCompletionBefore))
            {
                body.Add(EmptyCompletionExpression);
            }

            var loop = new ForEachLoop
            {
                LoopedReference = _exprVisitor.ToVariableRef(stmt.Collection, body)
            };

            body.Add(loop);

            foreach (var itDecl in stmt.IteratorDeclarations)
            {
                var localVar = itDecl.DeclaredElement.GetName <ILocalVariableName>();
                loop.Declaration = new VariableDeclaration
                {
                    Reference = new VariableReference {
                        Identifier = localVar.Name
                    },
                    Type = localVar.ValueType
                };
            }

            if (IsTargetMatch(stmt, CompletionCase.InBody))
            {
                loop.Body.Add(EmptyCompletionExpression);
            }

            if (stmt.Body != null)
            {
                stmt.Body.Accept(this, loop.Body);
            }


            if (IsTargetMatch(stmt, CompletionCase.EmptyCompletionAfter))
            {
                body.Add(EmptyCompletionExpression);
            }
        }
Example #6
0
        private static IList <string> SuggestIteratorVariableNames([NotNull] IForeachStatement statement)
        {
            var iteratorDeclaration = statement.IteratorDeclaration;
            var namingManager       = statement.GetPsiServices().Naming;

            var policyProvider = namingManager.Policy.GetPolicyProvider(
                iteratorDeclaration.Language, iteratorDeclaration.GetSourceFile());

            var collection = namingManager.Suggestion.CreateEmptyCollection(
                PluralityKinds.Single, iteratorDeclaration.Language, true, policyProvider);

            var expression = statement.Collection;

            if (expression != null)
            {
                collection.Add(expression, new EntryOptions {
                    PluralityKind          = PluralityKinds.Plural,
                    SubrootPolicy          = SubrootPolicy.Decompose,
                    PredefinedPrefixPolicy = PredefinedPrefixPolicy.Remove
                });
            }

            var variableType = iteratorDeclaration.DeclaredElement.Type;

            if (variableType.IsResolved)
            {
                collection.Add(variableType, new EntryOptions {
                    PluralityKind = PluralityKinds.Single,
                    SubrootPolicy = SubrootPolicy.Decompose
                });
            }

            collection.Prepare(iteratorDeclaration.DeclaredElement,
                               new SuggestionOptions {
                UniqueNameContext = statement
            });

            return(collection.AllNames());
        }
 protected CognitiveComplexityHintBase(ITreeNode node, DocumentOffset offset, int value)
 {
     _node       = node;
     _offset     = offset;
     Value       = value;
     Description = node switch
     {
         IWhileStatement _ => "While-Statement (increases nesting)",
         ISwitchStatement _ => "Switch-Statement (increases nesting)",
         IDoStatement _ => "Do-While-Statement (increases nesting)",
         IIfStatement _ => "If-Statement (increases nesting)",
         IForStatement _ => "For-Statement (increases nesting)",
         IForeachStatement _ => "Foreach-Statement (increases nesting)",
         ICatchClause _ => "Catch-Clause (increases nesting)",
         IGotoStatement _ => "Goto-Statement",
         IBreakStatement _ => "Break-Statement",
         IConditionalOrExpression _ => "First/alternating conditional Expression",
         IConditionalAndExpression _ => "First/alternating conditional Expression",
         ICSharpStatement _ => "If-Statement (increases nesting)",
         ICSharpExpression _ => "Recursive Call",
                         _ => throw new NotSupportedException(node.GetType().FullName)
     };
 }
Example #8
0
 public override void VisitForeachStatement(IForeachStatement foreachStatement, IHighlightingConsumer consumer)
 {
     VisitLoop(foreachStatement, consumer);
 }
 public override void VisitForeachStatement(IForeachStatement foreachStatement, IHighlightingConsumer consumer)
 {
     VisitLoop(foreachStatement, consumer);
 }
Example #10
0
        private static void CheckForeachDeclaration(
            [NotNull] IForeachStatement foreachStatement, [NotNull] IHighlightingConsumer consumer)
        {
            var collection = foreachStatement.Collection;

            if (collection == null)
            {
                return;
            }

            var collectionType = collection.Type() as IDeclaredType;

            if (collectionType == null || collectionType.IsUnknown)
            {
                return;
            }

            // no allocations because of compiler support (just like arrays)
            if (collectionType.IsString())
            {
                return;
            }

            var typeElement = collectionType.GetTypeElement();

            if (typeElement == null)
            {
                return;
            }

            // search for GetEnumerator() method
            var symbolTable = ResolveUtil.GetSymbolTableByTypeElement(
                typeElement, SymbolTableMode.FULL, typeElement.Module);

            foreach (var symbolInfo in symbolTable.GetSymbolInfos("GetEnumerator"))
            {
                var method = symbolInfo.GetDeclaredElement() as IMethod;
                if (method == null)
                {
                    continue;
                }
                if (!CSharpDeclaredElementUtil.IsForeachEnumeratorPatternMember(method))
                {
                    continue;
                }

                // with ref-return
                if (method.ReturnType.Classify == TypeClassification.REFERENCE_TYPE)
                {
                    DocumentRange range;
                    var           inToken = collection.GetPreviousMeaningfulToken();
                    if (inToken != null && inToken.GetTokenType().IsKeyword)
                    {
                        range = inToken.GetDocumentRange();
                    }
                    else
                    {
                        range = collection.GetExpressionRange();
                    }

                    consumer.AddHighlighting(
                        new ObjectAllocationHighlighting(foreachStatement,
                                                         "possible enumerator allocation (except iterators or collection with cached enumerator)"),
                        range);
                }

                break;
            }
        }
 public override void VisitForeachStatement <TTypeReference, TExpression, TStatement>(IForeachStatement <TTypeReference, TExpression, TStatement> foreachStatement)
 {
     Value = new Statement()
     {
         ForeachStatement = new ForeachStatementFactory(foreachStatement).Value
     };
 }