Search in sources :

Example 1 with PsiDeclaredTarget

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;
}
Also used : InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) PomTarget(com.intellij.pom.PomTarget) TextRange(com.intellij.openapi.util.TextRange) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) PsiDeclaredTarget(com.intellij.pom.PsiDeclaredTarget) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PsiDeclaredTarget

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);
}
Also used : PomTarget(com.intellij.pom.PomTarget) Language(com.intellij.lang.Language) InjectedLanguageManager(com.intellij.lang.injection.InjectedLanguageManager) NavigationItem(com.intellij.navigation.NavigationItem) Lookup(com.intellij.codeInsight.lookup.Lookup) Document(com.intellij.openapi.editor.Document) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PomService(com.intellij.pom.references.PomService) Project(com.intellij.openapi.project.Project) EditorEx(com.intellij.openapi.editor.ex.EditorEx) PomTarget(com.intellij.pom.PomTarget) CompletionUtil(com.intellij.codeInsight.completion.CompletionUtil) Extensions(com.intellij.openapi.extensions.Extensions) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) PsiDeclaredTarget(com.intellij.pom.PsiDeclaredTarget) ThreeState(com.intellij.util.ThreeState) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Collection(java.util.Collection) LookupManager(com.intellij.codeInsight.lookup.LookupManager) LanguageExtension(com.intellij.lang.LanguageExtension) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ServiceManager(com.intellij.openapi.components.ServiceManager) PomDeclarationSearcher(com.intellij.pom.PomDeclarationSearcher) EditSourceUtil(com.intellij.ide.util.EditSourceUtil) BitUtil(com.intellij.util.BitUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Navigatable(com.intellij.pom.Navigatable) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Consumer(com.intellij.util.Consumer) TextRange(com.intellij.openapi.util.TextRange) PsiDeclaredTarget(com.intellij.pom.PsiDeclaredTarget) PomDeclarationSearcher(com.intellij.pom.PomDeclarationSearcher) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)2 TextRange (com.intellij.openapi.util.TextRange)2 PomTarget (com.intellij.pom.PomTarget)2 PsiDeclaredTarget (com.intellij.pom.PsiDeclaredTarget)2 Nullable (org.jetbrains.annotations.Nullable)2 CompletionUtil (com.intellij.codeInsight.completion.CompletionUtil)1 DaemonCodeAnalyzer (com.intellij.codeInsight.daemon.DaemonCodeAnalyzer)1 Lookup (com.intellij.codeInsight.lookup.Lookup)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupManager (com.intellij.codeInsight.lookup.LookupManager)1 EditSourceUtil (com.intellij.ide.util.EditSourceUtil)1 Language (com.intellij.lang.Language)1 LanguageExtension (com.intellij.lang.LanguageExtension)1 NavigationItem (com.intellij.navigation.NavigationItem)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ServiceManager (com.intellij.openapi.components.ServiceManager)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 EditorUtil (com.intellij.openapi.editor.ex.util.EditorUtil)1