Search in sources :

Example 1 with DocstringQuickFix

use of com.jetbrains.python.inspections.quickfix.DocstringQuickFix 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 2 with DocstringQuickFix

use of com.jetbrains.python.inspections.quickfix.DocstringQuickFix 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)

Aggregations

ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)2 DocstringQuickFix (com.jetbrains.python.inspections.quickfix.DocstringQuickFix)2 NotNull (org.jetbrains.annotations.NotNull)2 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PlainDocString (com.jetbrains.python.documentation.docstrings.PlainDocString)1 Substring (com.jetbrains.python.toolbox.Substring)1