use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class XmlElementImpl method getNavigationElement.
@Override
@NotNull
public PsiElement getNavigationElement() {
if (!isPhysical()) {
final XmlElement including = getUserData(INCLUDING_ELEMENT);
if (including != null) {
return including;
}
PsiElement astParent = getAstParent();
PsiElement parentNavigation = astParent.getNavigationElement();
if (parentNavigation.getTextOffset() == getTextOffset())
return parentNavigation;
return this;
}
return super.getNavigationElement();
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class XmlEnumeratedTypeImpl method getEnumeratedValues.
@Override
public XmlElement[] getEnumeratedValues() {
final List<XmlElement> result = new ArrayList<>();
processElements(new FilterElementProcessor(new XmlTokenTypeFilter(XmlTokenType.XML_NAME), result), this);
return result.toArray(new XmlElement[result.size()]);
}
use of com.intellij.psi.xml.XmlElement in project intellij-community by JetBrains.
the class AddSchemaPrefixIntention method invoke.
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final XmlAttribute xmlns = getXmlnsDeclaration(element);
if (xmlns == null)
return;
final String namespace = xmlns.getValue();
final XmlTag tag = xmlns.getParent();
if (tag != null) {
final Set<String> ns = tag.getLocalNamespaceDeclarations().keySet();
final String nsPrefix = Messages.showInputDialog(project, "Namespace Prefix:", StringUtil.capitalize(NAME), Messages.getInformationIcon(), "", new InputValidator() {
@Override
public boolean checkInput(String inputString) {
return !ns.contains(inputString);
}
@Override
public boolean canClose(String inputString) {
return checkInput(inputString);
}
});
if (nsPrefix == null)
return;
final List<XmlTag> tags = new ArrayList<>();
final List<XmlAttributeValue> values = new ArrayList<>();
new WriteCommandAction(project, NAME, tag.getContainingFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
tag.accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
if (tag.getNamespace().equals(namespace) && tag.getNamespacePrefix().isEmpty()) {
tags.add(tag);
}
super.visitXmlTag(tag);
}
@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
PsiReference ref = null;
boolean skip = false;
for (PsiReference reference : value.getReferences()) {
if (reference instanceof TypeOrElementOrAttributeReference) {
ref = reference;
} else if (reference instanceof SchemaPrefixReference) {
skip = true;
break;
}
}
if (!skip && ref != null) {
final PsiElement xmlElement = ref.resolve();
if (xmlElement instanceof XmlElement) {
final XmlTag tag = PsiTreeUtil.getParentOfType(xmlElement, XmlTag.class, false);
if (tag != null) {
if (tag.getNamespace().equals(namespace)) {
if (ref.getRangeInElement().getLength() == value.getValue().length()) {
//no ns prefix
values.add(value);
}
}
}
}
}
}
});
for (XmlAttributeValue value : values) {
((XmlAttribute) value.getParent()).setValue(nsPrefix + ":" + value.getValue());
}
for (XmlTag xmlTag : tags) {
xmlTag.setName(nsPrefix + ":" + xmlTag.getLocalName());
}
xmlns.setName("xmlns:" + nsPrefix);
}
}.execute();
}
}
use of com.intellij.psi.xml.XmlElement in project android by JetBrains.
the class AttributeProcessingUtil method isLayoutAttributeRequired.
/**
* Check whether layout tag attribute with given name should be marked as required.
* Currently, tests for layout_width and layout_height attribute and marks them as required in appropriate context.
*/
public static boolean isLayoutAttributeRequired(@NotNull XmlName attributeName, @NotNull DomElement element) {
// Mark layout_width and layout_height required - if the context calls for it
String localName = attributeName.getLocalName();
if (!(ATTR_LAYOUT_WIDTH.equals(localName) || ATTR_LAYOUT_HEIGHT.equals(localName))) {
return false;
}
if ((element instanceof LayoutViewElement || element instanceof Fragment) && NS_RESOURCES.equals(attributeName.getNamespaceKey())) {
XmlElement xmlElement = element.getXmlElement();
XmlTag tag = xmlElement instanceof XmlTag ? (XmlTag) xmlElement : null;
String tagName = tag != null ? tag.getName() : null;
if (!SIZE_NOT_REQUIRED_TAG_NAMES.contains(tagName) && (tag == null || tag.getAttribute(ATTR_STYLE) == null)) {
XmlTag parentTag = tag != null ? tag.getParentTag() : null;
String parentTagName = parentTag != null ? parentTag.getName() : null;
if (!SIZE_NOT_REQUIRED_PARENT_TAG_NAMES.contains(parentTagName)) {
return true;
}
}
}
return false;
}
use of com.intellij.psi.xml.XmlElement 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);
}
}
}
Aggregations