Search in sources :

Example 21 with Substring

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

Example 22 with Substring

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

the class GoogleCodeStyleDocString method parseSectionField.

/**
   * <h3>Examples</h3>
   * <ol>
   * <li>
   * mayHaveType=true, preferType=false
   * <pre>{@code
   * Attributes:
   *     arg1 (int): description; `(int)` is optional
   * }</pre>
   * </li>
   * <li>
   * mayHaveType=true, preferType=true
   * <pre>{@code
   * Returns:
   *     code (int): description; `(int)` is optional
   * }</pre>
   * </li>
   * <li>
   * mayHaveType=false, preferType=false
   * <pre>{@code
   * Methods:
   *     my_method() : description
   * }</pre>
   * </li>
   * <li>
   * mayHaveType=false, preferType=true
   * <pre>{@code
   * Raises:
   *     Exception : description
   * }</pre>
   * </li>
   * </li>
   * </ol>
   */
@Override
protected Pair<SectionField, Integer> parseSectionField(int lineNum, int sectionIndent, boolean mayHaveType, boolean preferType) {
    final Substring line = getLine(lineNum);
    Substring name, type = null;
    // Napoleon requires that each parameter line contains a colon - we don't because
    // we need to parse and complete parameter names before colon is typed
    final List<Substring> colonSeparatedParts = splitByFirstColon(line);
    assert colonSeparatedParts.size() <= 2;
    final Substring textBeforeColon = colonSeparatedParts.get(0);
    name = textBeforeColon.trim();
    if (mayHaveType) {
        final Matcher matcher = FIELD_NAME_AND_TYPE.matcher(textBeforeColon);
        if (matcher.matches()) {
            name = textBeforeColon.getMatcherGroup(matcher, 1).trim();
            type = textBeforeColon.getMatcherGroup(matcher, 2).trim();
        }
    }
    if (preferType && type == null) {
        type = name;
        name = null;
    }
    if (name != null) {
        name = cleanUpName(name);
    }
    if (name != null ? !isValidName(name.toString()) : !isValidType(type.toString())) {
        return Pair.create(null, lineNum);
    }
    final Pair<List<Substring>, Integer> pair;
    if (colonSeparatedParts.size() == 2) {
        Substring description = colonSeparatedParts.get(1);
        // parse line with indentation at least one space greater than indentation of the field
        pair = parseIndentedBlock(lineNum + 1, getLineIndentSize(lineNum));
        final List<Substring> nestedBlock = pair.getFirst();
        if (!nestedBlock.isEmpty()) {
            //noinspection ConstantConditions
            description = description.union(ContainerUtil.getLastItem(nestedBlock));
        }
        description = description.trim();
        return Pair.create(new SectionField(name, type, description), pair.getSecond());
    }
    return Pair.create(new SectionField(name, type, null), lineNum + 1);
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) Matcher(java.util.regex.Matcher) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 23 with Substring

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

the class NumpyDocString method parseSectionField.

@Override
protected Pair<SectionField, Integer> parseSectionField(int lineNum, int sectionIndent, boolean mayHaveType, boolean preferType) {
    final Substring line = getLine(lineNum);
    Substring namesPart, type = null, description = null;
    if (mayHaveType) {
        final List<Substring> colonSeparatedParts = splitByFirstColon(line);
        namesPart = colonSeparatedParts.get(0).trim();
        if (colonSeparatedParts.size() == 2) {
            type = colonSeparatedParts.get(1).trim();
        }
    } else {
        namesPart = line.trim();
    }
    if (preferType && type == null) {
        type = namesPart;
        namesPart = null;
    }
    final List<Substring> names = new ArrayList<>();
    if (namesPart != null) {
        //     Input arrays, description of `x1`, `x2`.
        for (Substring name : namesPart.split(NAME_SEPARATOR)) {
            final Substring identifier = cleanUpName(name);
            if (!isValidName(identifier.toString())) {
                return Pair.create(null, lineNum);
            }
            names.add(identifier);
        }
    }
    if (namesPart == null && !isValidType(type.toString())) {
        return Pair.create(null, lineNum);
    }
    final Pair<List<Substring>, Integer> parsedDescription = parseIndentedBlock(lineNum + 1, getLineIndentSize(lineNum));
    final List<Substring> descriptionLines = parsedDescription.getFirst();
    if (!descriptionLines.isEmpty()) {
        description = descriptionLines.get(0).union(descriptionLines.get(descriptionLines.size() - 1));
    }
    return Pair.create(new SectionField(names, type, description != null ? description.trim() : null), parsedDescription.getSecond());
}
Also used : Substring(com.jetbrains.python.toolbox.Substring) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList)

Example 24 with Substring

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

the class SpecifyTypeInDocstringIntention method isParamTypeDefined.

@Override
protected boolean isParamTypeDefined(@NotNull PyParameter parameter) {
    final PyFunction pyFunction = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
    if (pyFunction != null) {
        final StructuredDocString structuredDocString = pyFunction.getStructuredDocString();
        if (structuredDocString == null) {
            return false;
        }
        final Substring typeSub = structuredDocString.getParamTypeSubstring(StringUtil.notNullize(parameter.getName()));
        return typeSub != null && !typeSub.isEmpty();
    }
    return false;
}
Also used : Substring(com.jetbrains.python.toolbox.Substring)

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