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"));
}
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;
}
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);
}
}
Aggregations