ASTNode (ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { boolean addingNewName = first == last && first.getElementType() == PyElementTypes.IMPORT_ELEMENT && (anchor == null || anchor.getElementType() == PyElementTypes.IMPORT_ELEMENT); if (!addingNewName) { return super.addInternal(first, last, anchor, before); } if (anchor == null) { final PyImportElement[] elements = getImportElements(); if (elements.length != 0) { if (before) { anchor = elements[elements.length - 1].getNode(); before = false; } else { anchor = elements[0].getNode(); before = true; } } } // In an incomplete from import statement there is a special sentinel empty PyImportElement at the end if (anchor != null && anchor.getTextLength() == 0) { getNode().replaceChild(anchor, first); return first; } else { final ASTNode result = super.addInternal(first, last, anchor, before); if (anchor != null && anchor.getElementType() == PyElementTypes.IMPORT_ELEMENT && result.getElementType() == PyElementTypes.IMPORT_ELEMENT) { ASTNode comma = PyElementGenerator.getInstance(getProject()).createComma(); super.addInternal(comma, comma, before ? result : anchor, false); } return result; } }