public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (!context.CanBeStatement) return; if (!context.LooseChecks && context.ExpressionType.IsUnknown) return; var declaration = context.ContainingFunction; if (declaration != null && !declaration.IsAsync && !declaration.IsIterator) { var declaredElement = declaration.DeclaredElement; if (declaredElement != null) { var returnType = declaredElement.ReturnType; if (returnType.IsVoid()) return; if (!context.LooseChecks) { var rule = context.Expression.GetTypeConversionRule(); if (!rule.IsImplicitlyConvertibleTo(context.ExpressionType, returnType)) return; } consumer.Add(new PostfixLookupItem(context, "return", "return $EXPR$")); } } // todo: yield }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (!context.CanBeStatement || context.ExpressionType.IsUnknown) return; if (!context.Expression.IsPure()) return; // todo: better fix? string lengthProperty = null; if (context.ExpressionType is IArrayType) lengthProperty = "Length"; else { var predefined = context.Expression.GetPredefinedType(); var rule = context.Expression.GetTypeConversionRule(); if (rule.IsImplicitlyConvertibleTo(context.ExpressionType, predefined.GenericICollection)) lengthProperty = "Count"; } if (lengthProperty == null) return; var forTemplate = string.Format("for (var $NAME$ = 0; $NAME$ < $EXPR$.{0}; $NAME$++) ", lengthProperty); var forrTemplate = string.Format("for (var $NAME$ = $EXPR$.{0}; $NAME$ >= 0; $NAME$--) ", lengthProperty); consumer.Add(new NameSuggestionPostfixLookupItem( context, "for", forTemplate, context.Expression, PluralityKinds.Plural, ScopeKind.LocalSelfScoped)); consumer.Add(new NameSuggestionPostfixLookupItem( context, "forr", forrTemplate, context.Expression, PluralityKinds.Plural, ScopeKind.LocalSelfScoped)); }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (context.ExpressionType.IsUnknown) { if (!context.LooseChecks) return; } else { var canBeNull = context.ExpressionType.IsNullable() || (context.ExpressionType.Classify == TypeClassification.REFERENCE_TYPE); if (!canBeNull) return; } var state = CSharpControlFlowNullReferenceState.UNKNOWN; if (!context.LooseChecks) { var declaredElement = context.ExpressionReferencedElement; var declaration = context.ContainingFunction; if (declaration != null && declaredElement != null && declaration.IsPhysical()) { var graph = CSharpControlFlowBuilder.Build(declaration); if (graph != null) { var result = graph.Inspect(ValueAnalysisMode.OPTIMISTIC); if (!result.HasComplexityOverflow) { var referenceExpression = context.ReferenceExpression; foreach (var element in graph.AllElements) if (element.SourceElement == referenceExpression) { state = result.GetVariableStateAt(element, declaredElement); break; } } } } } switch (state) { case CSharpControlFlowNullReferenceState.MAY_BE_NULL: case CSharpControlFlowNullReferenceState.UNKNOWN: { if (context.CanBeStatement) { consumer.Add(new PostfixLookupItem(context, "notnull", "if ($EXPR$ != null) ")); consumer.Add(new PostfixLookupItem(context, "null", "if ($EXPR$ == null) ")); } else { consumer.Add(new PostfixLookupItem(context, "notnull", "$EXPR$ != null")); consumer.Add(new PostfixLookupItem(context, "null", "$EXPR$ == null")); } break; } } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (context.CanBeStatement) { if (context.LooseChecks || context.ExpressionType.IsBool()) consumer.Add(new PostfixLookupItem(context, "whilenot", "while (!$EXPR$) $CARET$")); } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { string lengthPropertyName; if (CreateItems(context, out lengthPropertyName)) { consumer.Add(new ForLookupItem(context.InnerExpression, lengthPropertyName)); } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { var typeElement = context.ExpressionReferencedElement as ITypeElement; if (typeElement != null) { consumer.Add(new PostfixLookupItem(context, "list", "List<$EXPR$>$CARET$")); } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (context.ExpressionType.IsString()) { consumer.Add(context.CanBeStatement ? new PostfixLookupItem(context, "ifempty", "if (string.IsNullOrEmpty($EXPR$)) ") : new PostfixLookupItem(context, "ifempty", "string.IsNullOrEmpty($EXPR$)")); } }
// todo: detect relational expressions public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (context.CanBeStatement) { // todo: smart caret? stay in condition when loose? if (context.ExpressionType.IsBool() || context.LooseChecks) consumer.Add(new PostfixLookupItem(context, "if", "if ($EXPR$) $CARET$")); } }
public void CreateItems( PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { foreach (var exprContext in context.Expressions) { var type = exprContext.Type; if (type.IsResolved && type.IsString()) { consumer.Add(new LookupItem("tryParse", exprContext, context.LookupItemsOwner, true)); break; } } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (context.CanBeStatement) { var declaration = context.ContainingFunction; if (declaration == null) return; // only in constructors by default if (context.LooseChecks || declaration.DeclaredElement is IConstructor) { consumer.Add(new IntroduceFieldLookupItem(context)); } } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { var function = context.ContainingFunction; if (function == null) return; if (context.LooseChecks || function.IsAsync) if (context.ExpressionType.IsTask() || context.ExpressionType.IsGenericTask()) { // check expression is not already awaited var awaitExpression = AwaitExpressionNavigator.GetByTask( context.ReferenceExpression.GetContainingParenthesizedExpression() as IUnaryExpression); if (awaitExpression == null) consumer.Add(new PostfixLookupItem(context, "await", "await $EXPR$")); } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (!context.LooseChecks) { if (!context.ExpressionType.IsBool()) return; // do not show if expression is already negated var unary = UnaryOperatorExpressionNavigator.GetByOperand( context.ReferenceExpression.GetContainingParenthesizedExpression() as IUnaryExpression); if (unary != null && unary.OperatorSign.GetTokenType() != CSharpTokenType.EXCL) return; } consumer.Add(new PostfixLookupItem(context, "not", "!$EXPR$")); }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (context.CanBeStatement) { if (!context.LooseChecks) { if (context.ExpressionType.IsUnknown) return; var rule = context.Expression.GetTypeConversionRule(); var predefinedType = context.Expression.GetPredefinedType(); if (!rule.IsImplicitlyConvertibleTo(context.ExpressionType, predefinedType.Exception)) return; } consumer.Add(new PostfixLookupItem(context, "throw", "throw $EXPR$")); } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (!context.CanBeStatement || context.ExpressionType.IsUnknown) return; // todo: check for 'foreach pattern' // todo: support untyped collections // todo: infer type by indexer like F# var predefined = context.Expression.GetPredefinedType(); var rule = context.Expression.GetTypeConversionRule(); if (rule.IsImplicitlyConvertibleTo(context.ExpressionType, predefined.IEnumerable)) { consumer.Add(new NameSuggestionPostfixLookupItem( context, "foreach", "foreach (var $NAME$ in $EXPR$) $CARET$", context.Expression, PluralityKinds.Plural, ScopeKind.LocalSelfScoped)); } }
// todo: available here: var alreadyHasVar = smth.using public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (!context.CanBeStatement) return; // check declaration var predefined = context.Expression.GetPredefinedType(); var rule = context.Expression.GetTypeConversionRule(); if (!rule.IsImplicitlyConvertibleTo(context.ExpressionType, predefined.IDisposable)) return; // check expression is local variable reference ILocalVariable usingVar = null; var expr = context.Expression as IReferenceExpression; if (expr != null && expr.QualifierExpression == null) usingVar = expr.Reference.Resolve().DeclaredElement as ILocalVariable; ITreeNode node = context.Expression; while (true) // inspect containing using statements { // todo: get out of ISandBox? var usingStatement = node.GetContainingNode<IUsingStatement>(); if (usingStatement == null) break; // check if expressions is variable declared with using statement var declaration = usingStatement.Declaration; if (usingVar != null && declaration != null) foreach (var member in declaration.DeclaratorsEnumerable) if (Equals(member.DeclaredElement, usingVar)) return; // check expression is already in using statement expression if (declaration == null) foreach (var e in usingStatement.ExpressionsEnumerable) if (MiscUtil.AreExpressionsEquivalent(e, context.Expression)) return; node = usingStatement; } consumer.Add(new NameSuggestionPostfixLookupItem( context, "using", "using (var $NAME$ = $EXPR$) $CARET$", context.Expression)); }
public void CreateItems( PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { foreach (var expressionContext in context.Expressions) { if (expressionContext.Type.IsBool() || IsBooleanExpression(expressionContext.Expression)) { if (CreateBooleanItems(expressionContext, consumer)) return; } } if (context.ForceMode) { foreach (var expressionContext in context.Expressions) { if (CreateBooleanItems(expressionContext, consumer)) return; } } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { if (context.Expression is IReferenceExpression) { // filter out too simple locals expressions var target = context.ExpressionReferencedElement; if (target == null || target is IParameter || target is ILocalVariable) return; } if (context.CanBeStatement) { consumer.Add(new NameSuggestionPostfixLookupItem( context, "var", "var $NAME$ = $EXPR$", context.Expression)); } else if (context.LooseChecks) { consumer.Add(new IntroduceVariableLookupItem(context)); } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { var typeElement = context.ExpressionReferencedElement as ITypeElement; if (typeElement is IStruct || typeElement is IEnum || typeElement is IClass) { // filter out abstract classes var classType = typeElement as IClass; if (classType != null && classType.IsAbstract) return; // check type has any constructor accessable var accessContext = new ElementAccessContext(context.Expression); foreach (var constructor in typeElement.Constructors) { if (!constructor.IsStatic && AccessUtil.IsSymbolAccessible(constructor, accessContext)) { consumer.Add(new PostfixLookupItem(context, "new", "new $EXPR$($CARET$)")); break; } } } }
public void CreateItems(PostfixTemplateAcceptanceContext context, ICollection<ILookupItem> consumer) { var exprContext = context.OuterExpression; if (!exprContext.CanBeStatement) return; if (!context.ForceMode) { if (exprContext.Type.IsUnknown) return; if (!IsNullableType(exprContext.Type)) return; } var state = CSharpControlFlowNullReferenceState.UNKNOWN; if (!context.ForceMode) state = CheckNullabilityState(exprContext); switch (state) { case CSharpControlFlowNullReferenceState.MAY_BE_NULL: case CSharpControlFlowNullReferenceState.UNKNOWN: consumer.Add(new LookupItem("null", exprContext, "if(expr==null)")); break; } }