Пример #1
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start = _stopwatch.ElapsedMilliseconds;

            var analysis = GetAnalysisEntry();

            if (analysis == null)
            {
                return(null);
            }

            var index = VsProjectAnalyzer.TranslateIndex(
                Span.GetEndPoint(TextBuffer.CurrentSnapshot).Position,
                TextBuffer.CurrentSnapshot,
                analysis
                );

            var completions = analysis.Analyzer.GetAllAvailableMembersAsync(analysis, index, GetMemberOptions.None).Result
                              .Where(IsDecoratorType)
                              .Select(member => PythonCompletion(glyphService, member))
                              .OrderBy(completion => completion.DisplayText);


            var res = new FuzzyCompletionSet("PythonDecorators", "Python", Span, completions, _options, CompletionComparer.UnderscoresLast);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime)
            {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} completions", this, end - start, res.Completions.Count));
            }

            return(res);
        }
Пример #2
0
        public override CompletionSet GetCompletions(IGlyphService glyphService) {
            var start = _stopwatch.ElapsedMilliseconds;

            var analysis = GetAnalysisEntry();
            if (analysis == null) {
                return null;
            }

            var index = VsProjectAnalyzer.TranslateIndex(
                Span.GetEndPoint(TextBuffer.CurrentSnapshot).Position,
                TextBuffer.CurrentSnapshot,
                analysis
            );

            var completions = analysis.GetAllAvailableMembers(index, GetMemberOptions.None)
                .Where(IsExceptionType)
                .Select(member => PythonCompletion(glyphService, member))
                .OrderBy(completion => completion.DisplayText);


            var res = new FuzzyCompletionSet("PythonExceptions", "Python", Span, completions, _options, CompletionComparer.UnderscoresLast);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime) {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} classes", this, end - start, res.Completions.Count));
            }

            return res;
        }
Пример #3
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start = _stopwatch.ElapsedMilliseconds;

            var line     = Span.GetStartPoint(TextBuffer.CurrentSnapshot).GetContainingLine();
            var startPos = line.Start.Position + line.GetText().IndexOf("def");

            var analysis = GetAnalysisEntry();

            if (analysis == null)
            {
                return(null);
            }

            var snapshot = TextBuffer.CurrentSnapshot;
            var pos      = VsProjectAnalyzer.TranslateIndex(startPos, snapshot, analysis);

            var cls         = analysis.GetDefinitionTree(pos).LastOrDefault(member => member.MemberType == Interpreter.PythonMemberType.Class);
            var members     = analysis.GetOverrideable(pos).ToArray();
            var completions = members
                              .Select(member => PythonCompletion(glyphService,
                                                                 member.Name,
                                                                 MakeCompletionString(member, cls.Name),
                                                                 member.Documentation,
                                                                 StandardGlyphGroup.GlyphGroupOverload));

            CompletionSet res;

            if (completions.Any())
            {
                var set = new FuzzyCompletionSet(
                    "PythonOverrides",
                    "Python",
                    Span,
                    completions,
                    _options,
                    CompletionComparer.UnderscoresLast
                    );
                set.CommitByDefault = false;
                res = set;
            }
            else
            {
                res = new CompletionSet();
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime)
            {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} classes", this, end - start, res.Completions.Count));
            }

            return(res);
        }
Пример #4
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start = _stopwatch.ElapsedMilliseconds;

            var completions = GetModules(_namespace, true)
                              .Where(m => _validNameRegex.IsMatch(m.Name))
                              .Select(m => PythonCompletion(glyphService, m));

            var res = new FuzzyCompletionSet("PythonImports", "Python", Span, completions, _options, CompletionComparer.UnderscoresLast);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime)
            {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} imports", this, end - start, res.Completions.Count));
            }

            return(res);
        }
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start       = _stopwatch.ElapsedMilliseconds;
            var completions = GetModules(_namespace, false).Select(m => PythonCompletion(glyphService, m));

            if (_includeStar)
            {
                var completion = new[] { PythonCompletion(glyphService, "*", "Import all members from the module", StandardGlyphGroup.GlyphArrow) };
                completions = completions.Concat(completion);
            }

            var res = new FuzzyCompletionSet("PythonFromImports", "Python", Span, completions, _options, CompletionComparer.UnderscoresLast);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime)
            {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} imports", this, end - start, res.Completions.Count));
            }

            return(res);
        }
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start = _stopwatch.ElapsedMilliseconds;

            var line     = Span.GetStartPoint(TextBuffer.CurrentSnapshot).GetContainingLine();
            var startPos = line.Start.Position + line.GetText().IndexOf("def");

            var analysis = GetAnalysisEntry();

            if (analysis == null)
            {
                return(null);
            }

            var span      = Span.GetSpan(TextBuffer.CurrentSnapshot);
            int defIndent = span.Start.GetContainingLine().GetText().IndexOf("def");

            string indentation;

            if (_options.ConvertTabsToSpaces)
            {
                indentation = new string(' ', defIndent + _options.IndentSize);
            }
            else
            {
                indentation = new string('\t', defIndent / 8 + 1);
            }

            var snapshot = TextBuffer.CurrentSnapshot;
            var pos      = VsProjectAnalyzer.TranslateIndex(startPos, snapshot, analysis);

            var completions = analysis.Analyzer.GetOverrideCompletionsAsync(
                analysis,
                TextBuffer,
                pos,
                indentation
                ).WaitOrDefault(1000)?.overrides;

            CompletionSet res;

            if (completions != null && completions.Any())
            {
                var set = new FuzzyCompletionSet(
                    "PythonOverrides",
                    "Python",
                    Span,
                    completions.Select(
                        x => PythonCompletion(
                            glyphService,
                            x.name,
                            x.completion,
                            x.doc,
                            StandardGlyphGroup.GlyphGroupOverload
                            )
                        ).ToArray(),
                    _options,
                    CompletionComparer.UnderscoresLast
                    );
                set.CommitByDefault = false;
                res = set;
            }
            else
            {
                res = new CompletionSet();
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime)
            {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} classes", this, end - start, res.Completions.Count));
            }

            return(res);
        }
Пример #7
0
        public override CompletionSet GetCompletions(IGlyphService glyphService) {
            var start = _stopwatch.ElapsedMilliseconds;

            var completions = GetModules(_namespace, true)
                .Where(m => _validNameRegex.IsMatch(m.Name))
                .Select(m => PythonCompletion(glyphService, m));

            var res = new FuzzyCompletionSet("PythonImports", "Python", Span, completions, _options, CompletionComparer.UnderscoresLast);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime) {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} imports", this, end - start, res.Completions.Count));
            }

            return res;
        }
Пример #8
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start1 = _stopwatch.ElapsedMilliseconds;

            IEnumerable <CompletionResult> members     = null;
            IEnumerable <CompletionResult> replMembers = null;

            var interactiveWindow = _snapshot.TextBuffer.GetInteractiveWindow();
            var pyReplEval        = interactiveWindow?.Evaluator as IPythonInteractiveIntellisense;

            var analysis = GetAnalysisEntry();

            string       text;
            SnapshotSpan statementRange;

            if (!GetPrecedingExpression(out text, out statementRange))
            {
                return(null);
            }
            else if (string.IsNullOrEmpty(text))
            {
                if (analysis != null)
                {
                    var analyzer = analysis.Analyzer;
                    lock (analyzer) {
                        var location = VsProjectAnalyzer.TranslateIndex(
                            statementRange.Start.Position,
                            statementRange.Snapshot,
                            analysis
                            );
                        var parameters = Enumerable.Empty <CompletionResult>();
                        var sigs       = analyzer.WaitForRequest(analyzer.GetSignaturesAsync(analysis, View, _snapshot, Span), "GetCompletions.GetSignatures");
                        if (sigs != null && sigs.Signatures.Any())
                        {
                            parameters = sigs.Signatures
                                         .SelectMany(s => s.Parameters)
                                         .Select(p => p.Name)
                                         .Distinct()
                                         .Select(n => new CompletionResult(n, PythonMemberType.Field));
                        }
                        members = analyzer.WaitForRequest(analyzer.GetAllAvailableMembersAsync(analysis, location, _options.MemberOptions), "GetCompletions.GetAllAvailableMembers")
                                  .MaybeEnumerate()
                                  .Union(parameters, CompletionComparer.MemberEquality);
                    }

                    if (pyReplEval == null)
                    {
                        var expansions = analyzer.WaitForRequest(EditorServices.Python?.GetExpansionCompletionsAsync(), "GetCompletions.GetExpansionCompletions");
                        if (expansions != null)
                        {
                            // Expansions should come first, so that they replace our keyword
                            // completions with the more detailed snippets.
                            if (members != null)
                            {
                                members = expansions.Union(members, CompletionComparer.MemberEquality);
                            }
                            else
                            {
                                members = expansions;
                            }
                        }
                    }
                }

                if (pyReplEval != null)
                {
                    replMembers = pyReplEval.GetMemberNames(string.Empty);
                }
            }
            else
            {
                var analyzer = analysis.Analyzer;
                if (analysis != null && (pyReplEval == null || !pyReplEval.LiveCompletionsOnly))
                {
                    lock (analyzer) {
                        var location = VsProjectAnalyzer.TranslateIndex(
                            statementRange.Start.Position,
                            statementRange.Snapshot,
                            analysis
                            );

                        members = analyzer.WaitForRequest(analyzer.GetMembersAsync(analysis, text, location, _options.MemberOptions), "GetCompletions.GetMembers");
                    }
                }

                if (pyReplEval != null && pyReplEval.Analyzer.ShouldEvaluateForCompletion(text))
                {
                    replMembers = pyReplEval.GetMemberNames(text);
                }
            }

            if (replMembers != null)
            {
                if (members != null)
                {
                    members = members.Union(replMembers, CompletionComparer.MemberEquality);
                }
                else
                {
                    members = replMembers;
                }
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime)
            {
                if (members != null)
                {
                    var memberArray = members.ToArray();
                    members = memberArray;
                    Trace.WriteLine(String.Format("{0} lookup time {1} for {2} members", this, end - start1, members.Count()));
                }
                else
                {
                    Trace.WriteLine(String.Format("{0} lookup time {1} for zero members", this, end - start1));
                }
            }

            if (members == null)
            {
                // The expression is invalid so we shouldn't provide
                // a completion set at all.
                return(null);
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var result = new FuzzyCompletionSet(
                "Python",
                "Python",
                Span,
                members.Select(m => PythonCompletion(glyphService, m)),
                _options,
                CompletionComparer.UnderscoresLast,
                matchInsertionText: true
                );

            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);
        }
Пример #9
0
        public CompletionSet GetCompletions(IGlyphService glyphService)
        {
            if (_snapshot == null)
            {
                return(null);
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var interactiveWindow = _snapshot.TextBuffer.GetInteractiveWindow();
            var pyReplEval        = interactiveWindow?.Evaluator as IPythonInteractiveIntellisense;

            var bufferInfo = PythonTextBufferInfo.TryGetForBuffer(_textBuffer);

            Debug.Assert(bufferInfo != null, "Getting completions from uninitialized buffer " + _textBuffer);

            var analysis = bufferInfo?.AnalysisEntry;
            var analyzer = analysis?.Analyzer;

            if (analyzer == null)
            {
                return(null);
            }

            var point = _point.GetPoint(bufferInfo.CurrentSnapshot);

            var location = VsProjectAnalyzer.TranslateIndex(
                point.Position,
                point.Snapshot,
                analysis
                );

            var triggerChar = _session.GetTriggerCharacter();
            var completions = analyzer.WaitForRequest(analyzer.GetCompletionsAsync(
                                                          analysis,
                                                          location,
                                                          _options.MemberOptions,
                                                          triggerChar == '\0' ? Analysis.LanguageServer.CompletionTriggerKind.Invoked : Analysis.LanguageServer.CompletionTriggerKind.TriggerCharacter,
                                                          triggerChar == '\0' ? null : triggerChar.ToString()
                                                          ), "GetCompletions.GetMembers", null, pyReplEval?.Analyzer == null ? 1 : 5);

            if (completions == null)
            {
                return(null);
            }

            var snapshotSpan = completions._applicableSpan.HasValue
                ? ((SourceSpan)completions._applicableSpan.Value).ToSnapshotSpan(_snapshot)
                : new SnapshotSpan(point, 0);

            _span = bufferInfo.CurrentSnapshot.CreateTrackingSpan(snapshotSpan, SpanTrackingMode.EdgeInclusive);
            var members = completions.items.MaybeEnumerate().Select(c => new CompletionResult(
                                                                        // if a method stub generation (best guess by comparing length),
                                                                        // merge entry based on label, for everything else, use insertion text
                                                                        c.filterText ?? (c.insertText != null && c.insertText.Length <= c.label.Length ? c.insertText : c.label),
                                                                        c.label,
                                                                        c.insertText ?? c.label,
                                                                        c.documentation?.value,
                                                                        Enum.TryParse(c._kind, true, out PythonMemberType mt) ? mt : PythonMemberType.Unknown,
                                                                        null
                                                                        ));

            if (pyReplEval?.Analyzer != null && (string.IsNullOrEmpty(completions._expr) || pyReplEval.Analyzer.ShouldEvaluateForCompletion(completions._expr)))
            {
                var replMembers = pyReplEval.GetMemberNames(completions._expr ?? "");

                if (_services.Python.InteractiveOptions.LiveCompletionsOnly)
                {
                    members = replMembers ?? Array.Empty <CompletionResult>();
                }
                else if (replMembers != null)
                {
                    members = members.Union(replMembers, CompletionMergeKeyComparer.Instance);
                }
            }

            if (pyReplEval == null && (completions._allowSnippet ?? false))
            {
                var expansions = analyzer.WaitForRequest(_services.Python?.GetExpansionCompletionsAsync(), "GetCompletions.GetExpansions", null, 5);
                if (expansions != null)
                {
                    // Expansions should come first, so that they replace our keyword
                    // completions with the more detailed snippets.
                    members = expansions.Union(members, CompletionMergeKeyComparer.Instance);
                }
            }


            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start) > TooMuchTime)
            {
                var memberArray = members.ToArray();
                members = memberArray;
                Trace.WriteLine($"{this} lookup time {end - start} for {memberArray.Length} members");
            }

            start = _stopwatch.ElapsedMilliseconds;

            var result = new FuzzyCompletionSet(
                "Python",
                "Python",
                _span,
                members.Select(m => PythonCompletion(glyphService, m)),
                _options,
                CompletionComparer.UnderscoresLast,
                matchInsertionText: true
                )
            {
                CommitByDefault = completions._commitByDefault ?? true
            };


            end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start) > TooMuchTime)
            {
                Trace.WriteLine($"{this} completion set time {end - start} total time {end - start}");
            }

            return(result);
        }
Пример #10
0
 private IEnumerable<string> HideAdvancedMembersHelper(PythonEditor view, string text, params string[] completions) {
     view.Text = text;
     var snapshot = view.CurrentSnapshot;
     var set = new FuzzyCompletionSet(
         "Test Completions",
         "Test Completions",
         snapshot.CreateTrackingSpan(0, snapshot.Length, SpanTrackingMode.EdgeInclusive),
         completions.Select(c => new DynamicallyVisibleCompletion(c)),
         new CompletionOptions { HideAdvancedMembers = true, IntersectMembers = false },
         CompletionComparer.UnderscoresLast
     );
     set.Filter();
     return set.Completions.Select(c => c.DisplayText).ToList();
 }
Пример #11
0
        public override CompletionSet GetCompletions(IGlyphService glyphService) {
            var start = _stopwatch.ElapsedMilliseconds;
            var completions = GetModules(_namespace, false).Select(m => PythonCompletion(glyphService, m));

            if (_includeStar) {
                var completion = new[] { PythonCompletion(glyphService, "*", "Import all members from the module", StandardGlyphGroup.GlyphArrow) };
                completions = completions.Concat(completion);
            }

            var res = new FuzzyCompletionSet("PythonFromImports", "Python", Span, completions, _options, CompletionComparer.UnderscoresLast);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime) {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} imports", this, end - start, res.Completions.Count));
            }

            return res;
        }
Пример #12
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start1 = _stopwatch.ElapsedMilliseconds;

            var interactiveWindow = _snapshot.TextBuffer.GetInteractiveWindow();
            var pyReplEval        = interactiveWindow?.Evaluator as IPythonInteractiveIntellisense;

            var bufferInfo = GetBufferInfo();
            var analysis   = bufferInfo?.AnalysisEntry;
            var analyzer   = analysis?.Analyzer;

            if (analyzer == null)
            {
                return(null);
            }

            IEnumerable <CompletionResult> members = null;

            var span  = Span.GetSpan(bufferInfo.CurrentSnapshot);
            var point = span.Start;

            var location = VsProjectAnalyzer.TranslateIndex(
                point.Position,
                point.Snapshot,
                analysis
                );

            var triggerChar = Session.GetTriggerCharacter();
            var completions = analyzer.WaitForRequest(analyzer.GetCompletionsAsync(
                                                          analysis,
                                                          location,
                                                          _options.MemberOptions,
                                                          triggerChar == '\0' ? Analysis.LanguageServer.CompletionTriggerKind.Invoked : Analysis.LanguageServer.CompletionTriggerKind.TriggerCharacter,
                                                          triggerChar == '\0' ? null : triggerChar.ToString()
                                                          ), "GetCompletions.GetMembers");

            if (completions.items == null)
            {
                return(null);
            }

            members = completions.items.Select(c => new CompletionResult(
                                                   c.label,
                                                   c.insertText ?? c.label,
                                                   c.documentation?.value,
                                                   Enum.TryParse(c._kind, true, out PythonMemberType mt) ? mt : PythonMemberType.Unknown,
                                                   null
                                                   ));

            if (pyReplEval?.Analyzer != null && (string.IsNullOrEmpty(completions._expr) || pyReplEval.Analyzer.ShouldEvaluateForCompletion(completions._expr)))
            {
                var replMembers = pyReplEval.GetMemberNames(completions._expr ?? "");
                if (replMembers != null)
                {
                    if (members != null)
                    {
                        members = members.Union(replMembers, CompletionComparer.MemberEquality);
                    }
                    else
                    {
                        members = replMembers;
                    }
                }
            }

            if (pyReplEval == null && (completions._allowSnippet ?? false))
            {
                var expansions = analyzer.WaitForRequest(EditorServices.Python?.GetExpansionCompletionsAsync(), "GetCompletions.GetExpansions", null, 5);
                if (expansions != null)
                {
                    // Expansions should come first, so that they replace our keyword
                    // completions with the more detailed snippets.
                    if (members != null)
                    {
                        members = expansions.Union(members, CompletionComparer.MemberEquality);
                    }
                    else
                    {
                        members = expansions;
                    }
                }
            }


            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime)
            {
                if (members != null)
                {
                    var memberArray = members.ToArray();
                    members = memberArray;
                    Trace.WriteLine(String.Format("{0} lookup time {1} for {2} members", this, end - start1, members.Count()));
                }
                else
                {
                    Trace.WriteLine(String.Format("{0} lookup time {1} for zero members", this, end - start1));
                }
            }

            if (members == null)
            {
                // The expression is invalid so we shouldn't provide
                // a completion set at all.
                return(null);
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var result = new FuzzyCompletionSet(
                "Python",
                "Python",
                Span,
                members.Select(m => PythonCompletion(glyphService, m)),
                _options,
                CompletionComparer.UnderscoresLast,
                matchInsertionText: true
                );

            result.CommitByDefault = completions._commitByDefault ?? true;

            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);
        }
Пример #13
0
        public override CompletionSet GetCompletions(IGlyphService glyphService) {
            var start = _stopwatch.ElapsedMilliseconds;

            var line = Span.GetStartPoint(TextBuffer.CurrentSnapshot).GetContainingLine();
            var startPos = line.Start.Position + line.GetText().IndexOf("def");

            var analysis = GetAnalysisEntry();
            if (analysis == null) {
                return null;
            }

            var span = Span.GetSpan(TextBuffer.CurrentSnapshot);
            int defIndent = span.Start.GetContainingLine().GetText().IndexOf("def");

            string indentation;
            if (_options.ConvertTabsToSpaces) {
                indentation = new string(' ', defIndent + _options.IndentSize);
            } else {
                indentation = new string('\t', defIndent / 8 + 1);
            }

            var snapshot = TextBuffer.CurrentSnapshot;
            var pos = VsProjectAnalyzer.TranslateIndex(startPos, snapshot, analysis);

            var completions = analysis.Analyzer.GetOverrideCompletionsAsync(
                analysis,
                TextBuffer,
                pos,
                indentation
            ).WaitOrDefault(1000)?.overrides;

            CompletionSet res;
            if (completions != null && completions.Any()) {
                var set = new FuzzyCompletionSet(
                    "PythonOverrides",
                    "Python",
                    Span,
                    completions.Select(
                        x => PythonCompletion(
                            glyphService,
                            x.name,
                            x.completion,
                            x.doc,
                            StandardGlyphGroup.GlyphGroupOverload
                        )
                    ).ToArray(),
                    _options,
                    CompletionComparer.UnderscoresLast
                );
                set.CommitByDefault = false;
                res = set;
            } else {
                res = new CompletionSet();
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime) {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} classes", this, end - start, res.Completions.Count));
            }

            return res;
        }
Пример #14
0
        public override CompletionSet GetCompletions(IGlyphService glyphService) {
            var start1 = _stopwatch.ElapsedMilliseconds;

            IEnumerable<CompletionResult> members = null;
            IEnumerable<CompletionResult> replMembers = null;

            var interactiveWindow = _snapshot.TextBuffer.GetInteractiveWindow();
            var pyReplEval = interactiveWindow?.Evaluator as IPythonInteractiveIntellisense;

            var analysis = GetAnalysisEntry();

            string text;
            SnapshotSpan statementRange;
            if (!GetPrecedingExpression(out text, out statementRange)) {
                return null;
            } else if (string.IsNullOrEmpty(text)) {
                if (analysis != null) {
                    lock (_analyzer) {
                        var location = VsProjectAnalyzer.TranslateIndex(
                            statementRange.Start.Position,
                            statementRange.Snapshot,
                            analysis
                        );
                        var parameters = Enumerable.Empty<CompletionResult>();
                        var sigs = VsProjectAnalyzer.GetSignaturesAsync(_serviceProvider, View, _snapshot, Span).WaitOrDefault(1000);
                        if (sigs != null && sigs.Signatures.Any()) {
                            parameters = sigs.Signatures
                                .SelectMany(s => s.Parameters)
                                .Select(p => p.Name)
                                .Distinct()
                                .Select(n => new CompletionResult(n, PythonMemberType.Field));
                        }
                        members = (analysis.Analyzer.GetAllAvailableMembersAsync(analysis, location, _options.MemberOptions).WaitOrDefault(1000) ?? new CompletionResult[0])
                            .Union(parameters, CompletionComparer.MemberEquality);
                    }
                }

                if (pyReplEval != null) {
                    replMembers = pyReplEval.GetMemberNames(string.Empty);
                }
            } else {
                if (analysis != null && (pyReplEval == null || !pyReplEval.LiveCompletionsOnly)) {
                    lock (_analyzer) {
                        var location = VsProjectAnalyzer.TranslateIndex(
                            statementRange.Start.Position,
                            statementRange.Snapshot,
                            analysis
                        );

                        members = analysis.Analyzer.GetMembersAsync(analysis, text, location, _options.MemberOptions).WaitOrDefault(1000);
                    }
                }

                if (pyReplEval != null && _analyzer.ShouldEvaluateForCompletion(text)) {
                    Debug.Assert(pyReplEval.Analyzer == _analyzer);
                    replMembers = pyReplEval.GetMemberNames(text);
                }
            }

            if (replMembers != null) {
                if (members != null) {
                    members = members.Union(replMembers, CompletionComparer.MemberEquality);
                } else {
                    members = replMembers;
                }
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime) {
                if (members != null) {
                    var memberArray = members.ToArray();
                    members = memberArray;
                    Trace.WriteLine(String.Format("{0} lookup time {1} for {2} members", this, end - start1, members.Count()));
                } else {
                    Trace.WriteLine(String.Format("{0} lookup time {1} for zero members", this, end - start1));
                }
            }

            if (members == null) {
                // The expression is invalid so we shouldn't provide
                // a completion set at all.
                return null;
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var result = new FuzzyCompletionSet(
                "Python",
                "Python",
                Span,
                members.Select(m => PythonCompletion(glyphService, m)),
                _options,
                CompletionComparer.UnderscoresLast,
                matchInsertionText: true
            );

            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;
        }
Пример #15
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start1 = _stopwatch.ElapsedMilliseconds;

            IEnumerable <CompletionResult> members     = null;
            IEnumerable <CompletionResult> replMembers = null;

            var interactiveWindow = _snapshot.TextBuffer.GetInteractiveWindow();
            var pyReplEval        = interactiveWindow?.Evaluator as IPythonInteractiveIntellisense;

            var analysis = GetAnalysisEntry();
            var analyzer = analysis?.Analyzer;

            if (analyzer == null)
            {
                return(null);
            }

            string       text;
            SnapshotSpan statementRange;

            if (!GetPrecedingExpression(out text, out statementRange))
            {
                return(null);
            }

            if (string.IsNullOrEmpty(text))
            {
                var expansionCompletionsTask = pyReplEval == null?EditorServices.Python?.GetExpansionCompletionsAsync() : null;

                if (analysis != null)
                {
                    members = GetAvailableCompletions(analysis, statementRange.Start);
                }

                if (pyReplEval != null)
                {
                    replMembers = pyReplEval.GetMemberNames(string.Empty);
                }

                if (expansionCompletionsTask != null)
                {
                    var expansions = analyzer.WaitForRequest(expansionCompletionsTask, "GetCompletions.GetExpansionCompletions", null, 5);
                    if (expansions != null)
                    {
                        // Expansions should come first, so that they replace our keyword
                        // completions with the more detailed snippets.
                        if (members != null)
                        {
                            members = expansions.Union(members, CompletionComparer.MemberEquality);
                        }
                        else
                        {
                            members = expansions;
                        }
                    }
                }
            }
            else
            {
                Task <IEnumerable <CompletionResult> > analyzerTask = null;

                if (pyReplEval == null || !pyReplEval.LiveCompletionsOnly)
                {
                    lock (analyzer) {
                        var location = VsProjectAnalyzer.TranslateIndex(
                            statementRange.Start.Position,
                            statementRange.Snapshot,
                            analysis
                            );

                        // Start the task and wait for it below - this allows a bit more time
                        // when there is a REPL attached, so we are more likely to get results.
                        analyzerTask = analyzer.GetMembersAsync(analysis, text, location, _options.MemberOptions);
                    }
                }

                if (pyReplEval != null && pyReplEval.Analyzer.ShouldEvaluateForCompletion(text))
                {
                    replMembers = pyReplEval.GetMemberNames(text);
                }

                if (analyzerTask != null)
                {
                    members = analyzer.WaitForRequest(analyzerTask, "GetCompletions.GetMembers");
                }
            }

            if (replMembers != null)
            {
                if (members != null)
                {
                    members = members.Union(replMembers, CompletionComparer.MemberEquality);
                }
                else
                {
                    members = replMembers;
                }
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime)
            {
                if (members != null)
                {
                    var memberArray = members.ToArray();
                    members = memberArray;
                    Trace.WriteLine(String.Format("{0} lookup time {1} for {2} members", this, end - start1, members.Count()));
                }
                else
                {
                    Trace.WriteLine(String.Format("{0} lookup time {1} for zero members", this, end - start1));
                }
            }

            if (members == null)
            {
                // The expression is invalid so we shouldn't provide
                // a completion set at all.
                return(null);
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var result = new FuzzyCompletionSet(
                "Python",
                "Python",
                Span,
                members.Select(m => PythonCompletion(glyphService, m)),
                _options,
                CompletionComparer.UnderscoresLast,
                matchInsertionText: true
                );

            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);
        }
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start1 = _stopwatch.ElapsedMilliseconds;

            IEnumerable <CompletionResult> members     = null;
            IEnumerable <CompletionResult> replMembers = null;

            IInteractiveEvaluator   eval;
            IPythonReplIntellisense pyReplEval = null;

            if (_snapshot.TextBuffer.Properties.TryGetProperty(typeof(IInteractiveEvaluator), out eval))
            {
                pyReplEval = eval as IPythonReplIntellisense;
            }

            var analysis = GetAnalysisEntry();

            string       text;
            SnapshotSpan statementRange;

            if (!GetPrecedingExpression(out text, out statementRange))
            {
                return(null);
            }
            else if (string.IsNullOrEmpty(text))
            {
                if (analysis != null)
                {
                    lock (_analyzer) {
                        var location = VsProjectAnalyzer.TranslateIndex(
                            statementRange.Start.Position,
                            statementRange.Snapshot,
                            analysis
                            );
                        var parameters = Enumerable.Empty <CompletionResult>();
                        var sigs       = VsProjectAnalyzer.GetSignaturesAsync(_serviceProvider, _snapshot, Span).WaitOrDefault(1000);
                        if (sigs != null && sigs.Signatures.Any())
                        {
                            parameters = sigs.Signatures
                                         .SelectMany(s => s.Parameters)
                                         .Select(p => p.Name)
                                         .Distinct()
                                         .Select(n => new CompletionResult(n, PythonMemberType.Field));
                        }
                        members = (analysis.Analyzer.GetAllAvailableMembersAsync(analysis, location, _options.MemberOptions).WaitOrDefault(1000) ?? new CompletionResult[0])
                                  .Union(parameters, CompletionComparer.MemberEquality);
                    }
                }

                if (pyReplEval != null)
                {
                    replMembers = pyReplEval.GetMemberNames(string.Empty);
                }
            }
            else
            {
                if (analysis != null && (pyReplEval == null || !pyReplEval.LiveCompletionsOnly))
                {
                    lock (_analyzer) {
                        var location = VsProjectAnalyzer.TranslateIndex(
                            statementRange.Start.Position,
                            statementRange.Snapshot,
                            analysis
                            );

                        members = analysis.Analyzer.GetMembersAsync(analysis, text, location, _options.MemberOptions).WaitOrDefault(1000);
                    }
                }

                if (pyReplEval != null && _snapshot.TextBuffer.GetAnalyzer(_serviceProvider).ShouldEvaluateForCompletion(text))
                {
                    replMembers = pyReplEval.GetMemberNames(text);
                }
            }

            if (replMembers != null)
            {
                if (members != null)
                {
                    members = members.Union(replMembers, CompletionComparer.MemberEquality);
                }
                else
                {
                    members = replMembers;
                }
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime)
            {
                if (members != null)
                {
                    var memberArray = members.ToArray();
                    members = memberArray;
                    Trace.WriteLine(String.Format("{0} lookup time {1} for {2} members", this, end - start1, members.Count()));
                }
                else
                {
                    Trace.WriteLine(String.Format("{0} lookup time {1} for zero members", this, end - start1));
                }
            }

            if (members == null)
            {
                // The expression is invalid so we shouldn't provide
                // a completion set at all.
                return(null);
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var result = new FuzzyCompletionSet(
                "Python",
                "Python",
                Span,
                members.Select(m => PythonCompletion(glyphService, m)),
                _options,
                CompletionComparer.UnderscoresLast,
                matchInsertionText: true
                );

            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);
        }
Пример #17
0
        public override CompletionSet GetCompletions(IGlyphService glyphService) {
            var start = _stopwatch.ElapsedMilliseconds;

            var line = Span.GetStartPoint(TextBuffer.CurrentSnapshot).GetContainingLine();
            var startPos = line.Start.Position + line.GetText().IndexOf("def");

            var analysis = GetAnalysisEntry();
            if (analysis == null) {
                return null;
            }

            var snapshot = TextBuffer.CurrentSnapshot;
            var pos = VsProjectAnalyzer.TranslateIndex(startPos, snapshot, analysis);

            var cls = analysis.GetDefinitionTree(pos).LastOrDefault(member => member.MemberType == Interpreter.PythonMemberType.Class);
            var members = analysis.GetOverrideable(pos).ToArray();
            var completions = members
                .Select(member => PythonCompletion(glyphService,
                    member.Name,
                    MakeCompletionString(member, cls.Name),
                    member.Documentation,
                    StandardGlyphGroup.GlyphGroupOverload));

            CompletionSet res;
            if (completions.Any()) {
                var set = new FuzzyCompletionSet(
                    "PythonOverrides",
                    "Python",
                    Span,
                    completions,
                    _options,
                    CompletionComparer.UnderscoresLast
                );
                set.CommitByDefault = false;
                res = set;
            } else {
                res = new CompletionSet();
            }

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ end - start > TooMuchTime) {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} classes", this, end - start, res.Completions.Count));
            }

            return res;
        }