use of com.jetbrains.python.codeInsight.PySubstitutionChunkReference in project intellij-community by JetBrains.
the class NewStyleConvertToFStringProcessor method convertSubstitutionChunk.
@Override
protected boolean convertSubstitutionChunk(@NotNull Field field, @NotNull StringBuilder fStringText) {
final String stringText = myPyString.getText();
// Actual format field
fStringText.append("{");
final PySubstitutionChunkReference reference = createReference(field);
final PyExpression resolveResult = adjustResolveResult(reference.resolve());
if (resolveResult == null)
return false;
final PsiElement adjusted = prepareExpressionToInject(resolveResult, field);
if (adjusted == null)
return false;
fStringText.append(adjusted.getText());
final String quotedAttrsAndItems = quoteItemsInFragments(field);
if (quotedAttrsAndItems == null)
return false;
fStringText.append(quotedAttrsAndItems);
// Conversion is copied as is if it's present
final String conversion = field.getConversion();
if (conversion != null) {
fStringText.append(conversion);
}
// Format spec is copied if present handling nested fields
final TextRange specRange = field.getFormatSpecRange();
if (specRange != null) {
int specOffset = specRange.getStartOffset();
// Do not proceed too nested fields
if (field.getDepth() == 1) {
for (Field nestedField : field.getNestedFields()) {
// Copy text of the format spec between nested fragments
fStringText.append(stringText, specOffset, nestedField.getLeftBraceOffset());
specOffset = nestedField.getFieldEnd();
// recursively format nested field
if (!convertSubstitutionChunk(nestedField, fStringText)) {
return false;
}
}
}
if (specOffset < specRange.getEndOffset()) {
fStringText.append(stringText, specOffset, specRange.getEndOffset());
}
}
fStringText.append("}");
return true;
}
use of com.jetbrains.python.codeInsight.PySubstitutionChunkReference in project intellij-community by JetBrains.
the class OldStyleConvertToFStringProcessor method convertSubstitutionChunk.
@Override
protected boolean convertSubstitutionChunk(@NotNull SubstitutionChunk subsChunk, @NotNull StringBuilder fStringText) {
final char conversionChar = subsChunk.getConversionType();
String widthAndPrecision = StringUtil.notNullize(subsChunk.getWidth());
if (StringUtil.isNotEmpty(subsChunk.getPrecision())) {
widthAndPrecision += "." + subsChunk.getPrecision();
}
final String conversionFlags = subsChunk.getConversionFlags();
fStringText.append("{");
final PySubstitutionChunkReference reference = createReference(subsChunk);
final PyExpression resolveResult = adjustResolveResult(reference.resolve());
assert resolveResult != null;
final PsiElement adjusted = prepareExpressionToInject(resolveResult, subsChunk);
if (adjusted == null)
return false;
fStringText.append(adjusted.getText());
// TODO mostly duplicates the logic of ConvertFormatOperatorToMethodIntention
if (conversionChar == 'r') {
fStringText.append("!r");
}
if ((conversionChar != 'r' && conversionChar != 's') || StringUtil.isNotEmpty(conversionFlags) || StringUtil.isNotEmpty(widthAndPrecision)) {
fStringText.append(":");
}
fStringText.append(convertFormatSpec(StringUtil.notNullize(conversionFlags), widthAndPrecision, String.valueOf(conversionChar)));
if (StringUtil.isNotEmpty(widthAndPrecision)) {
fStringText.append(widthAndPrecision);
}
if ('i' == conversionChar || 'u' == conversionChar) {
fStringText.append("d");
} else if ('s' != conversionChar && 'r' != conversionChar) {
fStringText.append(conversionChar);
}
fStringText.append("}");
return true;
}
use of com.jetbrains.python.codeInsight.PySubstitutionChunkReference in project intellij-community by JetBrains.
the class BaseConvertToFStringProcessor method isRefactoringAvailable.
public final boolean isRefactoringAvailable() {
// TODO support glued/concatenated string literal with multiple nodes
if (myPyString.getStringNodes().size() > 1 || myNodeInfo.isBytes() || myNodeInfo.isFormatted())
return false;
final PsiElement valuesSource = getValuesSource();
if (valuesSource == null)
return false;
final List<T> chunks = extractAllSubstitutionChunks();
for (T chunk : chunks) {
if (!checkChunk(chunk))
return false;
final PySubstitutionChunkReference reference = createReference(chunk);
final PyExpression referencedExpr = adjustResolveResult(reference.resolve());
if (referencedExpr == null)
return false;
if (!PsiTreeUtil.isAncestor(valuesSource, referencedExpr, false))
return false;
if (referencedExpr instanceof PyStarExpression || referencedExpr instanceof PyStarArgument)
return false;
if (!checkReferencedExpression(chunks, chunk, valuesSource, referencedExpr))
return false;
}
return true;
}
Aggregations