OffsetsElementSignatureProvider cleanup

This commit is contained in:
Dmitry Batrak
2018-10-11 14:40:56 +03:00
parent a902fb05f8
commit 66f75aa1ce
@@ -155,11 +155,8 @@ public class OffsetsElementSignatureProvider extends AbstractElementSignaturePro
// There is a possible case that we have a hierarchy of PSI elements that target the same document range. We need to find
// out the right one then.
int indexFromRoot = 0;
for (PsiElement e = element.getParent(); e != null && range.equals(e.getTextRange()); e = e.getParent()) {
indexFromRoot++;
}
int indexFromRoot = getElementHierarchyIndex(element);
if (processingInfoStorage != null) {
processingInfoStorage.append(String.format("Target element index is %d. Current index from root is %d%n", index, indexFromRoot));
@@ -203,10 +200,8 @@ public class OffsetsElementSignatureProvider extends AbstractElementSignaturePro
// There is a possible case that given PSI element has a parent or child that targets the same range. So, we remember
// not only target range offsets but 'hierarchy index' as well.
int index = 0;
for (PsiElement e = element.getParent(); e != null && range.equals(e.getTextRange()); e = e.getParent()) {
index++;
}
int index = getElementHierarchyIndex(element);
buffer.append(ELEMENT_TOKENS_SEPARATOR).append(index);
PsiFile containingFile = element.getContainingFile();
if (containingFile != null && containingFile.getViewProvider().getLanguages().size() > 1) {
@@ -214,4 +209,13 @@ public class OffsetsElementSignatureProvider extends AbstractElementSignaturePro
}
return buffer.toString();
}
private static int getElementHierarchyIndex(@NotNull PsiElement element) {
TextRange range = element.getTextRange();
int index = 0;
for (PsiElement e = element.getParent(); e != null && range.equals(e.getTextRange()); e = e.getParent()) {
index++;
}
return index;
}
}