public IPostfixLookupItem CreateItem(PostfixTemplateContext context) { var expressionContext = context.OuterExpression; if (expressionContext == null || !expressionContext.CanBeStatement) { return(null); } var expression = expressionContext.Expression; var shouldCreateVariable = true; var disposableType = expression.GetPredefinedType().IDisposable; if (!disposableType.IsResolved) { return(null); } var conversionRule = expression.GetTypeConversionRule(); var expressionType = expressionContext.ExpressionType; if (context.IsAutoCompletion) { if (!expressionType.IsResolved) { return(null); } if (!expressionType.IsImplicitlyConvertibleTo(disposableType, conversionRule)) { return(null); } } if (expressionType.IsResolved && disposableType.Equals(expressionType.ToIType())) { shouldCreateVariable = false; } // check expression is local variable reference var resourceVariable = expressionContext.ReferencedElement as ITypeOwner; if (resourceVariable is ILocalVariable || resourceVariable is IParameter) { shouldCreateVariable = false; } for (ITreeNode node = expression; context.IsAutoCompletion;) { // inspect containing using statements var usingStatement = node.GetContainingNode <IUsingStatement>(); if (usingStatement == null) { break; } // check if expressions is variable declared with using statement var declaration = usingStatement.Declaration; if (resourceVariable is ILocalVariable && declaration != null) { foreach (var member in declaration.DeclaratorsEnumerable) { if (Equals(member.DeclaredElement, resourceVariable)) { return(null); } } } // check expression is already in using statement expression if (declaration == null) { foreach (var expr in usingStatement.ExpressionsEnumerable) { if (MiscUtil.AreExpressionsEquivalent(expr, expression)) { return(null); } } } node = usingStatement; } return(new UsingItem(expressionContext, shouldCreateVariable)); }