private static void addEntityRefCompletions(PsiElement context, CompletionResultSet resultSet) {
    XmlFile containingFile = ObjectUtils.tryCast(context.getContainingFile(), XmlFile.class);
    if (containingFile == null) {
      return;
    }
    List<XmlFile> descriptorFiles = XmlExtension.getExtension(containingFile).getCharEntitiesDTDs(containingFile);
    final XmlTag tag = PsiTreeUtil.getParentOfType(context, XmlTag.class);
    if (tag != null && descriptorFiles.isEmpty()) {
      descriptorFiles = ContainerUtil.packNullables(findDescriptorFile(tag, containingFile));
    }

    final boolean acceptSystemEntities = containingFile.getFileType() == XmlFileType.INSTANCE;
    final PsiElementProcessor<PsiElement> processor = new PsiElementProcessor<>() {
      @Override
      public boolean execute(final @NotNull PsiElement element) {
        if (element instanceof XmlEntityDecl xmlEntityDecl) {
          if (xmlEntityDecl.isInternalReference() || acceptSystemEntities) {
            final LookupElementBuilder _item = buildEntityLookupItem(xmlEntityDecl);
            if (_item != null) {
              resultSet.addElement(_item);
              resultSet.stopHere();
            }
          }
        }
        return true;
      }
    };

    for (XmlFile descriptorFile: descriptorFiles) {
      XmlUtil.processXmlElements(descriptorFile, processor, true);
    }

    final XmlDocument document = containingFile.getDocument();
    if (acceptSystemEntities && document != null) {
      final XmlProlog element = document.getProlog();
      if (element != null) XmlUtil.processXmlElements(element, processor, true);
    }
  }
private static void addEntityRefCompletions(PsiElement context, CompletionResultSet resultSet)