Skip to content

1010: Fixed module context actions available in the theme #1064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface;
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -50,13 +52,14 @@ public AbstractContextAction(
}

@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public void update(final @NotNull AnActionEvent event) {
event.getPresentation().setEnabled(false);
event.getPresentation().setVisible(false);

final Project project = event.getProject();

if (project == null) {
if (project == null || !Settings.isEnabled(project)) {
return;
}
final DataContext context = event.getDataContext();
Expand Down Expand Up @@ -86,6 +89,12 @@ public void update(final @NotNull AnActionEvent event) {
|| !isVisible(moduleData, targetDirectory, targetFile)) {
return;
}

if (moduleData.getType().equals(ComponentType.module)
&& !GetMagentoModuleUtil.isEditableModule(moduleData)) {
return;
}

customDataContext = SimpleDataContext
.builder()
.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.magento.idea.magento2plugin.actions.generation.util.IsClickedDirectoryInsideProject;
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.GetMagentoModuleUtil;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import javax.swing.Icon;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -62,7 +63,8 @@ public void update(final AnActionEvent event) {
final PsiDirectory moduleDirectory = new ModuleIndex(project)
.getModuleDirectoryByModuleName(moduleName);

if (moduleDirectory != null) {
if (moduleDirectory != null
&& GetMagentoModuleUtil.isDirectoryInEditableModule(moduleDirectory)) {
event.getPresentation().setVisible(true);
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/com/magento/idea/magento2plugin/util/RegExUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public static class Magento {

public static final String TEST_CLASS_FQN =
"^(\\\\)?(\\w+\\\\){1}(\\w+\\\\){1}Test(\\\\\\w+)+$";

public static final String CUSTOM_VENDOR_NAME =
"app\\/code\\/(\\w+)\\/";
}

public static class PhpRegex {
Expand Down Expand Up @@ -111,6 +114,7 @@ public static class JsRegex {
}

public static class CustomTheme {

public static final String MODULE_NAME =
"app\\/design\\/(adminhtml|frontend)\\/\\w*\\/\\w*\\/\\w*";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import com.magento.idea.magento2plugin.magento.files.RegistrationPhp;
import com.magento.idea.magento2plugin.magento.packages.ComponentType;
import com.magento.idea.magento2plugin.magento.packages.Package;
import com.magento.idea.magento2plugin.util.RegExUtil;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -49,12 +52,9 @@ public static MagentoModuleData getByContext(
if (registrationFile == null) {
return null;
}
final PsiDirectory configDir = registrationFile
.getContainingDirectory()
.findSubdirectory(Package.moduleBaseAreaDir);
final PsiDirectory viewDir = registrationFile
.getContainingDirectory()
.findSubdirectory(Package.moduleViewDir);
final PsiDirectory moduleDir = registrationFile.getContainingDirectory();
final PsiDirectory configDir = moduleDir.findSubdirectory(Package.moduleBaseAreaDir);
final PsiDirectory viewDir = moduleDir.findSubdirectory(Package.moduleViewDir);
final Collection<MethodReference> methodReferences = PsiTreeUtil.findChildrenOfType(
registrationFile,
MethodReference.class
Expand All @@ -80,12 +80,42 @@ public static MagentoModuleData getByContext(
return null;
}

return new MagentoModuleData(name, resolvedType, configDir, viewDir);
return new MagentoModuleData(name, resolvedType, moduleDir, configDir, viewDir);
}

return null;
}

/**
* Check if specified module is in the app/code directory.
*
* @param moduleData MagentoModuleData
*
* @return boolean
*/
public static boolean isEditableModule(final @NotNull MagentoModuleData moduleData) {
final Pattern pattern = Pattern.compile(RegExUtil.Magento.CUSTOM_VENDOR_NAME);
final Matcher matcher = pattern.matcher(
moduleData.getModuleDir().getVirtualFile().getPath()
);

return matcher.find();
}

/**
* Check if specified directory is in the app/code.
*
* @param directory PsiDirectory
*
* @return boolean
*/
public static boolean isDirectoryInEditableModule(final @NotNull PsiDirectory directory) {
final Pattern pattern = Pattern.compile(RegExUtil.Magento.CUSTOM_VENDOR_NAME);
final Matcher matcher = pattern.matcher(directory.getVirtualFile().getPath());

return matcher.find();
}

private static PsiFile getModuleRegistrationFile(
final @NotNull PsiDirectory directory,
final @NotNull String basePath
Expand Down Expand Up @@ -136,6 +166,7 @@ public static class MagentoModuleData {

private final String name;
private final ComponentType type;
private final PsiDirectory moduleDir;
private final PsiDirectory configDir;
private final PsiDirectory viewDir;

Expand All @@ -144,30 +175,35 @@ public static class MagentoModuleData {
*
* @param name String
* @param type ComponentType
* @param moduleDir PsiDirectory
*/
public MagentoModuleData(
final @NotNull String name,
final @NotNull ComponentType type
final @NotNull ComponentType type,
final @NotNull PsiDirectory moduleDir
) {
this(name, type, null, null);
this(name, type, moduleDir, null, null);
}

/**
* Constructor with a config directory specified.
*
* @param name String
* @param type ComponentType
* @param moduleDir PsiDirectory
* @param configDir PsiDirectory
* @param viewDir PsiDirectory
*/
public MagentoModuleData(
final @NotNull String name,
final @NotNull ComponentType type,
final @NotNull PsiDirectory moduleDir,
final @Nullable PsiDirectory configDir,
final @Nullable PsiDirectory viewDir
) {
this.name = name;
this.type = type;
this.moduleDir = moduleDir;
this.configDir = configDir;
this.viewDir = viewDir;
}
Expand All @@ -180,6 +216,10 @@ public ComponentType getType() {
return type;
}

public PsiDirectory getModuleDir() {
return moduleDir;
}

public @Nullable PsiDirectory getConfigDir() {
return configDir;
}
Expand Down