Ejemplo n.º 1
0
 public ExtractedMethodCreator(JAst ast, ScopeStatement[] scopes, HashSet<JVariable> inputVariables, HashSet<JVariable> outputVariables, SelectionTarget target, int indentSize, bool insertTabs)
 {
     _ast = ast;
     _scopes = scopes;
     _inputVars = new List<JVariable>(inputVariables);
     _outputVars = new List<JVariable>(outputVariables);
     _inputVars.Sort(CompareVariables);
     _outputVars.Sort(CompareVariables);
     _target = target;
     _indentSize = indentSize;
     _insertTabs = insertTabs;
 }
Ejemplo n.º 2
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);
     }
 }
Ejemplo n.º 3
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.GetStartIncludingWhiteSpace(_root) :
                    node.Statements[startIndex].GetStartIncludingWhiteSpace(_root);
            }
        }
Ejemplo n.º 4
0
 public OuterVariableWalker(JAst root, SelectionTarget target, InnerVariableWalker inputCollector, HashSet<JVariable> readBeforeInitialized, HashSet<JVariable> readByFollowingCodeBeforeInit)
 {
     _root = root;
     _target = target;
     _inputCollector = inputCollector;
     _readBeforeInitialized = readBeforeInitialized;
     _readByFollowingCodeBeforeInit = readByFollowingCodeBeforeInit;
     _define = new DefineWalker(this);
 }
Ejemplo n.º 5
0
 private static bool WasSelectionExpanded(SelectionTarget target, SnapshotPoint selectionStart, SnapshotPoint selectionEnd)
 {
     if (target.Start != selectionStart.Position) {
         for (var curChar = selectionStart - 1; curChar.Position >= target.Start; 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;
 }
Ejemplo n.º 6
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;
        }