Skip to content

Commit

Permalink
Merge pull request #936 from bohdan-harniuk/853-fix-observer-action-v…
Browse files Browse the repository at this point in the history
…isibility

853: Fix observer action visibility
  • Loading branch information
Andrii Beziazychnyi authored Jan 23, 2022
2 parents bfb3a63 + 1b44e22 commit 3e44c6e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jetbrains.annotations.NotNull;

public class CreateAnObserverAction extends DumbAwareAction {

public static final String ACTION_NAME = "Create a new Observer for this event";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Observer";
public String targetEvent;
Expand All @@ -40,19 +41,24 @@ public CreateAnObserverAction() {
* Updates the state of action.
*/
@Override
public void update(final AnActionEvent event) {
public void update(final @NotNull AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);
if (project == null) {
return;
}

if (!Settings.isEnabled(project)) {
this.setStatus(event, false);
return;
}
final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);

if (!(psiFile instanceof PhpFile)) {
this.setStatus(event, false);
return;
}

final PsiElement element = getElement(event);

if (element == null) {
this.setStatus(event, false);
return;
Expand All @@ -67,21 +73,36 @@ public void update(final AnActionEvent event) {
this.setStatus(event, false);
}

private PsiElement getElement(@NotNull final AnActionEvent event) {
@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
if (event.getProject() == null) {
return;
}
CreateAnObserverDialog.open(event.getProject(), this.targetEvent);
}

@Override
public boolean isDumbAware() {
return false;
}

private PsiElement getElement(final @NotNull AnActionEvent event) {
final Caret caret = event.getData(PlatformDataKeys.CARET);

if (caret == null) {
return null;
}
final int offset = caret.getOffset();
final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
final PsiElement element = psiFile.findElementAt(offset);
if (element == null) {

if (psiFile == null) {
return null;
}
return element;
final int offset = caret.getOffset();

return psiFile.findElementAt(offset);
}

private boolean isObserverEventNameClicked(@NotNull final PsiElement element) {
private boolean isObserverEventNameClicked(final @NotNull PsiElement element) {
return checkIsElementStringLiteral(element)
&& checkIsParametersList(element.getParent().getParent())
&& checkIsMethodReference(element.getParent().getParent().getParent())
Expand All @@ -90,11 +111,11 @@ && checkIsEventDispatchMethod(
);
}

private boolean checkIsParametersList(@NotNull final PsiElement element) {
private boolean checkIsParametersList(final @NotNull PsiElement element) {
return element instanceof ParameterList;
}

private boolean checkIsMethodReference(@NotNull final PsiElement element) {
private boolean checkIsMethodReference(final @NotNull PsiElement element) {
return element instanceof MethodReference;
}

Expand All @@ -107,40 +128,34 @@ private boolean checkIsEventDispatchMethod(final MethodReference element) {
if (!(method instanceof Method)) {
return false;
}
if (!((Method) method).getName().equals(Observer.DISPATCH_METHOD)) {
if (!Observer.DISPATCH_METHOD.equals(((Method) method).getName())) {
return false;
}
final PsiElement phpClass = method.getParent();
if (!(phpClass instanceof PhpClass)) {
return false;
}
final String fqn = ((PhpClass) phpClass).getPresentableFQN();
return fqn.equals(Observer.INTERFACE);

return Observer.INTERFACE.equals(fqn)
|| Observer.IMPLEMENTATION.equals(fqn)
|| Observer.ENTITY_IMPL.equals(fqn)
|| Observer.STAGING_IMPL.equals(fqn);
}

private boolean checkIsElementStringLiteral(@NotNull final PsiElement element) {
private boolean checkIsElementStringLiteral(final @NotNull PsiElement element) {
final ASTNode astNode = element.getNode();
if (astNode == null) {
return false;
}
final IElementType elementType = astNode.getElementType();

return elementType == PhpTokenTypes.STRING_LITERAL
|| elementType == PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE;
return elementType.equals(PhpTokenTypes.STRING_LITERAL)
|| elementType.equals(PhpTokenTypes.STRING_LITERAL_SINGLE_QUOTE);
}

private void setStatus(final AnActionEvent event, final boolean status) {
event.getPresentation().setVisible(status);
event.getPresentation().setEnabled(status);
}

@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
CreateAnObserverDialog.open(event.getProject(), this.targetEvent);
}

@Override
public boolean isDumbAware() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class Observer implements ModuleFileInterface {

private final String fileName;
public static final String INTERFACE = "Magento\\Framework\\Event\\ManagerInterface";
public static final String IMPLEMENTATION = "Magento\\Framework\\Event\\Manager";
public static final String ENTITY_IMPL = "Magento\\Framework\\EntityManager\\EventManager";
public static final String STAGING_IMPL = "Magento\\Staging\\Model\\Event\\Manager";
public static final String DISPATCH_METHOD = "dispatch";
public static final String NAME_ATTRIBUTE = "name";
public static final String OBSERVER_EXECUTE_TEMPLATE_NAME = "Magento Observer Execute Method";
Expand Down

0 comments on commit 3e44c6e

Please sign in to comment.