Search in sources :

Example 16 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class DocStringReferenceProvider method returnTypes.

private static List<PsiReference> returnTypes(PsiElement element, StructuredDocString docString, int offset) {
    List<PsiReference> result = new ArrayList<>();
    final Substring rtype = docString.getReturnTypeSubstring();
    if (rtype != null) {
        result.addAll(parseTypeReferences(element, rtype, offset));
    }
    return result;
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference)

Example 17 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class PyStructuredDocstringFormatter method formatStructuredDocString.

private static String formatStructuredDocString(@NotNull final StructuredDocString docString) {
    final StringBuilder result = new StringBuilder();
    final String attributeDescription = docString.getAttributeDescription();
    if (attributeDescription != null) {
        result.append(attributeDescription);
        final String attrType = docString.getParamType(null);
        if (attrType != null) {
            result.append(" <i>Type: ").append(attrType).append("</i>");
        }
    }
    formatParameterDescriptions(docString, result, false);
    formatParameterDescriptions(docString, result, true);
    final String returnDescription = docString.getReturnDescription();
    final String returnType = docString.getReturnType();
    if (returnDescription != null || returnType != null) {
        result.append("<br><b>Return value:</b><br>");
        if (returnDescription != null) {
            result.append(returnDescription);
        }
        if (returnType != null) {
            result.append(" <i>Type: ").append(returnType).append("</i>");
        }
    }
    final List<String> raisedException = docString.getRaisedExceptions();
    if (raisedException.size() > 0) {
        result.append("<br><b>Raises:</b><br>");
        for (String s : raisedException) {
            result.append("<b>").append(s).append("</b> - ").append(docString.getRaisedExceptionDescription(s)).append("<br>");
        }
    }
    if (docString instanceof TagBasedDocString) {
        final TagBasedDocString taggedDocString = (TagBasedDocString) docString;
        final List<String> additionalTags = taggedDocString.getAdditionalTags();
        if (!additionalTags.isEmpty()) {
            result.append("<br/><br/><b>Additional:</b><br/>");
            result.append("<table>");
            for (String tagName : additionalTags) {
                final List<Substring> args = taggedDocString.getTagArguments(tagName);
                for (Substring arg : args) {
                    final String s = arg.toString();
                    result.append("<tr><td align=\"right\"><b>").append(tagName);
                    result.append(" ").append(s).append(":</b>");
                    result.append("</td><td>").append(taggedDocString.getTagValue(tagName, s)).append("</td></tr>");
                }
                result.append("</table>");
            }
        }
    }
    return result.toString();
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) StructuredDocString(com.jetbrains.python.psi.StructuredDocString)

Example 18 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class SectionBasedDocString method splitByFirstColon.

/**
   * Properly partitions line by first colon taking into account possible Sphinx references inside
   * <p/>
   * <h3>Example</h3>
   * <pre>{@code
   *   runtime (:class:`Runtime`): Use it to access the environment.
   * }</pre>
   */
@NotNull
protected static List<Substring> splitByFirstColon(@NotNull Substring line) {
    final List<Substring> parts = line.split(SPHINX_REFERENCE_RE);
    if (parts.size() > 1) {
        for (Substring part : parts) {
            final int i = part.indexOf(":");
            if (i >= 0) {
                final Substring beforeColon = new Substring(line.getSuperString(), line.getStartOffset(), part.getStartOffset() + i);
                final Substring afterColon = new Substring(line.getSuperString(), part.getStartOffset() + i + 1, line.getEndOffset());
                return Arrays.asList(beforeColon, afterColon);
            }
        }
        return Collections.singletonList(line);
    }
    return line.split(":", 1);
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with Substring

use of com.jetbrains.python.toolbox.Substring in project intellij-community by JetBrains.

the class SectionBasedDocString method parseSection.

@NotNull
protected Pair<Section, Integer> parseSection(int sectionStartLine) {
    final Pair<Substring, Integer> parsedHeader = parseSectionHeader(sectionStartLine);
    if (parsedHeader.getFirst() == null) {
        return Pair.create(null, sectionStartLine);
    }
    final String normalized = getNormalizedSectionTitle(parsedHeader.getFirst().toString());
    if (normalized == null) {
        return Pair.create(null, sectionStartLine);
    }
    final List<SectionField> fields = new ArrayList<>();
    final int sectionIndent = getLineIndentSize(sectionStartLine);
    int lineNum = consumeEmptyLines(parsedHeader.getSecond());
    while (!isSectionBreak(lineNum, sectionIndent)) {
        if (!isEmpty(lineNum)) {
            final Pair<SectionField, Integer> parsedField = parseSectionField(lineNum, normalized, sectionIndent);
            if (parsedField.getFirst() != null) {
                fields.add(parsedField.getFirst());
                lineNum = parsedField.getSecond();
                continue;
            } else {
                myOtherContent.add(getLine(lineNum));
            }
        }
        lineNum++;
    }
    return Pair.create(new Section(parsedHeader.getFirst(), fields), lineNum);
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) StructuredDocString(com.jetbrains.python.psi.StructuredDocString) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with Substring

use of com.jetbrains.python.toolbox.Substring 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)

Aggregations

Substring (com.jetbrains.python.toolbox.Substring)24 NotNull (org.jetbrains.annotations.NotNull)6 ArrayList (java.util.ArrayList)5 StructuredDocString (com.jetbrains.python.psi.StructuredDocString)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiReference (com.intellij.psi.PsiReference)3 EpydocString (com.jetbrains.python.documentation.docstrings.EpydocString)3 Section (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.Section)3 SectionField (com.jetbrains.python.documentation.docstrings.SectionBasedDocString.SectionField)3 Matcher (java.util.regex.Matcher)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PlainDocString (com.jetbrains.python.documentation.docstrings.PlainDocString)1 DocstringQuickFix (com.jetbrains.python.inspections.quickfix.DocstringQuickFix)1