Beispiel #1
0
        public IList <IPostfixLookupItem> CollectItems([NotNull] PostfixTemplateContext context, [CanBeNull] string templateName = null)
        {
            var store    = context.Reference.GetSettingsStore();
            var settings = store.GetKey <PostfixTemplatesSettings>(SettingsOptimization.OptimizeDefault);

            settings.DisabledProviders.SnapshotAndFreeze();

            var innerExpression = context.InnerExpression; // shit happens

            if (innerExpression != null && innerExpression.ReferencedElement is INamespace)
            {
                return(EmptyList <IPostfixLookupItem> .InstanceList);
            }

            var lookupItems = new List <IPostfixLookupItem>();

            foreach (var info in myTemplateProvidersInfos)
            {
                // check disabled providers
                {
                    bool isEnabled;
                    if (!settings.DisabledProviders.TryGet(info.SettingsKey, out isEnabled))
                    {
                        isEnabled = !info.Metadata.DisabledByDefault;
                    }

                    if (!isEnabled)
                    {
                        continue;
                    }
                }

                if (templateName != null)
                {
                    var name = info.Metadata.TemplateName;
                    if (!string.Equals(templateName, name, StringComparison.Ordinal))
                    {
                        continue;
                    }
                }

                var lookupItem = info.Provider.CreateItem(context);
                if (lookupItem != null)
                {
                    lookupItems.Add(lookupItem);
                }
            }

            return(lookupItems);
        }
Beispiel #2
0
        public bool IsTemplateAvailableByName(PostfixTemplateContext context, string templateName)
        {
            var specificContext = (TPostfixTemplateContext)context;

            foreach (var templateRegistration in GetEnabledTemplates(specificContext))
            {
                if (string.Equals(templateRegistration.Metadata.TemplateName, templateName, StringComparison.Ordinal))
                {
                    var templateInfo = templateRegistration.Template.TryCreateInfo(specificContext);
                    if (templateInfo != null)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #3
0
        public void ExecuteTemplateByName(PostfixTemplateContext context, string templateName, ITextControl textControl, TextRange nameRange)
        {
            var specificContext = (TPostfixTemplateContext)context;

            foreach (var templateRegistration in GetEnabledTemplates(specificContext))
            {
                if (string.Equals(templateRegistration.Metadata.TemplateName, templateName, StringComparison.Ordinal))
                {
                    var postfixTemplateInfo = templateRegistration.Template.TryCreateInfo(specificContext);
                    if (postfixTemplateInfo != null)
                    {
                        var behavior = templateRegistration.Template.CreateBehavior(postfixTemplateInfo);
                        behavior.Accept(
                            textControl, nameRange, LookupItemInsertType.Insert, Suffix.Empty, context.ExecutionContext.Solution, false);
                    }
                }
            }
        }
Beispiel #4
0
        public static PrefixExpressionContext[] FindExpressionWithValuesContexts([NotNull] PostfixTemplateContext context,
                                                                                 [CanBeNull] Predicate <ICSharpExpression> predicate = null)
        {
            var results = new LocalList <PrefixExpressionContext>();

            foreach (var expressionContext in context.Expressions.Reverse())
            {
                if (IsValidExpressionWithValue(expressionContext.Expression))
                {
                    if (predicate == null || predicate(expressionContext.Expression))
                    {
                        results.Add(expressionContext);
                    }
                }
            }

            return(results.ToArray());
        }
        public PrefixExpressionContext([NotNull] PostfixTemplateContext postfixContext, [NotNull] ICSharpExpression expression)
        {
            PostfixContext = postfixContext;
            Expression     = expression;
            CanBeStatement = GetContainingStatement() != null;

            var brokenType = IsBrokenAsExpressionCase(expression, postfixContext.Reference)
                             ?? IsBrokenByAwaitCase(expression, postfixContext.Reference);

            ExpressionType = brokenType ?? expression.GetExpressionType();
            Type           = brokenType ?? expression.Type();

            var referenceExpression = expression as IReferenceExpression;

            if (referenceExpression != null)
            {
                var resolveResult = referenceExpression.Reference.Resolve().Result;
                ReferencedElement = resolveResult.DeclaredElement;

                var element = ReferencedElement as ITypeElement;
                if (element != null)
                {
                    ReferencedType = TypeFactory.CreateType(element, resolveResult.Substitution);
                }
            }

            var predefinedTypeExpression = expression as IPredefinedTypeExpression;

            if (predefinedTypeExpression != null)
            {
                var typeName = predefinedTypeExpression.PredefinedTypeName;
                if (typeName != null)
                {
                    var resolveResult = typeName.Reference.Resolve().Result;
                    ReferencedElement = resolveResult.DeclaredElement;

                    var element = ReferencedElement as ITypeElement;
                    if (element != null)
                    {
                        ReferencedType = TypeFactory.CreateType(element, resolveResult.Substitution);
                    }
                }
            }
        }