示例#1
0
 // DecoratorStatement
 public virtual bool Walk(DecoratorStatement node)
 {
     return(true);
 }
示例#2
0
 public override void PostWalk(DecoratorStatement node)
 {
 }
示例#3
0
 // DecoratorStatement
 public override bool Walk(DecoratorStatement node)
 {
     return(Location >= node.StartIndex && Location <= node.EndIndex);
 }
示例#4
0
            private void AddTagIfNecessary(int startIndex, int endIndex, int headerIndex = -1, DecoratorStatement decorator = null, int minLinesToCollapse = 3)
            {
                var startLine = _snapshot.GetLineFromPosition(startIndex).LineNumber;
                var endLine   = _snapshot.GetLineFromPosition(endIndex).LineNumber;
                var lines     = endLine - startLine + 1;

                // Collapse if more than 3 lines.
                if (lines >= minLinesToCollapse)
                {
                    TagSpan tagSpan = OutliningTagger.GetTagSpan(
                        _snapshot,
                        startIndex,
                        endIndex,
                        headerIndex,
                        decorator);
                    TagSpans.Add(tagSpan);
                }
            }
示例#5
0
 // DecoratorStatement
 public override bool Walk(DecoratorStatement node)
 {
     return(false);
 }
 // DecoratorStatement
 public override bool Walk(DecoratorStatement node)
 {
     return(Contains(node));
 }
示例#7
0
 private void AddTagIfNecessary(Node node, int headerIndex = -1, DecoratorStatement decorator = null)
 {
     AddTagIfNecessary(node.StartIndex, node.EndIndex, headerIndex, decorator);
 }
 // DecoratorStatement
 public override bool Walk(DecoratorStatement node)
 {
     return(ShouldWalkWorker(node));
 }
 public override void PostWalk(DecoratorStatement node)
 {
     PostWalkWorker(node);
 }
示例#10
0
 // DecoratorStatement
 public virtual bool Walk(DecoratorStatement node)
 {
     return true;
 }
示例#11
0
 public override void PostWalk(DecoratorStatement node)
 {
 }
示例#12
0
 public virtual void PostWalk(DecoratorStatement node)
 {
 }
示例#13
0
 // DecoratorStatement
 public override bool Walk(DecoratorStatement node)
 {
     return false;
 }
示例#14
0
 // DecoratorStatement
 public override bool Walk(DecoratorStatement node) { return Location >= node.StartIndex && Location <= node.EndIndex; }
示例#15
0
 public virtual void PostWalk(DecoratorStatement node)
 {
 }
示例#16
0
            internal static TagSpan GetTagSpan(ITextSnapshot snapshot, int start, int end, int headerIndex = -1, DecoratorStatement decorators = null)
            {
                TagSpan tagSpan = null;

                try {
                    if (decorators != null)
                    {
                        // we don't want to collapse the decorators, we like them visible, so
                        // we base our starting position on where the decorators end.
                        start = decorators.EndIndex + 1;
                    }

                    // if the user provided a -1, we should figure out the end of the first line
                    if (headerIndex < 0)
                    {
                        int startLineEnd = snapshot.GetLineFromPosition(start).End.Position;
                        headerIndex = startLineEnd;
                    }

                    if (start != -1 && end != -1)
                    {
                        int length = end - headerIndex;
                        if (length > 0)
                        {
                            var span = GetFinalSpan(snapshot,
                                                    headerIndex,
                                                    length
                                                    );

                            tagSpan = new TagSpan(
                                new SnapshotSpan(snapshot, span),
                                new OutliningTag(snapshot, span, true)
                                );
                        }
                    }
                } catch (ArgumentException) {
                    // sometimes Python's parser gives us bad spans, ignore those and fix the parser
                    Debug.Assert(false, "bad argument when making span/tag");
                }

                return(tagSpan);
            }
示例#17
0
        public ExtractMethodResult GetExtractionResult()
        {
            bool   isStaticMethod = false, isClassMethod = false;
            var    parameters = new List <Parameter>();
            string selfParam  = null;

            if (_targetScope is ClassDefinition)
            {
                var fromScope = _scopes[_scopes.Length - 1] as FunctionDefinition;
                Debug.Assert(fromScope != null);  // we don't allow extracting from classes, so we have to be coming from a function
                if (fromScope != null)
                {
                    if (fromScope.Decorators != null)
                    {
                        foreach (var decorator in fromScope.Decorators.Decorators)
                        {
                            NameExpression name = decorator as NameExpression;
                            if (name != null)
                            {
                                if (name.Name == "staticmethod")
                                {
                                    isStaticMethod = true;
                                }
                                else if (name.Name == "classmethod")
                                {
                                    isClassMethod = true;
                                }
                            }
                        }
                    }

                    if (!isStaticMethod)
                    {
                        if (fromScope.Parameters.Count > 0)
                        {
                            selfParam = fromScope.Parameters[0].Name;
                            parameters.Add(new Parameter(selfParam, ParameterKind.Normal));
                        }
                    }
                }
            }

            foreach (var param in _parameters)
            {
                var newParam = new Parameter(param, ParameterKind.Normal);
                if (parameters.Count > 0)
                {
                    newParam.AddPreceedingWhiteSpace(_ast, " ");
                }
                parameters.Add(newParam);
            }

            // include any non-closed over parameters as well...
            foreach (var input in _inputVars)
            {
                var variableScope = input.Scope;
                var parentScope   = _targetScope;

                // are these variables a child of the target scope so we can close over them?
                while (parentScope != null && parentScope != variableScope)
                {
                    parentScope = parentScope.Parent;
                }

                if (parentScope == null && input.Name != selfParam)
                {
                    // we can either close over or pass these in as parameters, add them to the list
                    var newParam = new Parameter(input.Name, ParameterKind.Normal);
                    if (parameters.Count > 0)
                    {
                        newParam.AddPreceedingWhiteSpace(_ast, " ");
                    }
                    parameters.Add(newParam);
                }
            }

            var body        = _target.GetBody(_ast);
            var isCoroutine = IsCoroutine(body);

            // reset leading indentation to single newline + indentation, this
            // strips out any proceeding comments which we don't extract
            var leading = _newline + body.GetIndentationLevel(_ast);

            body.SetLeadingWhiteSpace(_ast, leading);

            if (_outputVars.Count > 0)
            {
                // need to add a return statement
                Expression   retValue;
                Expression[] names       = new Expression[_outputVars.Count];
                int          outputIndex = 0;
                foreach (var name in _outputVars)
                {
                    var nameExpr = new NameExpression(name.Name);
                    nameExpr.AddPreceedingWhiteSpace(_ast, " ");
                    names[outputIndex++] = nameExpr;
                }
                var tuple = new TupleExpression(false, names);
                tuple.RoundTripHasNoParenthesis(_ast);
                retValue = tuple;

                var retStmt = new ReturnStatement(retValue);
                retStmt.SetLeadingWhiteSpace(_ast, leading);

                body = new SuiteStatement(
                    new Statement[] {
                    body,
                    retStmt
                }
                    );
            }
            else
            {
                // we need a SuiteStatement to give us our colon
                body = new SuiteStatement(new Statement[] { body });
            }

            DecoratorStatement decorators = null;

            if (isStaticMethod)
            {
                decorators = new DecoratorStatement(new[] { new NameExpression("staticmethod") });
            }
            else if (isClassMethod)
            {
                decorators = new DecoratorStatement(new[] { new NameExpression("classmethod") });
            }

            var res = new FunctionDefinition(new NameExpression(_name), parameters.ToArray(), body, decorators);

            res.IsCoroutine = isCoroutine;

            StringBuilder newCall = new StringBuilder();

            newCall.Append(_target.IndentationLevel);
            var method = res.ToCodeString(_ast);

            // fix up indentation...
            for (int curScope = 0; curScope < _scopes.Length; curScope++)
            {
                if (_scopes[curScope] == _targetScope)
                {
                    // this is our target indentation level.
                    var indentationLevel = _scopes[curScope].Body.GetIndentationLevel(_ast);
                    var lines            = method.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
                    int minWhiteSpace    = Int32.MaxValue;
                    for (int curLine = decorators == null ? 1 : 2; curLine < lines.Length; curLine++)
                    {
                        var line = lines[curLine];

                        for (int i = 0; i < line.Length; i++)
                        {
                            if (!Char.IsWhiteSpace(line[i]))
                            {
                                minWhiteSpace = Math.Min(minWhiteSpace, i);
                                break;
                            }
                        }
                    }

                    StringBuilder newLines = new StringBuilder();
                    newLines.Append(indentationLevel);
                    newLines.Append(lines[0]);
                    if (decorators != null)
                    {
                        newLines.Append(_newline);
                        newLines.Append(indentationLevel);
                        newLines.Append(lines[1]);
                    }

                    // don't include a bunch of blank lines...
                    int endLine = lines.Length - 1;
                    for (; endLine >= 0 && String.IsNullOrWhiteSpace(lines[endLine]); endLine--)
                    {
                    }

                    newLines.Append(_newline);
                    for (int curLine = decorators == null ? 1 : 2; curLine <= endLine; curLine++)
                    {
                        var line = lines[curLine];

                        newLines.Append(indentationLevel);
                        if (_insertTabs)
                        {
                            newLines.Append('\t');
                        }
                        else
                        {
                            newLines.Append(' ', _indentSize);
                        }

                        if (line.Length > minWhiteSpace)
                        {
                            newLines.Append(line, minWhiteSpace, line.Length - minWhiteSpace);
                        }
                        newLines.Append(_newline);
                    }
                    newLines.Append(_newline);
                    method = newLines.ToString();
                    break;
                }
            }

            string comma;

            if (_outputVars.Count > 0)
            {
                comma = "";
                foreach (var outputVar in _outputVars)
                {
                    newCall.Append(comma);
                    newCall.Append(outputVar.Name);
                    comma = ", ";
                }
                newCall.Append(" = ");
            }
            else if (_target.ContainsReturn)
            {
                newCall.Append("return ");
            }

            if (isCoroutine)
            {
                newCall.Append("await ");
            }

            if (_targetScope is ClassDefinition)
            {
                var fromScope = _scopes[_scopes.Length - 1] as FunctionDefinition;
                Debug.Assert(fromScope != null);  // we don't allow extracting from classes, so we have to be coming from a function

                if (isStaticMethod)
                {
                    newCall.Append(_targetScope.Name);
                    newCall.Append('.');
                }
                else if (fromScope != null && fromScope.Parameters.Count > 0)
                {
                    newCall.Append(fromScope.Parameters[0].Name);
                    newCall.Append('.');
                }
            }

            newCall.Append(_name);
            newCall.Append('(');

            comma = "";
            foreach (var param in parameters)
            {
                if (param.Name != selfParam)
                {
                    newCall.Append(comma);
                    newCall.Append(param.Name);
                    comma = ", ";
                }
            }

            newCall.Append(')');

            return(new ExtractMethodResult(
                       method,
                       newCall.ToString()
                       ));
        }
示例#18
0
        private void AddTagIfNecessary(int startIndex, int endIndex, int headerIndex = -1, DecoratorStatement decorator = null, int minLinesToCollapse = 3)
        {
            var startLine = _ast.IndexToLocation(startIndex).Line;
            var endLine   = _ast.IndexToLocation(endIndex).Line;
            var lines     = endLine - startLine + 1;

            // Collapse if more than 3 lines.
            if (lines >= minLinesToCollapse)
            {
                if (decorator != null)
                {
                    // we don't want to collapse the decorators, we like them visible, so
                    // we base our starting position on where the decorators end.
                    startIndex = decorator.EndIndex + 1;
                }

                var tagSpan = new AP.OutliningTag()
                {
                    startIndex  = startIndex,
                    endIndex    = endIndex,
                    headerIndex = headerIndex
                };
                TagSpans.Add(tagSpan);
            }
        }