Esempio n. 1
0
 public PreviewChangesEngine(IRenameVariableInput input, ExpressionAnalysis analysis, RenameVariableRequest request, string originalName, string privatePrefix, VsProjectAnalyzer analyzer, IEnumerable<IAnalysisVariable> variables)
 {
     _analysis = analysis;
     _analyzer = analyzer;
     _renameReq = request;
     _originalName = originalName;
     _privatePrefix = privatePrefix;
     _variables = variables;
     _input = input;
     _list = new PreviewList(CreatePreviewItems().ToArray());
 }
Esempio n. 2
0
 public PreviewChangesEngine(IServiceProvider serviceProvider, IRenameVariableInput input, string expr, RenameVariableRequest request, string originalName, string privatePrefix, VsProjectAnalyzer analyzer, IEnumerable<AnalysisVariable> variables) {
     _serviceProvider = serviceProvider;
     _expr = expr;
     _analyzer = analyzer;
     _renameReq = request;
     _originalName = originalName;
     _privatePrefix = privatePrefix;
     _variables = variables;
     _input = input;
     _list = new PreviewList(CreatePreviewItems().ToArray());
 }
Esempio n. 3
0
 public PreviewChangesEngine(IServiceProvider serviceProvider, IRenameVariableInput input, string expr, RenameVariableRequest request, string originalName, string privatePrefix, VsProjectAnalyzer analyzer, IEnumerable <AnalysisVariable> variables)
 {
     _serviceProvider = serviceProvider;
     _expr            = expr;
     _analyzer        = analyzer;
     Request          = request;
     OriginalName     = originalName;
     PrivatePrefix    = privatePrefix;
     _variables       = variables;
     _input           = input;
     _list            = new PreviewList(CreatePreviewItems().ToArray());
 }
Esempio n. 4
0
 public PreviewChangesEngine(IServiceProvider serviceProvider, IRenameVariableInput input, ExpressionAnalysis analysis, RenameVariableRequest request, string originalName, string privatePrefix, GeneroProjectAnalyzer analyzer, IEnumerable <IAnalysisVariable> variables)
 {
     _serviceProvider = serviceProvider;
     _analysis        = analysis;
     _analyzer        = analyzer;
     _renameReq       = request;
     _originalName    = originalName;
     _privatePrefix   = privatePrefix;
     _variables       = variables;
     _input           = input;
     _list            = new PreviewList(CreatePreviewItems().ToArray());
 }
Esempio n. 5
0
        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());
                }
            }
        }
Esempio n. 6
0
        private bool IsModuleName(IRenameVariableInput input)
        {
            // make sure we're in
            var span       = _view.GetCaretSpan();
            var buffer     = span.TextBuffer;
            var snapshot   = buffer.CurrentSnapshot;
            var classifier = buffer.GetPythonClassifier();

            bool sawImport = false, sawFrom = false, sawName = false;
            var  walker = ReverseExpressionParser.ReverseClassificationSpanEnumerator(classifier, span.GetEndPoint(snapshot));

            while (walker.MoveNext())
            {
                var current = walker.Current;
                if (current == null)
                {
                    // new-line
                    break;
                }

                var text = current.Span.GetText();
                if (current.ClassificationType.IsOfType(PredefinedClassificationTypeNames.Identifier))
                {
                    // identifiers are ok
                    sawName = true;
                }
                else if (current.ClassificationType == classifier.Provider.DotClassification ||
                         current.ClassificationType == classifier.Provider.CommaClassification)
                {
                    // dots and commas are ok
                }
                else if (current.ClassificationType == classifier.Provider.GroupingClassification)
                {
                    if (text != "(" && text != ")")
                    {
                        // list/dict groupings are not ok
                        break;
                    }
                }
                else if (current.ClassificationType.IsOfType(PredefinedClassificationTypeNames.Keyword))
                {
                    if (text == "import")
                    {
                        sawImport = true;
                    }
                    else if (text == "from")
                    {
                        sawFrom = true;
                        break;
                    }
                    else if (text == "as")
                    {
                        if (sawName)
                        {
                            // import fob as oar
                            // from fob import oar as baz
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            // we saw from, but not import, so we're renaming a module name (from fob, renaming fob)
            // or we saw import, but not a from, so we're renaming a module name
            return(sawFrom != sawImport);
        }
        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. 8
0
        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());
                }
            }
        }
Esempio n. 9
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());
                }
            }
        }
Esempio n. 10
0
        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());
                }
            }
        }
Esempio n. 11
0
        private bool IsModuleName(IRenameVariableInput input) {
            // make sure we're in 
            var span = _view.GetCaretSpan();
            var buffer = span.TextBuffer;
            var snapshot = buffer.CurrentSnapshot;
            var classifier = buffer.GetPythonClassifier();

            bool sawImport = false, sawFrom = false, sawName = false;
            var walker = ReverseExpressionParser.ReverseClassificationSpanEnumerator(classifier, span.GetEndPoint(snapshot));
            while (walker.MoveNext()) {
                var current = walker.Current;
                if (current == null) {
                    // new-line
                    break;
                }

                var text = current.Span.GetText();
                if (current.ClassificationType.IsOfType(PredefinedClassificationTypeNames.Identifier)) {
                    // identifiers are ok
                    sawName = true;
                } else if (current.ClassificationType == classifier.Provider.DotClassification ||
                    current.ClassificationType == classifier.Provider.CommaClassification) {
                    // dots and commas are ok
                } else if (current.ClassificationType == classifier.Provider.GroupingClassification) {
                    if (text != "(" && text != ")") {
                        // list/dict groupings are not ok
                        break;
                    }
                } else if (current.ClassificationType.IsOfType(PredefinedClassificationTypeNames.Keyword)) {
                    if (text == "import") {
                        sawImport = true;
                    } else if (text == "from") {
                        sawFrom = true;
                        break;
                    } else if (text == "as") {
                        if (sawName) {
                            // import fob as oar
                            // from fob import oar as baz
                            break;
                        }
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }

            // we saw from, but not import, so we're renaming a module name (from fob, renaming fob)
            // or we saw import, but not a from, so we're renaming a module name
            return sawFrom != sawImport;
        }
Esempio n. 12
0
        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());
                }
            }
        }