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"); } }
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; }
public MethodExtractor(ITextView textView) { _view = textView; var snapshot = _view.TextBuffer.CurrentSnapshot; _ast = _view.GetAnalyzer().ParseSnapshot(snapshot); }
public MethodExtractor(IServiceProvider serviceProvider, ITextView textView) { _view = textView; _serviceProvider = serviceProvider; var snapshot = _view.TextBuffer.CurrentSnapshot; _ast = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot); }
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; }
public ImportRemover(ITextView textView, bool allScopes) { _view = textView; var snapshot = _view.TextBuffer.CurrentSnapshot; _ast = _view.GetAnalyzer().ParseFile(snapshot); _allScopes = allScopes; }
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; }
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); } } }
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); } }
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); }
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()); } } }
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); }
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()); } } }
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()); } } }