Skip to content

Commit 82af1b2

Browse files
committed
1263: Refactor and enhance file path comparison logic
Introduce helper method to normalize URLs for consistent comparison, ensuring accurate handling of schemes like "file://" or "temp://". Add `final` modifiers for variables to improve immutability and readability.
1 parent 21972e7 commit 82af1b2

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/main/java/com/magento/idea/magento2plugin/util/magento/IsFileInEditableModuleUtil.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ private IsFileInEditableModuleUtil() {}
2222
* @return boolean
2323
*/
2424
public static boolean execute(final PsiFile file) {
25-
Project project = file.getProject();
26-
VirtualFile virtualFile = file.getVirtualFile();
25+
final Project project = file.getProject();
26+
final VirtualFile virtualFile = file.getVirtualFile();
2727

2828
return execute(project, virtualFile);
2929
}
@@ -35,10 +35,10 @@ public static boolean execute(final PsiFile file) {
3535
* @param virtualFile the file to check against editable module directories
3636
* @return true if the file is in an editable module directory, false otherwise
3737
*/
38-
public static boolean execute(Project project, VirtualFile virtualFile) {
39-
Settings settings = Settings.getInstance(project);
38+
public static boolean execute(final Project project, final VirtualFile virtualFile) {
39+
final Settings settings = Settings.getInstance(project);
4040
List<String> magentoToFolders = settings.getMagentoFolders();
41-
String magentoPathUrl = MagentoPathUrlUtil.execute(project);
41+
final String magentoPathUrl = MagentoPathUrlUtil.execute(project);
4242
if (magentoPathUrl != null) {
4343
if (magentoToFolders == null) {
4444
magentoToFolders = List.of(
@@ -52,17 +52,31 @@ public static boolean execute(Project project, VirtualFile virtualFile) {
5252
}
5353

5454

55-
final String filePath = virtualFile.getUrl();
5655

5756
if (magentoToFolders == null) {
5857
return false;
5958
}
6059

61-
for (String editablePath : magentoToFolders) {
62-
if (filePath.startsWith(editablePath)) {
60+
final String filePath = virtualFile.getUrl();
61+
for (final String editablePath : magentoToFolders) {
62+
if (normalizeUrl(filePath).startsWith(normalizeUrl(editablePath))) {
6363
return true;
6464
}
6565
}
6666
return false;
6767
}
68+
69+
/**
70+
* Normalizes a URL by removing the scheme (e.g., temp://, file://) to allow proper comparisons.
71+
*
72+
* @param url the URL to normalize
73+
* @return the normalized URL as a String
74+
*/
75+
private static String normalizeUrl(final String url) {
76+
final int schemeSeparatorIndex = url.indexOf("://");
77+
if (schemeSeparatorIndex != -1) {
78+
return url.substring(schemeSeparatorIndex + 3);
79+
}
80+
return url;
81+
}
6882
}

0 commit comments

Comments
 (0)