Skip to content

Commit f8901e2

Browse files
Merge branch '4.3.0-develop' of github.com:magento/magento2-phpstorm-plugin into 409-add-argument-replacement-action
2 parents aeffd6f + e06a65b commit f8901e2

File tree

10 files changed

+128
-31
lines changed

10 files changed

+128
-31
lines changed

src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import com.intellij.psi.PsiFile;
1616
import com.intellij.psi.PsiManager;
1717
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
18+
import java.util.ArrayList;
1819
import java.util.Arrays;
1920
import java.util.List;
21+
import javax.imageio.ImageIO;
2022
import org.jetbrains.annotations.NotNull;
2123
import org.jetbrains.annotations.Nullable;
2224

@@ -27,6 +29,8 @@ public class CopyMagentoPath extends CopyPathProvider {
2729
public static final String CSS_EXTENSION = "css";
2830
private final List<String> acceptedTypes
2931
= Arrays.asList(PHTML_EXTENSION, JS_EXTENSION, CSS_EXTENSION);
32+
private static final List<String> SUPPORTED_IMAGE_EXTENSIONS
33+
= new ArrayList<>(Arrays.asList(ImageIO.getReaderFormatNames()));
3034
public static final String SEPARATOR = "::";
3135
private int index;
3236

@@ -44,6 +48,15 @@ public class CopyMagentoPath extends CopyPathProvider {
4448
"web/"
4549
};
4650

51+
/**
52+
* Copy Magento Path actions for phtml, css, js, images extensions.
53+
*/
54+
public CopyMagentoPath() {
55+
super();
56+
57+
SUPPORTED_IMAGE_EXTENSIONS.add("svg");
58+
}
59+
4760
@Override
4861
public void update(@NotNull final AnActionEvent event) {
4962
final VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
@@ -54,7 +67,8 @@ public void update(@NotNull final AnActionEvent event) {
5467

5568
private boolean isNotValidFile(final VirtualFile virtualFile) {
5669
return virtualFile != null && virtualFile.isDirectory()
57-
|| virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension());
70+
|| virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension())
71+
&& !SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension());
5872
}
5973

6074
@Override
@@ -85,24 +99,23 @@ private boolean isNotValidFile(final VirtualFile virtualFile) {
8599
if (PHTML_EXTENSION.equals(virtualFile.getExtension())) {
86100
paths = templatePaths;
87101
} else if (JS_EXTENSION.equals(virtualFile.getExtension())
88-
|| CSS_EXTENSION.equals(virtualFile.getExtension())) {
102+
|| CSS_EXTENSION.equals(virtualFile.getExtension())
103+
|| SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension())) {
89104
paths = webPaths;
90105
} else {
91106
return fullPath.toString();
92107
}
93-
int endIndex;
94108

95109
try {
96-
endIndex = getIndexOf(paths, fullPath, paths[++index]);
97-
} catch (ArrayIndexOutOfBoundsException exception) {
98-
// endIndex could not be found.
99-
return "";
100-
}
101-
final int offset = paths[index].length();
110+
final int endIndex = getIndexOf(paths, fullPath, paths[++index]);
111+
final int offset = paths[index].length();
102112

103-
fullPath.replace(0, endIndex + offset, "");
113+
fullPath.replace(0, endIndex + offset, "");
104114

105-
return moduleName + SEPARATOR + fullPath;
115+
return moduleName + SEPARATOR + fullPath;
116+
} catch (ArrayIndexOutOfBoundsException exception) {
117+
return fullPath.toString();
118+
}
106119
}
107120

108121
/**

src/com/magento/idea/magento2plugin/actions/generation/NewModuleAction.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.magento.idea.magento2plugin.actions.generation.util.IsClickedDirectoryInsideProject;
2020
import com.magento.idea.magento2plugin.project.Settings;
2121
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
22+
import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil;
2223
import org.jetbrains.annotations.NotNull;
2324

2425
public class NewModuleAction extends com.intellij.openapi.actionSystem.AnAction {
@@ -89,6 +90,16 @@ public void update(final AnActionEvent event) {
8990
final String moduleName = GetModuleNameByDirectoryUtil
9091
.execute((PsiDirectory) psiElement, project);
9192
if (moduleName == null) {
93+
final String sourceDirPath = ((PsiDirectory) psiElement).getVirtualFile().getPath();
94+
final boolean isCustomCodeSourceDirValid =
95+
MagentoBasePathUtil.isCustomCodeSourceDirValid(sourceDirPath);
96+
final boolean isCustomVendorDirValid =
97+
MagentoBasePathUtil.isCustomVendorDirValid(sourceDirPath);
98+
99+
if (!isCustomCodeSourceDirValid && !isCustomVendorDirValid) { //NOPMD
100+
event.getPresentation().setVisible(false);
101+
return;
102+
}
92103
event.getPresentation().setVisible(true);
93104
return;
94105
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModuleDialog.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.magento.idea.magento2plugin.actions.generation.dialog;//NOPMD
77

88
import com.intellij.openapi.project.Project;
9+
import com.intellij.openapi.vfs.VirtualFile;
910
import com.intellij.psi.PsiDirectory;
1011
import com.intellij.psi.PsiFile;
1112
import com.magento.idea.magento2plugin.actions.generation.NewModuleAction;
@@ -23,9 +24,9 @@
2324
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
2425
import com.magento.idea.magento2plugin.magento.files.ComposerJson;
2526
import com.magento.idea.magento2plugin.magento.packages.Licenses;
26-
import com.magento.idea.magento2plugin.magento.packages.Package;
2727
import com.magento.idea.magento2plugin.project.Settings;
2828
import com.magento.idea.magento2plugin.util.CamelCaseToHyphen;
29+
import com.magento.idea.magento2plugin.util.magento.MagentoBasePathUtil;
2930
import com.magento.idea.magento2plugin.util.magento.MagentoVersionUtil;
3031
import java.awt.event.ActionEvent;
3132
import java.awt.event.KeyEvent;
@@ -163,8 +164,9 @@ public void windowClosing(final WindowEvent event) {
163164
}
164165

165166
private void detectPackageName(final @NotNull PsiDirectory initialBaseDir) {
166-
final PsiDirectory parentDir = initialBaseDir.getParent();
167-
if (parentDir != null && parentDir.toString().endsWith(Package.packagesRoot)) {
167+
final VirtualFile initialBaseDirVf = initialBaseDir.getVirtualFile();
168+
169+
if (MagentoBasePathUtil.isCustomVendorDirValid(initialBaseDirVf.getPath())) {
168170
packageName.setVisible(false);
169171
packageNameLabel.setVisible(false);
170172
this.detectedPackageName = initialBaseDir.getName();

src/com/magento/idea/magento2plugin/magento/packages/Package.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.magento.idea.magento2plugin.magento.packages;
77

88
public class Package { //NOPMD
9+
public static final String V_FILE_SEPARATOR = "/";
910
public static String packagesRoot = "app/code";
1011
public static String libWebRoot = "lib/web";
1112
public static String frameworkRootComposer = "vendor/magento/framework";

src/com/magento/idea/magento2plugin/magento/packages/UiFormButtonTypeSettings.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,28 @@ public enum UiFormButtonTypeSettings {
1414
SAVE("Save", "save primary", "''", "[\n"
1515
+ " 'mage-init' => ['button' => ['event' => 'save']],\n"
1616
+ " 'form-role' => 'save'\n"
17-
+ " ]", 10, "Save entity button."),
18-
DELETE("Delete", "delete", "'deleteConfirm(\\''\n"
19-
+ " . __('Are you sure you want to delete this $varName?')\n"
20-
+ " . '\\', \\'' . $this->getUrl(\n'*/*/delete',\n[$varIdConst => "
21-
+ "$this->$varEntityIdAccessor]\n) . '\\')'", "[]", 20, "Delete entity button."),
17+
+ " ]", 30, "Save entity button."),
18+
DELETE(
19+
"Delete",
20+
"delete",
21+
"sprintf(\"deleteConfirm('%s', '%s')\", \n"
22+
+ "__('Are you sure you want to delete this $varName?'),\n"
23+
+ "$this->getUrl(\n'*/*/delete',\n[$varIdConst => "
24+
+ "$this->$varEntityIdAccessor]\n)\n)",
25+
"[]",
26+
20,
27+
"Delete entity button."),
2228
BACK("Back To Grid", "back",
2329
"sprintf(\"location.href = '%s';\", $this->getUrl('*/*/'))",
24-
"[]", 30, "Back to list button."),
25-
CUSTOM("Custom Button", "custom", "''", "[]", 0, "Custom button.");
30+
"[]", 10, "Back to list button."),
31+
CUSTOM(
32+
"Custom Button",
33+
"custom",
34+
"''",
35+
"[]",
36+
0,
37+
"Custom button."
38+
);
2639

2740
private final String label;
2841
private final String classes;

src/com/magento/idea/magento2plugin/util/magento/GetModuleNameByDirectoryUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ private GetModuleNameByDirectoryUtil() {}
4444
final String moduleNamePath = matcher.group(0);
4545

4646
if (!moduleNamePath.isEmpty()) {
47-
return moduleNamePath.split("/")[5];
47+
final String[] moduleNamePathParts = moduleNamePath.split("/");
48+
49+
if (moduleNamePathParts.length >= 6) { //NOPMD
50+
return moduleNamePath.split("/")[5];
51+
}
4852
}
4953
}
5054

src/com/magento/idea/magento2plugin/util/magento/MagentoBasePathUtil.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import com.intellij.openapi.vfs.LocalFileSystem;
1010
import com.intellij.openapi.vfs.VfsUtil;
1111
import com.intellij.openapi.vfs.VirtualFile;
12-
import com.magento.idea.magento2plugin.magento.packages.File;
1312
import com.magento.idea.magento2plugin.magento.packages.Package;
13+
import java.io.File;
14+
import java.util.Arrays;
15+
import org.jetbrains.annotations.NotNull;
1416

1517
public final class MagentoBasePathUtil {
1618

@@ -38,4 +40,54 @@ public static boolean isMagentoFolderValid(final String path) {
3840
}
3941
return false;
4042
}
43+
44+
/**
45+
* Check if specified path belongs to the correct vendor name.
46+
*
47+
* @param path String
48+
*
49+
* @return boolean
50+
*/
51+
public static boolean isCustomVendorDirValid(final @NotNull String path) {
52+
final String[] pathParts = path.split(Package.V_FILE_SEPARATOR);
53+
54+
if (pathParts.length < 3) { //NOPMD
55+
return false;
56+
}
57+
58+
final String[] sourceCandidateParts = Arrays.copyOfRange(
59+
pathParts,
60+
pathParts.length - 3,
61+
pathParts.length - 1
62+
);
63+
64+
return Package.packagesRoot.equals(
65+
String.join(Package.V_FILE_SEPARATOR, sourceCandidateParts)
66+
);
67+
}
68+
69+
/**
70+
* Check if specified path belongs to the correct packages folder.
71+
*
72+
* @param path String
73+
*
74+
* @return boolean
75+
*/
76+
public static boolean isCustomCodeSourceDirValid(final @NotNull String path) {
77+
final String[] pathParts = path.split(Package.V_FILE_SEPARATOR);
78+
79+
if (pathParts.length < 2) { //NOPMD
80+
return false;
81+
}
82+
83+
final String[] sourceCandidateParts = Arrays.copyOfRange(
84+
pathParts,
85+
pathParts.length - 2,
86+
pathParts.length
87+
);
88+
89+
return Package.packagesRoot.equals(
90+
String.join(Package.V_FILE_SEPARATOR, sourceCandidateParts)
91+
);
92+
}
4193
}

testData/actions/generation/generator/FormButtonBlockGenerator/generateBackButtonBlock/MyBackButton.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function getButtonData(): array
2121
'back',
2222
sprintf("location.href = '%s';", $this->getUrl('*/*/')),
2323
[],
24-
30
24+
10
2525
);
2626
}
2727
}

testData/actions/generation/generator/FormButtonBlockGenerator/generateDeleteButtonBlock/DeleteBlock.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public function getButtonData(): array
2020
return $this->wrapButtonSettings(
2121
'Delete',
2222
'delete',
23-
'deleteConfirm(\''
24-
. __('Are you sure you want to delete this book?')
25-
. '\', \'' . $this->getUrl(
26-
'*/*/delete',
27-
[BookData::BOOK_ID => $this->getBookId()]
28-
) . '\')',
23+
sprintf("deleteConfirm('%s', '%s')",
24+
__('Are you sure you want to delete this book?'),
25+
$this->getUrl(
26+
'*/*/delete',
27+
[BookData::BOOK_ID => $this->getBookId()]
28+
)
29+
),
2930
[],
3031
20
3132
);

testData/actions/generation/generator/FormButtonBlockGenerator/generateSaveButtonBlock/SaveBlock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getButtonData(): array
2424
'mage-init' => ['button' => ['event' => 'save']],
2525
'form-role' => 'save'
2626
],
27-
10
27+
30
2828
);
2929
}
3030
}

0 commit comments

Comments
 (0)