use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareCustomViewRenaming.
private static void prepareCustomViewRenaming(PsiClass cls, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
AppResourceRepository appResources = AppResourceRepository.getAppResources(facet, true);
String oldName = cls.getName();
if (appResources.hasResourceItem(DECLARE_STYLEABLE, oldName)) {
LocalResourceManager manager = facet.getLocalResourceManager();
for (PsiElement element : manager.findResourcesByFieldName(STYLEABLE.getName(), oldName)) {
if (element instanceof XmlAttributeValue) {
if (element.getParent() instanceof XmlAttribute) {
XmlTag tag = ((XmlAttribute) element.getParent()).getParent();
String tagName = tag.getName();
if (tagName.equals(TAG_DECLARE_STYLEABLE)) {
// Rename main styleable field
for (PsiField field : AndroidResourceUtil.findResourceFields(facet, STYLEABLE.getName(), oldName, false)) {
String escaped = AndroidResourceUtil.getFieldNameByResourceName(newName);
allRenames.put(field, escaped);
}
// Rename dependent attribute fields
PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, false);
if (styleableFields.length > 0) {
for (PsiField resField : styleableFields) {
String fieldName = resField.getName();
String newAttributeName;
if (fieldName.startsWith(oldName)) {
newAttributeName = newName + fieldName.substring(oldName.length());
} else {
newAttributeName = oldName;
}
String escaped = AndroidResourceUtil.getFieldNameByResourceName(newAttributeName);
allRenames.put(resField, escaped);
}
}
}
}
}
}
}
}
use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareResourceFieldRenaming.
private static void prepareResourceFieldRenaming(PsiField field, String newName, Map<PsiElement, String> allRenames) {
new RenameJavaVariableProcessor().prepareRenaming(field, newName, allRenames);
List<PsiElement> resources = AndroidResourceUtil.findResourcesByField(field);
PsiElement res = resources.get(0);
final String newResName;
if (res instanceof PsiFile) {
// Resource comes from XML file, don't need to suggest change underscores to dots
newResName = newName;
} else if (res instanceof XmlAttributeValue) {
newResName = getResourceName(field.getProject(), newName, ((XmlAttributeValue) res).getValue());
} else {
// AndroidResourceUtil.findResourcesByField supposed to return a list of PsiElements that are
// either PsiFile or XmlAttributeValue. Previous version of this code doesn't handle other
// possibilities at all and would crash with ClassCastException, having an explicit error message
// seems to be a slightly better option.
Logger.getInstance(AndroidResourceRenameResourceProcessor.class).error(String.format("%s: res is neither PsiFile nor XmlAttributeValue", AndroidResourceRenameResourceProcessor.class.getSimpleName()));
newResName = newName;
}
for (PsiElement resource : resources) {
if (resource instanceof PsiFile) {
PsiFile file = (PsiFile) resource;
String extension = FileUtilRt.getExtension(file.getName());
allRenames.put(resource, newResName + '.' + extension);
} else if (resource instanceof XmlAttributeValue) {
XmlAttributeValue value = (XmlAttributeValue) resource;
final String s = AndroidResourceUtil.isIdDeclaration(value) ? NEW_ID_PREFIX + newResName : newResName;
allRenames.put(new ValueResourceElementWrapper(value), s);
// we have to rename not just R.styleable.Foo but the also R.styleable.Foo_* attributes
if (value.getParent() instanceof XmlAttribute) {
XmlAttribute parent = (XmlAttribute) value.getParent();
XmlTag tag = parent.getParent();
if (tag.getName().equals(TAG_DECLARE_STYLEABLE)) {
AndroidFacet facet = AndroidFacet.getInstance(tag);
String oldName = tag.getAttributeValue(ATTR_NAME);
if (facet != null && oldName != null) {
for (XmlTag attr : tag.getSubTags()) {
if (attr.getName().equals(TAG_ATTR)) {
String name = attr.getAttributeValue(ATTR_NAME);
if (name != null) {
String oldAttributeName = oldName + '_' + name;
PsiField[] fields = AndroidResourceUtil.findResourceFields(facet, STYLEABLE.getName(), oldAttributeName, true);
if (fields.length > 0) {
String newAttributeName = newName + '_' + name;
for (PsiField f : fields) {
allRenames.put(f, newAttributeName);
}
}
}
}
}
}
}
}
}
}
}
use of com.intellij.psi.xml.XmlAttributeValue in project idea-php-typo3-plugin by cedricziel.
the class TranslationFoldingBuilder method buildFoldRegions.
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
FoldingGroup group = FoldingGroup.newGroup("TYPO3Translation");
List<FoldingDescriptor> descriptors = new ArrayList<>();
Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);
for (final StringLiteralExpression literalExpression : literalExpressions) {
String value = literalExpression.getContents();
if (value.startsWith("LLL:")) {
Project project = literalExpression.getProject();
final List<StubTranslation> properties = TranslationIndex.findById(project, value);
StubTranslation defaultTranslation = findDefaultTranslationFromVariants(properties);
if (defaultTranslation != null) {
TextRange foldingRange = new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1);
descriptors.add(new FoldingDescriptor(literalExpression.getNode(), foldingRange, group) {
@Nullable
@Override
public String getPlaceholderText() {
PsiElement[] definitionElements = TranslationUtil.findDefinitionElements(project, value);
for (PsiElement definitionElement : definitionElements) {
if (definitionElement instanceof XmlTag) {
if (((XmlTag) definitionElement).getName().equals("label")) {
return ((XmlTag) definitionElement).getValue().getTrimmedText();
}
for (XmlTag xmlTag : ((XmlTag) definitionElement).getSubTags()) {
if (xmlTag.getName().equals("source")) {
return xmlTag.getValue().getTrimmedText();
}
}
}
if (definitionElement instanceof XmlAttributeValue) {
if (((XmlTag) definitionElement.getParent().getParent()).getName().equals("label")) {
return ((XmlTag) definitionElement).getValue().getTrimmedText();
}
for (XmlTag xmlTag : ((XmlTag) definitionElement.getParent().getParent()).getSubTags()) {
if (xmlTag.getName().equals("source")) {
return xmlTag.getValue().getTrimmedText();
}
}
}
}
return null;
}
});
}
}
}
return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
}
use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class AndroidColorAnnotator method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (element instanceof XmlTag) {
XmlTag tag = (XmlTag) element;
String tagName = tag.getName();
if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
String value = tag.getValue().getText().trim();
annotateXml(element, holder, value);
}
} else if (TAG_ITEM.equals(tagName)) {
XmlTagValue value = tag.getValue();
String text = value.getText();
annotateXml(element, holder, text);
}
} else if (element instanceof XmlAttributeValue) {
XmlAttributeValue v = (XmlAttributeValue) element;
String value = v.getValue();
if (value == null || value.isEmpty()) {
return;
}
annotateXml(element, holder, value);
} else if (element instanceof PsiReferenceExpression) {
ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
if (referenceType != ResourceReferenceType.NONE) {
// (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
// same expression, which would result in both elements getting annotated and the icon showing up
// in the gutter twice. Instead we only count the outer one.
ResourceType type = AndroidPsiUtils.getResourceType(element);
if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
String name = AndroidPsiUtils.getResourceName(element);
annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
}
}
}
}
use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class AndroidGotoRelatedProvider method findDeclarationInManifest.
@Nullable
private static GotoRelatedItem findDeclarationInManifest(@NotNull PsiClass psiClass) {
final AndroidAttributeValue<PsiClass> domAttrValue = AndroidDomUtil.findComponentDeclarationInManifest(psiClass);
if (domAttrValue == null) {
return null;
}
final XmlAttributeValue attrValue = domAttrValue.getXmlAttributeValue();
return attrValue != null ? new MyGotoManifestItem(attrValue) : null;
}
Aggregations