Exemplo n.º 1
0
        public override IDictionary <string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None)
        {
            if (!options.HasFlag(GetMemberOptions.DeclaredOnly) && (_functionAttrs == null || _functionAttrs.Count == 0))
            {
                return(ProjectState.ClassInfos[BuiltinTypeId.Function].GetAllMembers(moduleContext, options));
            }

            Dictionary <string, IAnalysisSet> res;

            if (options.HasFlag(GetMemberOptions.DeclaredOnly))
            {
                res = new Dictionary <string, IAnalysisSet>();
            }
            else
            {
                res = new Dictionary <string, IAnalysisSet>(ProjectState.ClassInfos[BuiltinTypeId.Function].Instance.GetAllMembers(moduleContext));
            }

            if (_functionAttrs != null)
            {
                foreach (var variable in _functionAttrs)
                {
                    IAnalysisSet existing;
                    if (!res.TryGetValue(variable.Key, out existing))
                    {
                        res[variable.Key] = variable.Value.Types;
                    }
                    else
                    {
                        res[variable.Key] = existing.Union(variable.Value.TypesNoCopy);
                    }
                }
            }
            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets all members of this class that are not inherited from its base classes.
        /// </summary>
        public IDictionary <string, IAnalysisSet> GetAllImmediateMembers(IModuleContext moduleContext, GetMemberOptions options)
        {
            var result = new Dictionary <string, IAnalysisSet>(Scope.VariableCount);

            foreach (var v in Scope.AllVariables)
            {
                if (!options.ForEval())
                {
                    v.Value.ClearOldValues();
                }
                if (v.Value.VariableStillExists)
                {
                    result[v.Key] = v.Value.Types;
                }
            }

            if (!options.HasFlag(GetMemberOptions.DeclaredOnly))
            {
                if (!result.ContainsKey("__doc__"))
                {
                    result["__doc__"] = GetObjectMember(moduleContext, "__doc__");
                }
                if (!result.ContainsKey("__class__"))
                {
                    result["__class__"] = GetObjectMember(moduleContext, "__class__");
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public override IDictionary <string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None)
        {
            IDictionary <string, IAnalysisSet> result;

            if (options.HasFlag(GetMemberOptions.DeclaredOnly))
            {
                result = GetAllImmediateMembers(moduleContext, options);
            }
            else
            {
                result = _mro.GetAllMembers(moduleContext, options);
            }

            if (_metaclass != null)
            {
                foreach (var type in _metaclass.Types)
                {
                    if (type.Push())
                    {
                        try {
                            foreach (var nameValue in type.GetAllMembers(moduleContext))
                            {
                                result[nameValue.Key] = nameValue.Value.GetDescriptor(null, this, type, Instance.ProjectState._evalUnit);
                            }
                        } finally {
                            type.Pop();
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the available names at the given location.  This includes
        /// built-in variables, global variables, and locals.
        /// </summary>
        /// <param name="location">
        /// The location in the file where the available members should be
        /// looked up.
        /// </param>
        /// <remarks>New in 2.2</remarks>
        public IEnumerable <MemberResult> GetAllAvailableMembers(SourceLocation location, GetMemberOptions options = GetMemberOptions.IntersectMultipleResults)
        {
            var result = new Dictionary <string, IEnumerable <AnalysisValue> >();

            // collect builtins
            if (!options.HasFlag(GetMemberOptions.ExcludeBuiltins))
            {
                foreach (var variable in ProjectState.BuiltinModule.GetAllMembers(ProjectState._defaultContext))
                {
                    result[variable.Key] = new List <AnalysisValue>(variable.Value);
                }
            }

            // collect variables from user defined scopes
            var scope = FindScope(location);

            foreach (var s in scope.EnumerateTowardsGlobal)
            {
                foreach (var kvp in s.GetAllMergedVariables())
                {
                    // deliberately overwrite variables from outer scopes
                    result[kvp.Key] = new List <AnalysisValue>(kvp.Value.TypesNoCopy);
                }
            }

            var res = MemberDictToResultList(GetPrivatePrefix(scope), options, result);

            if (options.Keywords())
            {
                res = GetKeywordMembers(options, scope).Union(res);
            }

            return(res);
        }
Exemplo n.º 5
0
        public override IDictionary <string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None)
        {
            if (options.HasFlag(GetMemberOptions.DeclaredOnly))
            {
                return(new Dictionary <string, IAnalysisSet>());
            }

            return(_builtinInfo.GetAllMembers(moduleContext, options));
        }
Exemplo n.º 6
0
        public override IDictionary <string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None)
        {
            var res = new Dictionary <string, IAnalysisSet>();

            if (_instanceAttrs != null)
            {
                foreach (var kvp in _instanceAttrs)
                {
                    var types = kvp.Value.TypesNoCopy;
                    var key   = kvp.Key;
                    kvp.Value.ClearOldValues();
                    if (kvp.Value.VariableStillExists)
                    {
                        MergeTypes(res, key, types);
                    }
                }
            }

            // check and see if it's defined in a base class instance as well...
            if (!options.HasFlag(GetMemberOptions.DeclaredOnly))
            {
                foreach (var b in _classInfo.Bases)
                {
                    foreach (var ns in b)
                    {
                        if (ns.Push())
                        {
                            try {
                                ClassInfo baseClass = ns as ClassInfo;
                                if (baseClass != null &&
                                    baseClass.Instance._instanceAttrs != null)
                                {
                                    foreach (var kvp in baseClass.Instance._instanceAttrs)
                                    {
                                        kvp.Value.ClearOldValues();
                                        if (kvp.Value.VariableStillExists)
                                        {
                                            MergeTypes(res, kvp.Key, kvp.Value.TypesNoCopy);
                                        }
                                    }
                                }
                            } finally {
                                ns.Pop();
                            }
                        }
                    }
                }

                foreach (var classMem in _classInfo.GetAllMembers(moduleContext))
                {
                    MergeTypes(res, classMem.Key, classMem.Value);
                }
            }
            return(res);
        }
Exemplo n.º 7
0
        public override IDictionary <string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None)
        {
            var mro = ClassInfo._mro;

            if (!mro.IsValid)
            {
                return(new Dictionary <string, IAnalysisSet>());
            }

            if (options.HasFlag(GetMemberOptions.DeclaredOnly))
            {
                return(Values.Mro.GetAllMembersOfMro(mro.Skip(1).Take(1), moduleContext, options));
            }

            // First item in MRO list is always the class itself.
            return(Values.Mro.GetAllMembersOfMro(mro.Skip(1), moduleContext, options));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Evaluates a given expression and returns a list of members which
        /// exist in the expression.
        ///
        /// If the expression is an empty string returns all available members
        /// at that location.
        /// </summary>
        /// <param name="exprText">The expression to find members for.</param>
        /// </param>
        /// <param name="location">
        /// The location in the file where the expression should be evaluated.
        /// </param>
        /// <remarks>New in 2.2</remarks>
        public IEnumerable <MemberResult> GetMembers(
            string exprText,
            SourceLocation location,
            GetMemberOptions options = GetMemberOptions.IntersectMultipleResults
            )
        {
            if (exprText.Length == 0)
            {
                return(GetAllAvailableMembers(location, options));
            }

            var scope         = FindScope(location);
            var privatePrefix = GetPrivatePrefixClassName(scope);

            var expr = Statement.GetExpression(GetAstFromText(exprText, privatePrefix).Body);

            if (expr is ConstantExpression && ((ConstantExpression)expr).Value is int)
            {
                // no completions on integer ., the user is typing a float
                return(Enumerable.Empty <MemberResult>());
            }

            var errorWalker = new ErrorWalker();

            expr.Walk(errorWalker);
            if (errorWalker.HasError)
            {
                return(null);
            }

            var          unit = GetNearestEnclosingAnalysisUnit(scope);
            var          eval = new ExpressionEvaluator(unit.CopyForEval(), scope, mergeScopes: true);
            IAnalysisSet lookup;

            if (options.HasFlag(GetMemberOptions.NoMemberRecursion))
            {
                lookup = eval.EvaluateNoMemberRecursion(expr);
            }
            else
            {
                lookup = eval.Evaluate(expr);
            }

            return(GetMemberResults(lookup, scope, options));
        }
Exemplo n.º 9
0
        public override IDictionary<string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None) {
            var res = new Dictionary<string, IAnalysisSet>();
            if (_instanceAttrs != null) {
                foreach (var kvp in _instanceAttrs) {
                    var types = kvp.Value.TypesNoCopy;
                    var key = kvp.Key;
                    kvp.Value.ClearOldValues();
                    if (kvp.Value.VariableStillExists) {
                        MergeTypes(res, key, types);
                    }
                }
            }

            // check and see if it's defined in a base class instance as well...
            if (!options.HasFlag(GetMemberOptions.DeclaredOnly)) {
                foreach (var b in _classInfo.Bases) {
                    foreach (var ns in b) {
                        if (ns.Push()) {
                            try {
                                ClassInfo baseClass = ns as ClassInfo;
                                if (baseClass != null &&
                                    baseClass.Instance._instanceAttrs != null) {
                                    foreach (var kvp in baseClass.Instance._instanceAttrs) {
                                        kvp.Value.ClearOldValues();
                                        if (kvp.Value.VariableStillExists) {
                                            MergeTypes(res, kvp.Key, kvp.Value.TypesNoCopy);
                                        }
                                    }
                                }
                            } finally {
                                ns.Pop();
                            }
                        }
                    }
                }

                foreach (var classMem in _classInfo.GetAllMembers(moduleContext)) {
                    MergeTypes(res, classMem.Key, classMem.Value);
                }
            }
            return res;
        }
Exemplo n.º 10
0
 public static bool Intersect(this GetMemberOptions self) => self.HasFlag(GetMemberOptions.IntersectMultipleResults);
Exemplo n.º 11
0
        public override IDictionary<string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None) {
            if (options.HasFlag(GetMemberOptions.DeclaredOnly)) {
                return new Dictionary<string, IAnalysisSet>();
            }

            return _builtinInfo.GetAllMembers(moduleContext, options);
        }
Exemplo n.º 12
0
        private AP.Completion[] ToCompletions(MemberResult[] memberResult, GetMemberOptions options) {
            AP.Completion[] res = new AP.Completion[memberResult.Length];
            for (int i = 0; i < memberResult.Length; i++) {
                var member = memberResult[i];

                res[i] = new AP.Completion() {
                    name = member.Name,
                    completion = member.Completion,
                    doc = member.Documentation,
                    memberType = member.MemberType
                };

                if (options.HasFlag(GetMemberOptions.DetailedInformation)) {
                    List<AP.CompletionValue> values = new List<AnalysisProtocol.CompletionValue>();

                    foreach (var value in member.Values) {
                        var descComps = new List<AP.DescriptionComponent>();
                        if (value is FunctionInfo) {
                            var def = ((FunctionInfo)value).FunctionDefinition;
                            ((FunctionInfo)value).GetDescription((text, kind) => {
                                descComps.Add(new AP.DescriptionComponent(text, kind));
                            });
                        } else if (value is ClassInfo) {
                            FillClassDescription(descComps, ((ClassInfo)value).ClassDefinition);
                        }
                        values.Add(
                            new AP.CompletionValue() {
                                description = descComps.ToArray(),
                                doc = value.Documentation,
                                locations = value.Locations.Select(x => MakeReference(x, VariableType.Definition)).ToArray()
                            }
                        );
                    }
                    res[i].detailedValues = values.ToArray();
                }
            }
            return res;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets the available names at the given location.  This includes
        /// built-in variables, global variables, and locals.
        /// </summary>
        /// <param name="location">
        /// The location in the file where the available members should be
        /// looked up.
        /// </param>
        /// <remarks>New in 2.2</remarks>
        public IEnumerable<MemberResult> GetAllAvailableMembers(SourceLocation location, GetMemberOptions options = GetMemberOptions.IntersectMultipleResults)
        {
            var result = new Dictionary<string, IEnumerable<AnalysisValue>>();

            // collect builtins
            if (!options.HasFlag(GetMemberOptions.ExcludeBuiltins)) {
                foreach (var variable in ProjectState.BuiltinModule.GetAllMembers(ProjectState._defaultContext)) {
                    result[variable.Key] = new List<AnalysisValue>(variable.Value);
                }
            }

            // collect variables from user defined scopes
            var scope = FindScope(location);
            foreach (var s in scope.EnumerateTowardsGlobal) {
                foreach (var kvp in s.GetAllMergedVariables()) {
                    // deliberately overwrite variables from outer scopes
                    result[kvp.Key] = new List<AnalysisValue>(kvp.Value.TypesNoCopy);
                }
            }

            var res = MemberDictToResultList(GetPrivatePrefix(scope), options, result);
            if (options.Keywords()) {
                res = GetKeywordMembers(options, scope).Union(res);
            }

            return res;
        }
Exemplo n.º 14
0
        private AP.Completion[] ToCompletions(MemberResult[] memberResult, GetMemberOptions options) {
            AP.Completion[] res = new AP.Completion[memberResult.Length];
            for (int i = 0; i < memberResult.Length; i++) {
                var member = memberResult[i];

                res[i] = new AP.Completion() {
                    name = member.Name,
                    completion = member.Completion,
                    doc = member.Documentation,
                    memberType = member.MemberType
                };

                if (options.HasFlag(GetMemberOptions.DetailedInformation)) {
                    List<AP.CompletionValue> values = new List<AnalysisProtocol.CompletionValue>();

                    foreach (var value in member.Values) {
                        var descComps = Array.Empty<AP.DescriptionComponent>();
                        var hasDesc = value as IHasRichDescription;
                        if (hasDesc != null) {
                            descComps = hasDesc
                                .GetRichDescription()
                                .Select(kv => new AP.DescriptionComponent(kv.Value, kv.Key))
                                .ToArray();
                        }
                        values.Add(
                            new AP.CompletionValue() {
                                description = descComps,
                                doc = value.Documentation,
                                locations = value.Locations.Select(x => MakeReference(x, VariableType.Definition)).ToArray()
                            }
                        );
                    }
                    res[i].detailedValues = values.ToArray();
                }
            }
            return res;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Evaluates a given expression and returns a list of members which
        /// exist in the expression.
        /// 
        /// If the expression is an empty string returns all available members
        /// at that location.
        /// </summary>
        /// <param name="exprText">The expression to find members for.</param>
        /// </param>
        /// <param name="location">
        /// The location in the file where the expression should be evaluated.
        /// </param>
        /// <remarks>New in 2.2</remarks>
        public IEnumerable<MemberResult> GetMembers(
            string exprText,
            SourceLocation location,
            GetMemberOptions options = GetMemberOptions.IntersectMultipleResults
            )
        {
            if (exprText.Length == 0) {
                return GetAllAvailableMembers(location, options);
            }

            var scope = FindScope(location);
            var privatePrefix = GetPrivatePrefixClassName(scope);

            var expr = Statement.GetExpression(GetAstFromText(exprText, privatePrefix).Body);
            if (expr is ConstantExpression && ((ConstantExpression)expr).Value is int) {
                // no completions on integer ., the user is typing a float
                return Enumerable.Empty<MemberResult>();
            }

            var errorWalker = new ErrorWalker();
            expr.Walk(errorWalker);
            if (errorWalker.HasError) {
                return null;
            }

            var unit = GetNearestEnclosingAnalysisUnit(scope);
            var eval = new ExpressionEvaluator(unit.CopyForEval(), scope, mergeScopes: true);
            IAnalysisSet lookup;
            if (options.HasFlag(GetMemberOptions.NoMemberRecursion)) {
                lookup = eval.EvaluateNoMemberRecursion(expr);
            } else {
                lookup = eval.Evaluate(expr);
            }

            return GetMemberResults(lookup, scope, options);
        }
Exemplo n.º 16
0
 public static bool ExpressionKeywords(this GetMemberOptions self) => self.HasFlag(GetMemberOptions.IncludeExpressionKeywords);
Exemplo n.º 17
0
 public static bool StatementKeywords(this GetMemberOptions self) => self.HasFlag(GetMemberOptions.IncludeStatementKeywords);
Exemplo n.º 18
0
 public static bool HideAdvanced(this GetMemberOptions self) => self.HasFlag(GetMemberOptions.HideAdvancedMembers);
Exemplo n.º 19
0
        public override IDictionary<string, IAnalysisSet> GetAllMembers(IModuleContext moduleContext, GetMemberOptions options = GetMemberOptions.None)
        {
            var mro = ClassInfo._mro;
            if (!mro.IsValid) {
                return new Dictionary<string, IAnalysisSet>();
            }

            if (options.HasFlag(GetMemberOptions.DeclaredOnly)) {
                return Values.Mro.GetAllMembersOfMro(mro.Skip(1).Take(1), moduleContext, options);
            }

            // First item in MRO list is always the class itself.
            return Values.Mro.GetAllMembersOfMro(mro.Skip(1), moduleContext, options);
        }