예제 #1
0
 internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     _left.AppendCodeString(res, ast, format);
     res.Append(this.GetProceedingWhiteSpace(ast));
     res.Append(_op.ToCodeString());
     res.Append('=');
     _right.AppendCodeString(res, ast, format);
 }
예제 #2
0
        internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
            format.ReflowComment(res, this.GetProceedingWhiteSpace(ast));
            res.Append("with");
            var itemWhiteSpace = this.GetListWhiteSpace(ast);
            int whiteSpaceIndex = 0;
            for (int i = 0; i < _items.Length; i++) {
                var item = _items[i];
                if (i != 0) {
                    if (itemWhiteSpace != null) {
                        res.Append(itemWhiteSpace[whiteSpaceIndex++]);
                    }
                    res.Append(',');
                }

                item.ContextManager.AppendCodeString(res, ast, format);
                if (item.Variable != null) {
                    if (itemWhiteSpace != null) {
                        res.Append(itemWhiteSpace[whiteSpaceIndex++]);
                    } else {
                        res.Append(' ');
                    }
                    res.Append("as");
                    item.Variable.AppendCodeString(res, ast, format);
                }
            }

            _body.AppendCodeString(res, ast, format);
        }
예제 #3
0
        internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
            Target.AppendCodeString(res, ast, format);
            format.Append(
                res,
                format.SpaceBeforeIndexBracket,
                " ",
                "",
                this.GetProceedingWhiteSpace(ast)
            );

            res.Append('[');
            _index.AppendCodeString(
                res, 
                ast, 
                format, 
                format.SpaceWithinIndexBrackets != null ? format.SpaceWithinIndexBrackets.Value ? " " : "" : null
            );

            if (!this.IsMissingCloseGrouping(ast)) {
                format.Append(
                    res,
                    format.SpaceWithinIndexBrackets,
                    " ",
                    "",
                    this.GetSecondWhiteSpace(ast)
                );
                res.Append(']');
            }
        }
예제 #4
0
 public override string GetLeadingWhiteSpace(PythonAst ast) {
     var whitespace = this.GetListWhiteSpace(ast);
     if (whitespace != null && whitespace.Length > 0) {
         return whitespace[0];
     }
     return null;
 }
예제 #5
0
 public override bool Walk(PythonAst node) {
     if (_ast != node) {
         throw new InvalidOperationException("walking wrong AST");
     }
     _scope.Push(_members);
     return base.Walk(node);
 }
예제 #6
0
 public override string GetLeadingWhiteSpace(PythonAst ast) {
     var decorateWhiteSpace = this.GetNamesWhiteSpace(ast);
     if (decorateWhiteSpace != null && decorateWhiteSpace.Length > 0) {
         return decorateWhiteSpace[0];
     }
     return "";
 }
예제 #7
0
 public override void SetLeadingWhiteSpace(PythonAst ast, string whiteSpace) {
     var decorateWhiteSpace = this.GetNamesWhiteSpace(ast);
     if (decorateWhiteSpace != null && decorateWhiteSpace.Length > 0) {
         decorateWhiteSpace[0] = whiteSpace;
     }
     
 }
예제 #8
0
        public MethodExtractor(IServiceProvider serviceProvider, ITextView textView) {
            _view = textView;
            _serviceProvider = serviceProvider;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot);
        }
예제 #9
0
        internal static IEnumerable<IScopeNode> EnumerateBody(PythonAst ast, Statement body, bool includeAssignments = true) {
            SuiteStatement suite = body as SuiteStatement;
            if (suite != null) {
                foreach (Statement stmt in suite.Statements) {
                    ClassDefinition klass = stmt as ClassDefinition;
                    if (klass != null) {
                        yield return new ClassScopeNode(klass);
                        continue;
                    }

                    FunctionDefinition func = stmt as FunctionDefinition;
                    if (func != null) {
                        yield return new FunctionScopeNode(func);
                        continue;
                    }

                    AssignmentStatement assign;
                    if (includeAssignments && (assign = stmt as AssignmentStatement) != null) {
                        foreach (var target in assign.Left) {
                            NameExpression name = target as NameExpression;
                            if (name != null) {
                                yield return new AssignmentScopeNode(ast, assign, name);
                            }
                        }
                    }
                }
            }
        }
예제 #10
0
        internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
            Expression left = _left;
            Expression right = _right;
            string op1, op2;

            if (Operator == PythonOperator.NotIn) {
                op1 = "not";
                if (!this.IsIncompleteNode(ast)) {
                    op2 = "in";
                } else {
                    op2 = null;
                }
            } else if (Operator == PythonOperator.IsNot) {
                op1 = "is";
                op2 = "not";
            } else if ((op1 = this.GetVerbatimImage(ast)) != null) {
                // operator image differs from the operator enum, for example <> is always NotEqual which is !=
                // so we store the verbatim image and use it here.
                op2 = null;
            } else {
                op1 = Operator.ToCodeString();
                op2 = null;
            }
            BinaryToCodeString(res, ast, format, this, _left, _right, op1, op2);
        }
예제 #11
0
 public override void SetLeadingWhiteSpace(PythonAst ast, string whiteSpace) {
     if (this.IsAltForm(ast)) {
         Items[0].SetLeadingWhiteSpace(ast, whiteSpace);
     } else {
         base.SetLeadingWhiteSpace(ast, whiteSpace);
     }
 }
예제 #12
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string leadingWhiteSpace) {
     string kwOnlyText = this.GetExtraVerbatimText(ast);
     if (kwOnlyText != null) {
         if (leadingWhiteSpace != null) {
             res.Append(leadingWhiteSpace);
             res.Append(kwOnlyText.TrimStart());
             leadingWhiteSpace = null;
         } else {
             res.Append(kwOnlyText);
         }
     }
     bool isAltForm = this.IsAltForm(ast);
     if (isAltForm) {
         res.Append(leadingWhiteSpace ?? this.GetProceedingWhiteSpace(ast));
         res.Append('(');
         leadingWhiteSpace = null;
     }
     _error.AppendCodeString(res, ast, format, leadingWhiteSpace);
     if (this.DefaultValue != null) {
         res.Append(this.GetSecondWhiteSpace(ast));
         res.Append('=');
         this.DefaultValue.AppendCodeString(res, ast, format);
     }
     if (isAltForm && !this.IsMissingCloseGrouping(ast)) {
         res.Append(this.GetSecondWhiteSpace(ast));
         res.Append(')');
     }
 }
예제 #13
0
        public void UpdateTree(PythonAst newAst, IAnalysisCookie newCookie) {
            lock (this) {
                if (_updatesPending > 0) {
                    _updatesPending--;
                }
                if (newAst == null) {
                    // there was an error in parsing, just let the waiter go...
                    if (_curWaiter != null) {
                        _curWaiter.Set();
                    }
                    _tree = null;
                    return;
                }

                _tree = newAst;
                _cookie = newCookie;

                if (_curWaiter != null) {
                    _curWaiter.Set();
                }
            }

            var newParse = OnNewParseTree;
            if (newParse != null) {
                newParse(this, EventArgs.Empty);
            }
        }
예제 #14
0
 public override void SetLeadingWhiteSpace(PythonAst ast, string whiteSpace)
 {
     var itemWhiteSpace = this.GetListWhiteSpace(ast);
     if (itemWhiteSpace != null && itemWhiteSpace.Length > 0) {
         itemWhiteSpace[0] = whiteSpace;
     }
 }
예제 #15
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     if (this.IsAltForm(ast)) {
         this.AppendCodeString(res, ast, format, "", "", _item);
     } else {
         this.AppendCodeString(res, ast, format, "(", this.IsMissingCloseGrouping(ast) ? "" : ")", _item);
     }
 }
예제 #16
0
 public CoverageMapper(PythonAst ast, string filename, HashSet<int> hits) {
     _ast = ast;
     _filename = filename;
     GlobalScope = new CoverageScope(ast);
     CurScopes.Add(GlobalScope);
     _blockCount++;
     _hits = hits;
 }
예제 #17
0
        public ImportRemover(IServiceProvider serviceProvider, ITextView textView, bool allScopes) {
            _view = textView;
            _serviceProvider = serviceProvider;
            var snapshot = _view.TextBuffer.CurrentSnapshot;

            _ast = _view.GetAnalyzer(_serviceProvider).ParseSnapshot(snapshot);
            _allScopes = allScopes;
        }
예제 #18
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     format.ReflowComment(res, this.GetProceedingWhiteSpaceDefaultNull(ast));
     if (format.UseVerbatimImage) {
         res.Append(this.GetVerbatimImage(ast) ?? _name);
     } else {
         res.Append(_name);
     }
 }
예제 #19
0
 public AstPythonParameterInfo(PythonAst ast, Parameter p) {
     Name = p.Name;
     Documentation = "";
     DefaultValue = p.DefaultValue?.ToCodeString(ast) ?? "";
     IsParamArray = p.Kind == ParameterKind.List;
     IsKeywordDict = p.Kind == ParameterKind.Dictionary;
     ParameterTypes = new IPythonType[0];
 }
예제 #20
0
        public AstPythonType(PythonAst ast, IPythonModule declModule, ClassDefinition def, string doc, LocationInfo loc) {
            _members = new Dictionary<string, IMember>();

            Name = def.Name;
            Documentation = doc;
            DeclaringModule = declModule;
            Mro = new IPythonType[0];
            Locations = new[] { loc };
        }
예제 #21
0
        public OverviewWalker(ProjectEntry entry, AnalysisUnit topAnalysis, PythonAst tree) {
            _entry = entry;
            _curUnit = topAnalysis;

            _tree = tree;
            Debug.Assert(_tree != null);

            _scope = topAnalysis.Scope;
        }
예제 #22
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     format.ReflowComment(res, this.GetProceedingWhiteSpace(ast));
     res.Append('`');
     _expression.AppendCodeString(res, ast, format);
     if (!this.IsMissingCloseGrouping(ast)) {
         res.Append(this.GetSecondWhiteSpace(ast));
         res.Append('`');
     }
 }
예제 #23
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     _target.AppendCodeString(res, ast, format);
     res.Append(this.GetProceedingWhiteSpaceDefaultNull(ast));
     res.Append('.');
     if (!this.IsIncompleteNode(ast)) {
         res.Append(this.GetSecondWhiteSpaceDefaultNull(ast));
         res.Append(this.GetVerbatimImage(ast) ?? _name);
     }
 }
예제 #24
0
        /// <summary>
        /// Removes the import at the specified index (which must be in the range of
        /// the Names property) and returns a new ImportStatement which is the same
        /// as this one minus the imported name.  Preserves all round-tripping metadata
        /// in the process.
        /// 
        /// New in 1.1.
        /// </summary>
        public ImportStatement RemoveImport(PythonAst ast, int index) {
            if (index < 0 || index >= _names.Length) {
                throw new ArgumentOutOfRangeException("index");
            }
            if (ast == null) {
                throw new ArgumentNullException("ast");
            }

            ModuleName[] names = new ModuleName[_names.Length - 1];
            NameExpression[] asNames = _asNames == null ? null : new NameExpression[_asNames.Length - 1];
            var asNameWhiteSpace = this.GetNamesWhiteSpace(ast);
            var itemWhiteSpace = this.GetListWhiteSpace(ast);
            List<string> newAsNameWhiteSpace = new List<string>();
            List<string> newListWhiteSpace = new List<string>();
            int asIndex = 0;
            for (int i = 0, write = 0; i < _names.Length; i++) {
                bool includingCurrentName = i != index;

                // track the white space, this needs to be kept in sync w/ ToCodeString and how the
                // parser creates the white space.
                if (i > 0 && itemWhiteSpace != null) {
                    if (includingCurrentName) {
                        newListWhiteSpace.Add(itemWhiteSpace[i - 1]);
                    }
                }

                if (includingCurrentName) {
                    names[write] = _names[i];

                    if (_asNames != null) {
                        asNames[write] = _asNames[i];
                    }

                    write++;
                }

                if (AsNames[i] != null && includingCurrentName) {
                    if (asNameWhiteSpace != null) {
                        newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                    }

                    if (_asNames[i].Name.Length != 0) {
                        if (asNameWhiteSpace != null) {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                    }
                }
            }

            var res = new ImportStatement(names, asNames, _forceAbsolute);
            ast.CopyAttributes(this, res);
            ast.SetAttribute(res, NodeAttributes.NamesWhiteSpace, newAsNameWhiteSpace.ToArray());
            ast.SetAttribute(res, NodeAttributes.ListWhiteSpace, newListWhiteSpace.ToArray());

            return res;
        }
예제 #25
0
 internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     format.ReflowComment(res, this.GetProceedingWhiteSpace(ast));
     res.Append("assert");
     _test.AppendCodeString(res, ast, format);
     if (_message != null) {
         res.Append(this.GetSecondWhiteSpace(ast));
         res.Append(',');
         _message.AppendCodeString(res, ast, format);
     }
 }
예제 #26
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     res.Append(this.GetProceedingWhiteSpace(ast));
     res.Append("for");
     _lhs.AppendCodeString(res, ast, format);
     if (!this.IsIncompleteNode(ast)) {
         res.Append(this.GetSecondWhiteSpace(ast));
         res.Append("in");
         _list.AppendCodeString(res, ast, format);
     }
 }
예제 #27
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     var whitespace = this.GetListWhiteSpace(ast);
     for (int i = 0; i < _dotCount; i++) {
         if (whitespace != null) {
             res.Append(whitespace[i]);
         }
         res.Append('.');
     }
     base.AppendCodeString(res, ast, format);
 }
예제 #28
0
 internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     format.ReflowComment(res, this.GetProceedingWhiteSpace(ast));
     res.Append("while");
     _test.AppendCodeString(res, ast, format);
     _body.AppendCodeString(res, ast, format);
     if (_else != null) {
         format.ReflowComment(res, this.GetSecondWhiteSpaceDefaultNull(ast));
         res.Append("else");
         _else.AppendCodeString(res, ast, format);
     }
 }
예제 #29
0
        internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
            _target.AppendCodeString(res, ast, format);
            format.Append(
                res,
                format.SpaceBeforeCallParen,
                " ",
                "",
                this.GetProceedingWhiteSpaceDefaultNull(ast)
            ); 

            res.Append('(');

            if (_args.Length == 0) {
                if (format.SpaceWithinEmptyCallArgumentList != null && format.SpaceWithinEmptyCallArgumentList.Value) {
                    res.Append(' ');
                }
            } else {
                var listWhiteSpace = this.GetListWhiteSpace(ast);
                for (int i = 0; i < _args.Length; i++) {
                    if (i > 0) {
                        if (listWhiteSpace != null) {
                            res.Append(listWhiteSpace[i - 1]);
                        }
                        res.Append(',');
                    } else if (format.SpaceWithinCallParens != null) {
                        _args[i].AppendCodeString(res, ast, format, format.SpaceWithinCallParens.Value ? " " : "");
                        continue;
                    }

                    _args[i].AppendCodeString(res, ast, format);
                }

                if (listWhiteSpace != null && listWhiteSpace.Length == _args.Length) {
                    // trailing comma
                    res.Append(listWhiteSpace[listWhiteSpace.Length - 1]);
                    res.Append(",");
                }
            }
            
            if (!this.IsMissingCloseGrouping(ast)) {
                if (Args.Count != 0 || 
                    format.SpaceWithinEmptyCallArgumentList == null ||
                    !String.IsNullOrWhiteSpace(this.GetSecondWhiteSpaceDefaultNull(ast))) {
                    format.Append(
                        res,
                        format.SpaceWithinCallParens,
                        " ",
                        "",
                        this.GetSecondWhiteSpaceDefaultNull(ast)
                    );
                }
                res.Append(')');
            }
        }
예제 #30
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) {
     format.ReflowComment(res, this.GetProceedingWhiteSpace(ast));
     res.Append("await");
     if (!this.IsAltForm(ast)) {
         _expression.AppendCodeString(res, ast, format);
         var itemWhiteSpace = this.GetListWhiteSpace(ast);
         if (itemWhiteSpace != null) {
             res.Append(",");
             res.Append(itemWhiteSpace[0]);
         }
     }
 }
예제 #31
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     format.ReflowComment(res, this.GetProceedingWhiteSpaceDefaultNull(ast));
     res.Append(this.GetVerbatimImage(ast) ?? _name);
 }
예제 #32
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     AppendCodeString(res, ast, format, null);
 }
예제 #33
0
 public override string GetLeadingWhiteSpace(PythonAst ast)
 {
     return(_expression.GetLeadingWhiteSpace(ast));
 }
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast));
     res.Append(_op.ToCodeString());
     _expression.AppendCodeString(res, ast, format);
 }
예제 #35
0
 internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast));
     res.Append("continue");
 }
예제 #36
0
 public override void SetLeadingWhiteSpace(PythonAst ast, string whiteSpace)
 {
     _left.SetLeadingWhiteSpace(ast, whiteSpace);
 }
예제 #37
0
 public override string GetLeadingWhiteSpace(PythonAst ast)
 {
     return(_left.GetLeadingWhiteSpace(ast));
 }
예제 #38
0
파일: Statement.cs 프로젝트: szh2bg/PTVS
 internal abstract void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format);
예제 #39
0
파일: Statement.cs 프로젝트: szh2bg/PTVS
 internal override sealed void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     AppendCodeStringStmt(res, ast, format);
 }
예제 #40
0
 internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     _body.AppendCodeString(res, ast, format);
     res.Append(this.GetExtraVerbatimText(ast));
 }
예제 #41
0
 internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     _expression.AppendCodeString(res, ast, format);
 }
예제 #42
0
파일: Node.cs 프로젝트: xNUTs/PTVS
 /// <summary>
 /// Returns the proceeeding whitespace (newlines and comments) that
 /// shows up before this node.
 ///
 /// New in 1.1.
 /// </summary>
 public virtual string GetLeadingWhiteSpace(PythonAst ast)
 {
     return(this.GetProceedingWhiteSpaceDefaultNull(ast) ?? "");
 }
예제 #43
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     _test.AppendCodeString(res, ast, format);
     _body.AppendCodeString(res, ast, format);
 }
예제 #44
0
        internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
        {
            if (Decorators != null)
            {
                Decorators.AppendCodeString(res, ast, format);
            }

            format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast));
            res.Append("class");
            res.Append(this.GetSecondWhiteSpace(ast));
            res.Append(this.GetVerbatimImage(ast) ?? Name);

            if (!this.IsAltForm(ast))
            {
                format.Append(
                    res,
                    format.SpaceBeforeClassDeclarationParen,
                    " ",
                    "",
                    this.GetThirdWhiteSpace(ast)
                    );

                res.Append('(');
            }

            if (Bases.Count != 0)
            {
                ListExpression.AppendItems(
                    res,
                    ast,
                    format,
                    "",
                    "",
                    this,
                    this.Bases.Count,
                    (i, sb) => {
                    if (format.SpaceWithinClassDeclarationParens != null && i == 0)
                    {
                        // need to remove any leading whitespace which was preserved for
                        // the 1st param, and then force the correct whitespace.
                        Bases[i].AppendCodeString(sb, ast, format, format.SpaceWithinClassDeclarationParens.Value ? " " : "");
                    }
                    else
                    {
                        Bases[i].AppendCodeString(sb, ast, format);
                    }
                }
                    );
            }
            else if (!this.IsAltForm(ast))
            {
                if (format.SpaceWithinEmptyBaseClassList != null && format.SpaceWithinEmptyBaseClassList.Value)
                {
                    res.Append(' ');
                }
            }

            if (!this.IsAltForm(ast) && !this.IsMissingCloseGrouping(ast))
            {
                if (Bases.Count != 0 ||
                    format.SpaceWithinEmptyBaseClassList == null ||
                    !String.IsNullOrWhiteSpace(this.GetFourthWhiteSpace(ast)))
                {
                    format.Append(
                        res,
                        format.SpaceWithinClassDeclarationParens,
                        " ",
                        "",
                        this.GetFourthWhiteSpace(ast)
                        );
                }

                res.Append(')');
            }

            _body.AppendCodeString(res, ast, format);
        }
예제 #45
0
 public SourceLocation GetHeader(PythonAst ast)
 {
     return(ast.IndexToLocation(_headerIndex));
 }
예제 #46
0
        /// <summary>
        /// Removes the import at the specified index (which must be in the range of
        /// the Names property) and returns a new ImportStatement which is the same
        /// as this one minus the imported name.  Preserves all round-tripping metadata
        /// in the process.
        ///
        /// New in 1.1.
        /// </summary>
        public ImportStatement RemoveImport(PythonAst ast, int index)
        {
            if (index < 0 || index >= _names.Length)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (ast == null)
            {
                throw new ArgumentNullException("ast");
            }

            ModuleName[]     names            = new ModuleName[_names.Length - 1];
            NameExpression[] asNames          = _asNames == null ? null : new NameExpression[_asNames.Length - 1];
            var           asNameWhiteSpace    = this.GetNamesWhiteSpace(ast);
            var           itemWhiteSpace      = this.GetListWhiteSpace(ast);
            List <string> newAsNameWhiteSpace = new List <string>();
            List <string> newListWhiteSpace   = new List <string>();
            int           asIndex             = 0;

            for (int i = 0, write = 0; i < _names.Length; i++)
            {
                bool includingCurrentName = i != index;

                // track the white space, this needs to be kept in sync w/ ToCodeString and how the
                // parser creates the white space.
                if (i > 0 && itemWhiteSpace != null)
                {
                    if (includingCurrentName)
                    {
                        newListWhiteSpace.Add(itemWhiteSpace[i - 1]);
                    }
                }

                if (includingCurrentName)
                {
                    names[write] = _names[i];

                    if (_asNames != null)
                    {
                        asNames[write] = _asNames[i];
                    }

                    write++;
                }

                if (AsNames[i] != null && includingCurrentName)
                {
                    if (asNameWhiteSpace != null)
                    {
                        newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                    }

                    if (_asNames[i].Name.Length != 0)
                    {
                        if (asNameWhiteSpace != null)
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                    }
                }
            }

            var res = new ImportStatement(names, asNames, _forceAbsolute);

            ast.CopyAttributes(this, res);
            ast.SetAttribute(res, NodeAttributes.NamesWhiteSpace, newAsNameWhiteSpace.ToArray());
            ast.SetAttribute(res, NodeAttributes.ListWhiteSpace, newListWhiteSpace.ToArray());

            return(res);
        }
        internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
        {
            // SuiteStatement comes in 3 forms:
            //  1. The body of a if/else/while/for/etc... where there's an opening colon
            //  2. A set of semi-colon separated items
            //  3. A top-level group of statements in a top-level PythonAst node.
            var itemWhiteSpace  = this.GetListWhiteSpace(ast);
            var colonWhiteSpace = this.GetPreceedingWhiteSpaceDefaultNull(ast);

            if (this.IsAltForm(ast))
            {
                // suite statement in top-level PythonAst, we have no colons or other delimiters
                foreach (var statement in _statements)
                {
                    statement.AppendCodeString(res, ast, format);
                }
            }
            else if (itemWhiteSpace != null)
            {
                if (format.BreakMultipleStatementsPerLine)
                {
                    string leadingWhiteSpace = "";
                    for (int i = 0; i < _statements.Length; i++)
                    {
                        if (i == 0)
                        {
                            StringBuilder tmp = new StringBuilder();
                            _statements[i].AppendCodeString(tmp, ast, format);
                            var stmt = tmp.ToString();
                            res.Append(stmt);

                            // figure out the whitespace needed for the next statement based upon the current statement
                            for (int curChar = 0; curChar < stmt.Length; curChar++)
                            {
                                if (!char.IsWhiteSpace(stmt[curChar]))
                                {
                                    leadingWhiteSpace = format.GetNextLineProceedingText(stmt.Substring(0, curChar));
                                    break;
                                }
                            }
                        }
                        else
                        {
                            _statements[i].AppendCodeString(res, ast, format, leadingWhiteSpace);
                        }
                    }
                }
                else
                {
                    // form 2, semi-colon seperated list.
                    for (int i = 0; i < _statements.Length; i++)
                    {
                        if (i > 0)
                        {
                            if (i - 1 < itemWhiteSpace.Length)
                            {
                                res.Append(itemWhiteSpace[i - 1]);
                            }
                            res.Append(';');
                        }
                        _statements[i].AppendCodeString(res, ast, format);
                    }
                }

                if (itemWhiteSpace != null && itemWhiteSpace.Length == _statements.Length && _statements.Length != 0)
                {
                    // trailing semi-colon
                    if (!format.RemoveTrailingSemicolons)
                    {
                        res.Append(itemWhiteSpace[itemWhiteSpace.Length - 1]);
                        res.Append(";");
                    }
                }
            }
            else
            {
                // 3rd form, suite statement as the body of a class/function, we include the colon.
                if (colonWhiteSpace != null)
                {
                    res.Append(colonWhiteSpace);
                }
                res.Append(':');

                foreach (var statement in _statements)
                {
                    statement.AppendCodeString(res, ast, format);
                }
            }
        }
 /// <summary>
 /// Marks this tuple expression as having no parenthesis for the purposes of round tripping.
 /// </summary>
 public void RoundTripHasNoParenthesis(PythonAst ast)
 {
     ast.SetAttribute(this, NodeAttributes.IsAltFormValue, NodeAttributes.IsAltFormValue);
 }
예제 #49
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     // WithStatement expands us
     throw new InvalidOperationException();
 }
예제 #50
0
 public void AddPreceedingWhiteSpace(PythonAst ast, string whiteSpace)
 {
     ast.SetAttribute(this, NodeAttributes.PreceedingWhiteSpace, whiteSpace);
 }
예제 #51
0
 public void RoundTripRemoveValueWhiteSpace(PythonAst ast)
 {
     ast.SetAttribute(this, NodeAttributes.IsAltFormValue, NodeAttributes.IsAltFormValue);
 }
예제 #52
0
        internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string leadingWhiteSpace)
        {
            string kwOnlyText = this.GetExtraVerbatimText(ast);

            if (kwOnlyText != null)
            {
                if (leadingWhiteSpace != null)
                {
                    res.Append(leadingWhiteSpace);
                    res.Append(kwOnlyText.TrimStart());
                    leadingWhiteSpace = null;
                }
                else
                {
                    res.Append(kwOnlyText);
                }
            }
            switch (Kind)
            {
            case ParameterKind.Dictionary:
                res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpace(ast));
                res.Append("**");
                res.Append(this.GetSecondWhiteSpace(ast));
                res.Append(this.GetVerbatimImage(ast) ?? _name);
                AppendAnnotation(res, ast, format);
                break;

            case ParameterKind.List:
                res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpace(ast));
                res.Append('*');
                res.Append(this.GetSecondWhiteSpace(ast));
                res.Append(this.GetVerbatimImage(ast) ?? _name);
                AppendAnnotation(res, ast, format);
                break;

            case ParameterKind.Normal:
                if (this.IsAltForm(ast))
                {
                    res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpace(ast));
                    res.Append('(');
                    res.Append(this.GetThirdWhiteSpace(ast));
                    res.Append(this.GetVerbatimImage(ast) ?? _name);
                    if (!this.IsMissingCloseGrouping(ast))
                    {
                        res.Append(this.GetSecondWhiteSpace(ast));
                        res.Append(')');
                    }
                }
                else
                {
                    res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast));
                    res.Append(this.GetVerbatimImage(ast) ?? _name);
                    AppendAnnotation(res, ast, format);
                }
                break;

            case ParameterKind.KeywordOnly:
                res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpace(ast));
                res.Append(this.GetVerbatimImage(ast) ?? _name);
                AppendAnnotation(res, ast, format);
                break;

            default: throw new InvalidOperationException();
            }

            if (_defaultValue != null)
            {
                format.Append(
                    res,
                    format.SpaceAroundDefaultValueEquals,
                    " ",
                    "",
                    NodeAttributes.GetWhiteSpace(this, ast, WhitespacePrecedingAssign)
                    );

                res.Append('=');
                if (format.SpaceAroundDefaultValueEquals != null)
                {
                    _defaultValue.AppendCodeString(res, ast, format, format.SpaceAroundDefaultValueEquals.Value ? " " : "");
                }
                else
                {
                    _defaultValue.AppendCodeString(res, ast, format);
                }
            }
        }
예제 #53
0
 public PythonReference[] GetReferences(PythonAst ast)
 {
     return(GetVariableReferences(this, ast));
 }
예제 #54
0
 internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
 {
     res.Append(this.GetProceedingWhiteSpace(ast));
     res.Append('*');
     _expr.AppendCodeString(res, ast, format);
 }
예제 #55
0
파일: Node.cs 프로젝트: xNUTs/PTVS
 public SourceSpan GetSpan(PythonAst parent)
 {
     return(new SourceSpan(GetStart(parent), GetEnd(parent)));
 }
예제 #56
0
 public override void SetLeadingWhiteSpace(PythonAst ast, string whiteSpace)
 {
     _expression.SetLeadingWhiteSpace(ast, whiteSpace);
 }
예제 #57
0
 /// <summary>
 /// Gets the variable reference for the specific assignment to the variable for this function definition.
 /// </summary>
 public PythonReference GetVariableReference(PythonAst ast)
 {
     return(GetVariableReference(this, ast));
 }
예제 #58
0
파일: Node.cs 프로젝트: xNUTs/PTVS
 public static void CopyLeadingWhiteSpace(PythonAst parentNode, Node fromNode, Node toNode)
 {
     parentNode.SetAttribute(toNode, NodeAttributes.PreceedingWhiteSpace, fromNode.GetLeadingWhiteSpace(parentNode));
 }
예제 #59
0
        internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format)
        {
            var decorateWhiteSpace = this.GetNamesWhiteSpace(ast);

            if (Decorators != null)
            {
                Decorators.AppendCodeString(res, ast, format);
            }
            format.ReflowComment(res, this.GetProceedingWhiteSpaceDefaultNull(ast));
            res.Append("def");
            var name = this.GetVerbatimImage(ast) ?? Name;

            if (!String.IsNullOrEmpty(name))
            {
                res.Append(this.GetSecondWhiteSpace(ast));
                res.Append(name);
                if (!this.IsIncompleteNode(ast))
                {
                    format.Append(
                        res,
                        format.SpaceBeforeFunctionDeclarationParen,
                        " ",
                        "",
                        this.GetThirdWhiteSpaceDefaultNull(ast)
                        );

                    res.Append('(');
                    if (Parameters.Count != 0)
                    {
                        var commaWhiteSpace = this.GetListWhiteSpace(ast);
                        ParamsToString(res,
                                       ast,
                                       commaWhiteSpace,
                                       format,
                                       format.SpaceWithinFunctionDeclarationParens != null ?
                                       format.SpaceWithinFunctionDeclarationParens.Value ? " " : "" :
                                       null
                                       );
                    }

                    string namedOnly = this.GetExtraVerbatimText(ast);
                    if (namedOnly != null)
                    {
                        res.Append(namedOnly);
                    }

                    if (!this.IsMissingCloseGrouping(ast))
                    {
                        format.Append(
                            res,
                            Parameters.Count != 0 ?
                            format.SpaceWithinFunctionDeclarationParens :
                            format.SpaceWithinEmptyParameterList,
                            " ",
                            "",
                            this.GetFourthWhiteSpaceDefaultNull(ast)
                            );

                        res.Append(')');
                    }
                    if (ReturnAnnotation != null)
                    {
                        format.Append(
                            res,
                            format.SpaceAroundAnnotationArrow,
                            " ",
                            "",
                            this.GetFifthWhiteSpace(ast)
                            );
                        res.Append("->");
                        _returnAnnotation.AppendCodeString(
                            res,
                            ast,
                            format,
                            format.SpaceAroundAnnotationArrow != null ?
                            format.SpaceAroundAnnotationArrow.Value ? " " : "" :
                            null
                            );
                    }
                    if (Body != null)
                    {
                        Body.AppendCodeString(res, ast, format);
                    }
                }
            }
        }
예제 #60
0
파일: Node.cs 프로젝트: xNUTs/PTVS
 public SourceLocation GetEnd(PythonAst parent)
 {
     return(parent.IndexToLocation(EndIndex));
 }