diff --git a/python/src/com/jetbrains/python/actions/ImportFromExistingAction.java b/python/src/com/jetbrains/python/actions/ImportFromExistingAction.java index 6ac65f4abe00..40a7be92e485 100644 --- a/python/src/com/jetbrains/python/actions/ImportFromExistingAction.java +++ b/python/src/com/jetbrains/python/actions/ImportFromExistingAction.java @@ -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 mySources; // list of - 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 sources, String name, Editor editor, boolean useQualified) { + public ImportFromExistingAction(@NotNull PyElement target, @NotNull List 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() { + @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) { diff --git a/python/src/com/jetbrains/python/actions/ImportFromExistingFix.java b/python/src/com/jetbrains/python/actions/ImportFromExistingFix.java index 04ffe2f53c04..9553cf3aa8a4 100644 --- a/python/src/com/jetbrains/python/actions/ImportFromExistingFix.java +++ b/python/src/com/jetbrains/python/actions/ImportFromExistingFix.java @@ -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; }