Skip to content

Commit d1a0cff

Browse files
author
Vitaliy
authored
Merge pull request #1063 from bohdan-harniuk/1048-fix-IllegalArgumentException-while-instantiating-an-TextRange-object
1048: Fixed bug with wrong text range
2 parents 1982e56 + 6c6e73b commit d1a0cff

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/com/magento/idea/magento2plugin/reference/provider/FilePathReferenceProvider.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public class FilePathReferenceProvider extends PsiReferenceProvider {
3333
"PMD.CognitiveComplexity",
3434
"PMD.CyclomaticComplexity",
3535
"PMD.NPathComplexity",
36-
"PMD.AvoidInstantiatingObjectsInLoops"
36+
"PMD.AvoidInstantiatingObjectsInLoops",
37+
"PMD.AvoidDeeplyNestedIfStmts"
3738
})
3839
@NotNull
3940
@Override
@@ -92,14 +93,13 @@ public PsiReference[] getReferencesByElement(
9293
if (null != psiElement) {
9394
final int currentPathIndex = currentPath.lastIndexOf('/') == -1
9495
? 0 : currentPath.lastIndexOf('/') + 1;
96+
final int startOffset = origValue.indexOf(filePath) + currentPathIndex;
97+
final int endOffset = startOffset + pathPart.length();
9598

96-
final TextRange pathRange = new TextRange(
97-
origValue.indexOf(filePath)
98-
+ currentPathIndex,
99-
origValue.indexOf(filePath)
100-
+ currentPathIndex
101-
+ pathPart.length()
102-
);
99+
if (!isProperRange(startOffset, endOffset)) {
100+
continue;
101+
}
102+
final TextRange pathRange = new TextRange(startOffset, endOffset);
103103

104104
if (psiPathElements.containsKey(pathRange)) {
105105
psiPathElements.get(pathRange).add(psiElement);
@@ -186,4 +186,8 @@ private Collection<VirtualFile> getFiles(final @NotNull PsiElement element) {
186186
private boolean isModuleNamePresent(final @NotNull PsiElement element) {
187187
return GetModuleNameUtil.getInstance().execute(element.getText()) != null;
188188
}
189+
190+
private boolean isProperRange(final int startOffset, final int endOffset) {
191+
return startOffset <= endOffset && startOffset >= 0;
192+
}
189193
}

0 commit comments

Comments
 (0)