/// <summary>
        /// Returns a new FromImport statement that is identical to this one but has
        /// removed the specified import statement.  Otherwise preserves any attributes
        /// for the statement.
        ///
        /// New in 1.1.
        /// <param name="ast">The parent AST whose attributes should be updated for the new node.</param>
        /// <param name="index">The index in Names of the import to be removed.</param>
        /// </summary>
        public FromImportStatement RemoveImport(PythonAst ast, int index)
        {
            if (index < 0 || index >= Names.Count)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (ast == null)
            {
                throw new ArgumentNullException("ast");
            }

            NameExpression[] names            = new NameExpression[Names.Count - 1];
            NameExpression[] asNames          = AsNames == null ? null : new NameExpression[AsNames.Count - 1];
            var           asNameWhiteSpace    = this.GetNamesWhiteSpace(ast);
            List <string> newAsNameWhiteSpace = new List <string>();
            int           importIndex         = ImportIndex;
            int           asIndex             = 0;

            for (int i = 0, write = 0; i < Names.Count; 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 (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                {
                    if (write > 0)
                    {
                        if (includingCurrentName)
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                        else
                        {
                            asIndex++;
                        }
                    }
                    else if (i > 0)
                    {
                        asIndex++;
                    }
                }

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                {
                    if (includingCurrentName)
                    {
                        if (newAsNameWhiteSpace.Count == 0)
                        {
                            // no matter what we want the 1st entry to have the whitespace after the import keyword
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[0]);
                            asIndex++;
                        }
                        else
                        {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                    }
                    else
                    {
                        asIndex++;
                    }
                }

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

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

                    write++;
                }

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

                    if (AsNames[i].Name.Length != 0)
                    {
                        if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
                        {
                            if (i != index)
                            {
                                newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                            }
                            else
                            {
                                asIndex++;
                            }
                        }
                    }
                    else
                    {
                        asIndex++;
                    }
                }
            }

            if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length)
            {
                // trailing comma
                newAsNameWhiteSpace.Add(asNameWhiteSpace[asNameWhiteSpace.Length - 1]);
            }

            var res = new FromImportStatement(Root, names, asNames, IsFromFuture, ForceAbsolute, importIndex);

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

            return(res);
        }
        /// <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);
        }
Exemple #3
0
        private static AP.ChangeInfo UpdateFromImport(
            PythonAst curAst,
            FromImportStatement fromImport,
            string name
        ) {
            NameExpression[] names = new NameExpression[fromImport.Names.Count + 1];
            NameExpression[] asNames = fromImport.AsNames == null ? null : new NameExpression[fromImport.AsNames.Count + 1];
            NameExpression newName = new NameExpression(name);
            for (int i = 0; i < fromImport.Names.Count; i++) {
                names[i] = fromImport.Names[i];
            }
            names[fromImport.Names.Count] = newName;

            if (asNames != null) {
                for (int i = 0; i < fromImport.AsNames.Count; i++) {
                    asNames[i] = fromImport.AsNames[i];
                }
            }

            var newImport = new FromImportStatement((ModuleName)fromImport.Root, names, asNames, fromImport.IsFromFuture, fromImport.ForceAbsolute);
            curAst.CopyAttributes(fromImport, newImport);

            var newCode = newImport.ToCodeString(curAst);

            var span = fromImport.GetSpan(curAst);
            int leadingWhiteSpaceLength = (fromImport.GetLeadingWhiteSpace(curAst) ?? "").Length;
            return new AP.ChangeInfo() {
                start = span.Start.Index - leadingWhiteSpaceLength,
                length = span.Length + leadingWhiteSpaceLength,
                newText = newCode
            };
        }
Exemple #4
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;
        }
        private static void UpdateFromImport(
            PythonAst curAst,
            ITextBuffer buffer,
            FromImportStatement fromImport,
            string name
        ) {
            NameExpression[] names = new NameExpression[fromImport.Names.Count + 1];
            NameExpression[] asNames = fromImport.AsNames == null ? null : new NameExpression[fromImport.AsNames.Count + 1];
            NameExpression newName = new NameExpression(name);
            for (int i = 0; i < fromImport.Names.Count; i++) {
                names[i] = fromImport.Names[i];
            }
            names[fromImport.Names.Count] = newName;

            if (asNames != null) {
                for (int i = 0; i < fromImport.AsNames.Count; i++) {
                    asNames[i] = fromImport.AsNames[i];
                }
            }

            var newImport = new FromImportStatement((ModuleName)fromImport.Root, names, asNames, fromImport.IsFromFuture, fromImport.ForceAbsolute);
            curAst.CopyAttributes(fromImport, newImport);

            var newCode = newImport.ToCodeString(curAst);

            var span = fromImport.GetSpan(curAst);
            int leadingWhiteSpaceLength = (fromImport.GetLeadingWhiteSpace(curAst) ?? "").Length;
            using (var edit = buffer.CreateEdit()) {
                edit.Delete(span.Start.Index - leadingWhiteSpaceLength, span.Length + leadingWhiteSpaceLength);
                edit.Insert(span.Start.Index, newCode);
                edit.Apply();
            }
        }
Exemple #6
0
        /// <summary>
        /// Returns a new FromImport statement that is identical to this one but has
        /// removed the specified import statement.  Otherwise preserves any attributes
        /// for the statement.
        /// 
        /// New in 1.1.
        /// <param name="ast">The parent AST whose attributes should be updated for the new node.</param>
        /// <param name="index">The index in Names of the import to be removed.</param>
        /// </summary>
        public FromImportStatement RemoveImport(PythonAst ast, int index) {
            if (index < 0 || index >= _names.Length) {
                throw new ArgumentOutOfRangeException("index");
            }
            if (ast == null) {
                throw new ArgumentNullException("ast");
            }

            NameExpression[] names = new NameExpression[_names.Length - 1];
            NameExpression[] asNames = _asNames == null ? null : new NameExpression[_asNames.Length - 1];
            var asNameWhiteSpace = this.GetNamesWhiteSpace(ast);
            List<string> newAsNameWhiteSpace = 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 (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                    if (write > 0) {
                        if (includingCurrentName) {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        } else {
                            asIndex++;
                        }
                    } else if (i > 0) {
                        asIndex++;
                    }
                }

                if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                    if (includingCurrentName) {
                        if (newAsNameWhiteSpace.Count == 0) {
                            // no matter what we want the 1st entry to have the whitespace after the import keyword
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[0]);
                            asIndex++;
                        } else {
                            newAsNameWhiteSpace.Add(asNameWhiteSpace[asIndex++]);
                        }
                    } else {
                        asIndex++;
                    }
                }

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

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

                    write++;
                }

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

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

            if (asNameWhiteSpace != null && asIndex < asNameWhiteSpace.Length) {
                // trailing comma
                newAsNameWhiteSpace.Add(asNameWhiteSpace[asNameWhiteSpace.Length - 1]);
            }

            var res = new FromImportStatement(_root, names, asNames, IsFromFuture, ForceAbsolute);
            ast.CopyAttributes(this, res);
            ast.SetAttribute(res, NodeAttributes.NamesWhiteSpace, newAsNameWhiteSpace.ToArray());

            return res;
        }