use of com.intellij.psi.XmlRecursiveElementVisitor 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.XmlRecursiveElementVisitor in project intellij-community by JetBrains.
the class ConvertSchemaPrefixToDefaultIntention method invoke.
@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final XmlAttribute xmlns = getXmlnsDeclaration(element);
if (xmlns == null)
return;
SchemaPrefixReference prefixRef = null;
for (PsiReference ref : xmlns.getReferences()) {
if (ref instanceof SchemaPrefixReference) {
prefixRef = (SchemaPrefixReference) ref;
break;
}
}
if (prefixRef == null)
return;
final SchemaPrefix prefix = prefixRef.resolve();
final String ns = prefixRef.getNamespacePrefix();
final ArrayList<XmlTag> tags = new ArrayList<>();
final ArrayList<XmlAttribute> attrs = new ArrayList<>();
xmlns.getParent().accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
if (ns.equals(tag.getNamespacePrefix())) {
tags.add(tag);
}
super.visitXmlTag(tag);
}
@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
if (value.getValue().startsWith(ns + ":")) {
for (PsiReference ref : value.getReferences()) {
if (ref instanceof SchemaPrefixReference && ref.isReferenceTo(prefix)) {
attrs.add((XmlAttribute) value.getParent());
}
}
}
}
});
if (!FileModificationService.getInstance().preparePsiElementsForWrite(xmlns.getContainingFile()))
return;
CommandProcessor.getInstance().executeCommand(project, () -> {
convertTagsAndAttributes(ns, tags, attrs, project);
ApplicationManager.getApplication().runWriteAction(() -> {
xmlns.setName("xmlns");
});
}, NAME, null);
new WriteCommandAction(project, NAME, xmlns.getContainingFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
xmlns.setName("xmlns");
}
}.execute();
}
use of com.intellij.psi.XmlRecursiveElementVisitor in project android by JetBrains.
the class DeepLinkChooserDialog method searchXmlTagsByName.
@NotNull
private static List<XmlTag> searchXmlTagsByName(@NotNull XmlTag root, @NotNull final String tagName) {
final List<XmlTag> tags = Lists.newArrayList();
root.accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
super.visitXmlTag(tag);
if (tag.getName().equalsIgnoreCase(tagName)) {
tags.add(tag);
}
}
});
return tags;
}
Aggregations