/** * Add import directive corresponding to a type to file when it is needed. * * @param type type to import * @param file file where import directive should be added */ public static void addImportDirectivesIfNeeded(@NotNull JetType type, @NotNull JetFile file) { if (JetPluginUtil.checkTypeIsStandard(type, file.getProject()) || ErrorUtils.isErrorType(type)) { return; } BindingContext bindingContext = getContextForSingleFile(file); PsiElement element = BindingContextUtils.descriptorToDeclaration( bindingContext, type.getMemberScope().getContainingDeclaration()); if (element != null && element.getContainingFile() == file) { // declaration is in the same file, so no import is needed return; } for (ClassDescriptor clazz : TypeUtils.getAllClassDescriptors(type)) { addImportDirective(DescriptorUtils.getFQName(getTopLevelClass(clazz)).toSafe(), file); } }