public static boolean insertImport(@NotNull PsiElement anchor,
                                     @NotNull PsiNamedElement element,
                                     @Nullable String asName,
                                     boolean preferFromImport) {
    if (PyBuiltinCache.getInstance(element).isBuiltin(element)) return false;
    final PsiFileSystemItem elementSource = element instanceof PsiDirectory ? (PsiFileSystemItem)element : element.getContainingFile();
    final PsiFile file = anchor.getContainingFile();
    if (elementSource == file || elementSource == file.getOriginalFile()) return false;
    final QualifiedName qname = QualifiedNameFinder.findCanonicalImportPath(element, anchor);
    if (qname == null || !isValidQualifiedName(qname)) {
      return false;
    }
    final QualifiedName containingQName;
    final String importedName;
    final boolean importingModuleOrPackage = element instanceof PyFile || element instanceof PsiDirectory;
    if (importingModuleOrPackage) {
      containingQName = qname.removeLastComponent();
      importedName = qname.getLastComponent();
    }
    // See PyClassRefactoringUtil.DynamicNamedElement
    else if (PyUtil.isTopLevel(element) || element instanceof LightElement) {
      containingQName = qname;
      importedName = getOriginalName(element);
    }
    else {
      return false;
    }
    final AddImportHelper.ImportPriority priority = AddImportHelper.getImportPriority(anchor, elementSource);
    if (preferFromImport && !containingQName.getComponents().isEmpty() || !importingModuleOrPackage) {
      return AddImportHelper.addOrUpdateFromImportStatement(file, containingQName.toString(), importedName, asName, priority, anchor);
    }
    else {
      return AddImportHelper.addImportStatement(file, containingQName.append(importedName).toString(), asName, priority, anchor);
    }
  }
public static boolean insertImport(@NotNull PsiElement anchor,
                                     @NotNull PsiNamedElement element,
                                     @Nullable String asName,
                                     boolean preferFromImport)