ImportFromExistingAction doesn't require an editor

This commit is contained in:
Dmitry Jemerov
2010-06-24 13:30:42 +04:00
parent bf982a42e0
commit e8f44748bd
2 changed files with 19 additions and 20 deletions
@@ -1,13 +1,15 @@
package com.jetbrains.python.actions;
import com.intellij.codeInsight.hint.QuestionAction;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
import com.intellij.openapi.util.AsyncResult;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.ui.SimpleColoredComponent;
@@ -28,7 +30,6 @@ import java.util.List;
public class ImportFromExistingAction implements QuestionAction {
PyElement myTarget;
List<ImportCandidateHolder> mySources; // list of <import, imported_item>
Editor myEditor;
String myName;
boolean myUseQualifiedImport;
private Runnable myOnDoneCallback;
@@ -37,14 +38,13 @@ public class ImportFromExistingAction implements QuestionAction {
* @param target element to become qualified as imported.
* @param sources clauses of import to be used.
* @param name relevant name ot the target element (e.g. of identifier in an expression).
* @param editor target's editor.
* @param useQualified if True, use qualified "import modulename" instead of "from modulename import ...".
*/
public ImportFromExistingAction(@NotNull PyElement target, @NotNull List<ImportCandidateHolder> sources, String name, Editor editor, boolean useQualified) {
public ImportFromExistingAction(@NotNull PyElement target, @NotNull List<ImportCandidateHolder> sources, String name,
boolean useQualified) {
myTarget = target;
mySources = sources;
myName = name;
myEditor = editor;
myUseQualifiedImport = useQualified;
}
@@ -85,7 +85,7 @@ public class ImportFromExistingAction implements QuestionAction {
final JList list = new JList(items);
list.setCellRenderer(new CellRenderer(myName));
Runnable runnable = new Runnable() {
final Runnable runnable = new Runnable() {
public void run() {
int index = list.getSelectedIndex();
if (index < 0) return;
@@ -94,12 +94,16 @@ public class ImportFromExistingAction implements QuestionAction {
}
};
new PopupChooserBuilder(list).
setTitle(myUseQualifiedImport? PyBundle.message("ACT.qualify.with.module") : PyBundle.message("ACT.from.some.module.import")).
setItemChoosenCallback(runnable).
createPopup().
showInBestPositionFor(myEditor)
;
DataManager.getInstance().getDataContextFromFocus().doWhenDone(new AsyncResult.Handler<DataContext>() {
@Override
public void run(DataContext dataContext) {
new PopupChooserBuilder(list).
setTitle(myUseQualifiedImport? PyBundle.message("ACT.qualify.with.module") : PyBundle.message("ACT.from.some.module.import")).
setItemChoosenCallback(runnable).
createPopup().
showInBestPositionFor(dataContext);
}
});
}
private void doIt(final ImportCandidateHolder item) {
@@ -6,8 +6,6 @@ import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInspection.HintAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.DataConstants;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
@@ -108,7 +106,7 @@ public class ImportFromExistingFix implements HintAction, LocalQuickFix {
myImports.size() > 1,
ImportCandidateHolder.getQualifiedName(myName, myImports.get(0).getPath(), myImports.get(0).getImportElement())
);
final ImportFromExistingAction action = new ImportFromExistingAction(myNode, myImports, myName, editor, myUseQualifiedImport);
final ImportFromExistingAction action = new ImportFromExistingAction(myNode, myImports, myName, myUseQualifiedImport);
action.onDone(new Runnable() {
public void run() {
myExpended = true;
@@ -126,10 +124,7 @@ public class ImportFromExistingFix implements HintAction, LocalQuickFix {
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
Editor editor = (Editor)DataManager.getInstance().getDataContext().getData(DataConstants.EDITOR);
if (editor != null) {
invoke(project, editor, descriptor.getPsiElement().getContainingFile());
}
invoke(project, null, descriptor.getPsiElement().getContainingFile());
myExpended = true;
}
@@ -137,7 +132,7 @@ public class ImportFromExistingFix implements HintAction, LocalQuickFix {
// make sure file is committed, writable, etc
if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
// act
ImportFromExistingAction action = new ImportFromExistingAction(myNode, myImports, myName, editor, myUseQualifiedImport);
ImportFromExistingAction action = new ImportFromExistingAction(myNode, myImports, myName, myUseQualifiedImport);
action.execute(); // assume that action runs in WriteAction on its own behalf
myExpended = true;
}