use of com.intellij.psi.XmlElementVisitor in project intellij-community by JetBrains.
the class HtmlLocalInspectionTool method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlToken(final XmlToken token) {
IElementType tokenType = token.getTokenType();
if (tokenType == XmlTokenType.XML_NAME || tokenType == XmlTokenType.XML_TAG_NAME) {
PsiElement element = token.getPrevSibling();
while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
PsiElement parent = element.getParent();
if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
XmlTag tag = (XmlTag) parent;
checkTag(tag, holder, isOnTheFly);
}
}
}
}
@Override
public void visitXmlAttribute(final XmlAttribute attribute) {
checkAttribute(attribute, holder, isOnTheFly);
}
};
}
use of com.intellij.psi.XmlElementVisitor in project intellij-community by JetBrains.
the class CheckEmptyTagInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlTag(final XmlTag tag) {
if (!isTagWithEmptyEndNotAllowed(tag)) {
return;
}
final ASTNode child = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(tag.getNode());
if (child == null) {
return;
}
final LocalQuickFix fix = new MyLocalQuickFix();
holder.registerProblem(tag, XmlBundle.message("html.inspections.check.empty.script.message"), tag.getContainingFile().getContext() != null ? ProblemHighlightType.INFORMATION : ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fix);
}
};
}
use of com.intellij.psi.XmlElementVisitor in project intellij-community by JetBrains.
the class XsltDeclarationInspection method buildVisitor.
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
if (!(holder.getFile() instanceof XmlFile))
return PsiElementVisitor.EMPTY_VISITOR;
return new XmlElementVisitor() {
@Override
public void visitXmlTag(final XmlTag tag) {
final XmlAttribute nameAttr = tag.getAttribute("name", null);
if (nameAttr == null || PsiTreeUtil.hasErrorElements(nameAttr))
return;
if (XsltSupport.isVariableOrParam(tag)) {
final XsltNamedElement instance = getXsltElementFactory().wrapElement(tag, XsltNamedElement.class);
checkDeclaration(instance, nameAttr.getValue(), holder);
} else if (XsltSupport.isTemplate(tag)) {
final XsltTemplate tmpl = getXsltElementFactory().wrapElement(tag, XsltTemplate.class);
checkDeclaration(tmpl, nameAttr.getValue(), holder);
}
}
private void checkDeclaration(final XsltNamedElement element, final String name, ProblemsHolder holder) {
final XmlTag tag = element.getTag();
final PsiElement token = element.getNameIdentifier();
if (name == null || name.length() == 0) {
if (token != null) {
holder.registerProblem(token, "Empty name not permitted");
} else {
final XmlAttribute attribute = element.getNameAttribute();
if (attribute != null) {
final XmlAttributeValue e = attribute.getValueElement();
if (e != null) {
holder.registerProblem(e, "Empty name not permitted");
}
}
}
} else if (!isLegalName(name, holder.getManager().getProject())) {
assert token != null;
holder.registerProblem(token, "Illegal name");
} else {
assert token != null;
final XmlFile file = (XmlFile) tag.getContainingFile();
final XmlTag duplicatedSymbol = DeclarationChecker.getInstance(file).getDuplicatedSymbol(tag);
if (duplicatedSymbol != null) {
if (duplicatedSymbol.getContainingFile() == file) {
holder.registerProblem(token, "Duplicate declaration");
} else {
holder.registerProblem(token, "Duplicates declaration from '" + duplicatedSymbol.getContainingFile().getName() + "'");
}
}
}
}
private boolean isLegalName(String value, Project project) {
return getNamesValidator().isIdentifier(value, project);
}
};
}
use of com.intellij.psi.XmlElementVisitor in project intellij-community by JetBrains.
the class XmlAspectImpl method update.
@Override
public void update(PomModelEvent event) {
if (!event.getChangedAspects().contains(myTreeAspect))
return;
final TreeChangeEvent changeSet = (TreeChangeEvent) event.getChangeSet(myTreeAspect);
if (changeSet == null)
return;
final ASTNode rootElement = changeSet.getRootElement();
final PsiFile file = (PsiFile) rootElement.getPsi();
if (!(file instanceof XmlFile))
return;
final XmlAspectChangeSetImpl xmlChangeSet = event.registerChangeSetIfAbsent(this, new XmlAspectChangeSetImpl(myModel));
xmlChangeSet.addChangedFile((XmlFile) file);
final ASTNode[] changedElements = changeSet.getChangedElements();
final CharTable table = ((FileElement) changeSet.getRootElement()).getCharTable();
for (ASTNode changedElement : changedElements) {
TreeChange changesByElement = changeSet.getChangesByElement(changedElement);
PsiElement psiElement = null;
while (changedElement != null && (psiElement = changedElement.getPsi()) == null) {
final ASTNode parent = changedElement.getTreeParent();
final ChangeInfoImpl changeInfo = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, changedElement);
changeInfo.compactChange(changesByElement);
changesByElement = new TreeChangeImpl(parent);
changesByElement.addChange(changedElement, changeInfo);
changedElement = parent;
}
if (changedElement == null)
continue;
final TreeChange finalChangedElement = changesByElement;
psiElement.accept(new XmlElementVisitor() {
TreeChange myChange = finalChangedElement;
@Override
public void visitElement(PsiElement element) {
final ASTNode child = element.getNode();
final ASTNode treeParent = child.getTreeParent();
if (treeParent == null)
return;
final PsiElement parent = treeParent.getPsi();
final ChangeInfoImpl changeInfo = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, child);
changeInfo.compactChange(myChange);
myChange = new TreeChangeImpl(treeParent);
myChange.addChange(child, changeInfo);
parent.accept(this);
}
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
final ASTNode[] affectedChildren = myChange.getAffectedChildren();
String oldName = null;
String oldValue = null;
for (final ASTNode treeElement : affectedChildren) {
final ChangeInfo changeByChild = myChange.getChangeByChild(treeElement);
final int changeType = changeByChild.getChangeType();
if (treeElement.getElementType() == XmlTokenType.XML_NAME) {
if (changeType == ChangeInfo.REMOVED) {
oldName = treeElement.getText();
} else if (changeType == ChangeInfo.REPLACE) {
oldName = ((ReplaceChangeInfo) changeByChild).getReplaced().getText();
}
}
if (treeElement.getElementType() == XmlElementType.XML_ATTRIBUTE_VALUE) {
if (changeType == ChangeInfo.REMOVED) {
oldValue = treeElement.getText();
} else if (changeType == ChangeInfo.REPLACE) {
oldValue = ((ReplaceChangeInfo) changeByChild).getReplaced().getText();
}
}
}
if (oldName != null && !oldName.equals(attribute.getName())) {
xmlChangeSet.add(new XmlAttributeSetImpl(attribute.getParent(), oldName, null));
xmlChangeSet.add(new XmlAttributeSetImpl(attribute.getParent(), attribute.getName(), attribute.getValue()));
} else if (oldValue != null) {
xmlChangeSet.add(new XmlAttributeSetImpl(attribute.getParent(), attribute.getName(), attribute.getValue()));
} else {
xmlChangeSet.add(new XmlElementChangedImpl(attribute));
}
}
@Override
public void visitXmlTag(XmlTag tag) {
ASTNode[] affectedChildren = shortenChange(myChange.getAffectedChildren(), changeSet);
for (final ASTNode treeElement : affectedChildren) {
if (!(treeElement.getPsi() instanceof XmlTagChild)) {
visitElement(tag);
return;
}
}
for (final ASTNode treeElement : affectedChildren) {
final ChangeInfo changeByChild = myChange.getChangeByChild(treeElement);
final int changeType = changeByChild.getChangeType();
final IElementType type = treeElement.getElementType();
if (type == TokenType.WHITE_SPACE)
continue;
/*
if (type == ElementType.XML_NAME) {
final XmlToken xmlToken = (XmlToken)((ReplaceChangeInfo)changeByChild).getReplaced();
xmlChangeSet.add(new XmlTagNameChangedImpl(tag, xmlToken.getText()));
continue;
}
*/
final PsiElement element = treeElement.getPsi();
switch(changeType) {
case ChangeInfo.ADD:
xmlChangeSet.add(new XmlTagChildAddImpl(tag, (XmlTagChild) element));
break;
case ChangeInfo.REMOVED:
treeElement.putUserData(CharTable.CHAR_TABLE_KEY, table);
xmlChangeSet.add(new XmlTagChildRemovedImpl(tag, (XmlTagChild) element));
break;
case ChangeInfo.CONTENTS_CHANGED:
xmlChangeSet.add(new XmlTagChildChangedImpl(tag, (XmlTagChild) element));
break;
case ChangeInfo.REPLACE:
final PsiElement psi = ((ReplaceChangeInfo) changeByChild).getReplaced().getPsi();
if (psi instanceof XmlTagChild) {
final XmlTagChild replaced = (XmlTagChild) psi;
replaced.putUserData(CharTable.CHAR_TABLE_KEY, table);
xmlChangeSet.add(new XmlTagChildRemovedImpl(tag, replaced));
xmlChangeSet.add(new XmlTagChildAddImpl(tag, (XmlTagChild) element));
}
break;
}
}
}
@Override
public void visitXmlDocument(XmlDocument document) {
xmlChangeSet.clear();
xmlChangeSet.add(new XmlDocumentChangedImpl(document));
}
@Override
public void visitFile(PsiFile file) {
final XmlDocument document = ((XmlFile) file).getDocument();
if (document != null) {
xmlChangeSet.clear();
xmlChangeSet.add(new XmlDocumentChangedImpl(document));
}
}
});
}
}
use of com.intellij.psi.XmlElementVisitor in project intellij-community by JetBrains.
the class InspectionMappingConsistencyInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
return new XmlElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
DomElement element = DomUtil.getDomElement(tag);
if (element instanceof Extension) {
ExtensionPoint extensionPoint = ((Extension) element).getExtensionPoint();
if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), "com.intellij.codeInspection.InspectionEP")) {
boolean key = tag.getAttribute("key") != null;
boolean groupKey = tag.getAttribute("groupKey") != null;
if (key) {
if (tag.getAttribute("bundle") == null) {
checkDefaultBundle(element, holder);
}
} else if (tag.getAttribute("displayName") == null) {
registerProblem(element, holder, "displayName or key should be specified", "displayName", "key");
}
if (groupKey) {
if (tag.getAttribute("bundle") == null && tag.getAttribute("groupBundle") == null) {
checkDefaultBundle(element, holder);
}
} else if (tag.getAttribute("groupName") == null) {
registerProblem(element, holder, "groupName or groupKey should be specified", "groupName", "groupKey");
}
}
}
}
};
}
Aggregations