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());
}
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);
}
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);
}
}
}
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();
}
}
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;
}
Aggregations