Search in sources :

Example 1 with PyImportStatementBase

use of com.jetbrains.python.psi.PyImportStatementBase in project intellij-community by JetBrains.

the class PyAddImportFix method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PyElementGenerator generator = PyElementGenerator.getInstance(project);
    final PyImportStatementBase statement = generator.createFromText(LanguageLevel.forElement(myFile), PyImportStatementBase.class, myImportToAdd);
    final PsiElement recommendedPosition = AddImportHelper.getFileInsertPosition(myFile);
    myFile.addAfter(statement, recommendedPosition);
}
Also used : PyImportStatementBase(com.jetbrains.python.psi.PyImportStatementBase) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyImportStatementBase

use of com.jetbrains.python.psi.PyImportStatementBase in project intellij-community by JetBrains.

the class PyOptimizeImportsTest method testExtractImportBlockNoWhitespaceAtEnd.

public void testExtractImportBlockNoWhitespaceAtEnd() {
    myFixture.configureByFile(getTestName(true) + ".py");
    final PyFileImpl file = assertInstanceOf(myFixture.getFile(), PyFileImpl.class);
    final List<PyImportStatementBase> block = file.getImportBlock();
    assertSize(2, block);
}
Also used : PyImportStatementBase(com.jetbrains.python.psi.PyImportStatementBase) PyFileImpl(com.jetbrains.python.psi.impl.PyFileImpl)

Example 3 with PyImportStatementBase

use of com.jetbrains.python.psi.PyImportStatementBase in project intellij-community by JetBrains.

the class PyOptimizeImportsTest method testExtractImportBlockWithIntermediateComments.

public void testExtractImportBlockWithIntermediateComments() {
    myFixture.configureByFile(getTestName(true) + ".py");
    final PyFileImpl file = assertInstanceOf(myFixture.getFile(), PyFileImpl.class);
    final List<PyImportStatementBase> block = file.getImportBlock();
    assertSize(2, block);
}
Also used : PyImportStatementBase(com.jetbrains.python.psi.PyImportStatementBase) PyFileImpl(com.jetbrains.python.psi.impl.PyFileImpl)

Example 4 with PyImportStatementBase

use of com.jetbrains.python.psi.PyImportStatementBase in project intellij-community by JetBrains.

the class PyImportFilteringRule method isVisible.

@Override
public boolean isVisible(@NotNull Usage usage) {
    if (usage instanceof PsiElementUsage) {
        final PsiElement psiElement = ((PsiElementUsage) usage).getElement();
        final PsiFile containingFile = psiElement.getContainingFile();
        if (containingFile instanceof PyFile) {
            // check whether the element is in the import list
            final PyImportStatementBase importStatement = PsiTreeUtil.getParentOfType(psiElement, PyImportStatementBase.class, true);
            return importStatement == null;
        }
    }
    return true;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PyImportStatementBase(com.jetbrains.python.psi.PyImportStatementBase) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement) PsiElementUsage(com.intellij.usages.rules.PsiElementUsage)

Aggregations

PyImportStatementBase (com.jetbrains.python.psi.PyImportStatementBase)4 PsiElement (com.intellij.psi.PsiElement)2 PyFileImpl (com.jetbrains.python.psi.impl.PyFileImpl)2 PsiFile (com.intellij.psi.PsiFile)1 PsiElementUsage (com.intellij.usages.rules.PsiElementUsage)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyFile (com.jetbrains.python.psi.PyFile)1