Search in sources :

Example 1 with PomTarget

use of com.intellij.pom.PomTarget in project intellij-community by JetBrains.

the class AntDomDocumentationProvider method getQuickNavigateInfo.

@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
    // todo!
    if (element instanceof PomTargetPsiElement) {
        final PomTarget pomTarget = ((PomTargetPsiElement) element).getTarget();
        if (pomTarget instanceof DomTarget) {
            final DomElement domElement = ((DomTarget) pomTarget).getDomElement();
            if (domElement instanceof AntDomTarget) {
                final AntDomTarget antTarget = (AntDomTarget) domElement;
                final String description = antTarget.getDescription().getRawText();
                if (description != null && description.length() > 0) {
                    final String targetName = antTarget.getName().getRawText();
                    final StringBuilder builder = StringBuilderSpinAllocator.alloc();
                    try {
                        builder.append("Target");
                        if (targetName != null) {
                            builder.append(" \"").append(targetName).append("\"");
                        }
                        final XmlElement xmlElement = antTarget.getXmlElement();
                        if (xmlElement != null) {
                            final PsiFile containingFile = xmlElement.getContainingFile();
                            if (containingFile != null) {
                                final String fileName = containingFile.getName();
                                builder.append(" [").append(fileName).append("]");
                            }
                        }
                        return builder.append(" ").append(description).toString();
                    } finally {
                        StringBuilderSpinAllocator.dispose(builder);
                    }
                }
            }
        } else if (pomTarget instanceof DomChildrenDescription) {
            final DomChildrenDescription description = (DomChildrenDescription) pomTarget;
            Type type = null;
            try {
                type = description.getType();
            } catch (UnsupportedOperationException e) {
                LOG.info(e);
            }
            if (type instanceof Class && AntDomElement.class.isAssignableFrom(((Class) type))) {
                final String elemName = description.getName();
                if (elemName != null) {
                    final AntDomElement.Role role = description.getUserData(AntDomElement.ROLE);
                    final StringBuilder builder = StringBuilderSpinAllocator.alloc();
                    try {
                        if (role == AntDomElement.Role.TASK) {
                            builder.append("Task ");
                        } else if (role == AntDomElement.Role.DATA_TYPE) {
                            builder.append("Data structure ");
                        }
                        builder.append(elemName);
                        return builder.toString();
                    } finally {
                        StringBuilderSpinAllocator.dispose(builder);
                    }
                }
            }
        }
    }
    return null;
}
Also used : AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) DomTarget(com.intellij.util.xml.DomTarget) DomChildrenDescription(com.intellij.util.xml.reflect.DomChildrenDescription) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) PomTarget(com.intellij.pom.PomTarget) Type(java.lang.reflect.Type) DomElement(com.intellij.util.xml.DomElement) AntDomElement(com.intellij.lang.ant.dom.AntDomElement) XmlElement(com.intellij.psi.xml.XmlElement) PsiFile(com.intellij.psi.PsiFile) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PomTarget

use of com.intellij.pom.PomTarget in project android by JetBrains.

the class AndroidXmlDocumentationProvider method generateDoc.

@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    if (element instanceof ProvidedDocumentationPsiElement) {
        return ((ProvidedDocumentationPsiElement) element).getDocumentation();
    }
    if (element instanceof LazyValueResourceElementWrapper) {
        LazyValueResourceElementWrapper wrapper = (LazyValueResourceElementWrapper) element;
        ValueResourceInfo resourceInfo = wrapper.getResourceInfo();
        ResourceType type = resourceInfo.getType();
        String name = resourceInfo.getName();
        Module module = ModuleUtilCore.findModuleForPsiElement(element);
        if (module == null) {
            return null;
        }
        AndroidFacet facet = AndroidFacet.getInstance(element);
        if (facet == null) {
            return null;
        }
        ResourceUrl url;
        ResourceUrl originalUrl = originalElement != null ? ResourceUrl.parse(originalElement.getText()) : null;
        if (originalUrl != null && name.equals(originalUrl.name)) {
            url = originalUrl;
        } else {
            boolean isFramework = false;
            if (originalUrl != null) {
                isFramework = originalUrl.framework;
            } else {
                // Figure out if this resource is a framework file.
                // We really should store that info in the ValueResourceInfo instances themselves.
                // For now, attempt to figure it out
                SystemResourceManager systemResourceManager = facet.getSystemResourceManager();
                VirtualFile containingFile = resourceInfo.getContainingFile();
                if (systemResourceManager != null) {
                    VirtualFile parent = containingFile.getParent();
                    if (parent != null) {
                        VirtualFile resDir = parent.getParent();
                        if (resDir != null) {
                            isFramework = systemResourceManager.isResourceDir(resDir);
                        }
                    }
                }
            }
            url = ResourceUrl.create(type, name, isFramework, false);
        }
        return generateDoc(element, url);
    } else if (element instanceof MyResourceElement) {
        return getResourceDocumentation(element, ((MyResourceElement) element).myResource);
    } else if (element instanceof XmlAttributeValue) {
        return getResourceDocumentation(element, ((XmlAttributeValue) element).getValue());
    }
    if (originalElement instanceof XmlToken) {
        XmlToken token = (XmlToken) originalElement;
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_START_DELIMITER) {
            PsiElement next = token.getNextSibling();
            if (next instanceof XmlToken) {
                token = (XmlToken) next;
            }
        } else if (token.getTokenType() == XML_ATTRIBUTE_VALUE_END_DELIMITER) {
            PsiElement prev = token.getPrevSibling();
            if (prev instanceof XmlToken) {
                token = (XmlToken) prev;
            }
        }
        if (token.getTokenType() == XML_ATTRIBUTE_VALUE_TOKEN) {
            String documentation = getResourceDocumentation(originalElement, token.getText());
            if (documentation != null) {
                return documentation;
            }
        } else if (token.getTokenType() == XML_DATA_CHARACTERS) {
            String text = token.getText().trim();
            String documentation = getResourceDocumentation(originalElement, text);
            if (documentation != null) {
                return documentation;
            }
        }
    }
    if (element instanceof PomTargetPsiElement && originalElement != null) {
        final PomTarget target = ((PomTargetPsiElement) element).getTarget();
        if (target instanceof DomAttributeChildDescription) {
            synchronized (ANDROID_ATTRIBUTE_DOCUMENTATION_CACHE_KEY) {
                return generateDocForXmlAttribute((DomAttributeChildDescription) target, originalElement);
            }
        }
    }
    if (element instanceof MyDocElement) {
        return ((MyDocElement) element).myDocumentation;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) DomAttributeChildDescription(com.intellij.util.xml.reflect.DomAttributeChildDescription) XmlToken(com.intellij.psi.xml.XmlToken) PomTarget(com.intellij.pom.PomTarget) LazyValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Module(com.intellij.openapi.module.Module) ResourceUrl(com.android.ide.common.resources.ResourceUrl) SystemResourceManager(org.jetbrains.android.resourceManagers.SystemResourceManager) FakePsiElement(com.intellij.psi.impl.FakePsiElement) PsiElement(com.intellij.psi.PsiElement) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) ValueResourceInfo(org.jetbrains.android.resourceManagers.ValueResourceInfo)

Example 3 with PomTarget

use of com.intellij.pom.PomTarget in project intellij-community by JetBrains.

the class GrUnresolvedAccessChecker method shouldHighlightAsUnresolved.

private static boolean shouldHighlightAsUnresolved(@NotNull GrReferenceExpression referenceExpression) {
    if (GrHighlightUtil.isDeclarationAssignment(referenceExpression))
        return false;
    GrExpression qualifier = referenceExpression.getQualifier();
    if (qualifier != null && qualifier.getType() == null && !isRefToPackage(qualifier))
        return false;
    if (qualifier != null && referenceExpression.getDotTokenType() == GroovyTokenTypes.mMEMBER_POINTER && referenceExpression.multiResolve(false).length > 0) {
        return false;
    }
    if (!GroovyUnresolvedHighlightFilter.shouldHighlight(referenceExpression))
        return false;
    CollectConsumer<PomTarget> consumer = new CollectConsumer<>();
    for (PomDeclarationSearcher searcher : PomDeclarationSearcher.EP_NAME.getExtensions()) {
        searcher.findDeclarationsAt(referenceExpression, 0, consumer);
        if (!consumer.getResult().isEmpty())
            return false;
    }
    return true;
}
Also used : PomTarget(com.intellij.pom.PomTarget) CollectConsumer(com.intellij.util.CollectConsumer) PomDeclarationSearcher(com.intellij.pom.PomDeclarationSearcher)

Example 4 with PomTarget

use of com.intellij.pom.PomTarget 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 5 with PomTarget

use of com.intellij.pom.PomTarget 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

PomTarget (com.intellij.pom.PomTarget)10 PomTargetPsiElement (com.intellij.pom.PomTargetPsiElement)6 Nullable (org.jetbrains.annotations.Nullable)4 PsiElement (com.intellij.psi.PsiElement)3 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)2 TextRange (com.intellij.openapi.util.TextRange)2 PomDeclarationSearcher (com.intellij.pom.PomDeclarationSearcher)2 PsiDeclaredTarget (com.intellij.pom.PsiDeclaredTarget)2 DomElement (com.intellij.util.xml.DomElement)2 ResourceUrl (com.android.ide.common.resources.ResourceUrl)1 ResourceType (com.android.resources.ResourceType)1 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 AntDomElement (com.intellij.lang.ant.dom.AntDomElement)1