Esempio n. 1
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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public object CreateDiffView(AnalysisProtocol.ChangeInfo[] changes, PythonTextBufferInfo buffer, int atVersion)
        {
            if (changes == null || buffer == null || !buffer.LocationTracker.CanTranslateFrom(atVersion))
            {
                return(null);
            }

            var snapshot = buffer.CurrentSnapshot;

            // Create a copy of the left hand buffer (we're going to remove all of the
            // content we don't care about from it).
            var leftBuffer = _bufferFactory.CreateTextBuffer(buffer.Buffer.ContentType);

            using (var edit = leftBuffer.CreateEdit()) {
                edit.Insert(0, snapshot.GetText());
                edit.Apply();
            }

            // create a buffer for the right hand side, copy the original buffer
            // into it, and then apply the changes.
            var rightBuffer = _bufferFactory.CreateTextBuffer(buffer.Buffer.ContentType);

            using (var edit = rightBuffer.CreateEdit()) {
                edit.Insert(0, snapshot.GetText());
                edit.Apply();
            }

            var startingVersion = rightBuffer.CurrentSnapshot;

            VsProjectAnalyzer.ApplyChanges(changes, rightBuffer, buffer.LocationTracker, atVersion);

            var textChanges = startingVersion.Version.Changes;
            int minPos = startingVersion.Length, maxPos = 0;

            foreach (var change in textChanges)
            {
                minPos = Math.Min(change.OldPosition, minPos);
                maxPos = Math.Max(change.OldPosition, maxPos);
            }

            if (minPos == startingVersion.Length && maxPos == 0)
            {
                // no changes?  that's weird...
                return(null);
            }

            MinimizeBuffers(leftBuffer, rightBuffer, startingVersion, minPos, maxPos);

            // create the difference buffer and view...
            var diffBuffer = _diffBufferFactory.CreateDifferenceBuffer(leftBuffer, rightBuffer);
            var diffView   = _diffFactory.CreateDifferenceView(diffBuffer, _previewRoleSet);

            diffView.ViewMode              = DifferenceViewMode.Inline;
            diffView.InlineView.ZoomLevel *= .75;
            diffView.InlineView.VisualElement.Focusable = false;
            diffView.InlineHost.GetTextViewMargin("deltadifferenceViewerOverview").VisualElement.Visibility = System.Windows.Visibility.Collapsed;

            // Reduce the size of the buffer once it's ready
            diffView.DifferenceBuffer.SnapshotDifferenceChanged += (sender, args) => {
                diffView.InlineView.DisplayTextLineContainingBufferPosition(
                    new SnapshotPoint(diffView.DifferenceBuffer.CurrentInlineBufferSnapshot, 0),
                    0.0, ViewRelativePosition.Top, double.MaxValue, double.MaxValue
                    );

                var width  = Math.Max(diffView.InlineView.MaxTextRightCoordinate * (diffView.InlineView.ZoomLevel / 100), 400); // Width of the widest line.
                var height = diffView.InlineView.LineHeight * (diffView.InlineView.ZoomLevel / 100) *                           // Height of each line.
                             diffView.DifferenceBuffer.CurrentInlineBufferSnapshot.LineCount;

                diffView.VisualElement.Width  = width;
                diffView.VisualElement.Height = height;
            };

            return(diffView.VisualElement);
        }
Esempio n. 4
0
        public async Task <bool> ExtractMethod(IExtractMethodInput input)
        {
            var analyzer    = _view.GetAnalyzer(_serviceProvider);
            var projectFile = _view.TextBuffer.GetAnalysisEntry();

            // extract once to validate the selection
            var extractInfo = await analyzer.ExtractMethodAsync(
                projectFile,
                _view.TextBuffer,
                _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)
            {
                _view.Selection.Select(
                    new SnapshotSpan(
                        _view.TextBuffer.CurrentSnapshot,
                        Span.FromBounds(extract.startIndex.Value, extract.endIndex.Value)
                        ),
                    false
                    );
            }

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

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

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

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

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

            return(true);
        }