private static PsiPackage findLongestExistingPackage(@NotNull Project project, @NotNull String packageName) {
    String nameToMatch = packageName;
    while (true) {
      PsiPackage aPackage = JavaPsiFacade.getInstance(PsiManager.getInstance(project).getProject()).findPackage(nameToMatch);
      if (aPackage != null && isWritablePackage(aPackage)) return aPackage;
      int lastDotIndex = nameToMatch.lastIndexOf('.');
      if (lastDotIndex >= 0) {
        nameToMatch = nameToMatch.substring(0, lastDotIndex);
      }
      else {
        return null;
      }
    }
  }
private static PsiPackage findLongestExistingPackage(@NotNull Project project, @NotNull String packageName)