use of com.intellij.pom.PsiDeclaredTarget in project intellij-community by JetBrains.
the class HighlightUsagesHandler method getNameIdentifierRange.
@Nullable
public static TextRange getNameIdentifierRange(PsiFile file, PsiElement element) {
final InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(element.getProject());
if (element instanceof PomTargetPsiElement) {
final PomTarget target = ((PomTargetPsiElement) element).getTarget();
if (target instanceof PsiDeclaredTarget) {
final PsiDeclaredTarget declaredTarget = (PsiDeclaredTarget) target;
final TextRange range = declaredTarget.getNameIdentifierRange();
if (range != null) {
if (range.getStartOffset() < 0 || range.getLength() <= 0) {
return null;
}
final PsiElement navElement = declaredTarget.getNavigationElement();
if (PsiUtilBase.isUnderPsiRoot(file, navElement)) {
return injectedManager.injectedToHost(navElement, range.shiftRight(navElement.getTextRange().getStartOffset()));
}
}
}
}
if (!PsiUtilBase.isUnderPsiRoot(file, element)) {
return null;
}
PsiElement identifier = IdentifierUtil.getNameIdentifier(element);
if (identifier != null && PsiUtilBase.isUnderPsiRoot(file, identifier)) {
return injectedManager.injectedToHost(identifier, identifier.getTextRange());
}
return null;
}
use of com.intellij.pom.PsiDeclaredTarget in project intellij-community by JetBrains.
the class TargetElementUtil method getNamedElement.
@Override
@Nullable
public PsiElement getNamedElement(@Nullable final PsiElement element, final int offsetInElement) {
if (element == null)
return null;
final List<PomTarget> targets = ContainerUtil.newArrayList();
final Consumer<PomTarget> consumer = target -> {
if (target instanceof PsiDeclaredTarget) {
final PsiDeclaredTarget declaredTarget = (PsiDeclaredTarget) target;
final PsiElement navigationElement = declaredTarget.getNavigationElement();
final TextRange range = declaredTarget.getNameIdentifierRange();
if (range != null && !range.shiftRight(navigationElement.getTextRange().getStartOffset()).contains(element.getTextRange().getStartOffset() + offsetInElement)) {
return;
}
}
targets.add(target);
};
PsiElement parent = element;
int offset = offsetInElement;
while (parent != null) {
for (PomDeclarationSearcher searcher : PomDeclarationSearcher.EP_NAME.getExtensions()) {
searcher.findDeclarationsAt(parent, offset, consumer);
if (!targets.isEmpty()) {
final PomTarget target = targets.get(0);
return target == null ? null : PomService.convertToPsi(element.getProject(), target);
}
}
offset += parent.getStartOffsetInParent();
parent = parent.getParent();
}
return getNamedElement(element);
}
Aggregations