コード例 #1
0
ファイル: MethodExtractor.cs プロジェクト: xyongy/PTVS
        private bool WasSelectionExpanded(SelectionTarget target, int selectionStart, int selectionEnd)
        {
            int startIndex = target.Ast.LocationToIndex(target.StartIncludingIndentation);

            if (startIndex != selectionStart)
            {
                for (var curChar = selectionStart - 1; curChar >= startIndex; curChar -= 1)
                {
                    if (!Char.IsWhiteSpace(_code[curChar]))
                    {
                        return(true);
                    }
                }
            }
            int endIndex = target.Ast.LocationToIndex(target.End);

            if (endIndex != selectionEnd)
            {
                for (var curChar = selectionEnd + 1; curChar < endIndex; curChar += 1)
                {
                    if (!Char.IsWhiteSpace(_code[curChar]))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
コード例 #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);
 }
コード例 #3
0
ファイル: MethodExtractor.cs プロジェクト: szh2bg/PTVS
        private static bool IsValidExtraction(SelectionTarget target, out AP.CannotExtractReason failureReason)
        {
            if (target.Parents[target.Parents.Count - 1] is ClassDefinition)
            {
                failureReason = AP.CannotExtractReason.StatementsFromClassDefinition;
                return(false);
            }

            var breakContinueWalker = new ContinueBreakWalker();

            target.Walk(breakContinueWalker);
            if (breakContinueWalker.ContainsBreak)
            {
                failureReason = AP.CannotExtractReason.SelectionContainsBreakButNotEnclosingLoop;
                return(false);
            }
            else if (breakContinueWalker.ContainsContinue)
            {
                failureReason = AP.CannotExtractReason.SelectionContainsContinueButNotEnclosingLoop;
                return(false);
            }

            var yieldWalker = new YieldWalker();

            target.Walk(yieldWalker);
            if (yieldWalker.ContainsYield)
            {
                failureReason = AP.CannotExtractReason.ContainsYieldExpression;
                return(false);
            }

            var importStarWalker = new ImportStarWalker();

            target.Walk(importStarWalker);
            if (importStarWalker.ContainsImportStar)
            {
                failureReason = AP.CannotExtractReason.ContainsFromImportStar;
                return(false);
            }

            var returnWalker = new ReturnWalker();

            target.Walk(returnWalker);
            if (returnWalker.ContainsReturn && !returnWalker.Returns)
            {
                failureReason = AP.CannotExtractReason.SelectionContainsReturn;
                return(false);
            }

            target.ContainsReturn = returnWalker.ContainsReturn;
            failureReason         = AP.CannotExtractReason.None;
            return(true);
        }
コード例 #4
0
        private void PostWalkWorker(SuiteStatement node)
        {
            if (_targetNode == null && node.StartIndex <= _start && node.EndIndex >= _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 >= _start)
                    {
                        startIndex = i;
                        break;
                    }
                }
                for (int i = node.Statements.Count - 1; i >= 0; i--)
                {
                    if (node.Statements[i].StartIndex < _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(),
                    _end,
                    startIndex,
                    endIndex
                    );
                _insertLocations[_parents[_parents.Count - 1]] = node.Statements.Count == 0 ?
                                                                 node.GetStartIncludingIndentation(_root) :
                                                                 node.Statements[startIndex].GetStartIncludingIndentation(_root);
            }
        }
コード例 #5
0
 private void PostWalkWorker(Node node)
 {
     if (node is ScopeStatement)
     {
         _suites.Pop();
         _parents.Remove((ScopeStatement)node);
     }
     if (_targetNode == null &&
         node.StartIndex <= _start &&
         node.EndIndex >= _end &&
         node is Expression expression)
     {
         _targetNode = new NodeTarget(_insertLocations, _parents.ToArray(), expression);
     }
 }
コード例 #6
0
 public OutOfProcExtractedMethodCreator(PythonAst ast, ScopeStatement[] scopes, HashSet<PythonVariable> inputVariables, HashSet<PythonVariable> outputVariables, SelectionTarget target, int indentSize, bool insertTabs, string newline, string name, string[] parameters, ScopeStatement targetScope) {
     _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;
     _name = name;
     _parameters = parameters;
     _targetScope = targetScope;
 }
コード例 #7
0
 public OutOfProcExtractedMethodCreator(PythonAst ast, ScopeStatement[] scopes, HashSet <PythonVariable> inputVariables, HashSet <PythonVariable> outputVariables, SelectionTarget target, int indentSize, bool insertTabs, string newline, string name, string[] parameters, ScopeStatement targetScope)
 {
     _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;
     _name        = name;
     _parameters  = parameters;
     _targetScope = targetScope;
 }
コード例 #8
0
 private bool WasSelectionExpanded(SelectionTarget target, int selectionStart, int selectionEnd)
 {
     if (target.StartIncludingIndentation != selectionStart)
     {
         for (var curChar = selectionStart - 1; curChar >= target.StartIncludingIndentation; curChar -= 1)
         {
             if (!Char.IsWhiteSpace(_code[curChar]))
             {
                 return(true);
             }
         }
     }
     if (target.End != selectionEnd)
     {
         for (var curChar = selectionEnd + 1; curChar < target.End; curChar += 1)
         {
             if (!Char.IsWhiteSpace(_code[curChar]))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
コード例 #9
0
        private static bool IsValidExtraction(SelectionTarget target, out string failureReason)
        {
            if (target.Parents[target.Parents.Length - 1] is ClassDefinition)
            {
                failureReason = "Cannot extract statements from a class definition";
                return(false);
            }

            string invalidExtractMsg = target.InvalidExtractionMessage;

            if (invalidExtractMsg != null)
            {
                failureReason = invalidExtractMsg;
                return(false);
            }

            var breakContinueWalker = new ContinueBreakWalker();

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

            var yieldWalker = new YieldWalker();

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

            var importStarWalker = new ImportStarWalker();

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

            var returnWalker = new ReturnWalker();

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

            target.ContainsReturn = returnWalker.ContainsReturn;
            failureReason         = null;
            return(true);
        }
コード例 #10
0
ファイル: MethodExtractor.cs プロジェクト: xyongy/PTVS
        private static bool IsValidExtraction(SelectionTarget target, out string failureReason)
        {
            if (target.Parents[target.Parents.Count - 1] is ClassDefinition)
            {
                failureReason = Strings.ExtractMethodStatementsFromClassDefinition;
                return(false);
            }

            string invalidExtractMsg = target.InvalidExtractionMessage;

            if (invalidExtractMsg != null)
            {
                failureReason = invalidExtractMsg;
                return(false);
            }

            var breakContinueWalker = new ContinueBreakWalker();

            target.Walk(breakContinueWalker);
            if (breakContinueWalker.ContainsBreak)
            {
                failureReason = Strings.ExtractMethodSelectionContainsBreakButNotEnclosingLoop;
                return(false);
            }
            else if (breakContinueWalker.ContainsContinue)
            {
                failureReason = Strings.ExtractMethodSelectionContainsContinueButNotEnclosingLoop;
                return(false);
            }

            var yieldWalker = new YieldWalker();

            target.Walk(yieldWalker);
            if (yieldWalker.ContainsYield)
            {
                failureReason = Strings.ExtractMethodContainsYieldExpression;
                return(false);
            }

            var importStarWalker = new ImportStarWalker();

            target.Walk(importStarWalker);
            if (importStarWalker.ContainsImportStar)
            {
                failureReason = Strings.ExtractMethodContainsFromImportStar;
                return(false);
            }

            var returnWalker = new ReturnWalker();

            target.Walk(returnWalker);
            if (returnWalker.ContainsReturn && !returnWalker.Returns)
            {
                failureReason = Strings.ExtractMethodSelectionContainsReturn;
                return(false);
            }

            target.ContainsReturn = returnWalker.ContainsReturn;
            failureReason         = null;
            return(true);
        }
コード例 #11
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);
 }
コード例 #12
0
        private static bool IsValidExtraction(SelectionTarget target, out string failureReason) {
            if (target.Parents[target.Parents.Length - 1] is ClassDefinition) {
                failureReason = "Cannot extract statements from a class definition";
                return false;
            }

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

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

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

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

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

            target.ContainsReturn = returnWalker.ContainsReturn;
            failureReason = null;
            return true;
        }
コード例 #13
0
 private bool WasSelectionExpanded(SelectionTarget target, int selectionStart, int selectionEnd) {
     if (target.StartIncludingIndentation != selectionStart) {
         for (var curChar = selectionStart - 1; curChar >= target.StartIncludingIndentation; curChar -= 1) {
             if (!Char.IsWhiteSpace(_code[curChar])) {
                 return true;
             }
         }
     }
     if (target.End != selectionEnd) {
         for (var curChar = selectionEnd + 1; curChar< target.End; curChar += 1) {
             if (!Char.IsWhiteSpace(_code[curChar])) {
                 return true;
             }
         }
     }
     return false;
 }