Esempio n. 1
0
 private void UpdateStatusForIncompleteAnalysis() {
     var statusBar = (IVsStatusbar)_serviceProvider.GetService(typeof(SVsStatusbar));
     var analyzer = _textView.GetAnalyzer(_serviceProvider);
     if (analyzer != null && analyzer.IsAnalyzing) {
         statusBar.SetText("Python source analysis is not up to date");
     }
 }
Esempio n. 2
0
        private void UpdateStatusForIncompleteAnalysis()
        {
            var statusBar = (IVsStatusbar)VSGeneroPackage.GetGlobalService(typeof(SVsStatusbar));
            var analyzer  = _textView.GetAnalyzer();

            if (analyzer != null && analyzer.IsAnalyzing)
            {
                statusBar.SetText("Python source analysis is not up to date");
            }
        }
        public IIntellisenseController TryCreateIntellisenseController(ITextView textView, IList<ITextBuffer> subjectBuffers)
        {
            IntellisenseController controller;
            if (!textView.Properties.TryGetProperty<IntellisenseController>(typeof(IntellisenseController), out controller)) {
                controller = new IntellisenseController(this, textView);
            }

            var analyzer = textView.GetAnalyzer();
            if (analyzer != null) {
                var buffer = subjectBuffers[0];

                foreach (var subjBuf in subjectBuffers) {
                    // TODO: Check whether `buffer` should be `subjBuf`
                    controller.PropagateAnalyzer(buffer);
                }

                var entry = analyzer.MonitorTextBuffer(textView, buffer);
                textView.Closed += (sender, args) => analyzer.StopMonitoringTextBuffer(entry.BufferParser);

                for (int i = 1; i < subjectBuffers.Count; i++) {
                    entry.BufferParser.AddBuffer(subjectBuffers[i]);
                }
                controller.SetBufferParser(entry.BufferParser);
            }
            return controller;
        }
Esempio n. 4
0
        public MethodExtractor(ITextView textView)
        {
            _view = textView;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer().ParseSnapshot(snapshot);
        }
Esempio n. 5
0
        public MethodExtractor(IServiceProvider serviceProvider, ITextView textView) {
            _view = textView;
            _serviceProvider = serviceProvider;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot);
        }
Esempio n. 6
0
        public MethodExtractor(ITextView textView)
        {
            _view = textView;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer().ParseFile(snapshot);
        }
        public IIntellisenseController TryCreateIntellisenseController(ITextView textView, IList <ITextBuffer> subjectBuffers)
        {
            IntellisenseController controller;

            if (!textView.Properties.TryGetProperty <IntellisenseController>(typeof(IntellisenseController), out controller))
            {
                controller = new IntellisenseController(this, textView, _ServiceProvider);
            }

            var analyzer = textView.GetAnalyzer(_ServiceProvider);

            if (analyzer != null)
            {
                var buffer = subjectBuffers[0];

                foreach (var subjBuf in subjectBuffers)
                {
                    controller.PropagateAnalyzer(subjBuf);
                }

                var entry = analyzer.MonitorTextBuffer(textView, buffer);
                _hookedCloseEvents[textView] = Tuple.Create(entry.BufferParser, analyzer);
                textView.Closed += TextView_Closed;

                for (int i = 1; i < subjectBuffers.Count; i++)
                {
                    entry.BufferParser.AddBuffer(subjectBuffers[i]);
                }
                controller.SetBufferParser(entry.BufferParser);
            }
            return(controller);
        }
        public IIntellisenseController TryCreateIntellisenseController(ITextView textView, IList<ITextBuffer> subjectBuffers) {
            IntellisenseController controller;
            if (!textView.Properties.TryGetProperty<IntellisenseController>(typeof(IntellisenseController), out controller)) {
                controller = new IntellisenseController(this, textView, _ServiceProvider);
            }

            var analyzer = textView.GetAnalyzer(_ServiceProvider);
            if (analyzer != null) {
                var buffer = subjectBuffers[0];

                foreach (var subjBuf in subjectBuffers) {
                    controller.PropagateAnalyzer(subjBuf);
                }

                var entry = analyzer.MonitorTextBuffer(textView, buffer);
                _hookedCloseEvents[textView] = Tuple.Create(entry.BufferParser, analyzer);
                textView.Closed += TextView_Closed;

                for (int i = 1; i < subjectBuffers.Count; i++) {
                    entry.BufferParser.AddBuffer(subjectBuffers[i]);
                }
                controller.SetBufferParser(entry.BufferParser);
            }
            return controller;
        }
Esempio n. 9
0
        public MethodExtractor(IServiceProvider serviceProvider, ITextView textView)
        {
            _view            = textView;
            _serviceProvider = serviceProvider;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot);
        }
Esempio n. 10
0
        public ImportRemover(ITextView textView, bool allScopes)
        {
            _view = textView;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer().ParseFile(snapshot);
            _allScopes = allScopes;
        }
Esempio n. 11
0
        public ImportRemover(IServiceProvider serviceProvider, ITextView textView, bool allScopes) {
            _view = textView;
            _serviceProvider = serviceProvider;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot);
            _allScopes = allScopes;
        }
Esempio n. 12
0
        public ImportRemover(IServiceProvider serviceProvider, ITextView textView, bool allScopes)
        {
            _view            = textView;
            _serviceProvider = serviceProvider;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast       = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot);
            _allScopes = allScopes;
        }
Esempio n. 13
0
        public void VsTextViewCreated(VisualStudio.TextManager.Interop.IVsTextView textViewAdapter)
        {
            // TODO: We should probably only track text views in Python projects or loose files.
            ITextView textView = AdapterService.GetWpfTextView(textViewAdapter);

            if (textView != null)
            {
                var analyzer = textView.GetAnalyzer(_serviceProvider);
                if (analyzer != null)
                {
                    var monitorResult = analyzer.MonitorTextBuffer(textView, textView.TextBuffer);
                    textView.Closed += TextView_Closed;
                }
            }
        }
        internal IVsEditorAdaptersFactoryService AdapterService = null; // Set by MEF

        public void VsTextViewCreated(VisualStudio.TextManager.Interop.IVsTextView textViewAdapter)
        {
            // TODO: We should probably only track text views in Python projects or loose files.
            ITextView textView = AdapterService.GetWpfTextView(textViewAdapter);

            if (textView != null)
            {
                var analyzer = textView.GetAnalyzer();
                if (analyzer != null)
                {
                    var entry = analyzer.MonitorTextBuffer(textView.TextBuffer);

                    textView.Closed += (sender, args) => analyzer.StopMonitoringTextBuffer(entry.BufferParser);
                }
            }
        }
Esempio n. 15
0
        public void FormatCode(SnapshotSpan span, bool selectResult)
        {
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            var ast = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot);

            var walker = new EnclosingNodeWalker(ast, span.Start, span.End);

            ast.Walk(walker);

            if (walker.Target == null ||
                !walker.Target.IsValidSelection ||
                (walker.Target is SuiteTarget && _view.Selection.IsEmpty && selectResult))
            {
                return;
            }

            var body = walker.Target.GetNode(ast);

            // remove any leading comments before round tripping, not selecting them
            // gives a nicer overall experience, otherwise we have a selection to the
            // previous line which only covers white space.
            body.SetLeadingWhiteSpace(ast, body.GetIndentationLevel(ast));

            ITrackingSpan selectionSpan = null;

            if (selectResult)
            {
                selectionSpan = _view.TextBuffer.CurrentSnapshot.CreateTrackingSpan(
                    Span.FromBounds(walker.Target.StartIncludingIndentation, walker.Target.End),
                    SpanTrackingMode.EdgeInclusive
                    );
            }

            _view.ReplaceByLines(
                body.ToCodeString(ast, _format),
                Span.FromBounds(walker.Target.StartIncludingIndentation, walker.Target.End)
                );

            if (selectResult)
            {
                _view.Selection.Select(selectionSpan.GetSpan(_view.TextBuffer.CurrentSnapshot), false);
            }
        }
Esempio n. 16
0
        public IIntellisenseController TryCreateIntellisenseController(ITextView textView, IList <ITextBuffer> subjectBuffers)
        {
            IntellisenseController controller;

            if (!textView.Properties.TryGetProperty(typeof(IntellisenseController), out controller))
            {
                controller = new IntellisenseController(this, textView, _ServiceProvider);
            }

            var analyzer = textView.GetAnalyzer();

            if (analyzer != null)
            {
                var buffer = subjectBuffers[0];
                if (_PublicFunctionProvider != null)
                {
                    _PublicFunctionProvider.SetFilename(buffer.GetFilePath());
                }
                if (_DatabaseInfoProvider != null)
                {
                    _DatabaseInfoProvider.SetFilename(buffer.GetFilePath());
                }
                if (_ProgramFileProvider != null)
                {
                    if (VSGeneroPackage.Instance.ProgramFileProvider == null)
                    {
                        VSGeneroPackage.Instance.ProgramFileProvider = _ProgramFileProvider;
                    }
                }

                var entry = analyzer.MonitorTextBuffer(textView, buffer);
                _hookedCloseEvents[textView] = Tuple.Create(entry.BufferParser, analyzer);
                textView.Closed += TextView_Closed;

                for (int i = 1; i < subjectBuffers.Count; i++)
                {
                    entry.BufferParser.AddBuffer(subjectBuffers[i]);
                }
                controller.SetBufferParser(entry.BufferParser);
            }
            return(controller);
        }
Esempio n. 17
0
        internal IIncrementalSearchFactoryService _IncrementalSearch = null; // Set via MEF

        public IIntellisenseController TryCreateIntellisenseController(ITextView textView, IList <ITextBuffer> subjectBuffers)
        {
            // Only use the analyzer if the view is actually file backed. If it is the REPL window
            // we don't use this.
            var analyzer = textView.GetAnalyzer();

            if (analyzer != null)
            {
                var buffer = subjectBuffers[0];

                var entry = analyzer.MonitorTextBuffer(buffer);
                textView.Closed += (sender, args) => analyzer.StopMonitoringTextBuffer(entry.BufferParser);

                for (int i = 1; i < subjectBuffers.Count; i++)
                {
                    entry.BufferParser.AddBuffer(subjectBuffers[i]);
                }
                return(new IntellisenseController(this, subjectBuffers, textView, entry.BufferParser));
            }

            return(null);
        }
        public void RenameVariable(IRenameVariableInput input, IVsPreviewChangesService previewChanges)
        {
            if (IsModuleName(input))
            {
                input.CannotRename("Cannot rename a module name");
                return;
            }

            var analysis = _view.GetExpressionAnalysis(_serviceProvider);

            string     originalName  = null;
            string     privatePrefix = null;
            Expression expr          = null;

            if (analysis != ExpressionAnalysis.Empty)
            {
                PythonAst ast = analysis.GetEvaluatedAst();

                expr = Statement.GetExpression(ast.Body);

                NameExpression   ne = expr as NameExpression;
                MemberExpression me;
                if (ne != null)
                {
                    originalName = ne.Name;
                }
                else if ((me = expr as MemberExpression) != null)
                {
                    originalName = me.Name;
                }

                if (ast.PrivatePrefix != null && originalName.StartsWith("_" + ast.PrivatePrefix))
                {
                    originalName  = originalName.Substring(ast.PrivatePrefix.Length + 1);
                    privatePrefix = ast.PrivatePrefix;
                }

                if (originalName != null && _view.Selection.IsActive && !_view.Selection.IsEmpty)
                {
                    if (_view.Selection.Start.Position < analysis.Span.GetStartPoint(_view.TextBuffer.CurrentSnapshot) ||
                        _view.Selection.End.Position > analysis.Span.GetEndPoint(_view.TextBuffer.CurrentSnapshot))
                    {
                        originalName = null;
                    }
                }
            }

            if (originalName == null)
            {
                input.CannotRename("Please select a symbol to be renamed.");
                return;
            }

            bool hasVariables = false;

            foreach (var variable in analysis.Variables)
            {
                if (variable.Type == VariableType.Definition || variable.Type == VariableType.Reference)
                {
                    hasVariables = true;
                    break;
                }
            }

            IEnumerable <IAnalysisVariable> variables;

            if (!hasVariables)
            {
                List <IAnalysisVariable> paramVars = GetKeywordParameters(expr, originalName);

                if (paramVars.Count == 0)
                {
                    input.CannotRename(string.Format("No information is available for the variable '{0}'.", originalName));
                    return;
                }

                variables = paramVars;
            }
            else
            {
                variables = analysis.Variables;
            }

            PythonLanguageVersion languageVersion = PythonLanguageVersion.None;
            var analyzer = _view.GetAnalyzer(_serviceProvider);
            var factory  = analyzer != null ? analyzer.InterpreterFactory : null;

            if (factory != null)
            {
                languageVersion = factory.Configuration.Version.ToLanguageVersion();
            }

            var info = input.GetRenameInfo(originalName, languageVersion);

            if (info != null)
            {
                var engine = new PreviewChangesEngine(_serviceProvider, input, analysis, info, originalName, privatePrefix, _view.GetAnalyzer(_serviceProvider), variables);
                if (info.Preview)
                {
                    previewChanges.PreviewChanges(engine);
                }
                else
                {
                    ErrorHandler.ThrowOnFailure(engine.ApplyChanges());
                }
            }
        }
Esempio n. 19
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);
        }
Esempio n. 20
0
        public async Task RenameVariable(IRenameVariableInput input, IVsPreviewChangesService previewChanges)
        {
            if (IsModuleName(input))
            {
                input.CannotRename("Cannot rename a module name");
                return;
            }

            var caret    = _view.GetCaretPosition();
            var analysis = await VsProjectAnalyzer.AnalyzeExpressionAsync(caret.Value);

            if (analysis == null)
            {
                input.CannotRename("Unable to get analysis for current text view.");
                return;
            }

            string originalName  = null;
            string privatePrefix = null;

            if (!String.IsNullOrWhiteSpace(analysis.Expression))
            {
                originalName = analysis.MemberName;

                if (analysis.PrivatePrefix != null && originalName != null && originalName.StartsWith("_" + analysis.PrivatePrefix))
                {
                    originalName  = originalName.Substring(analysis.PrivatePrefix.Length + 1);
                    privatePrefix = analysis.PrivatePrefix;
                }

                if (originalName != null && _view.Selection.IsActive && !_view.Selection.IsEmpty)
                {
                    if (_view.Selection.Start.Position < analysis.Span.GetStartPoint(_view.TextBuffer.CurrentSnapshot) ||
                        _view.Selection.End.Position > analysis.Span.GetEndPoint(_view.TextBuffer.CurrentSnapshot))
                    {
                        originalName = null;
                    }
                }
            }

            if (originalName == null)
            {
                input.CannotRename("Please select a symbol to be renamed.");
                return;
            }

            bool hasVariables = false;

            foreach (var variable in analysis.Variables)
            {
                if (variable.Type == VariableType.Definition || variable.Type == VariableType.Reference)
                {
                    hasVariables = true;
                    break;
                }
            }

            IEnumerable <AnalysisVariable> variables;

            if (!hasVariables)
            {
                List <AnalysisVariable> paramVars = await GetKeywordParameters(analysis.Expression, originalName);

                if (paramVars.Count == 0)
                {
                    input.CannotRename(string.Format("No information is available for the variable '{0}'.", originalName));
                    return;
                }

                variables = paramVars;
            }
            else
            {
                variables = analysis.Variables;
            }

            PythonLanguageVersion languageVersion = PythonLanguageVersion.None;
            var analyzer = _view.GetAnalyzer(_serviceProvider);
            var factory  = analyzer != null ? analyzer.InterpreterFactory : null;

            if (factory != null)
            {
                languageVersion = factory.Configuration.Version.ToLanguageVersion();
            }

            var info = input.GetRenameInfo(originalName, languageVersion);

            if (info != null)
            {
                var engine = new PreviewChangesEngine(_serviceProvider, input, analysis.Expression, info, originalName, privatePrefix, _view.GetAnalyzer(_serviceProvider), variables);
                if (info.Preview)
                {
                    previewChanges.PreviewChanges(engine);
                }
                else
                {
                    ErrorHandler.ThrowOnFailure(engine.ApplyChanges());
                }
            }
        }
Esempio n. 21
0
        public void RenameVariable(IRenameVariableInput input, IVsPreviewChangesService previewChanges)
        {
            //if (IsModuleName(input))
            //{
            //    input.CannotRename("Cannot rename a module name");
            //    return;
            //}

            var analysis = _view.GetExpressionAnalysis(_funcProvider, _dbProvider, _progFileProvider);

            string originalName  = null;
            string privatePrefix = null;

            if (analysis != null && analysis.Value != null)
            {
                // TODO: really need to add a IRenamable API that allows us to determine whether the item selected can be renamed
                if (analysis.Value is VariableDef ||
                    analysis.Value is FunctionBlockNode)
                {
                    originalName = analysis.Value.Name;
                }
                //expr = Statement.GetExpression(ast.Body);

                //NameExpression ne = expr as NameExpression;
                //MemberExpression me;
                //if (ne != null)
                //{
                //    originalName = ne.Name;
                //}
                //else if ((me = expr as MemberExpression) != null)
                //{
                //    originalName = me.Name;
                //}

                //if (ast.PrivatePrefix != null && originalName != null && originalName.StartsWith("_" + ast.PrivatePrefix))
                //{
                //    originalName = originalName.Substring(ast.PrivatePrefix.Length + 1);
                //    privatePrefix = ast.PrivatePrefix;
                //}

                //if (originalName != null && _view.Selection.IsActive && !_view.Selection.IsEmpty)
                //{
                //    if (_view.Selection.Start.Position < analysis.Span.GetStartPoint(_view.TextBuffer.CurrentSnapshot) ||
                //        _view.Selection.End.Position > analysis.Span.GetEndPoint(_view.TextBuffer.CurrentSnapshot))
                //    {
                //        originalName = null;
                //    }
                //}
            }

            if (originalName == null)
            {
                input.CannotRename("Please select a symbol to be renamed.");
                return;
            }

            bool hasVariables = false;

            foreach (var variable in analysis.Variables)
            {
                if (variable.Type == VariableType.Definition || variable.Type == VariableType.Reference)
                {
                    hasVariables = true;
                    break;
                }
            }

            IEnumerable <IAnalysisVariable> variables = null;

            if (!hasVariables)
            {
                //List<IAnalysisVariable> paramVars = GetKeywordParameters(expr, originalName);

                //if (paramVars.Count == 0)
                //{
                //    input.CannotRename(string.Format("No information is available for the variable '{0}'.", originalName));
                //    return;
                //}

                //variables = paramVars;
            }
            else
            {
                variables = analysis.Variables;
            }

            //PythonLanguageVersion languageVersion = PythonLanguageVersion.None;
            //var analyzer = _view.GetAnalyzer(_serviceProvider);
            //var factory = analyzer != null ? analyzer.InterpreterFactory : null;
            //if (factory != null)
            //{
            //    languageVersion = factory.Configuration.Version.ToLanguageVersion();
            //}

            var info = input.GetRenameInfo(originalName);

            if (info != null)
            {
                var engine = new PreviewChangesEngine(_serviceProvider, input, analysis, info, originalName, privatePrefix, _view.GetAnalyzer(), variables);
                if (info.Preview)
                {
                    previewChanges.PreviewChanges(engine);
                }
                else
                {
                    ErrorHandler.ThrowOnFailure(engine.ApplyChanges());
                }
            }
        }