Search in sources :

Example 1 with AutoImportQuickFix

use of com.jetbrains.python.codeInsight.imports.AutoImportQuickFix in project intellij by bazelbuild.

the class BazelPyImportResolverStrategyTest method testImportQuickFix.

@Test
public void testImportQuickFix() {
    MockBlazeProjectDataBuilder builder = MockBlazeProjectDataBuilder.builder(workspaceRoot);
    builder.setTargetMap(TargetMapBuilder.builder().addTarget(TargetIdeInfo.builder().setLabel("//foo:foo").setBuildFile(source("foo/BUILD")).setKind("py_library").addSource(source("foo/lib/bar.py"))).build());
    registerProjectService(BlazeProjectDataManager.class, new MockBlazeProjectDataManager(builder.build()));
    workspace.createPsiFile(WorkspacePath.createIfValid("foo/lib/bar.py"));
    PsiFile source = workspace.createPsiFile(WorkspacePath.createIfValid("baz/source.py"), "bar");
    PyReferenceExpression ref = PsiUtils.findFirstChildOfClassRecursive(source, PyReferenceExpression.class);
    assertThat(ref).isNotNull();
    AutoImportQuickFix quickFix = getImportQuickFix(ref);
    assertThat(quickFix.isAvailable()).isTrue();
    assertThat(quickFix.getText()).isEqualTo("Import 'foo.lib.bar'");
    quickFix.applyFix();
    assertThat(source.getText()).isEqualTo(Joiner.on('\n').join("from foo.lib import bar", "", "bar"));
}
Also used : MockBlazeProjectDataBuilder(com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder) PsiFile(com.intellij.psi.PsiFile) MockBlazeProjectDataManager(com.google.idea.blaze.base.model.MockBlazeProjectDataManager) PyReferenceExpression(com.jetbrains.python.psi.PyReferenceExpression) AutoImportQuickFix(com.jetbrains.python.codeInsight.imports.AutoImportQuickFix) Test(org.junit.Test)

Example 2 with AutoImportQuickFix

use of com.jetbrains.python.codeInsight.imports.AutoImportQuickFix in project intellij by bazelbuild.

the class PyImportResolverStrategyTestCase method getImportQuickFix.

static AutoImportQuickFix getImportQuickFix(PyElement unresolvedSymbol) {
    PsiReference ref = unresolvedSymbol.getReference();
    assertThat(ref).isNotNull();
    String text = ref.getElement().getText();
    AutoImportQuickFix fix = new AutoImportQuickFix(unresolvedSymbol, ref.getClass(), text, false);
    for (PyImportCandidateProvider provider : Extensions.getExtensions(PyImportCandidateProvider.EP_NAME)) {
        provider.addImportCandidates(ref, text, fix);
    }
    fix.sortCandidates();
    return fix;
}
Also used : PsiReference(com.intellij.psi.PsiReference) PyImportCandidateProvider(com.jetbrains.python.codeInsight.imports.PyImportCandidateProvider) AutoImportQuickFix(com.jetbrains.python.codeInsight.imports.AutoImportQuickFix)

Example 3 with AutoImportQuickFix

use of com.jetbrains.python.codeInsight.imports.AutoImportQuickFix in project intellij-community by JetBrains.

the class AddImportQuickFixTest method doMultiFileAutoImportTest.

private void doMultiFileAutoImportTest(@NotNull String hintPrefix, @Nullable Processor<AutoImportQuickFix> checkQuickfix) {
    myFixture.copyDirectoryToProject(getTestName(true), "");
    myFixture.enableInspections(PyUnresolvedReferencesInspection.class);
    final String entryPoint = "main";
    myFixture.configureByFile(entryPoint + ".py");
    myFixture.checkHighlighting(true, false, false);
    final List<IntentionAction> intentions = myFixture.filterAvailableIntentions(hintPrefix);
    final IntentionAction intention = ContainerUtil.find(intentions, action -> {
        return action instanceof QuickFixWrapper && ((QuickFixWrapper) action).getFix() instanceof AutoImportQuickFix;
    });
    assertNotNull("Auto import quick fix starting with '" + hintPrefix + "' wasn't found", intention);
    final AutoImportQuickFix quickfix = (AutoImportQuickFix) ((QuickFixWrapper) intention).getFix();
    boolean applyFix = true;
    if (checkQuickfix != null) {
        applyFix = checkQuickfix.process(quickfix);
    }
    if (applyFix) {
        myFixture.launchAction(intention);
        myFixture.checkResultByFile(getTestName(true) + "/" + entryPoint + "_after.py", true);
    }
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) QuickFixWrapper(com.intellij.codeInspection.ex.QuickFixWrapper) AutoImportQuickFix(com.jetbrains.python.codeInsight.imports.AutoImportQuickFix)

Aggregations

AutoImportQuickFix (com.jetbrains.python.codeInsight.imports.AutoImportQuickFix)3 MockBlazeProjectDataBuilder (com.google.idea.blaze.base.model.MockBlazeProjectDataBuilder)1 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 QuickFixWrapper (com.intellij.codeInspection.ex.QuickFixWrapper)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 PyImportCandidateProvider (com.jetbrains.python.codeInsight.imports.PyImportCandidateProvider)1 PyReferenceExpression (com.jetbrains.python.psi.PyReferenceExpression)1 Test (org.junit.Test)1