Search in sources :

Example 91 with XmlAttribute

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

the class AddMissingPrefixQuickFix method apply.

@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(startElement, XmlAttribute.class, false);
    if (attribute == null) {
        return;
    }
    final XmlTag tag = attribute.getParent();
    if (tag == null) {
        LOG.debug("tag is null");
        return;
    }
    String androidNsPrefix = tag.getPrefixByNamespace(SdkConstants.NS_RESOURCES);
    if (androidNsPrefix == null) {
        final PsiFile file = tag.getContainingFile();
        final XmlNamespaceHelper extension = XmlNamespaceHelper.getHelper(file);
        if (extension == null) {
            LOG.debug("Cannot get XmlNamespaceHelper for file + " + file);
            return;
        }
        if (!(file instanceof XmlFile)) {
            LOG.debug(file + " is not XmlFile");
            return;
        }
        final XmlFile xmlFile = (XmlFile) file;
        final String defaultPrefix = "android";
        extension.insertNamespaceDeclaration(xmlFile, null, Collections.singleton(SdkConstants.NS_RESOURCES), defaultPrefix, null);
        androidNsPrefix = defaultPrefix;
    }
    attribute.setName(androidNsPrefix + ':' + attribute.getLocalName());
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) PsiFile(com.intellij.psi.PsiFile) XmlTag(com.intellij.psi.xml.XmlTag)

Example 92 with XmlAttribute

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

the class AndroidAddStringResourceQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    String defaultName = null;
    final PsiElement parent = myStartElement.getParent();
    if (parent instanceof XmlAttribute) {
        final String value = ((XmlAttribute) parent).getValue();
        if (value != null) {
            defaultName = buildResourceName(value);
        }
    }
    invokeIntention(project, editor, file, defaultName);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiElement(com.intellij.psi.PsiElement)

Example 93 with XmlAttribute

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

the class ConvertToDpQuickFix method apply.

@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
    if (context instanceof AndroidQuickfixContexts.BatchContext) {
        return;
    }
    final XmlTag tag = PsiTreeUtil.getParentOfType(startElement, XmlTag.class);
    final List<Density> densities = new ArrayList<Density>();
    for (Density density : Density.values()) {
        if (density.getDpiValue() > 0) {
            densities.add(density);
        }
    }
    final String[] densityPresentableNames = new String[densities.size()];
    String defaultValue = null;
    String initialValue = null;
    for (int i = 0; i < densities.size(); i++) {
        final Density density = densities.get(i);
        densityPresentableNames[i] = getLabelForDensity(density);
        final int dpi = density.getDpiValue();
        if (dpi == 0) {
            continue;
        }
        if (dpi == ourPrevDpi) {
            initialValue = densityPresentableNames[i];
        } else if (dpi == Density.DEFAULT_DENSITY) {
            defaultValue = densityPresentableNames[i];
        }
    }
    if (initialValue == null) {
        initialValue = defaultValue;
    }
    if (initialValue == null) {
        return;
    }
    final int dpi;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        dpi = Density.DEFAULT_DENSITY;
    } else {
        final int selectedIndex = Messages.showChooseDialog("What is the screen density the current px value works with?", "Choose density", densityPresentableNames, initialValue, null);
        if (selectedIndex < 0) {
            return;
        }
        dpi = densities.get(selectedIndex).getDpiValue();
    }
    //noinspection AssignmentToStaticFieldFromInstanceMethod
    ourPrevDpi = dpi;
    for (XmlAttribute attribute : tag.getAttributes()) {
        final String value = attribute.getValue();
        if (value != null && value.endsWith("px")) {
            final String newValue = convertToDp(value, dpi);
            if (newValue != null) {
                attribute.setValue(newValue);
            }
        }
    }
    final XmlTagValue tagValueElement = tag.getValue();
    final String tagValue = tagValueElement.getText();
    if (tagValue.endsWith("px")) {
        final String newValue = convertToDp(tagValue, dpi);
        if (newValue != null) {
            tagValueElement.setText(newValue);
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlTagValue(com.intellij.psi.xml.XmlTagValue) ArrayList(java.util.ArrayList) Density(com.android.resources.Density) XmlTag(com.intellij.psi.xml.XmlTag)

Example 94 with XmlAttribute

use of com.intellij.psi.xml.XmlAttribute 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 95 with XmlAttribute

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

the class ManifestUtils method findTag.

@Nullable
private static XmlTag findTag(@NotNull XmlTag[] manifestTags, @NotNull Element tag) {
    Attr nameAttribute = tag.getAttributeNodeNS(ANDROID_URI, ATTR_NAME);
    String name = nameAttribute == null ? null : nameAttribute.getValue();
    for (XmlTag xmlTag : manifestTags) {
        if (tag.getTagName().equals(xmlTag.getName())) {
            if (name != null) {
                XmlAttribute xmlAttribute = xmlTag.getAttribute(ATTR_NAME, ANDROID_URI);
                if (xmlAttribute != null && name.equals(xmlAttribute.getValue())) {
                    return xmlTag;
                }
            } else {
                return xmlTag;
            }
        }
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlAttribute (com.intellij.psi.xml.XmlAttribute)220 XmlTag (com.intellij.psi.xml.XmlTag)116 PsiElement (com.intellij.psi.PsiElement)60 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)57 NotNull (org.jetbrains.annotations.NotNull)44 XmlFile (com.intellij.psi.xml.XmlFile)37 Nullable (org.jetbrains.annotations.Nullable)27 Project (com.intellij.openapi.project.Project)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 TextRange (com.intellij.openapi.util.TextRange)18 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)18 ArrayList (java.util.ArrayList)18 PsiFile (com.intellij.psi.PsiFile)17 PsiReference (com.intellij.psi.PsiReference)17 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 DomElement (com.intellij.util.xml.DomElement)10 Result (com.intellij.openapi.application.Result)9 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)9 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 XmlElement (com.intellij.psi.xml.XmlElement)6