use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class DataBindingUtil method getBindingExprDefault.
@Nullable
public static String getBindingExprDefault(@NotNull XmlAttribute psiAttribute) {
XmlAttributeValue attrValue = psiAttribute.getValueElement();
if (attrValue instanceof PsiLanguageInjectionHost) {
final Ref<PsiElement> injections = Ref.create();
InjectedLanguageUtil.enumerate(attrValue, (injectedPsi, places) -> {
if (injectedPsi instanceof DbFile) {
injections.set(injectedPsi);
}
});
if (injections.get() != null) {
PsiDbDefaults defaults = PsiTreeUtil.getChildOfType(injections.get(), PsiDbDefaults.class);
if (defaults != null) {
PsiDbConstantValue constantValue = defaults.getConstantValue();
ASTNode stringLiteral = constantValue.getNode().findChildByType(DbTokenTypes.STRING_LITERAL);
if (stringLiteral == null) {
return constantValue.getText();
} else {
String text = stringLiteral.getText();
if (text.length() > 1) {
// return unquoted string literal.
return text.substring(1, text.length() - 1);
} else {
return text;
}
}
}
}
}
return null;
}
use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class ThemeEditorStyle method getNamePsiElement.
/**
* Returns a PsiElement of the name attribute for this theme
* made from a RANDOM sourceXml
*/
@Nullable
public PsiElement getNamePsiElement() {
Collection<ResourceItem> resources = getStyleResourceItems();
if (resources.isEmpty()) {
return null;
}
// Any sourceXml will do to get the name attribute from
final XmlTag sourceXml = LocalResourceRepository.getItemTag(myManager.getProject(), resources.iterator().next());
assert sourceXml != null;
final XmlAttribute nameAttribute = sourceXml.getAttribute("name");
if (nameAttribute == null) {
return null;
}
XmlAttributeValue attributeValue = nameAttribute.getValueElement();
if (attributeValue == null) {
return null;
}
return new ValueResourceElementWrapper(attributeValue);
}
use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareValueResourceRenaming.
private static void prepareValueResourceRenaming(PsiElement element, String newName, Map<PsiElement, String> allRenames, final AndroidFacet facet) {
ResourceManager manager = facet.getLocalResourceManager();
XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
assert tag != null;
String type = manager.getValueResourceType(tag);
assert type != null;
Project project = tag.getProject();
DomElement domElement = DomManager.getDomManager(project).getDomElement(tag);
assert domElement instanceof ResourceElement;
String name = ((ResourceElement) domElement).getName().getValue();
assert name != null;
List<ResourceElement> resources = manager.findValueResources(type, name);
for (ResourceElement resource : resources) {
XmlElement xmlElement = resource.getName().getXmlAttributeValue();
if (!element.getManager().areElementsEquivalent(element, xmlElement)) {
allRenames.put(xmlElement, newName);
}
}
if (getResourceType(element) == ResourceType.STYLE) {
// For styles, try also to find child styles defined by name (i.e. "ParentName.StyleName") and add them
// to the rename list. This will allow the rename processor to also handle the references to those. For example,
// If you rename "MyTheme" and your manifest theme is "MyTheme.NoActionBar", this will make sure that
// the reference from the manifest is also updated by adding "MyTheme.NoActionBar" to the rename list.
// We iterate the styles in order to cascade any changes to children down the hierarchy.
// List of styles that will be renamed.
HashSet<String> renamedStyles = Sets.newHashSet();
renamedStyles.add(name);
final String stylePrefix = name + ".";
Collection<String> renameCandidates;
ResourceType resourceType = ResourceType.getEnum(type);
if (resourceType == null) {
renameCandidates = Collections.emptyList();
} else {
renameCandidates = Collections2.filter(manager.getResourceNames(resourceType), new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith(stylePrefix);
}
});
}
for (String resourceName : ORDER_BY_LENGTH.sortedCopy(renameCandidates)) {
// resourceName.lastIndexOf will never return -1 because we've filtered all names that
// do not contain stylePrefix
String parentName = resourceName.substring(0, resourceName.lastIndexOf('.'));
if (!renamedStyles.contains(parentName)) {
// This resource's parent wasn't affected by the rename
continue;
}
for (ResourceElement resource : manager.findValueResources(type, resourceName)) {
if (!(resource instanceof Style) || ((Style) resource).getParentStyle().getXmlAttributeValue() != null) {
// This element is not a style or does have an explicit parent so we do not rename it.
continue;
}
XmlAttributeValue xmlElement = resource.getName().getXmlAttributeValue();
if (xmlElement != null) {
String newStyleName = newName + StringUtil.trimStart(resourceName, name);
allRenames.put(new ValueResourceElementWrapper(xmlElement), newStyleName);
renamedStyles.add(resourceName);
}
}
}
}
PsiField[] resFields = AndroidResourceUtil.findResourceFieldsForValueResource(tag, false);
for (PsiField resField : resFields) {
String escaped = AndroidResourceUtil.getFieldNameByResourceName(newName);
allRenames.put(resField, escaped);
}
// Also rename the dependent fields, e.g. if you rename <declare-styleable name="Foo">,
// we have to rename not just R.styleable.Foo but the also R.styleable.Foo_* attributes
PsiField[] styleableFields = AndroidResourceUtil.findStyleableAttributeFields(tag, false);
if (styleableFields.length > 0) {
String tagName = tag.getName();
boolean isDeclareStyleable = tagName.equals(TAG_DECLARE_STYLEABLE);
boolean isAttr = !isDeclareStyleable && tagName.equals(TAG_ATTR) && tag.getParentTag() != null;
assert isDeclareStyleable || isAttr;
String style = isAttr ? tag.getParentTag().getAttributeValue(ATTR_NAME) : null;
for (PsiField resField : styleableFields) {
String fieldName = resField.getName();
String newAttributeName;
if (isDeclareStyleable && fieldName.startsWith(name)) {
newAttributeName = newName + fieldName.substring(name.length());
} else if (isAttr && style != null) {
newAttributeName = style + '_' + newName;
} else {
newAttributeName = name;
}
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 renameElement.
@Override
public void renameElement(PsiElement element, final String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
if (element instanceof PsiField) {
new RenameJavaVariableProcessor().renameElement(element, newName, usages, listener);
} else {
if (element instanceof PsiNamedElement) {
super.renameElement(element, newName, usages, listener);
if (element instanceof PsiFile) {
VirtualFile virtualFile = ((PsiFile) element).getVirtualFile();
if (virtualFile != null && !LocalHistory.getInstance().isUnderControl(virtualFile)) {
DocumentReference ref = DocumentReferenceManager.getInstance().create(virtualFile);
UndoManager.getInstance(element.getProject()).nonundoableActionPerformed(ref, false);
}
}
} else if (element instanceof XmlAttributeValue) {
new RenameXmlAttributeProcessor().renameElement(element, newName, usages, listener);
}
}
}
use of com.intellij.psi.xml.XmlAttributeValue in project android by JetBrains.
the class AndroidResourceRenameResourceProcessor method prepareIdRenaming.
private static void prepareIdRenaming(XmlAttributeValue value, String newName, Map<PsiElement, String> allRenames, AndroidFacet facet) {
LocalResourceManager manager = facet.getLocalResourceManager();
allRenames.remove(value);
String id = AndroidResourceUtil.getResourceNameByReferenceText(value.getValue());
assert id != null;
List<XmlAttributeValue> idDeclarations = manager.findIdDeclarations(id);
for (XmlAttributeValue idDeclaration : idDeclarations) {
// framework which looks related to elements getting modified multiple times.
if (!ATTR_ID.equals(((XmlAttribute) idDeclaration.getParent()).getLocalName())) {
continue;
}
allRenames.put(new ValueResourceElementWrapper(idDeclaration), newName);
}
String name = AndroidResourceUtil.getResourceNameByReferenceText(newName);
if (name != null) {
for (PsiField resField : AndroidResourceUtil.findIdFields(value)) {
allRenames.put(resField, AndroidResourceUtil.getFieldNameByResourceName(name));
}
}
}
Aggregations