Search in sources :

Example 91 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class AntReferenceInjector method inject.

@NotNull
public PsiReference[] inject(@Nullable String unresolvedText, @NotNull PsiElement element, @NotNull ConvertContext context) {
    if (element instanceof XmlAttributeValue) {
        final XmlAttributeValue xmlAttributeValue = (XmlAttributeValue) element;
        final List<PsiReference> refs = PsiReferenceListSpinAllocator.alloc();
        try {
            addPropertyReferences(context, xmlAttributeValue, refs);
            addMacrodefParameterRefs(xmlAttributeValue, refs);
            return refs.size() == 0 ? PsiReference.EMPTY_ARRAY : ContainerUtil.toArray(refs, new PsiReference[refs.size()]);
        } finally {
            PsiReferenceListSpinAllocator.dispose(refs);
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) NotNull(org.jetbrains.annotations.NotNull)

Example 92 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class InspectionDescriptionInfo method doFindExtension.

@Nullable
private static Extension doFindExtension(Module module, PsiClass psiClass) {
    // Try search in narrow scopes first
    Project project = module.getProject();
    Set<DomFileElement<IdeaPlugin>> processed = new HashSet<>();
    for (GlobalSearchScope scope : DescriptionCheckerUtil.searchScopes(module)) {
        List<DomFileElement<IdeaPlugin>> origElements = DomService.getInstance().getFileElements(IdeaPlugin.class, project, scope);
        origElements.removeAll(processed);
        List<DomFileElement<IdeaPlugin>> elements = PluginDescriptorChooser.findAppropriateIntelliJModule(module.getName(), origElements);
        Query<PsiReference> query = ReferencesSearch.search(psiClass, new LocalSearchScope(elements.stream().map(DomFileElement::getFile).toArray(PsiElement[]::new)));
        Ref<Extension> result = Ref.create(null);
        query.forEach(ref -> {
            PsiElement element = ref.getElement();
            if (element instanceof XmlAttributeValue) {
                PsiElement parent = element.getParent();
                if (parent instanceof XmlAttribute && "implementationClass".equals(((XmlAttribute) parent).getName())) {
                    DomElement domElement = DomUtil.getDomElement(parent.getParent());
                    if (domElement instanceof Extension) {
                        Extension extension = (Extension) domElement;
                        ExtensionPoint extensionPoint = extension.getExtensionPoint();
                        if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), InspectionEP.class.getName())) {
                            result.set(extension);
                            return false;
                        }
                    }
                }
            }
            return true;
        });
        Extension extension = result.get();
        if (extension != null)
            return extension;
        processed.addAll(origElements);
    }
    return null;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ExtensionPoint(org.jetbrains.idea.devkit.dom.ExtensionPoint) DomFileElement(com.intellij.util.xml.DomFileElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Extension(org.jetbrains.idea.devkit.dom.Extension) Project(com.intellij.openapi.project.Project) DomElement(com.intellij.util.xml.DomElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 93 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class ResolveUtil method resolveFile.

@Nullable
public static PsiFile resolveFile(XmlAttribute location, PsiFile baseFile) {
    if (location == null)
        return null;
    final XmlAttributeValue valueElement = location.getValueElement();
    if (valueElement == null)
        return null;
    // prefer direct relative path
    final String value = valueElement.getValue();
    final PsiFile file = resolveFile(value, baseFile);
    if (file != baseFile && file instanceof XmlFile) {
        return file;
    }
    final PsiReference[] references = valueElement.getReferences();
    for (PsiReference reference : references) {
        final PsiElement target = reference.resolve();
        if (target == null && reference instanceof PsiPolyVariantReference) {
            final ResolveResult[] results = ((PsiPolyVariantReference) reference).multiResolve(false);
            for (ResolveResult result : results) {
                if (result.isValidResult()) {
                    // TODO: how to weigh/prioritize the results? 
                    final PsiElement element = result.getElement();
                    if (element != baseFile && element instanceof XmlFile) {
                        return (PsiFile) target;
                    }
                }
            }
        } else if (target != baseFile && target instanceof XmlFile) {
            return (PsiFile) target;
        }
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Nullable(org.jetbrains.annotations.Nullable)

Example 94 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class TemplateInvocationInspection method checkTemplateInvocation.

private static void checkTemplateInvocation(XsltTemplateInvocation call, ProblemsHolder holder, boolean onTheFly) {
    final XsltWithParam[] arguments = call.getArguments();
    final Map<String, XsltWithParam> argNames = new HashMap<>();
    for (XsltWithParam arg : arguments) {
        final XmlAttribute attr = arg.getNameAttribute();
        if (attr != null) {
            final String name = attr.getValue();
            if (argNames.containsKey(name)) {
                final PsiElement token = arg.getNameIdentifier();
                assert token != null;
                holder.registerProblem(token, "Duplicate Argument '" + name + "'");
            }
            argNames.put(name, arg);
        }
    }
    if (call instanceof XsltCallTemplate) {
        final XsltCallTemplate ct = ((XsltCallTemplate) call);
        final PsiElement nameToken = ct.getNameIdentifier();
        final XsltTemplate template = ct.getTemplate();
        if (template != null) {
            if (nameToken != null) {
                final XsltParameter[] parameters = template.getParameters();
                for (XsltParameter parameter : parameters) {
                    if (!argNames.containsKey(parameter.getName()) && !parameter.hasDefault()) {
                        final LocalQuickFix fix = new AddWithParamFix(parameter, call.getTag()).createQuickFix(onTheFly);
                        holder.registerProblem(nameToken, "Missing template parameter: " + parameter.getName(), AbstractFix.createFixes(fix));
                    }
                }
            }
            for (String s : argNames.keySet()) {
                final XmlAttribute argAttribute = argNames.get(s).getNameAttribute();
                assert argAttribute != null;
                final XmlAttributeValue valueElement = argAttribute.getValueElement();
                final PsiElement valueToken = XsltSupport.getAttValueToken(argAttribute);
                if (valueToken != null && s.trim().length() > 0) {
                    if (template.getParameter(s) == null) {
                        final LocalQuickFix fix1 = new AddParameterFix(s, template).createQuickFix(onTheFly);
                        final LocalQuickFix fix2 = new RemoveParamFix(argNames.get(s).getTag(), s).createQuickFix(onTheFly);
                        holder.registerProblem(valueToken, "Undeclared template parameter: " + s, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, AbstractFix.createFixes(fix1, fix2));
                    }
                } else if (valueElement != null) {
                    holder.registerProblem(valueElement, "Parameter name expected");
                }
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) RemoveParamFix(org.intellij.lang.xpath.xslt.quickfix.RemoveParamFix) HashMap(java.util.HashMap) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AddParameterFix(org.intellij.lang.xpath.xslt.quickfix.AddParameterFix) AddWithParamFix(org.intellij.lang.xpath.xslt.quickfix.AddWithParamFix) PsiElement(com.intellij.psi.PsiElement)

Example 95 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class PropertiesReferenceProvider method getReferencesByElement.

@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
    Object value = null;
    String bundleName = null;
    boolean propertyRefWithPrefix = false;
    boolean soft = myDefaultSoft;
    if (element instanceof PsiLiteralExpression) {
        PsiLiteralExpression literalExpression = (PsiLiteralExpression) element;
        value = literalExpression.getValue();
        final Map<String, Object> annotationParams = new HashMap<>();
        annotationParams.put(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER, null);
        if (JavaI18nUtil.mustBePropertyKey(literalExpression, annotationParams)) {
            soft = false;
            final Object resourceBundleName = annotationParams.get(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER);
            if (resourceBundleName instanceof PsiExpression) {
                PsiExpression expr = (PsiExpression) resourceBundleName;
                final Object bundleValue = JavaPsiFacade.getInstance(expr.getProject()).getConstantEvaluationHelper().computeConstantExpression(expr);
                bundleName = bundleValue == null ? null : bundleValue.toString();
            }
        }
    } else if (element instanceof XmlAttributeValue && isNonDynamicAttribute(element)) {
        if (element.getTextLength() < 2) {
            return PsiReference.EMPTY_ARRAY;
        }
        value = ((XmlAttributeValue) element).getValue();
        final XmlAttribute attribute = (XmlAttribute) element.getParent();
        if ("key".equals(attribute.getName())) {
            final XmlTag parent = attribute.getParent();
            if ("message".equals(parent.getLocalName()) && Arrays.binarySearch(XmlUtil.JSTL_FORMAT_URIS, parent.getNamespace()) >= 0) {
                propertyRefWithPrefix = true;
            }
        }
    }
    if (value instanceof String) {
        String text = (String) value;
        PsiReference reference = propertyRefWithPrefix ? new PrefixBasedPropertyReference(text, element, null, soft) : new PropertyReference(text, element, bundleName, soft);
        return new PsiReference[] { reference };
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) HashMap(java.util.HashMap) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PropertyReference(com.intellij.lang.properties.references.PropertyReference) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5