@Nullable private static PsiClass findTargetClass(@NotNull Editor editor, @NotNull PsiFile file) { int offset = editor.getCaretModel().getOffset(); PsiElement element = file.findElementAt(offset); return PsiTreeUtil.getParentOfType(element, PsiClass.class, false) == null ? null : TestIntegrationUtils.findOuterClass(element); }
@Override protected boolean isValidForClass(PsiClass targetClass) { List<TestFramework> frameworks = TestIntegrationUtils.findSuitableFrameworks(targetClass); if (frameworks.isEmpty()) return false; for (TestFramework each : frameworks) { if (isValidFor(targetClass, each)) return true; } return false; }
@Nullable private static PsiMethod generateDummyMethod(Editor editor, PsiFile file) throws IncorrectOperationException { final PsiMethod method = TestIntegrationUtils.createDummyMethod(file); final PsiGenerationInfo<PsiMethod> info = OverrideImplementUtil.createGenerationInfo(method); int offset = findOffsetToInsertMethodTo(editor, file); GenerateMembersUtil.insertMembersAtOffset(file, offset, Collections.singletonList(info)); final PsiMethod member = info.getPsiMember(); return member != null ? CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(member) : null; }
@Nullable @Override public AnAction createEditTemplateAction(DataContext dataContext) { final Project project = CommonDataKeys.PROJECT.getData(dataContext); final Editor editor = CommonDataKeys.EDITOR.getData(dataContext); final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext); final PsiClass targetClass = editor == null || file == null ? null : getTargetClass(editor, file); if (targetClass != null) { final List<TestFramework> frameworks = TestIntegrationUtils.findSuitableFrameworks(targetClass); final TestIntegrationUtils.MethodKind methodKind = ((MyHandler) getHandler()).myMethodKind; if (!frameworks.isEmpty()) { return new AnAction("Edit Template") { @Override public void actionPerformed(AnActionEvent e) { chooseAndPerform( editor, frameworks, new Consumer<TestFramework>() { @Override public void consume(TestFramework framework) { final FileTemplateDescriptor descriptor = methodKind.getFileTemplateDescriptor(framework); if (descriptor != null) { final String fileName = descriptor.getFileName(); AllFileTemplatesConfigurable.editCodeTemplate( FileUtil.getNameWithoutExtension(fileName), project); } else { HintManager.getInstance() .showErrorHint( editor, "No template found for " + framework.getName() + ":" + BaseGenerateTestSupportMethodAction.this .getTemplatePresentation() .getText()); } } }); } }; } } return null; }
public void invoke( @NotNull Project project, @NotNull final Editor editor, @NotNull final PsiFile file) { final PsiClass targetClass = findTargetClass(editor, file); final List<TestFramework> frameworks = new ArrayList<TestFramework>(TestIntegrationUtils.findSuitableFrameworks(targetClass)); for (Iterator<TestFramework> iterator = frameworks.iterator(); iterator.hasNext(); ) { if (myMethodKind.getFileTemplateDescriptor(iterator.next()) == null) { iterator.remove(); } } if (frameworks.isEmpty()) return; final Consumer<TestFramework> consumer = new Consumer<TestFramework>() { @Override public void consume(TestFramework framework) { if (framework == null) return; doGenerate(editor, file, targetClass, framework); } }; chooseAndPerform(editor, frameworks, consumer); }