Search in sources :

Example 1 with PlainDocString

use of com.jetbrains.python.documentation.docstrings.PlainDocString 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)

Aggregations

ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 PlainDocString (com.jetbrains.python.documentation.docstrings.PlainDocString)1 DocstringQuickFix (com.jetbrains.python.inspections.quickfix.DocstringQuickFix)1 Substring (com.jetbrains.python.toolbox.Substring)1 NotNull (org.jetbrains.annotations.NotNull)1