Skip to content

Commit

Permalink
Merge pull request #817 from bohdan-harniuk/bugfix/685-empty-psi-elem…
Browse files Browse the repository at this point in the history
…ent-in-module-xml-inspection

[Bugfix:685] Fixed empty psi element in module xml inspection
  • Loading branch information
Andrii Beziazychnyi authored Nov 30, 2021
2 parents a625f4b + 4dd5838 commit eac8b45
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,46 @@

public class ModuleDeclarationInModuleXmlInspection extends XmlSuppressableInspectionTool {

@NotNull
@Override
public PsiElementVisitor buildVisitor(
public @NotNull PsiElementVisitor buildVisitor(
final @NotNull ProblemsHolder problemsHolder,
final boolean isOnTheFly
) {
return new XmlElementVisitor() {

@Override
public void visitXmlAttributeValue(final XmlAttributeValue value) {
final PsiFile file = value.getContainingFile();
final String filename = file.getName();
if (!filename.equals(ModuleXml.FILE_NAME)) {

if (!ModuleXml.FILE_NAME.equals(filename)) {
return;
}

if (!IsFileInEditableModuleUtil.execute(file)) {
return;
}

if (isSubTag(value, (XmlFile) file)) {
return;
}

final PsiDirectory etcDirectory = file.getParent();

if (etcDirectory == null) {
return;
}

final String attributeName = XmlAttributeValuePattern.getLocalName(value);

if (attributeName != null && attributeName.equals(ModuleXml.MODULE_ATTR_NAME)) {
final String expectedName
= GetEditableModuleNameByRootFileUtil.execute(etcDirectory);
final String actualName = value.getValue();
if (actualName.equals(expectedName)) {

if (actualName.equals(expectedName) || actualName.trim().isEmpty()) {
return;
}
final InspectionBundle inspectionBundle = new InspectionBundle();

problemsHolder.registerProblem(
value,
inspectionBundle.message(
Expand All @@ -81,26 +85,27 @@ public void visitXmlAttributeValue(final XmlAttributeValue value) {

protected boolean isSubTag(final XmlAttributeValue value, final XmlFile file) {
final XmlAttribute xmlAttribute = PsiTreeUtil.getParentOfType(value, XmlAttribute.class);

if (xmlAttribute == null) {
return true;
}

final XmlTag xmlTag = PsiTreeUtil.getParentOfType(xmlAttribute, XmlTag.class);

if (xmlTag == null) {
return true;
}

final XmlDocument xmlDocument = file.getDocument();

if (xmlDocument == null) {
return true;
}

final XmlTag xmlRootTag = xmlDocument.getRootTag();

if (xmlRootTag == null) {
return true;
}

final XmlTag rootTag = PsiTreeUtil.getParentOfType(xmlTag, XmlTag.class);

return rootTag == null || !(rootTag.getName().equals(xmlRootTag.getName()));
}
}

0 comments on commit eac8b45

Please sign in to comment.