use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.
the class UndeclaredTestInspection method checkClass.
@Nullable
public ProblemDescriptor[] checkClass(@NotNull final PsiClass aClass, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
if (TestNGUtil.hasTest(aClass) && PsiClassUtil.isRunnableClass(aClass, true)) {
final Project project = aClass.getProject();
final String qName = aClass.getQualifiedName();
if (qName == null)
return null;
final String packageQName = StringUtil.getPackageName(qName);
final List<String> names = new ArrayList<>();
for (int i = 0; i < qName.length(); i++) {
if (qName.charAt(i) == '.') {
names.add(qName.substring(0, i));
}
}
names.add(qName);
Collections.reverse(names);
for (final String name : names) {
final boolean isFullName = qName.equals(name);
final boolean[] found = new boolean[] { false };
PsiSearchHelper.SERVICE.getInstance(project).processUsagesInNonJavaFiles(name, (file, startOffset, endOffset) -> {
if (file.findReferenceAt(startOffset) != null) {
if (!isFullName) {
//special package tag required
final XmlTag tag = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), XmlTag.class);
if (tag == null || !tag.getName().equals("package")) {
return true;
}
final XmlAttribute attribute = tag.getAttribute("name");
if (attribute == null)
return true;
final String value = attribute.getValue();
if (value == null)
return true;
if (!value.endsWith(".*") && !value.equals(packageQName))
return true;
}
found[0] = true;
return false;
}
return true;
}, new TestNGSearchScope(project));
if (found[0])
return null;
}
final PsiIdentifier nameIdentifier = aClass.getNameIdentifier();
LOG.assertTrue(nameIdentifier != null);
return new ProblemDescriptor[] { manager.createProblemDescriptor(nameIdentifier, "Undeclared test \'" + aClass.getName() + "\'", isOnTheFly, new LocalQuickFix[] { new RegisterClassFix(aClass), new CreateTestngFix() }, ProblemHighlightType.GENERIC_ERROR_OR_WARNING) };
}
return null;
}
use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.
the class XmlNsPrefixAnnotator method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (PsiUtilCore.getElementType(element) != XmlTokenType.XML_NAME)
return;
PsiElement parent = element.getParent();
if (!(parent instanceof XmlTag) && !(parent instanceof XmlAttribute))
return;
TextRange elementRange = element.getTextRange();
List<SchemaPrefixReference> references = ContainerUtil.findAll(parent.getReferences(), SchemaPrefixReference.class);
for (SchemaPrefixReference ref : references) {
TextRange rangeInElement = ref.getRangeInElement();
if (rangeInElement.isEmpty())
continue;
TextRange range = rangeInElement.shiftRight(ref.getElement().getTextRange().getStartOffset());
if (!range.intersects(elementRange))
continue;
holder.createInfoAnnotation(range, null).setTextAttributes(XmlHighlighterColors.XML_NS_PREFIX);
}
}
use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.
the class XmlUnusedNamespaceInspection method getLocationReferences.
private static PsiReference[] getLocationReferences(String namespace, XmlTag tag) {
XmlAttribute locationAttr = tag.getAttribute(XmlUtil.SCHEMA_LOCATION_ATT, XmlUtil.XML_SCHEMA_INSTANCE_URI);
if (locationAttr == null) {
return PsiReference.EMPTY_ARRAY;
}
XmlAttributeValue value = locationAttr.getValueElement();
return value == null ? PsiReference.EMPTY_ARRAY : getLocationReferences(namespace, value);
}
use of com.intellij.psi.xml.XmlAttribute in project intellij-community by JetBrains.
the class XmlUnusedNamespaceInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlAttribute(XmlAttribute attribute) {
PsiFile file = holder.getFile();
if (!(file instanceof XmlFile))
return;
XmlRefCountHolder refCountHolder = XmlRefCountHolder.getRefCountHolder((XmlFile) file);
if (refCountHolder == null)
return;
if (!attribute.isNamespaceDeclaration()) {
checkUnusedLocations(attribute, holder, refCountHolder);
return;
}
String namespace = attribute.getValue();
String declaredPrefix = getDeclaredPrefix(attribute);
if (namespace != null && !refCountHolder.isInUse(declaredPrefix)) {
ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
for (ImplicitUsageProvider provider : implicitUsageProviders) {
if (provider.isImplicitUsage(attribute))
return;
}
XmlAttributeValue value = attribute.getValueElement();
assert value != null;
holder.registerProblem(attribute, XmlBundle.message("xml.inspections.unused.schema.declaration"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, false, !refCountHolder.isUsedNamespace(namespace)));
XmlTag parent = attribute.getParent();
if (declaredPrefix.isEmpty()) {
XmlAttribute location = getDefaultLocation(parent);
if (location != null) {
holder.registerProblem(location, XmlBundle.message("xml.inspections.unused.schema.location"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
}
} else if (!refCountHolder.isUsedNamespace(namespace)) {
for (PsiReference reference : getLocationReferences(namespace, parent)) {
if (!XmlHighlightVisitor.hasBadResolve(reference, false))
holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
}
}
}
}
};
}
use of com.intellij.psi.xml.XmlAttribute 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);
}
};
}
Aggregations