Object[] () { int relativeLevel = -1; InsertHandler<LookupElement> insertHandler = null; // NOTE: could use getPointInImport() // are we in "import _" or "from foo import _"? PyFromImportStatement fromImport = PsiTreeUtil.getParentOfType(myElement, PyFromImportStatement.class); if (fromImport != null && myElement.getParent() != fromImport) { // in "from foo import _" PyReferenceExpression src = fromImport.getImportSource(); if (src != null) { ResolveResult[] resolved = src.getReference().multiResolve(false); for (ResolveResult result : resolved) { PsiElement modCandidate = result.getElement(); if (modCandidate instanceof PyExpression module) { addImportedNames(fromImport.getImportElements()); // don't propose already imported items // try to collect submodules PyType qualifierType = myContext.getType(module); if (qualifierType != null) { ProcessingContext ctx = new ProcessingContext(); ctx.put(PyType.CTX_NAMES, myNamesAlready); Collections.addAll(myObjects, qualifierType.getCompletionVariants(myElement.getName(), myElement, ctx)); } } else if (modCandidate instanceof PsiDirectory) { fillFromDir((PsiDirectory)modCandidate, ImportKeywordHandler.INSTANCE); } } if (!myObjects.isEmpty()) { return myObjects.toArray(); } } else { // null source, must be a "from ... import" relativeLevel = fromImport.getRelativeLevel(); if (relativeLevel > 0) { PsiDirectory relativeDir = ResolveImportUtil.stepBackFrom(myCurrentFile, relativeLevel); if (relativeDir != null) { addImportedNames(fromImport.getImportElements()); fillFromDir(relativeDir, null); } } } fillFromRootsIfNotRelative(relativeLevel, insertHandler); } else { // in "import _" or "from _ import" PsiElement prevElem = PyPsiUtils.getPrevNonWhitespaceSibling(myElement); while (prevElem != null && prevElem.getNode().getElementType() == PyTokenTypes.DOT) { relativeLevel += 1; prevElem = PyPsiUtils.getPrevNonWhitespaceSibling(prevElem); } if (fromImport != null) { addImportedNames(fromImport.getImportElements()); if (!alreadyHasImportKeyword()) { insertHandler = ImportKeywordHandler.INSTANCE; } } else { myNamesAlready.add(PyNames.FUTURE_MODULE); // never add it to "import ..." PyImportStatement importStatement = PsiTreeUtil.getParentOfType(myElement, PyImportStatement.class); if (importStatement != null) { addImportedNames(importStatement.getImportElements()); } } final PsiDirectory containingDirectory = myCurrentFile.getContainingDirectory(); // look at dir by level if (LanguageLevel.forElement(myCurrentFile).isPy3K() && containingDirectory != null && PyUtil.isExplicitPackage(containingDirectory)) { fillFromRootsIfNotRelative(relativeLevel, insertHandler); fillFromSameDirectoryOrRelative(relativeLevel, insertHandler); } else { fillFromSameDirectoryOrRelative(relativeLevel, insertHandler); fillFromRootsIfNotRelative(relativeLevel, insertHandler); } } return ArrayUtil.toObjectArray(myObjects); }