/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ide.actions;

import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.ShowIntentionsPass;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class ApplyIntentionAction extends AnAction {

  private final HighlightInfo.IntentionActionDescriptor myDescriptor;
  private final Editor myEditor;
  private final PsiFile myFile;

  public ApplyIntentionAction(HighlightInfo.IntentionActionDescriptor descriptor, Editor editor, PsiFile file) {
    super(descriptor.getAction().getText());
    myDescriptor = descriptor;
    myEditor = editor;
    myFile = file;
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    final IntentionAction action = myDescriptor.getAction();
    ShowIntentionActionsHandler.chooseActionAndInvoke(myFile, myEditor, action, action.getText());
  }

  public String getName() {
    return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
      @Override
      public String compute() {
        return myDescriptor.getAction().getText();
      }
    });
  }

  @Nullable
  public static ApplyIntentionAction[] getAvailableIntentions(final Editor editor, final PsiFile file) {
    final ShowIntentionsPass.IntentionsInfo info = new ShowIntentionsPass.IntentionsInfo();
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      @Override
      public void run() {
        ShowIntentionsPass.getActionsToShow(editor, file, info, -1);
      }
    });
    if (info.isEmpty()) return null;

    final List<HighlightInfo.IntentionActionDescriptor> actions = new ArrayList<HighlightInfo.IntentionActionDescriptor>();
    actions.addAll(info.errorFixesToShow);
    actions.addAll(info.inspectionFixesToShow);
    actions.addAll(info.intentionsToShow);

    final ApplyIntentionAction[] result = new ApplyIntentionAction[actions.size()];
    for (int i = 0; i < result.length; i++) {
      result[i] = new ApplyIntentionAction(actions.get(i), editor, file);
    }
    return result;
  }
}

@@ -1491,16 +1491,22 @@
 nAction(
+final 
 Highligh
@@ -1548,16 +1548,29 @@
 criptor,
+ String text,
  Editor 
@@ -1607,40 +1607,12 @@
 per(
-descriptor.getAction().getT
+t
 ext
-()
 );%0A 
@@ -2980,24 +2980,337 @@
 gth; i++) %7B%0A
+      final HighlightInfo.IntentionActionDescriptor descriptor = actions.get(i);%0A      final String actionText = ApplicationManager.getApplication().runReadAction(new Computable%3CString%3E() %7B%0A        @Override%0A        public String compute() %7B%0A          return descriptor.getAction().getText();%0A        %7D%0A      %7D);%0A
       result
@@ -3340,30 +3340,38 @@
 nAction(
-actions.get(i)
+descriptor, actionText
 , editor

read access