/// <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");
            }

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

            for (int i = 0, write = 0; i < _names.Length; i++)
            {
                var 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);
        }
Esempio n. 2
0
 public virtual void PostWalk(ModuleName node)
 {
 }
Esempio n. 3
0
 // ModuleName
 public virtual bool Walk(ModuleName node)
 {
     return(true);
 }
Esempio n. 4
0
 // ModuleName
 public override bool Walk(ModuleName node)
 {
     return(Location >= node.StartIndex && Location <= node.EndIndex);
 }
Esempio n. 5
0
 public override void PostWalk(ModuleName node)
 {
 }
Esempio n. 6
0
 // ModuleName
 public override bool Walk(ModuleName node)
 {
     return(false);
 }