Beispiel #1
0
        /// <summary>
        /// Returns the AnalysisEntry being used for where the caret is currently located in this view.
        ///
        /// Returns null if the caret isn't in Python code or an analysis doesn't exist for some reason.
        /// </summary>
        internal static AnalysisEntry GetAnalysisAtCaret(this ITextView textView, IServiceProvider serviceProvider)
        {
            var buffer = textView.GetPythonBufferAtCaret();

            if (buffer != null)
            {
                return(buffer.TryGetAnalysisEntry());
            }
            return(textView.TryGetAnalysisEntry(serviceProvider));
        }
Beispiel #2
0
        /// <summary>
        /// Returns the active VsProjectAnalyzer being used for where the caret is currently located in this view.
        /// </summary>
        internal static VsProjectAnalyzer GetAnalyzerAtCaret(this ITextView textView, IServiceProvider serviceProvider)
        {
            var buffer = textView.GetPythonBufferAtCaret();

            if (buffer != null)
            {
                return(textView.GetAnalysisEntry(buffer, serviceProvider)?.Analyzer);
            }

            return(null);
        }
Beispiel #3
0
        private void QueryStatusRename(OLECMD[] prgCmds, int i)
        {
            var analyzer = _textView.GetAnalyzerAtCaret(_serviceProvider);

            if (analyzer != null && _textView.GetPythonBufferAtCaret() != null)
            {
                prgCmds[i].cmdf = (uint)(OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_SUPPORTED);
            }
            else
            {
                prgCmds[i].cmdf = (uint)(OLECMDF.OLECMDF_INVISIBLE);
            }
        }
Beispiel #4
0
 public static bool? CanExtract(ITextView view) {
     if (view.GetPythonBufferAtCaret() != null) {
         if (view.Selection.IsEmpty ||
             view.Selection.Mode == TextSelectionMode.Box ||
             String.IsNullOrWhiteSpace(view.Selection.StreamSelectionSpan.GetText())) {
             return false;
         } else {
             return true;
         }
     } else {
         return null;
     }
 }
Beispiel #5
0
        /// <summary>
        /// Returns the AnalysisEntry being used for where the caret is currently located in this view.
        ///
        /// Returns null if the caret isn't in Python code or an analysis doesn't exist for some reason.
        /// </summary>
        internal static AnalysisEntry GetAnalysisAtCaret(this ITextView textView, IServiceProvider serviceProvider)
        {
            var buffer = textView.GetPythonBufferAtCaret();

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

            var           service = serviceProvider.GetEntryService();
            AnalysisEntry entry   = null;

            service?.TryGetAnalysisEntry(textView, buffer, out entry);
            return(entry);
        }
Beispiel #6
0
 public static bool?CanExtract(ITextView view)
 {
     if (view.GetPythonBufferAtCaret() != null)
     {
         if (view.Selection.IsEmpty ||
             view.Selection.Mode == TextSelectionMode.Box ||
             String.IsNullOrWhiteSpace(view.Selection.StreamSelectionSpan.GetText()))
         {
             return(false);
         }
         else
         {
             return(true);
         }
     }
     else
     {
         return(null);
     }
 }
Beispiel #7
0
        public async Task <bool> ExtractMethod(IExtractMethodInput input)
        {
            var analyzer = _view.GetAnalyzerAtCaret(_serviceProvider);

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

            var buffer      = _view.GetPythonBufferAtCaret();
            var snapshot    = buffer.CurrentSnapshot;
            var projectFile = _view.GetAnalysisAtCaret(_serviceProvider);

            if (projectFile == null)
            {
                return(false);
            }

            // extract once to validate the selection
            var extractInfo = await analyzer.ExtractMethodAsync(
                projectFile,
                buffer,
                _view,
                "method_name",
                null,
                null
                );

            if (extractInfo == null)
            {
                return(false);
            }

            var extract = extractInfo.Data;

            if (extract.cannotExtractMsg != null)
            {
                input.CannotExtract(extract.cannotExtractMsg);
                return(false);
            }

            if (extract.wasExpanded && !input.ShouldExpandSelection())
            {
                return(false);
            }

            if (extract.startIndex != null && extract.endIndex != null)
            {
                var selectionSpan = _view.BufferGraph.MapUpToBuffer(
                    new SnapshotSpan(
                        snapshot,
                        Span.FromBounds(extract.startIndex.Value, extract.endIndex.Value)
                        ),
                    SpanTrackingMode.EdgeInclusive,
                    _view.TextBuffer
                    );

                foreach (var span in selectionSpan)
                {
                    _view.Selection.Select(span, false);
                    break;
                }
            }

            var info = input.GetExtractionInfo(new ExtractedMethodCreator(analyzer, projectFile, _view, buffer, extract));

            if (info == null)
            {
                // user cancelled extract method
                return(false);
            }

            // extract again to get the final result...
            extractInfo = await analyzer.ExtractMethodAsync(
                projectFile,
                buffer,
                _view,
                info.Name,
                info.Parameters,
                info.TargetScope?.Scope.id
                );

            if (extractInfo == null)
            {
                return(false);
            }

            VsProjectAnalyzer.ApplyChanges(
                extractInfo.Data.changes,
                buffer,
                extractInfo.GetTracker(extractInfo.Data.version)
                );

            return(true);
        }
Beispiel #8
0
        public async Task <bool> ExtractMethod(IExtractMethodInput input)
        {
            var buffer = _view.GetPythonBufferAtCaret();
            var bi     = _services.GetBufferInfo(buffer);
            var entry  = bi?.AnalysisEntry;

            if (entry?.Analyzer == null)
            {
                return(false);
            }

            var snapshot = buffer.CurrentSnapshot;

            // extract once to validate the selection
            var extract = await entry.Analyzer.ExtractMethodAsync(
                bi,
                _view,
                "method_name",
                null,
                null
                );

            if (extract == null)
            {
                return(false);
            }

            if (extract.cannotExtractMsg != null)
            {
                input.CannotExtract(extract.cannotExtractMsg);
                return(false);
            }

            if (extract.wasExpanded && !input.ShouldExpandSelection())
            {
                return(false);
            }

            if (extract.startLine > 0 && extract.endLine > 0)
            {
                var selectionSpan = _view.BufferGraph.MapUpToBuffer(
                    new SourceSpan(
                        new SourceLocation(extract.startLine, extract.startCol),
                        new SourceLocation(extract.endLine, extract.endCol)
                        ).ToSnapshotSpan(snapshot),
                    SpanTrackingMode.EdgeInclusive,
                    _view.TextBuffer
                    );

                foreach (var span in selectionSpan)
                {
                    _view.Selection.Select(span, false);
                    break;
                }
            }

            var info = input.GetExtractionInfo(new ExtractedMethodCreator(bi, _view, extract));

            if (info == null)
            {
                // user cancelled extract method
                return(false);
            }

            // extract again to get the final result...
            extract = await entry.Analyzer.ExtractMethodAsync(
                bi,
                _view,
                info.Name,
                info.Parameters,
                info.TargetScope?.Scope.id
                );

            if (extract == null)
            {
                return(false);
            }

            VsProjectAnalyzer.ApplyChanges(
                extract.changes,
                buffer,
                bi.LocationTracker,
                extract.version
                );

            return(true);
        }