}
/**
- * Adds a new {@link PyFromImportStatement} statement below other top-level imports or as specified by anchor.
+ * Adds a new {@link PyFromImportStatement} statement within other top-level imports or as specified by anchor.
*
* @param file where to operate
* @param from import source (reference after {@code from} keyword)
@Nullable PsiElement anchor) {
final PyElementGenerator generator = PyElementGenerator.getInstance(file.getProject());
final LanguageLevel languageLevel = LanguageLevel.forElement(file);
- final PyFromImportStatement nodeToInsert = generator.createFromImportStatement(languageLevel, from, name, asName);
+ final PyFromImportStatement newImport = generator.createFromImportStatement(languageLevel, from, name, asName);
+ addFromImportStatement(file, newImport, priority, anchor);
+ }
+
+ /**
+ * Adds a new {@link PyFromImportStatement} statement within other top-level imports or as specified by anchor.
+ *
+ * @param file where to operate
+ * @param newImport new "from import" statement to insert. It may be generated, because it won't be used for resolving anyway.
+ * You might want to use overloaded version of this method to generate such statement automatically.
+ * @param anchor place where the imported name was used. It will be used to determine proper block where new import should be inserted,
+ * e.g. inside conditional block or try/except statement. Also if anchor is another import statement, new import statement
+ * will be inserted right after it.
+ * @see #addFromImportStatement(PsiFile, String, String, String, ImportPriority, PsiElement)
+ * @see #addFromImportStatement
+ */
+ public static void addFromImportStatement(@NotNull PsiFile file,
+ @NotNull PyFromImportStatement newImport,
+ @Nullable ImportPriority priority,
+ @Nullable PsiElement anchor) {
try {
- final PyImportStatementBase importStatement = PsiTreeUtil.getParentOfType(anchor, PyImportStatementBase.class, false);
+ final PyImportStatementBase parentImport = PsiTreeUtil.getParentOfType(anchor, PyImportStatementBase.class, false);
final PsiElement insertParent;
- if (importStatement != null && importStatement.getContainingFile() == file) {
- insertParent = importStatement.getParent();
+ if (parentImport != null && parentImport.getContainingFile() == file) {
+ insertParent = parentImport.getParent();
}
else {
insertParent = file;
}
if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) {
- final PsiElement element = insertParent.addBefore(nodeToInsert, getInsertPosition(insertParent, nodeToInsert, priority));
+ final PsiElement element = insertParent.addBefore(newImport, getInsertPosition(insertParent, newImport, priority));
PsiElement whitespace = element.getNextSibling();
if (!(whitespace instanceof PsiWhiteSpace)) {
whitespace = PsiParserFacade.SERVICE.getInstance(file.getProject()).createWhiteSpaceFromText(" >>> ");
}
else {
if (anchor instanceof PyImportStatementBase) {
- insertParent.addAfter(nodeToInsert, anchor);
+ insertParent.addAfter(newImport, anchor);
}
else {
- insertParent.addBefore(nodeToInsert, getInsertPosition(insertParent, nodeToInsert, priority));
+ insertParent.addBefore(newImport, getInsertPosition(insertParent, newImport, priority));
}
}
}
import com.intellij.psi.PsiFile;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.codeInsight.imports.AddImportHelper;
-import com.jetbrains.python.documentation.docstrings.DocStringUtil;
-import com.jetbrains.python.formatter.PyBlock;
import com.jetbrains.python.psi.PyFile;
-import com.jetbrains.python.psi.PyStringLiteralExpression;
+import com.jetbrains.python.psi.PyFromImportStatement;
import org.jetbrains.annotations.NotNull;
/**
* @author Alexey.Ivanov
*/
public class MoveFromFutureImportQuickFix implements LocalQuickFix {
+ @Override
@NotNull
public String getName() {
return PyBundle.message("QFIX.move.from.future.import");
}
+ @Override
@NotNull
public String getFamilyName() {
return getName();
}
+ @Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
- PsiElement problemElement = descriptor.getPsiElement();
- PsiFile psiFile = problemElement.getContainingFile();
+ final PsiElement problemElement = descriptor.getPsiElement();
+ final PsiFile psiFile = problemElement.getContainingFile();
if (psiFile instanceof PyFile) {
- problemElement.putCopyableUserData(PyBlock.IMPORT_GROUP_BEGIN, true);
- PyFile file = (PyFile)psiFile;
- PyStringLiteralExpression docString = DocStringUtil.findDocStringExpression(file);
- if (docString != null) {
- file.addAfter(problemElement, docString.getParent() /* PyExpressionStatement */);
- }
- else {
- file.addBefore(problemElement, file.getStatements().get(0));
- }
+ AddImportHelper.addFromImportStatement(psiFile, (PyFromImportStatement)problemElement, AddImportHelper.ImportPriority.FUTURE, null);
problemElement.delete();
}
}