Пример #1
0
        /// <summary>
        /// Gets a top-level list of all the available modules as a list of MemberResults.
        /// </summary>
        /// <returns></returns>
        public MemberResult[] GetModules()
        {
            var d = new Dictionary <string, HashSet <Namespace> >();

            foreach (var keyValue in Modules)
            {
                var modName   = keyValue.Key;
                var moduleRef = keyValue.Value;

                HashSet <Namespace> l;
                if (!d.TryGetValue(modName, out l))
                {
                    d[modName] = l = new HashSet <Namespace>();
                }
                if (moduleRef != null && moduleRef.Module != null)
                {
                    // The REPL shows up here with value=None
                    l.Add(moduleRef.Module);
                }
            }

            foreach (var r in _references)
            {
                foreach (string key in r.Value.GetMemberNames())
                {
                    var value = PythonAssemblyOps.GetBoundMember(_codeContext, r.Key, key);
                    HashSet <Namespace> l2;
                    if (!d.TryGetValue(key, out l2))
                    {
                        d[key] = l2 = new HashSet <Namespace>();
                    }
                    l2.Add(GetNamespaceFromObjects(value));
                }
            }

            var result = new MemberResult[d.Count];
            int pos    = 0;

            foreach (var kvp in d)
            {
                result[pos++] = new MemberResult(kvp.Key, kvp.Value);
            }
            return(result);
        }
Пример #2
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start1 = _stopwatch.ElapsedMilliseconds;

            MemberResult[] members = null;
            IReplEvaluator eval;
            IDlrEvaluator dlrEval;
            if (_snapshot.TextBuffer.Properties.TryGetProperty<IReplEvaluator>(typeof(IReplEvaluator), out eval) &&
                (dlrEval = eval as IDlrEvaluator) != null) {
                string text = Text;
                if(Text.EndsWith(".")) {
                    text = Text.Substring(0, Text.Length - 1);
                }
                var memberNames = dlrEval.GetMemberNames(text);

                if (memberNames != null && memberNames.Count > 0) {
                    members = new MemberResult[memberNames.Count];
                    int i = 0;
                    foreach(var member in memberNames) {
                        members[i++] = new MemberResult(member.Name, GetMemberType(member));
                    }
                }
            }

            if (members == null) {
                var analysis = GetAnalysisEntry();
                if (analysis != null) {
                    members = analysis.GetMembers(
                        Text,
                        _snapshot.GetLineNumberFromPosition(_pos) + 1,
                        _intersectMembers).ToArray();
                } else {
                    members = new MemberResult[0];
                }
            }

            members = DoFilterCompletions(members);
            Array.Sort(members, ModuleSort);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime) {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} members", this, end - start1, members.Length));
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var result = new PythonCompletionSet(
                Text,
                Text,
                _snapshot.CreateTrackingSpan(_pos, 0, SpanTrackingMode.EdgeInclusive),
                TransformMembers(glyphService, members),
                new Completion[0]);

            end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime) {
                Trace.WriteLine(String.Format("{0} completion set time {1} total time {2}", this, end - start, end - start1));
            }

            return result;
        }
Пример #3
0
 private IEnumerable<Completion> TransformMembers(IGlyphService glyphService, MemberResult[] members)
 {
     return members.Select(m => PythonCompletion(glyphService, m));
 }
Пример #4
0
 private MemberResult[] DoFilterCompletions(MemberResult[] members)
 {
     if (_hideAdvancedMembers) {
         members = FilterCompletions(members, Text, (completion, filter) => completion.StartsWith(filter) && (!completion.StartsWith("__") || ! completion.EndsWith("__")));
     } else {
         members = FilterCompletions(members, Text, (x, y) => x.StartsWith(y));
     }
     return members;
 }
Пример #5
0
 internal static int ModuleSort(MemberResult x, MemberResult y)
 {
     return MemberSortComparison(x.Name, y.Name);
 }
Пример #6
0
        internal static MemberResult[] FilterCompletions(MemberResult[] completions, string text, Func<string, string, bool> filterFunc)
        {
            var cut = text.LastIndexOfAny(new[] { '.', ']', ')' });
            var filter = (cut == -1) ? text : text.Substring(cut + 1);

            var result = new List<MemberResult>(completions.Length);
            foreach (var comp in completions) {
                if (filterFunc(comp.Name, filter)) {
                    result.Add(new MemberResult(comp.Name, comp.Name.Substring(filter.Length), comp.Namespaces));
                }
            }
            return result.ToArray();
        }
Пример #7
0
        /// <summary>
        /// returns the MemberResults associated with modules in the specified
        /// list of names.  The list of names is the path through the module, for example
        /// ['System', 'Runtime']
        /// </summary>
        /// <returns></returns>
        public MemberResult[] GetModuleMembers(string[] names, bool bottom)
        {
            IDictionary <string, ISet <Namespace> > d = null;
            var nses = GetReflectedNamespaces(names, bottom);

            if (nses == null)
            {
                return(new MemberResult[0]);
            }
            var ns = GetNamespaceFromObjects(nses);

            if (ns != null)
            {
                d = ns.GetAllMembers(true);
            }

            ModuleReference moduleRef;

            if (Modules.TryGetValue(names[0], out moduleRef) && moduleRef.Module != null)
            {
                var module  = moduleRef.Module;
                var newDict = new Dictionary <string, ISet <Namespace> >();
                if (d != null)
                {
                    Update(newDict, d);
                }

                d = newDict;

                var mod = module.SelfSet;
                if (bottom)
                {
                    for (int i = 1; i < names.Length; i++)
                    {
                        var next = names[i];
                        // import Foo.Bar as Baz, we need to get Bar
                        VariableDef      def;
                        ISet <Namespace> newMod  = EmptySet <Namespace> .Instance;
                        bool             madeSet = false;
                        foreach (var modItem in mod)
                        {
                            BuiltinModule builtinMod = modItem as BuiltinModule;
                            if (builtinMod != null)
                            {
                                ISet <Namespace> builtinValues;
                                if (builtinMod.VariableDict.TryGetValue(next, out builtinValues))
                                {
                                    newMod = newMod.Union(builtinValues, ref madeSet);
                                }
                            }
                            else
                            {
                                ModuleInfo userMod = modItem as ModuleInfo;
                                if (userMod != null && userMod.Scope.Variables.TryGetValue(next, out def))
                                {
                                    newMod = newMod.Union(def.Types, ref madeSet);
                                }
                            }
                        }

                        mod = newMod;
                        if (mod.Count == 0)
                        {
                            break;
                        }
                    }
                }

                foreach (var modItem in mod)
                {
                    Update(d, modItem.GetAllMembers(false));
                }
            }

            MemberResult[] result;
            if (d != null)
            {
                result = new MemberResult[d.Count];
                int pos = 0;
                foreach (var kvp in d)
                {
                    result[pos++] = new MemberResult(kvp.Key, kvp.Value);
                }
            }
            else
            {
                result = new MemberResult[0];
            }

            return(result);
        }
Пример #8
0
        internal static Completion PythonCompletion(IGlyphService service, MemberResult memberResult)
        {
            StandardGlyphGroup group = memberResult.MemberType.ToGlyphGroup();
            var icon = new IconDescription(group, StandardGlyphItem.GlyphItemPublic);

            var result = new LazyCompletion(memberResult.Name, () => memberResult.Completion, () => memberResult.ToolTip, service.GetGlyph(group, StandardGlyphItem.GlyphItemPublic));
            result.Properties.AddProperty(typeof(IconDescription), icon);
            return result;
        }
Пример #9
0
        public virtual Completion[] GetModules(IGlyphService glyphService, string text)
        {
            var analysis = GetAnalysisEntry();
            var path = text.Split('.');
            if (path.Length > 0) {
                // path = path[:-1]
                var newPath = new string[path.Length - 1];
                Array.Copy(path, newPath, path.Length - 1);
                path = newPath;
            }

            MemberResult[] modules = new MemberResult[0];
            if (path.Length == 0) {
                if (analysis != null) {
                    modules = analysis.ProjectState.GetModules();
                }

            #if REPL
                var repl = Intellisense.GetRepl(_textBuffer);
                if (repl != null) {
                    modules = Intellisense.MergeMembers(modules, repl.GetModules());
                }
            #endif
            } else {
                if (analysis != null) {
                    modules = analysis.ProjectState.GetModuleMembers(path);
                }
            }

            var sortedAndFiltered = NormalCompletionAnalysis.FilterCompletions(modules, text, (x, y) => x.StartsWith(y));
            Array.Sort(sortedAndFiltered, NormalCompletionAnalysis.ModuleSort);

            var result = new Completion[sortedAndFiltered.Length];
            for (int i = 0; i < sortedAndFiltered.Length; i++) {
                result[i] = PythonCompletion(glyphService, sortedAndFiltered[i]);
            }
            return result;
        }