ImportWeight (@NotNull final PsiElement element, @NotNull final ProximityLocation location) { final PsiElement position = location.getPosition(); if (position == null){ return ImportWeight.UNKNOWN; } PsiUtilCore.ensureValid(position); final PsiFile elementFile = element.getContainingFile(); final PsiFile positionFile = position.getContainingFile(); if (positionFile != null && elementFile != null && positionFile.getOriginalFile().equals(elementFile.getOriginalFile())) { return ImportWeight.DECLARED_IN_SAME_FILE; } if (element instanceof PsiClass psiClass) { final String qname = psiClass.getQualifiedName(); if (qname != null) { boolean topLevel = psiClass.getContainingClass() == null; List<String> importedNames = PLACE_IMPORTED_NAMES.getValue(location); if (importedNames.contains(qname)) return ImportWeight.CLASS_IMPORTED; String packageName = StringUtil.getPackageName(qname); if ("java.lang".equals(packageName)) return ImportWeight.CLASS_JAVA_LANG; if (importedNames.contains(packageName)) { if (topLevel) { // The whole package is imported on demand return ImportWeight.CLASS_ON_DEMAND_TOP_LEVEL; } // Nested class which is eligible for on demand import (import static pkg.TopClass.*) // Rank higher than non-imported classes but still lower than top-level imported classes. return ImportWeight.CLASS_ON_DEMAND_NESTED; } final PsiPackage placePackage = PLACE_PACKAGE.getValue(location); if (placePackage != null) { Module elementModule = ModuleUtilCore.findModuleForPsiElement(element); if (location.getPositionModule() == elementModule && placePackage.equals(getContextPackage(element))) { return topLevel ? ImportWeight.CLASS_DECLARED_IN_SAME_PACKAGE_TOP_LEVEL : ImportWeight.CLASS_DECLARED_IN_SAME_PACKAGE_NESTED; } } // check if anything from the same package is already imported in the file: // people are likely to refer to the same subsystem as they're already working if (containsImport(importedNames, packageName)) return ImportWeight.CLASS_HAS_SAME_PACKAGE_IMPORT; } return ImportWeight.UNKNOWN; } if (element instanceof PsiMember) { String qname = PsiUtil.getMemberQualifiedName((PsiMember)element); if (qname != null && PLACE_IMPORTED_NAMES.getValue(location).contains(qname)) { return ImportWeight.MEMBER_IMPORTED; } final PsiPackage placePackage = PLACE_PACKAGE.getValue(location); if (placePackage != null) { Module elementModule = ModuleUtilCore.findModuleForPsiElement(element); if (location.getPositionModule() == elementModule && placePackage.equals(getContextPackage(element))) { return ImportWeight.MEMBER_SAME_PACKAGE; } } } return ImportWeight.UNKNOWN; }