Exemple #1
0
        private static bool IsValidExtraction(IExtractMethodInput input, SelectionTarget target)
        {
            if (target.Parents[target.Parents.Length - 1] is ClassDefinition)
            {
                input.CannotExtract("Cannot extract statements from a class definition");
                return(false);
            }

            string invalidExtractMsg = target.InvalidExtractionMessage;

            if (invalidExtractMsg != null)
            {
                input.CannotExtract(invalidExtractMsg);
                return(false);
            }

            var breakContinueWalker = new ContinueBreakWalker();

            target.Walk(breakContinueWalker);
            if (breakContinueWalker.ContainsBreak)
            {
                input.CannotExtract("The selection contains a \"break\" statement, but not the enclosing loop");
                return(false);
            }
            else if (breakContinueWalker.ContainsContinue)
            {
                input.CannotExtract("The selection contains a \"continue\" statement, but not the enclosing loop");
                return(false);
            }

            var yieldWalker = new YieldWalker();

            target.Walk(yieldWalker);
            if (yieldWalker.ContainsYield)
            {
                input.CannotExtract("Cannot extract code containing \"yield\" expression");
                return(false);
            }

            var importStarWalker = new ImportStarWalker();

            target.Walk(importStarWalker);
            if (importStarWalker.ContainsImportStar)
            {
                input.CannotExtract("Cannot extract method containing from ... import * statement");
                return(false);
            }

            var returnWalker = new ReturnWalker();

            target.Walk(returnWalker);
            if (returnWalker.ContainsReturn && !returnWalker.Returns)
            {
                input.CannotExtract("When the selection contains a return statement, all code paths must be terminated by a return statement too.");
                return(false);
            }
            target.ContainsReturn = returnWalker.ContainsReturn;

            return(true);
        }
Exemple #2
0
 public OuterVariableWalker(PythonAst root, SelectionTarget target, InnerVariableWalker inputCollector, HashSet <PythonVariable> readBeforeInitialized, HashSet <PythonVariable> readByFollowingCodeBeforeInit, HashSet <PythonVariable> outputVars)
 {
     _root                          = root;
     _target                        = target;
     _inputCollector                = inputCollector;
     _readBeforeInitialized         = readBeforeInitialized;
     _readByFollowingCodeBeforeInit = readByFollowingCodeBeforeInit;
     _outputVars                    = outputVars;
     _define                        = new DefineWalker(this);
 }
 public ExtractedMethodCreator(PythonAst ast, ScopeStatement[] scopes, HashSet<PythonVariable> inputVariables, HashSet<PythonVariable> outputVariables, SelectionTarget target, int indentSize, bool insertTabs, string newline) {
     _ast = ast;
     _scopes = scopes;
     _inputVars = new List<PythonVariable>(inputVariables);
     _outputVars = new List<PythonVariable>(outputVariables);
     _inputVars.Sort(CompareVariables);
     _outputVars.Sort(CompareVariables);
     _target = target;
     _indentSize = indentSize;
     _insertTabs = insertTabs;
     _newline = newline;
 }
Exemple #4
0
 public ExtractedMethodCreator(PythonAst ast, ScopeStatement[] scopes, HashSet <PythonVariable> inputVariables, HashSet <PythonVariable> outputVariables, SelectionTarget target, int indentSize, bool insertTabs, string newline)
 {
     _ast        = ast;
     _scopes     = scopes;
     _inputVars  = new List <PythonVariable>(inputVariables);
     _outputVars = new List <PythonVariable>(outputVariables);
     _inputVars.Sort(CompareVariables);
     _outputVars.Sort(CompareVariables);
     _target     = target;
     _indentSize = indentSize;
     _insertTabs = insertTabs;
     _newline    = newline;
 }
Exemple #5
0
        private void PostWalkWorker(SuiteStatement node)
        {
            if (_targetNode == null && node.StartIndex <= _selectedSpan.Start && node.EndIndex >= _selectedSpan.End)
            {
                // figure out the range of statements we cover...
                int startIndex = 0, endIndex = node.Statements.Count - 1;
                for (int i = 0; i < node.Statements.Count; i++)
                {
                    if (node.Statements[i].EndIndex >= _selectedSpan.Start)
                    {
                        startIndex = i;
                        break;
                    }
                }
                for (int i = node.Statements.Count - 1; i >= 0; i--)
                {
                    if (node.Statements[i].StartIndex < _selectedSpan.End)
                    {
                        endIndex = i;
                        break;
                    }
                }
                List <SuiteStatement> followingSuites = new List <SuiteStatement>();
                for (int i = _suites.Count - 1; i >= 0; i--)
                {
                    if (_suites[i] == null)
                    {
                        // we hit our marker, this is a function/class boundary
                        // We don't care about any suites which come before the marker
                        // because they live in a different scope.  We insert the marker in
                        // ShouldWalkWorker(Node node) when we have a ScopeStatement.
                        break;
                    }

                    followingSuites.Add(_suites[i]);
                }
                _targetNode = new SuiteTarget(
                    _insertLocations,
                    _parents.ToArray(),
                    node,
                    followingSuites.ToArray(),
                    _selectedSpan,
                    startIndex,
                    endIndex
                    );
                _insertLocations[_parents[_parents.Count - 1]] = node.Statements.Count == 0 ?
                                                                 node.GetStartIncludingIndentation(_root) :
                                                                 node.Statements[startIndex].GetStartIncludingIndentation(_root);
            }
        }
Exemple #6
0
 private void PostWalkWorker(Node node)
 {
     if (node is ScopeStatement)
     {
         _suites.Pop();
         _parents.Remove((ScopeStatement)node);
     }
     if (_targetNode == null &&
         node.StartIndex <= _selectedSpan.Start &&
         node.EndIndex >= _selectedSpan.End &&
         node is Expression)
     {
         _targetNode = new NodeTarget(_insertLocations, _parents.ToArray(), node);
     }
 }
Exemple #7
0
 private static bool WasSelectionExpanded(SelectionTarget target, SnapshotPoint selectionStart, SnapshotPoint selectionEnd)
 {
     if (target.StartIncludingIndentation != selectionStart.Position)
     {
         for (var curChar = selectionStart - 1; curChar.Position >= target.StartIncludingIndentation; curChar -= 1)
         {
             if (!Char.IsWhiteSpace(curChar.GetChar()))
             {
                 return(true);
             }
         }
     }
     if (target.End != selectionEnd.Position)
     {
         for (var curChar = selectionEnd + 1; curChar.Position < target.End; curChar += 1)
         {
             if (!Char.IsWhiteSpace(curChar.GetChar()))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #8
0
 public OuterVariableWalker(PythonAst root, SelectionTarget target, InnerVariableWalker inputCollector, HashSet<PythonVariable> readBeforeInitialized, HashSet<PythonVariable> readByFollowingCodeBeforeInit, HashSet<PythonVariable> outputVars) {
     _root = root;
     _target = target;
     _inputCollector = inputCollector;
     _readBeforeInitialized = readBeforeInitialized;
     _readByFollowingCodeBeforeInit = readByFollowingCodeBeforeInit;
     _outputVars = outputVars;
     _define = new DefineWalker(this);
 }
Exemple #9
0
        private static bool IsValidExtraction(IExtractMethodInput input, SelectionTarget target) {
            if (target.Parents[target.Parents.Length - 1] is ClassDefinition) {
                input.CannotExtract("Cannot extract statements from a class definition");
                return false;
            }

            string invalidExtractMsg = target.InvalidExtractionMessage;
            if (invalidExtractMsg != null) {
                input.CannotExtract(invalidExtractMsg);
                return false;
            }

            var breakContinueWalker = new ContinueBreakWalker();
            target.Walk(breakContinueWalker);
            if (breakContinueWalker.ContainsBreak) {
                input.CannotExtract("The selection contains a \"break\" statement, but not the enclosing loop");
                return false;
            } else if (breakContinueWalker.ContainsContinue) {
                input.CannotExtract("The selection contains a \"continue\" statement, but not the enclosing loop");
                return false;
            }

            var yieldWalker = new YieldWalker();
            target.Walk(yieldWalker);
            if (yieldWalker.ContainsYield) {
                input.CannotExtract("Cannot extract code containing \"yield\" expression");
                return false;
            }

            var importStarWalker = new ImportStarWalker();
            target.Walk(importStarWalker);
            if (importStarWalker.ContainsImportStar) {
                input.CannotExtract("Cannot extract method containing from ... import * statement");
                return false;
            }

            var returnWalker = new ReturnWalker();
            target.Walk(returnWalker);
            if (returnWalker.ContainsReturn && !returnWalker.Returns) {
                input.CannotExtract("When the selection contains a return statement, all code paths must be terminated by a return statement too.");
                return false;
            }
            target.ContainsReturn = returnWalker.ContainsReturn;

            return true;
        }
Exemple #10
0
 private static bool WasSelectionExpanded(SelectionTarget target, SnapshotPoint selectionStart, SnapshotPoint selectionEnd) {
     if (target.StartIncludingIndentation != selectionStart.Position) {
         for (var curChar = selectionStart - 1; curChar.Position >= target.StartIncludingIndentation; curChar -= 1) {
             if (!Char.IsWhiteSpace(curChar.GetChar())) {
                 return true;
             }
         }
     }
     if (target.End != selectionEnd.Position) {
         for (var curChar = selectionEnd + 1; curChar.Position < target.End; curChar += 1) {
             if (!Char.IsWhiteSpace(curChar.GetChar())) {
                 return true;
             }
         }
     }
     return false;
 }
Exemple #11
0
        private void PostWalkWorker(SuiteStatement node) {
            if (_targetNode == null && node.StartIndex <= _selectedSpan.Start && node.EndIndex >= _selectedSpan.End) {
                // figure out the range of statements we cover...
                int startIndex = 0, endIndex = node.Statements.Count - 1;
                for (int i = 0; i < node.Statements.Count; i++) {
                    if (node.Statements[i].EndIndex >= _selectedSpan.Start) {
                        startIndex = i;
                        break;
                    }
                }
                for (int i = node.Statements.Count - 1; i >= 0; i--) {
                    if (node.Statements[i].StartIndex < _selectedSpan.End) {
                        endIndex = i;
                        break;
                    }
                }
                List<SuiteStatement> followingSuites = new List<SuiteStatement>();
                for (int i = _suites.Count - 1; i >= 0; i--) {
                    if (_suites[i] == null) {
                        // we hit our marker, this is a function/class boundary
                        // We don't care about any suites which come before the marker
                        // because they live in a different scope.  We insert the marker in 
                        // ShouldWalkWorker(Node node) when we have a ScopeStatement.
                        break;
                    }

                    followingSuites.Add(_suites[i]);
                }
                _targetNode = new SuiteTarget(
                    _insertLocations, 
                    _parents.ToArray(), 
                    node, 
                    followingSuites.ToArray(), 
                    _selectedSpan, 
                    startIndex, 
                    endIndex
                );
                _insertLocations[_parents[_parents.Count - 1]] = node.Statements.Count == 0 ?
                    node.GetStartIncludingIndentation(_root) :
                    node.Statements[startIndex].GetStartIncludingIndentation(_root);
            }
        }
Exemple #12
0
 private void PostWalkWorker(Node node) {
     if (node is ScopeStatement) {
         _suites.Pop();
         _parents.Remove((ScopeStatement)node);
     }
     if (_targetNode == null &&
         node.StartIndex <= _selectedSpan.Start &&
         node.EndIndex >= _selectedSpan.End &&
         node is Expression) {
         _targetNode = new NodeTarget(_insertLocations, _parents.ToArray(), node);
     }
 }