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