PsiFileSystemItem (@NotNull PyFile context, @NotNull String moduleLocation, @Nullable PyReferenceExpression referenceExpr, int relativeLevel) { String relativeImportBasePath = VirtualFileManager.extractPath(moduleLocation); for (int level = 0; level < relativeLevel; level++) { relativeImportBasePath = PathUtil.getParentPath(relativeImportBasePath); } if (!relativeImportBasePath.isEmpty()) { final String protocol = VirtualFileManager.extractProtocol(moduleLocation); assert protocol != null : "Original location: " + moduleLocation; final String relativeImportBaseUrl = VirtualFileManager.constructUrl(protocol, relativeImportBasePath); VirtualFile relativeImportBaseDir = VirtualFileManager.getInstance().findFileByUrl(relativeImportBaseUrl); VirtualFile sourceFile = relativeImportBaseDir; if (relativeImportBaseDir != null && relativeImportBaseDir.isDirectory() && referenceExpr != null) { final QualifiedName qualifiedName = referenceExpr.asQualifiedName(); if (qualifiedName == null) { return null; } final String relativePath = qualifiedName.join("/"); sourceFile = relativeImportBaseDir.findFileByRelativePath(relativePath); if (sourceFile == null) { sourceFile = relativeImportBaseDir.findFileByRelativePath(relativePath + PyNames.DOT_PY); } } if (sourceFile != null) { final PsiFileSystemItem sourceElement; if (sourceFile.isDirectory()) { sourceElement = context.getManager().findDirectory(sourceFile); } else { sourceElement = context.getManager().findFile(sourceFile); } return sourceElement; } } return null; }