Search in sources :

Example 6 with Section

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

the class PySectionBasedDocStringTest method testGoogleParametersSectionWithoutSummary.

public void testGoogleParametersSectionWithoutSummary() {
    final GoogleCodeStyleDocString docString = findAndParseGoogleStyleDocString();
    assertEmpty(docString.getSummary());
    assertSize(1, docString.getSections());
    final Section paramSection = docString.getSections().get(0);
    assertEquals("parameters", paramSection.getNormalizedTitle());
    assertSize(1, paramSection.getFields());
}
Also used : Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 7 with Section

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

Example 8 with Section

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

the class SectionBasedDocStringUpdater method findPreferredSectionLine.

/**
   * @return pair (lineNum, insertAfter), i.e. first item is line number,
   * second item is true if new section should be inserted after this line and false otherwise
   */
private Pair<Integer, Boolean> findPreferredSectionLine(@NotNull String sectionTitle) {
    final String normalized = SectionBasedDocString.getNormalizedSectionTitle(sectionTitle);
    final int index = CANONICAL_SECTION_ORDER.indexOf(normalized);
    if (index < 0) {
        return Pair.create(findLastNonEmptyLine(), true);
    }
    final Map<String, Section> namedSections = new HashMap<>();
    for (Section section : myOriginalDocString.getSections()) {
        final String normalizedTitle = section.getNormalizedTitle();
        // leave only first occurrences
        if (!namedSections.containsKey(normalizedTitle)) {
            namedSections.put(normalizedTitle, section);
        }
    }
    for (int i = index - 1; i >= 0; i--) {
        final Section previous = namedSections.get(CANONICAL_SECTION_ORDER.get(i));
        if (previous != null) {
            return Pair.create(getSectionEndLine(previous), true);
        }
    }
    for (int i = index + 1; i < CANONICAL_SECTION_ORDER.size(); i++) {
        final Section next = namedSections.get(CANONICAL_SECTION_ORDER.get(i));
        if (next != null) {
            return Pair.create(getSectionStartLine(next), false);
        }
    }
    return Pair.create(findLastNonEmptyLine(), true);
}
Also used : HashMap(java.util.HashMap) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 9 with Section

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

the class PySectionBasedDocStringTest method testGoogleEmptyParamTypeInParenthesis.

public void testGoogleEmptyParamTypeInParenthesis() {
    final GoogleCodeStyleDocString docString = findAndParseGoogleStyleDocString();
    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.getDescription());
    assertEmpty(param1.getType());
    assertNotNull(param1.getTypeAsSubstring());
    assertEquals(26, param1.getTypeAsSubstring().getStartOffset());
    assertEquals(26, param1.getTypeAsSubstring().getEndOffset());
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Example 10 with Section

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

the class PySectionBasedDocStringTest method testNamedReturnsAndYields.

public void testNamedReturnsAndYields() {
    final GoogleCodeStyleDocString docString = findAndParseGoogleStyleDocString();
    assertEmpty(docString.getSummary());
    assertSize(2, docString.getSections());
    final Section returnSection = docString.getSections().get(0);
    assertSize(2, returnSection.getFields());
    final SectionField return1 = returnSection.getFields().get(0);
    assertEquals("status_code", return1.getName());
    assertEquals("int", return1.getType());
    assertEquals("HTTP status code", return1.getDescription());
    final SectionField return2 = returnSection.getFields().get(1);
    assertEquals("template", return2.getName());
    assertEquals("str", return2.getType());
    assertEquals("path to template in template roots", return2.getDescription());
    final Section yieldSection = docString.getSections().get(1);
    assertSize(1, yieldSection.getFields());
    final SectionField yield1 = yieldSection.getFields().get(0);
    assertEquals("progress", yield1.getName());
    assertEquals("float", yield1.getType());
    assertEquals("floating point value in range [0, 1) indicating progress\n" + "of the task", yield1.getDescription());
}
Also used : SectionField(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField) Section(com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)

Aggregations

Section (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)19 SectionField (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField)14 Substring (com.jetbrains.python.toolbox.Substring)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1