public async Task RenameVariable(IRenameVariableInput input, IVsPreviewChangesService previewChanges) { if (IsModuleName(input)) { input.CannotRename(Strings.RenameVariable_CannotRenameModuleName); return; } var caret = _view.GetPythonCaret(); var entry = _view.GetAnalysisAtCaret(_serviceProvider); var buffer = entry?.TryGetBufferParser()?.DefaultBufferInfo ?? PythonTextBufferInfo.TryGetForBuffer(_view.TextBuffer); if (caret == null || entry == null || buffer == null) { input.CannotRename(Strings.RenameVariable_UnableGetAnalysisCurrentTextView); return; } var analysis = await entry.Analyzer.AnalyzeExpressionAsync(entry, caret.Value, ExpressionAtPointPurpose.Rename); if (analysis == null || string.IsNullOrEmpty(analysis.Expression) || !(analysis.Variables?.Any() ?? false)) { input.CannotRename(Strings.RenameVariable_UnableGetExpressionAnalysis); return; } string privatePrefix = analysis.PrivatePrefix; var originalName = analysis.Variables .Where(r => r.Type == VariableType.Definition) .Where(r => r.Location.DocumentUri == buffer.DocumentUri && buffer.LocationTracker.CanTranslateFrom(r.Version ?? -1)) .Select(r => { var snapshot = buffer.CurrentSnapshot; try { return(buffer.LocationTracker.Translate(r.Location.Span, r.Version ?? -1, snapshot).GetText()); } catch (ArgumentException) { return(null); } }) .Where(n => !string.IsNullOrEmpty(n)) .FirstOrDefault() ?? analysis.Expression; if (analysis.PrivatePrefix != null && originalName != null && originalName.StartsWithOrdinal("_" + 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(Strings.RenameVariable_SelectSymbol); return; } if (!analysis.Variables.Any(v => v.Type == VariableType.Definition || v.Type == VariableType.Reference)) { input.CannotRename(Strings.RenameVariable_NoInformationAvailableForVariable.FormatUI(originalName)); return; } PythonLanguageVersion languageVersion = PythonLanguageVersion.None; var analyzer = _view.GetAnalyzerAtCaret(_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.GetAnalyzerAtCaret(_serviceProvider), analysis.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(_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 RenameVariable(IRenameVariableInput input, IVsPreviewChangesService previewChanges) { if (IsModuleName(input)) { input.CannotRename(Strings.RenameVariable_CannotRenameModuleName); return; } var caret = _view.GetPythonCaret(); var entry = _view.GetAnalysisAtCaret(_serviceProvider); if (caret == null || entry == null) { input.CannotRename(Strings.RenameVariable_UnableGetAnalysisCurrentTextView); return; } var analysis = await entry.Analyzer.AnalyzeExpressionAsync(entry, _view, caret.Value); if (analysis == null) { input.CannotRename(Strings.RenameVariable_UnableGetAnalysisCurrentTextView); 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(Strings.RenameVariable_SelectSymbol); 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(Strings.RenameVariable_NoInformationAvailableForVariable.FormatUI(originalName)); return; } variables = paramVars; } else { variables = analysis.Variables; } PythonLanguageVersion languageVersion = PythonLanguageVersion.None; var analyzer = _view.GetAnalyzerAtCaret(_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.GetAnalyzerAtCaret(_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()); } } }
public async Task RenameVariable(IRenameVariableInput input, IVsPreviewChangesService previewChanges) { if (IsModuleName(input)) { input.CannotRename("Cannot rename a module name"); return; } var caret = _view.GetPythonCaret(); var analysis = await VsProjectAnalyzer.AnalyzeExpressionAsync(_serviceProvider, _view, 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.GetAnalyzerAtCaret(_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.GetAnalyzerAtCaret(_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(_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 != 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()); } } }