Search in sources :

Example 11 with SectionField

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

the class PySectionBasedDocStringTest method testNumpyCombinedParamDeclarations.

// PY-16908
public void testNumpyCombinedParamDeclarations() {
    final NumpyDocString docString = findAndParseNumpyStyleDocString();
    assertSize(1, docString.getSections());
    final Section paramSection = docString.getSections().get(0);
    assertSize(1, paramSection.getFields());
    final SectionField firstField = paramSection.getFields().get(0);
    assertSameElements(firstField.getNames(), "x", "y", "args", "kwargs");
    assertEquals(firstField.getType(), "Any");
    assertEquals(firstField.getDescription(), "description");
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 12 with SectionField

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

the class PySectionBasedDocStringTest method testSectionStartAfterQuotes.

public void testSectionStartAfterQuotes() {
    final GoogleCodeStyleDocString docString = findAndParseGoogleStyleDocString();
    assertEmpty(docString.getSummary());
    assertSize(2, docString.getSections());
    final Section examplesSection = docString.getSections().get(0);
    assertEquals("examples", examplesSection.getNormalizedTitle());
    assertSize(1, examplesSection.getFields());
    final SectionField example1 = examplesSection.getFields().get(0);
    assertEmpty(example1.getName());
    assertEmpty(example1.getType());
    assertEquals("Useless call\n" + "func() == func()", example1.getDescription());
    final Section notesSection = docString.getSections().get(1);
    assertEquals("notes", notesSection.getNormalizedTitle());
    assertSize(1, notesSection.getFields());
    final SectionField note1 = notesSection.getFields().get(0);
    assertEmpty(note1.getName());
    assertEmpty(note1.getType());
    assertEquals("some\n" + "notes", note1.getDescription());
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 13 with SectionField

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

the class PySectionBasedDocStringTest method testNumpySectionBlockBreaksOnDoubleEmptyLine.

public void testNumpySectionBlockBreaksOnDoubleEmptyLine() {
    final NumpyDocString docString = findAndParseNumpyStyleDocString();
    assertSize(1, docString.getSections());
    final Section paramSection = docString.getSections().get(0);
    assertEquals("parameters", paramSection.getNormalizedTitle());
    assertSize(1, paramSection.getFields());
    final SectionField param1 = paramSection.getFields().get(0);
    assertEquals("x", param1.getName());
    assertEmpty(param1.getType());
    assertEquals("First line\n" + "Second line\n" + "\n" + "Line after single break", param1.getDescription());
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 14 with SectionField

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

the class SectionBasedDocStringUpdater method beforeApplyingModifications.

@Override
protected void beforeApplyingModifications() {
    final List<AddParameter> newParams = new ArrayList<>();
    for (AddParameter param : myAddParameterRequests) {
        if (param.type != null) {
            final Substring typeSub = myOriginalDocString.getParamTypeSubstring(param.name);
            if (typeSub != null) {
                replace(typeSub.getTextRange(), param.type);
                continue;
            }
            final Substring nameSub = findParamNameSubstring(param.name);
            if (nameSub != null) {
                updateParamDeclarationWithType(nameSub, param.type);
                continue;
            }
        }
        newParams.add(param);
    }
    if (!newParams.isEmpty()) {
        final SectionBasedDocStringBuilder paramBlockBuilder = createBuilder();
        final Section firstParamSection = findFirstParametersSection();
        // Insert whole new parameter block
        if (firstParamSection == null) {
            paramBlockBuilder.startParametersSection();
            final SectionBasedDocStringBuilder builder = addParametersInBlock(paramBlockBuilder, newParams, getExpectedFieldIndent());
            insertNewSection(builder, SectionBasedDocString.PARAMETERS_SECTION);
        } else // Update existing parameter block
        {
            final SectionField firstParamField = ContainerUtil.getFirstItem(firstParamSection.getFields());
            // Section exist, but empty
            if (firstParamField == null) {
                final String blockText = buildBlock(paramBlockBuilder, newParams, getExpectedFieldIndent(), getSectionIndent(firstParamSection));
                insertAfterLine(getSectionTitleLastLine(firstParamSection), blockText);
            } else {
                // Section contain other parameter declarations
                final String blockText = buildBlock(paramBlockBuilder, newParams, getFieldIndent(firstParamSection, firstParamField), getSectionIndent(firstParamSection));
                insertBeforeLine(getFieldStartLine(firstParamField), blockText);
            }
        }
    }
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) ArrayList(java.util.ArrayList) SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 15 with SectionField

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

the class SectionBasedDocStringUpdater method addReturnValue.

@Override
public final void addReturnValue(@Nullable String type) {
    if (StringUtil.isEmpty(type)) {
        return;
    }
    final Substring typeSub = myOriginalDocString.getReturnTypeSubstring();
    if (typeSub != null) {
        replace(typeSub.getTextRange(), type);
        return;
    }
    final Section returnSection = findFirstReturnSection();
    if (returnSection != null) {
        final List<SectionField> fields = returnSection.getFields();
        if (!fields.isEmpty()) {
            final SectionField firstField = fields.get(0);
            final String newLine = createReturnLine(type, getSectionIndent(returnSection), getFieldIndent(returnSection, firstField));
            insertBeforeLine(getFieldStartLine(firstField), newLine);
        } else {
            final String newLine = createReturnLine(type, getSectionIndent(returnSection), getExpectedFieldIndent());
            insertAfterLine(getSectionTitleLastLine(returnSection), newLine);
        }
    } else {
        final SectionBasedDocStringBuilder builder = createBuilder().withSectionIndent(getExpectedFieldIndent()).startReturnsSection().addReturnValue(null, type, "");
        insertNewSection(builder, SectionBasedDocString.RETURNS_SECTION);
    }
}
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