@Test public void invoke() { testHelper.getPsiClass( classes -> { final CodeInsightTestFixture fixture = testHelper.getFixture(); final PsiMethodCallExpression call = PsiTreeUtil.findChildOfType(classes.get(0), PsiMethodCallExpression.class); final LightVirtualFile virtualFile = createVirtualFile(classes.get(0)); final String generatedOnEvent = "@com.facebook.litho.annotations.OnEvent(TestClass.class)\n" + " static void onTestClass(com.facebook.litho.ComponentContext c) {\n" + " }"; final PsiClass eventClass = JavaPsiFacade.getInstance(fixture.getProject()) .getElementFactory() .createClass("TestClass"); fixture.openFileInEditor(virtualFile); assertThat(fixture.getEditor().getDocument().getText()).doesNotContain(generatedOnEvent); AddArgumentFix.createNewMethodCallFix( call, "TestName", eventClass, Mockito.mock(PsiClass.class)) .invoke(fixture.getProject(), fixture.getEditor(), fixture.getFile()); assertThat(fixture.getEditor().getDocument().getText()).contains(generatedOnEvent); assertThat(call.getArgumentList().getExpressions()[0].getText()) .isEqualTo("TestName.onTestClass()"); return true; }, "EventHandlerAnnotatorSpec.java"); }
@Override public void invoke(Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final AtomicReference<PsiMethod> eventMethodRef = new AtomicReference<>(); final Runnable generateOnEvent = () -> OnEventGenerateAction.createHandler( (context, eventProject) -> event, eventMethodRef::set) .invoke(project, editor, file); final Runnable updateArgumentList = () -> Optional.ofNullable(eventMethodRef.get()) .map( eventMethod -> AddArgumentFix.createArgumentList( methodCall, clsName, eventMethod.getName(), JavaPsiFacade.getInstance(project).getElementFactory())) .ifPresent(argumentList -> methodCall.getArgumentList().replace(argumentList)); final Runnable action = () -> { TransactionGuard.getInstance().submitTransactionAndWait(generateOnEvent); WriteCommandAction.runWriteCommandAction(project, updateArgumentList); LithoLoggerProvider.getEventLogger().log(EventLogger.EVENT_FIX_EVENT_HANDLER + ".new"); }; final Application application = ApplicationManager.getApplication(); if (application.isUnitTestMode()) { action.run(); } else { application.invokeLater(action); } }
OnEventCreateFix extends BaseIntentionAction implements HighPriorityAction { @Override public void invoke(Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final AtomicReference<PsiMethod> eventMethodRef = new AtomicReference<>(); final Runnable generateOnEvent = () -> OnEventGenerateAction.createHandler( (context, eventProject) -> event, eventMethodRef::set) .invoke(project, editor, file); final Runnable updateArgumentList = () -> Optional.ofNullable(eventMethodRef.get()) .map( eventMethod -> AddArgumentFix.createArgumentList( methodCall, clsName, eventMethod.getName(), JavaPsiFacade.getInstance(project).getElementFactory())) .ifPresent(argumentList -> methodCall.getArgumentList().replace(argumentList)); final Runnable action = () -> { TransactionGuard.getInstance().submitTransactionAndWait(generateOnEvent); WriteCommandAction.runWriteCommandAction(project, updateArgumentList); LithoLoggerProvider.getEventLogger().log(EventLogger.EVENT_FIX_EVENT_HANDLER + ".new"); }; final Application application = ApplicationManager.getApplication(); if (application.isUnitTestMode()) { action.run(); } else { application.invokeLater(action); } } }
OnEventCreateFix extends BaseIntentionAction implements HighPriorityAction { @Override public void invoke(Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final AtomicReference<PsiMethod> eventMethodRef = new AtomicReference<>(); final Runnable generateOnEvent = () -> OnEventGenerateAction.createHandler( (context, eventProject) -> event, eventMethodRef::set) .invoke(project, editor, file); final Runnable updateArgumentList = () -> Optional.ofNullable(eventMethodRef.get()) .map( eventMethod -> AddArgumentFix.createArgumentList( methodCall, clsName, eventMethod.getName(), JavaPsiFacade.getInstance(project).getElementFactory())) .ifPresent(argumentList -> methodCall.getArgumentList().replace(argumentList)); final Runnable action = () -> { TransactionGuard.getInstance().submitTransactionAndWait(generateOnEvent); WriteCommandAction.runWriteCommandAction(project, updateArgumentList); LithoLoggerProvider.getEventLogger().log(EventLogger.EVENT_FIX_EVENT_HANDLER + ".new"); }; final Application application = ApplicationManager.getApplication(); if (application.isUnitTestMode()) { action.run(); } else { application.invokeLater(action); } } OnEventCreateFix(
      PsiCall methodCall,
      String clsName,
      PsiClass event,
      PsiClass layoutSpecToUpdate,
      String fixDescription); }
OnEventCreateFix extends BaseIntentionAction implements HighPriorityAction { @Override public void invoke(Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final AtomicReference<PsiMethod> eventMethodRef = new AtomicReference<>(); final Runnable generateOnEvent = () -> OnEventGenerateAction.createHandler( (context, eventProject) -> event, eventMethodRef::set) .invoke(project, editor, file); final Runnable updateArgumentList = () -> Optional.ofNullable(eventMethodRef.get()) .map( eventMethod -> AddArgumentFix.createArgumentList( methodCall, clsName, eventMethod.getName(), JavaPsiFacade.getInstance(project).getElementFactory())) .ifPresent(argumentList -> methodCall.getArgumentList().replace(argumentList)); final Runnable action = () -> { TransactionGuard.getInstance().submitTransactionAndWait(generateOnEvent); WriteCommandAction.runWriteCommandAction(project, updateArgumentList); LithoLoggerProvider.getEventLogger().log(EventLogger.EVENT_FIX_EVENT_HANDLER + ".new"); }; final Application application = ApplicationManager.getApplication(); if (application.isUnitTestMode()) { action.run(); } else { application.invokeLater(action); } } OnEventCreateFix(
      PsiCall methodCall,
      String clsName,
      PsiClass event,
      PsiClass layoutSpecToUpdate,
      String fixDescription); @Nls(capitalization = Nls.Capitalization.Sentence) @Override String getFamilyName(); @Override boolean isAvailable(Project project, Editor editor, PsiFile file); @Override void invoke(Project project, Editor editor, PsiFile file); }
OnEventCreateFix extends BaseIntentionAction implements HighPriorityAction { @Override public void invoke(Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final AtomicReference<PsiMethod> eventMethodRef = new AtomicReference<>(); final Runnable generateOnEvent = () -> OnEventGenerateAction.createHandler( (context, eventProject) -> event, eventMethodRef::set) .invoke(project, editor, file); final Runnable updateArgumentList = () -> Optional.ofNullable(eventMethodRef.get()) .map( eventMethod -> AddArgumentFix.createArgumentList( methodCall, clsName, eventMethod.getName(), JavaPsiFacade.getInstance(project).getElementFactory())) .ifPresent(argumentList -> methodCall.getArgumentList().replace(argumentList)); final Runnable action = () -> { TransactionGuard.getInstance().submitTransactionAndWait(generateOnEvent); WriteCommandAction.runWriteCommandAction(project, updateArgumentList); LithoLoggerProvider.getEventLogger().log(EventLogger.EVENT_FIX_EVENT_HANDLER + ".new"); }; final Application application = ApplicationManager.getApplication(); if (application.isUnitTestMode()) { action.run(); } else { application.invokeLater(action); } } OnEventCreateFix(
      PsiCall methodCall,
      String clsName,
      PsiClass event,
      PsiClass layoutSpecToUpdate,
      String fixDescription); @Nls(capitalization = Nls.Capitalization.Sentence) @Override String getFamilyName(); @Override boolean isAvailable(Project project, Editor editor, PsiFile file); @Override void invoke(Project project, Editor editor, PsiFile file);  }