// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.actions;

import com.intellij.ide.IdeBundle;
import com.intellij.ide.actions.searcheverywhere.ClassSearchEverywhereContributor;
import com.intellij.navigation.ChooseByNameRegistry;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.playback.commands.ActionCommand;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.awt.event.InputEvent;

public class GotoClassAction extends SearchEverywhereBaseAction implements DumbAware {

  public GotoClassAction() {
    //we need to change the template presentation to show the proper text for the action in Settings | Keymap
    Presentation presentation = getTemplatePresentation();
    presentation.setText(() -> IdeBundle.message("go.to.class.title.prefix", GotoClassPresentationUpdater.getActionTitle() + "..."));
    presentation.setDescription(() -> IdeBundle.message("go.to.class.action.description",
                                                        StringUtil.join(GotoClassPresentationUpdater.getElementKinds(), "/")));
    addTextOverride(ActionPlaces.MAIN_MENU, () -> GotoClassPresentationUpdater.getActionTitle() + "...");
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) return;

    boolean dumb = DumbService.isDumb(project);
    if (!dumb){
      showInSearchEverywherePopup(ClassSearchEverywhereContributor.class.getSimpleName(), e, true, true);
      return;
    }

    ClassSearchEverywhereContributor contributor = new ClassSearchEverywhereContributor(e);
    try {
      if (contributor.isDumbAware()) {
        showInSearchEverywherePopup(ClassSearchEverywhereContributor.class.getSimpleName(), e, true, true);
      }
      else {
        invokeGoToFile(project, e);
      }
    }
    finally {
      Disposer.dispose(contributor);
    }
  }

  static void invokeGoToFile(@NotNull Project project, @NotNull AnActionEvent e) {
    String actionTitle = StringUtil.trimEnd(ObjectUtils.notNull(
      e.getPresentation().getText(), GotoClassPresentationUpdater.getActionTitle()), "...");
    String message = IdeBundle.message("go.to.class.dumb.mode.message", actionTitle);
    DumbService.getInstance(project).showDumbModeNotification(message);
    AnAction action = ActionManager.getInstance().getAction(GotoFileAction.ID);
    InputEvent event = ActionCommand.getInputEvent(GotoFileAction.ID);
    Component component = e.getData(PlatformDataKeys.CONTEXT_COMPONENT);
    ActionManager.getInstance().tryToExecute(action, event, component, e.getPlace(), true);
  }

  @Override
  protected boolean hasContributors(@NotNull DataContext dataContext) {
    return ChooseByNameRegistry.getInstance().getClassModelContributors().length > 0;
  }
}

@@ -1769,17 +1769,47 @@
 f (!dumb
-)
+ %7C%7C isContributorDumbAware(e)) 
 %7B%0A      
@@ -1916,24 +1916,61 @@
     
-  return
+%7D%0A    else %7B%0A      invokeGoToFile(project, e)
 ;%0A    %7D%0A
 %0A   
@@ -1969,63 +1969,83 @@
   %7D%0A
-%0A
   
-  ClassSearchEverywhereContributor contributor = new
+%7D%0A%0A  private static boolean isContributorDumbAware(AnActionEvent e) %7B%0A   
  Cla
@@ -2073,19 +2073,35 @@
 tributor
-(e)
+ contributor = null
 ;%0A    tr
@@ -2110,20 +2110,16 @@
 %7B%0A      
-if (
 contribu
@@ -2125,198 +2125,91 @@
 utor
-.isDumbAware()) %7B%0A        showInSearchEverywherePopup(ClassSearchEverywhereContributor.class.getSimpleName(), e, true, true);%0A      %7D%0A      else %7B%0A        invokeGoToFile(project, e);%0A      %7D
+ = new ClassSearchEverywhereContributor(e);%0A      return contributor.isDumbAware();
 %0A   
@@ -2230,16 +2230,41 @@
  %7B%0A     
+ if (contributor != null)
  Dispose

fix leak in ClassSearchEverywhereContributor