Search in sources :

Example 51 with XmlAttributeValue

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

the class StateWriter method readDeclaration.

public void readDeclaration(XmlTag parentTag) {
    initNames();
    XmlTag[] tags = parentTag.getSubTags();
    states.ensureCapacity(tags.length);
    for (XmlTag tag : tags) {
        State state = new State(this, states.size());
        states.add(state);
        for (XmlAttribute attribute : tag.getAttributes()) {
            if (attribute.getLocalName().equals(FlexStateElementNames.NAME)) {
                state.name = attribute.getDisplayValue();
                addNameToStateMap(state.name, state);
            } else if (attribute.getLocalName().equals(FlexStateElementNames.STATE_GROUPS)) {
                XmlAttributeValue valueElement = attribute.getValueElement();
                assert valueElement != null;
                for (PsiReference reference : valueElement.getReferences()) {
                    addNameToStateMap(reference.getCanonicalText(), state);
                }
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 52 with XmlAttributeValue

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

the class AndroidLintInvalidUsesTagAttributeInspection method getQuickFixes.

@NotNull
@Override
public AndroidLintQuickFix[] getQuickFixes(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull String message) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(startElement, XmlAttribute.class);
    XmlAttributeValue attributeValue = attribute == null ? null : attribute.getValueElement();
    if (attributeValue != null && attributeValue.getTextLength() != 0) {
        String value = StringUtil.unquoteString(attributeValue.getText());
        String regexp = "(" + value + ")";
        String[] suggestions = AndroidAutoDetector.getAllowedAutomotiveAppTypes();
        List<AndroidLintQuickFix> fixes = Lists.newArrayListWithExpectedSize(suggestions.length);
        for (String suggestion : suggestions) {
            fixes.add(new ReplaceStringQuickFix("Replace with \"" + suggestion + "\"", regexp, suggestion));
        }
        return fixes.toArray(new AndroidLintQuickFix[fixes.size()]);
    } else {
        return AndroidLintQuickFix.EMPTY_ARRAY;
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) AndroidLintQuickFix(org.jetbrains.android.inspections.lint.AndroidLintQuickFix) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) NotNull(org.jetbrains.annotations.NotNull)

Example 53 with XmlAttributeValue

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

the class StringResourceSafeDeleteProcessorDelegate method getElementsToSearch.

@NotNull
@Override
public Collection<? extends PsiElement> getElementsToSearch(@NotNull PsiElement element, @Nullable Module module, @NotNull Collection<PsiElement> elementsToDelete) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        XmlAttribute attribute = tag.getAttribute(ATTR_NAME);
        assert attribute != null;
        Collection<PsiElement> elements = new ArrayList<>();
        // Find usages of the string element's name attribute value, such as @string/string_name references in XML files
        elements.add(attribute.getValueElement());
        // R.string.string_name references in Java files that are not LightElements
        Collection<PsiField> fields = Arrays.stream(AndroidResourceUtil.findResourceFieldsForValueResource(tag, true)).filter(field -> !(field instanceof LightElement)).collect(Collectors.toList());
        elements.addAll(fields);
        return elements;
    } else if (element instanceof XmlAttributeValue) {
        return getElementsToSearch(element.getParent().getParent(), module, elementsToDelete);
    } else {
        return Collections.emptyList();
    }
}
Also used : java.util(java.util) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ATTR_NAME(com.android.SdkConstants.ATTR_NAME) LightElement(com.intellij.psi.impl.light.LightElement) UsageInfo(com.intellij.usageView.UsageInfo) TAG_STRING(com.android.SdkConstants.TAG_STRING) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) SafeDeleteProcessorDelegateBase(com.intellij.refactoring.safeDelete.SafeDeleteProcessorDelegateBase) PsiElement(com.intellij.psi.PsiElement) AndroidResourceUtil(org.jetbrains.android.util.AndroidResourceUtil) Project(com.intellij.openapi.project.Project) PsiField(com.intellij.psi.PsiField) PsiNamedElement(com.intellij.psi.PsiNamedElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) SafeDeleteProcessor(com.intellij.refactoring.safeDelete.SafeDeleteProcessor) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) XmlTag(com.intellij.psi.xml.XmlTag) NonCodeUsageSearchInfo(com.intellij.refactoring.safeDelete.NonCodeUsageSearchInfo) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiField(com.intellij.psi.PsiField) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) LightElement(com.intellij.psi.impl.light.LightElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with XmlAttributeValue

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

the class AndroidResourceRenameResourceProcessor method getResourceName.

/** Looks up the resource name for the given refactored element. Uses the same
   * instanceof chain checkups as is done in {@link #canProcessElement} */
@Nullable
private static String getResourceName(PsiElement originalElement) {
    PsiElement element = LazyValueResourceElementWrapper.computeLazyElement(originalElement);
    if (element == null) {
        return null;
    }
    if (element instanceof PsiFile) {
        PsiFile file = (PsiFile) element;
        LocalResourceManager manager = LocalResourceManager.getInstance(element);
        if (manager != null) {
            String type = manager.getFileResourceType(file);
            if (type != null) {
                String name = file.getName();
                return AndroidCommonUtils.getResourceName(type, name);
            }
        }
        return LintUtils.getBaseName(file.getName());
    } else if (element instanceof PsiField) {
        PsiField field = (PsiField) element;
        return field.getName();
    } else if (element instanceof XmlAttributeValue) {
        if (AndroidResourceUtil.isIdDeclaration((XmlAttributeValue) element)) {
            return AndroidResourceUtil.getResourceNameByReferenceText(((XmlAttributeValue) element).getValue());
        }
        XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
        if (tag != null) {
            DomElement domElement = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement) {
                return ((ResourceElement) domElement).getName().getValue();
            }
        }
    }
    return null;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) LocalResourceManager(org.jetbrains.android.resourceManagers.LocalResourceManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with XmlAttributeValue

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

the class AndroidResourceRenameResourceProcessor method prepareRenaming.

@Override
public void prepareRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames) {
    final PsiElement element1 = LazyValueResourceElementWrapper.computeLazyElement(element);
    if (element1 == null) {
        return;
    }
    // TODO: support renaming alternative value resources
    AndroidFacet facet = AndroidFacet.getInstance(element1);
    assert facet != null;
    if (element1 instanceof PsiFile) {
        prepareResourceFileRenaming((PsiFile) element1, newName, allRenames, facet);
    } else if (element1 instanceof PsiClass) {
        PsiClass cls = (PsiClass) element1;
        if (AndroidDomUtil.isInheritor(cls, CLASS_VIEW)) {
            prepareCustomViewRenaming(cls, newName, allRenames, facet);
        }
    } else if (element1 instanceof XmlAttributeValue) {
        XmlAttributeValue value = (XmlAttributeValue) element1;
        if (AndroidResourceUtil.isIdDeclaration(value)) {
            prepareIdRenaming(value, newName, allRenames, facet);
        } else {
            prepareValueResourceRenaming(element1, newName, allRenames, facet);
        }
    } else if (element1 instanceof PsiField) {
        prepareResourceFieldRenaming((PsiField) element1, newName, allRenames);
    }
}
Also used : XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

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