use of com.intellij.codeInspection.ProblemsHolder 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.codeInspection.ProblemsHolder in project intellij-community by JetBrains.
the class PyIncorrectDocstringInspection method buildVisitor.
@NotNull
@Override
public Visitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
return new Visitor(holder, session) {
@Override
protected void checkDocString(@NotNull PyDocStringOwner node) {
final PyStringLiteralExpression docstringExpr = node.getDocStringExpression();
if (docstringExpr != null) {
checkParameters(node, docstringExpr);
}
}
private void checkParameters(@NotNull PyDocStringOwner pyDocStringOwner, @NotNull PyStringLiteralExpression node) {
final StructuredDocString docString = DocStringUtil.parseDocString(node);
if (docString instanceof PlainDocString) {
return;
}
if (pyDocStringOwner instanceof PyFunction) {
final PyParameter[] realParams = ((PyFunction) pyDocStringOwner).getParameterList().getParameters();
final List<PyNamedParameter> missingParams = getMissingParams(docString, realParams);
if (!missingParams.isEmpty()) {
for (PyNamedParameter param : missingParams) {
registerProblem(param, PyBundle.message("INSP.missing.parameter.in.docstring", param.getName()), new DocstringQuickFix(param, null));
}
}
final List<Substring> unexpectedParams = getUnexpectedParams(docString, realParams);
if (!unexpectedParams.isEmpty()) {
for (Substring param : unexpectedParams) {
final ProblemsHolder holder = getHolder();
if (holder != null) {
holder.registerProblem(node, param.getTextRange(), PyBundle.message("INSP.unexpected.parameter.in.docstring", param), new DocstringQuickFix(null, param.getValue()));
}
}
}
}
}
};
}
use of com.intellij.codeInspection.ProblemsHolder in project intellij-community by JetBrains.
the class PyMissingOrEmptyDocstringInspection method buildVisitor.
@NotNull
@Override
public Visitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
return new Visitor(holder, session) {
@Override
protected void checkDocString(@NotNull PyDocStringOwner node) {
final PyStringLiteralExpression docStringExpression = node.getDocStringExpression();
if (docStringExpression == null) {
for (PyInspectionExtension extension : Extensions.getExtensions(PyInspectionExtension.EP_NAME)) {
if (extension.ignoreMissingDocstring(node)) {
return;
}
}
PsiElement marker = null;
if (node instanceof PyClass) {
final ASTNode n = ((PyClass) node).getNameNode();
if (n != null)
marker = n.getPsi();
} else if (node instanceof PyFunction) {
final ASTNode n = ((PyFunction) node).getNameNode();
if (n != null)
marker = n.getPsi();
} else if (node instanceof PyFile) {
final TextRange tr = new TextRange(0, 0);
final ProblemsHolder holder = getHolder();
if (holder != null) {
holder.registerProblem(node, tr, PyBundle.message("INSP.no.docstring"));
}
return;
}
if (marker == null)
marker = node;
if (node instanceof PyFunction || (node instanceof PyClass && ((PyClass) node).findInitOrNew(false, null) != null)) {
registerProblem(marker, PyBundle.message("INSP.no.docstring"), new DocstringQuickFix(null, null));
} else {
registerProblem(marker, PyBundle.message("INSP.no.docstring"));
}
} else if (StringUtil.isEmptyOrSpaces(docStringExpression.getStringValue())) {
registerProblem(docStringExpression, PyBundle.message("INSP.empty.docstring"));
}
}
};
}
use of com.intellij.codeInspection.ProblemsHolder in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoUnusedVariableInspection method buildGoVisitor.
@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
return new GoVisitor() {
@Override
public void visitVarDefinition(@NotNull GoVarDefinition o) {
if (o.isBlank())
return;
GoCompositeElement varSpec = PsiTreeUtil.getParentOfType(o, GoVarSpec.class, GoTypeSwitchGuard.class);
GoVarDeclaration decl = PsiTreeUtil.getParentOfType(o, GoVarDeclaration.class);
if (shouldValidate(decl) && (varSpec != null || decl != null)) {
PsiReference reference = o.getReference();
PsiElement resolve = reference != null ? reference.resolve() : null;
if (resolve != null)
return;
boolean foundReference = !ReferencesSearch.search(o, o.getUseScope()).forEach(reference1 -> {
ProgressManager.checkCanceled();
PsiElement element = reference1.getElement();
if (element == null)
return true;
PsiElement parent = element.getParent();
if (parent instanceof GoLeftHandExprList) {
PsiElement grandParent = parent.getParent();
if (grandParent instanceof GoAssignmentStatement && ((GoAssignmentStatement) grandParent).getAssignOp().getAssign() != null) {
GoFunctionLit fn = PsiTreeUtil.getParentOfType(element, GoFunctionLit.class);
if (fn == null || !PsiTreeUtil.isAncestor(GoVarProcessor.getScope(o), fn, true)) {
return true;
}
}
}
if (parent instanceof GoShortVarDeclaration) {
int op = ((GoShortVarDeclaration) parent).getVarAssign().getStartOffsetInParent();
if (element.getStartOffsetInParent() < op) {
return true;
}
}
return false;
});
if (!foundReference) {
reportError(o, holder);
}
}
}
};
}
use of com.intellij.codeInspection.ProblemsHolder in project intellij-community by JetBrains.
the class DaemonRespondToChangesTest method testLIPGetAllParentsAfterCodeBlockModification.
public void testLIPGetAllParentsAfterCodeBlockModification() throws Throwable {
@Language("JAVA") String text = "class LQF {\n" + " int f;\n" + " public void me() {\n" + " //<caret>\n" + " }\n" + "}";
configureByText(StdFileTypes.JAVA, text);
final List<PsiElement> visitedElements = Collections.synchronizedList(new ArrayList<>());
class MyVisitor extends PsiElementVisitor {
@Override
public void visitElement(PsiElement element) {
visitedElements.add(element);
}
}
final LocalInspectionTool tool = new LocalInspectionTool() {
@Nls
@NotNull
@Override
public String getGroupDisplayName() {
return "fegna";
}
@Nls
@NotNull
@Override
public String getDisplayName() {
return getGroupDisplayName();
}
@NotNull
@Override
public String getShortName() {
return getGroupDisplayName();
}
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new MyVisitor();
}
};
disposeOnTearDown(() -> disableInspectionTool(tool.getShortName()));
enableInspectionTool(tool);
List<HighlightInfo> infos = highlightErrors();
assertEmpty(infos);
List<PsiElement> allPsi = CollectHighlightsUtil.getElementsInRange(myFile, 0, myFile.getTextLength());
assertEquals(new HashSet<>(allPsi), new HashSet<>(visitedElements));
// inside code block modification
visitedElements.clear();
backspace();
backspace();
infos = highlightErrors();
assertEmpty(infos);
PsiMethod method = ((PsiJavaFile) myFile).getClasses()[0].getMethods()[0];
List<PsiElement> methodAndParents = CollectHighlightsUtil.getElementsInRange(myFile, method.getTextRange().getStartOffset(), method.getTextRange().getEndOffset(), true);
assertEquals(new HashSet<>(methodAndParents), new HashSet<>(visitedElements));
}
Aggregations