Skip to content

Bugfix-798: Fixed ArrayIndexOutOfBoundsException exception #808

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
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
32 changes: 24 additions & 8 deletions src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jetbrains.annotations.Nullable;

public class CopyMagentoPath extends CopyPathProvider {

public static final String PHTML_EXTENSION = "phtml";
public static final String JS_EXTENSION = "js";
public static final String CSS_EXTENSION = "css";
Expand Down Expand Up @@ -56,23 +57,23 @@ private boolean isNotValidFile(final VirtualFile virtualFile) {
|| virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension());
}

@Nullable
@Override
public String getPathToElement(
@NotNull final Project project,
@Nullable final VirtualFile virtualFile,
@Nullable final Editor editor
public @Nullable String getPathToElement(
final @NotNull Project project,
final @Nullable VirtualFile virtualFile,
final @Nullable Editor editor
) {
if (virtualFile == null) {
return null;
}
final PsiFile file
= PsiManager.getInstance(project).findFile(virtualFile);
final PsiFile file = PsiManager.getInstance(project).findFile(virtualFile);

if (file == null) {
return null;
}
final PsiDirectory directory = file.getContainingDirectory();
final String moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);

if (moduleName == null) {
return null;
}
Expand All @@ -89,15 +90,30 @@ public String getPathToElement(
} else {
return fullPath.toString();
}
int endIndex;

final int endIndex = getIndexOf(paths, fullPath, paths[++index]);
try {
endIndex = getIndexOf(paths, fullPath, paths[++index]);
} catch (ArrayIndexOutOfBoundsException exception) {
// endIndex could not be found.
return "";
}
final int offset = paths[index].length();

fullPath.replace(0, endIndex + offset, "");

return moduleName + SEPARATOR + fullPath;
}

/**
* Get index where web|template path starts in the fullPath.
*
* @param paths String[]
* @param fullPath StringBuilder
* @param path String
*
* @return int
*/
private int getIndexOf(final String[] paths, final StringBuilder fullPath, final String path) {
return fullPath.lastIndexOf(path) == -1
? getIndexOf(paths, fullPath, paths[++index])
Expand Down