private static @NotNull GlobalSearchScope getReferenceScope(@NotNull CompletionParameters parameters,
                                                              boolean filterByScope,
                                                              boolean inPermitsList) {
    PsiElement insertedElement = parameters.getPosition();
    PsiFile psiFile = insertedElement.getContainingFile().getOriginalFile();
    Project project = insertedElement.getProject();
    if (!inPermitsList) {
      return filterByScope ? psiFile.getResolveScope() : GlobalSearchScope.allScope(project);
    }
    if (parameters.getInvocationCount() >= 2) {
      return GlobalSearchScope.allScope(project);
    }
    PsiJavaModule javaModule = JavaModuleGraphUtil.findDescriptorByElement(psiFile.getOriginalElement());
    JavaModuleScope moduleScope = javaModule == null ? null : JavaModuleScope.moduleScope(javaModule);
    if (moduleScope != null) {
      return moduleScope;
    }
    PsiDirectory dir = psiFile.getParent();
    PsiPackage psiPackage = dir == null ? null : JavaDirectoryService.getInstance().getPackage(dir);
    if (psiPackage != null) {
      return GlobalSearchScope.filesScope(project, ContainerUtil.map(psiPackage.getFiles(GlobalSearchScope.allScope(project)),
                                                                     PsiFile::getVirtualFile));
    }
    return GlobalSearchScope.fileScope(psiFile);
  }
private static @NotNull GlobalSearchScope getReferenceScope(@NotNull CompletionParameters parameters,
                                                              boolean filterByScope,
                                                              boolean inPermitsList)