private Collection<JSQualifiedNamedElement> getCandidates(Editor editor, PsiFile file) {
    final Collection<JSQualifiedNamedElement> candidates;

    if (myReference instanceof JSReferenceExpression
        && ((JSReferenceExpression) myReference).getQualifier() == null) {
      Collection<JSQualifiedNamedElement> c =
          getCandidates(editor, file, myReference.getCanonicalText());
      filterCandidates(c);
      candidates = new THashSet<>(c, JSPsiImplUtils.QUALIFIED_NAME_HASHING_STRATEGY);
    } else {
      JSQualifiedNamedElement invalidResult = null;

      for (ResolveResult r : myReference.multiResolve(false)) {
        PsiElement element = r.getElement();
        if (element instanceof JSQualifiedNamedElement) {
          invalidResult = (JSQualifiedNamedElement) element;
        }
      }
      if (invalidResult != null) {
        if (myReference.getElement().getParent() instanceof JSNewExpression
            && invalidResult instanceof JSFunction
            && ((JSFunction) invalidResult).isConstructor()) {
          invalidResult = (JSClass) invalidResult.getParent();
        }
        candidates = new SmartList<>();
        candidates.add(invalidResult);
      } else {
        candidates = Collections.emptyList();
      }
    }
    return candidates;
  }
  public boolean isAvailable(
      @NotNull final Project project, final Editor editor, final PsiFile file) {
    if (!myReference.getElement().isValid()) return false;
    final long modL =
        myReference.getElement().getManager().getModificationTracker().getModificationCount();

    if (!isAvailableCalculated || modL != modificationCount) {
      final ResolveResult[] results = myReference.multiResolve(false);
      boolean hasValidResult = false;

      for (ResolveResult r : results) {
        if (r.isValidResult()) {
          hasValidResult = true;
          break;
        }
      }

      if (!hasValidResult) {
        final Collection<JSQualifiedNamedElement> candidates = getCandidates(editor, file);

        isAvailableCalculated = true;
        isAvailable = candidates.size() > 0;
        String text;

        if (isAvailable) {
          final JSQualifiedNamedElement element = candidates.iterator().next();
          text = element.getQualifiedName() + "?";
          if (candidates.size() > 1) text += " (multiple choices...)";
          if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
            text += " Alt+Enter";
          }
        } else {
          text = "";
        }
        myText = text;
      } else {
        isAvailableCalculated = true;
        isAvailable = false;
        myText = "";
      }

      modificationCount = modL;
    }

    return isAvailable;
  }
 public boolean showHint(@NotNull final Editor editor) {
   myEditor = editor;
   final PsiElement element = myReference.getElement();
   TextRange range =
       InjectedLanguageManager.getInstance(element.getProject())
           .injectedToHost(element, element.getTextRange());
   HintManager.getInstance()
       .showQuestionHint(editor, getText(), range.getStartOffset(), range.getEndOffset(), this);
   return true;
 }
 private void doImport(final String qName) {
   ApplicationManager.getApplication()
       .runWriteAction(
           () -> {
             final PsiElement element = myReference.getElement();
             SmartPsiElementPointer<PsiElement> pointer =
                 SmartPointerManager.getInstance(element.getProject())
                     .createSmartPsiElementPointer(element);
             ImportUtils.doImport(element, qName, true);
             PsiElement newElement = pointer.getElement();
             if (newElement != null) {
               ImportUtils.insertUseNamespaceIfNeeded(qName, newElement);
             }
           });
 }
  public boolean execute() {
    final PsiFile containingFile = myReference.getElement().getContainingFile();
    invoke(containingFile.getProject(), myEditor, containingFile);

    return true;
  }