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);
}
}
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);
}
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());
}
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;
}
Aggregations