Search in sources :

Example 1 with SectionField

use of com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField in project intellij-community by JetBrains.

the class NumpyDocStringTypeProvider method getCallType.

@Nullable
@Override
public Ref<PyType> getCallType(@NotNull PyFunction function, @Nullable PyCallSiteExpression callSite, @NotNull TypeEvalContext context) {
    if (isApplicable(function)) {
        final PyExpression callee = callSite instanceof PyCallExpression ? ((PyCallExpression) callSite).getCallee() : null;
        final NumpyDocString docString = forFunction(function, callee);
        if (docString != null) {
            final List<SectionField> returns = docString.getReturnFields();
            final PyPsiFacade facade = getPsiFacade(function);
            switch(returns.size()) {
                case 0:
                    return null;
                case 1:
                    // Function returns single value
                    return Optional.ofNullable(returns.get(0).getType()).filter(StringUtil::isNotEmpty).map(typeName -> isUfuncType(function, typeName) ? facade.parseTypeAnnotation("T", function) : parseNumpyDocType(function, typeName)).map(Ref::create).orElse(null);
                default:
                    // Function returns a tuple
                    final List<PyType> unionMembers = new ArrayList<>();
                    final List<PyType> members = new ArrayList<>();
                    for (int i = 0; i < returns.size(); i++) {
                        final String memberTypeName = returns.get(i).getType();
                        final PyType returnType = StringUtil.isNotEmpty(memberTypeName) ? parseNumpyDocType(function, memberTypeName) : null;
                        final boolean isOptional = StringUtil.isNotEmpty(memberTypeName) && memberTypeName.contains("optional");
                        if (isOptional && i != 0) {
                            if (members.size() > 1) {
                                unionMembers.add(facade.createTupleType(members, function));
                            } else if (members.size() == 1) {
                                unionMembers.add(members.get(0));
                            }
                        }
                        members.add(returnType);
                        if (i == returns.size() - 1 && isOptional) {
                            unionMembers.add(facade.createTupleType(members, function));
                        }
                    }
                    final PyType type = unionMembers.isEmpty() ? facade.createTupleType(members, function) : facade.createUnionType(unionMembers);
                    return Ref.create(type);
            }
        }
    }
    return null;
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) NumpyDocString(com.jetbrains.python.documentation.docstrings.NumpyDocString) NumpyDocString(com.jetbrains.python.documentation.docstrings.NumpyDocString) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with SectionField

use of com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField in project intellij-community by JetBrains.

the class PySectionBasedDocStringTest method testGoogleMandatoryIndentationInsideSection.

// PY-16991
public void testGoogleMandatoryIndentationInsideSection() {
    final GoogleCodeStyleDocString docString = findAndParseGoogleStyleDocString();
    assertSize(3, docString.getSections());
    assertEmpty(docString.getSections().get(0).getFields());
    assertSize(1, docString.getSections().get(1).getFields());
    final Section thirdSection = docString.getSections().get(2);
    assertSize(1, thirdSection.getFields());
    final SectionField firstExample = thirdSection.getFields().get(0);
    assertEmpty(firstExample.getName());
    assertEmpty(firstExample.getType());
    assertEquals("first line\n" + "second line", firstExample.getDescription());
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 3 with SectionField

use of com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField in project intellij-community by JetBrains.

the class PySectionBasedDocStringTest method testNestedIndentation.

public void testNestedIndentation() {
    final GoogleCodeStyleDocString docString = findAndParseGoogleStyleDocString();
    assertSize(1, docString.getSections());
    final Section section1 = docString.getSections().get(0);
    assertEquals("parameters", section1.getNormalizedTitle());
    assertSize(1, section1.getFields());
    final SectionField param1 = section1.getFields().get(0);
    assertEquals("x", param1.getName());
    assertEquals("int", param1.getType());
    assertEquals("first line of the description\n" + "second line\n" + "  third line\n" + "\n" + "Example::\n" + "\n" + "    assert func(42) is None", param1.getDescription());
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 4 with SectionField

use of com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField in project intellij-community by JetBrains.

the class PySectionBasedDocStringTest method testGoogleReturnTypeNoDescription.

public void testGoogleReturnTypeNoDescription() {
    final GoogleCodeStyleDocString docString = findAndParseGoogleStyleDocString();
    assertSize(1, docString.getSections());
    final Section returnSection = docString.getSections().get(0);
    assertEquals("returns", returnSection.getNormalizedTitle());
    assertSize(1, returnSection.getFields());
    final SectionField return1 = returnSection.getFields().get(0);
    assertEmpty(return1.getName());
    assertEmpty(return1.getDescription());
    assertEquals("object", return1.getType());
    assertNotNull(return1.getTypeAsSubstring());
    assertEquals(20, return1.getTypeAsSubstring().getStartOffset());
    assertEquals(26, return1.getTypeAsSubstring().getEndOffset());
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 5 with SectionField

use of com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField in project intellij-community by JetBrains.

the class SectionBasedDocStringUpdater method removeParameter.

@Override
public void removeParameter(@NotNull final String name) {
    for (Section section : myOriginalDocString.getParameterSections()) {
        final List<SectionField> sectionFields = section.getFields();
        for (SectionField field : sectionFields) {
            final Substring nameSub = ContainerUtil.find(field.getNamesAsSubstrings(), substring -> substring.toString().equals(name));
            if (nameSub != null) {
                if (field.getNamesAsSubstrings().size() == 1) {
                    final int endLine = getFieldEndLine(field);
                    if (sectionFields.size() == 1) {
                        removeLinesAndSpacesAfter(getSectionStartLine(section), endLine + 1);
                    } else {
                        final int startLine = getFieldStartLine(field);
                        if (ContainerUtil.getLastItem(sectionFields) == field) {
                            removeLines(startLine, endLine + 1);
                        } else {
                            removeLinesAndSpacesAfter(startLine, endLine + 1);
                        }
                    }
                } else {
                    final Substring wholeParamName = expandParamNameSubstring(nameSub);
                    remove(wholeParamName.getStartOffset(), wholeParamName.getEndOffset());
                }
                break;
            }
        }
    }
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Aggregations

SectionField (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField)15 Section (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)14 Substring (com.jetbrains.python.toolbox.Substring)3 NumpyDocString (com.jetbrains.python.documentation.docstrings.NumpyDocString)1 PyType (com.jetbrains.python.psi.types.PyType)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1