Search in sources :

Example 1 with ProblemsHolder

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);
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) Project(com.intellij.openapi.project.Project) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XsltNamedElement(org.intellij.lang.xpath.xslt.psi.XsltNamedElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XsltTemplate(org.intellij.lang.xpath.xslt.psi.XsltTemplate) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ProblemsHolder

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()));
                        }
                    }
                }
            }
        }
    };
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) PlainDocString(com.jetbrains.python.documentation.docstrings.PlainDocString) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) DocstringQuickFix(com.jetbrains.python.inspections.quickfix.DocstringQuickFix) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ProblemsHolder

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"));
            }
        }
    };
}
Also used : TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) DocstringQuickFix(com.jetbrains.python.inspections.quickfix.DocstringQuickFix) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ProblemsHolder

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);
                }
            }
        }
    };
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GoDeleteVarDefinitionQuickFix(com.goide.quickfix.GoDeleteVarDefinitionQuickFix) com.goide.psi(com.goide.psi) LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) PsiReference(com.intellij.psi.PsiReference) GoVarProcessor(com.goide.psi.impl.GoVarProcessor) GoInspectionBase(com.goide.inspections.GoInspectionBase) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) GoRenameToBlankQuickFix(com.goide.quickfix.GoRenameToBlankQuickFix) PsiElement(com.intellij.psi.PsiElement) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ProblemsHolder

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));
}
Also used : NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) JavaLanguage(com.intellij.lang.java.JavaLanguage) Language(org.intellij.lang.annotations.Language) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool)

Aggregations

ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)10 NotNull (org.jetbrains.annotations.NotNull)10 LocalInspectionToolSession (com.intellij.codeInspection.LocalInspectionToolSession)4 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)3 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)3 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)3 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)2 ASTNode (com.intellij.lang.ASTNode)2 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)2 DocstringQuickFix (com.jetbrains.python.inspections.quickfix.DocstringQuickFix)2 java.util (java.util)2 Nls (org.jetbrains.annotations.Nls)2 NonNls (org.jetbrains.annotations.NonNls)2 Nullable (org.jetbrains.annotations.Nullable)2 GoInspectionBase (com.goide.inspections.GoInspectionBase)1 com.goide.psi (com.goide.psi)1 GoVarProcessor (com.goide.psi.impl.GoVarProcessor)1 GoDeleteVarDefinitionQuickFix (com.goide.quickfix.GoDeleteVarDefinitionQuickFix)1