boolean (@NotNull PsiElement element, @NotNull PyExpression value, @NotNull Project project, @NotNull Editor editor, @NotNull Map<PsiElement, PsiElement> simpleReplacements, @NotNull Map<PyFStringFragment, PyStringElement> fStringFragmentsReplacements) { PyFormattedStringElement targetFString = PsiTreeUtil.getParentOfType(element, PyFormattedStringElement.class, true, PyStatement.class); if (targetFString == null) { simpleReplacements.put(element, value); return true; } boolean fStringCanContainArbitraryStrings = LanguageLevel.forElement(element).isAtLeast(LanguageLevel.PYTHON312); if (!fStringCanContainArbitraryStrings && !targetFString.isTripleQuoted() && value.textContains('\n')) { CommonRefactoringUtil.showErrorHint(project, editor, PyPsiBundle.message("refactoring.inline.can.not.multiline.string.to.f.string"), getRefactoringName(), HELP_ID); return false; } boolean intoNestedFString = PsiTreeUtil.getParentOfType(targetFString, PyFormattedStringElement.class, true, PyStatement.class) != null; List<PyStringElement> valueStringElements = getStringElements(value); boolean entireValueIsSingleNonInterpolatedString = value instanceof PyStringLiteralExpression && valueStringElements.size() == 1; if (entireValueIsSingleNonInterpolatedString && element.getParent() instanceof PyFStringFragment fStringFragment && fStringFragment.getTypeConversion() == null && fStringFragment.getFormatPart() == null && !(value.textContains('\n') && !targetFString.isTripleQuoted())) { if (intoNestedFString && !stringContentCanBeInlinedIntoFString(valueStringElements.get(0), targetFString)) { CommonRefactoringUtil.showErrorHint(project, editor, PyPsiBundle.message("refactoring.inline.can.not.string.with.backslashes.or.quotes.to.f.string"), getRefactoringName(), HELP_ID); return false; } var valueReplacedQuotes = replaceQuotesInExpression(value, targetFString.getQuote().charAt(0)); var stringElements = getStringElements(valueReplacedQuotes); if (stringElements.size() == 1) { fStringFragmentsReplacements.put(fStringFragment, stringElements.get(0)); return true; } } if (!fStringCanContainArbitraryStrings && intoNestedFString) { CommonRefactoringUtil.showErrorHint(project, editor, PyPsiBundle.message("refactoring.inline.can.not.string.to.nested.f.string"), getRefactoringName(), HELP_ID); return false; } for (PyStringElement valueStringElement : valueStringElements) { if (!stringContentCanBeInlinedIntoFString(valueStringElement, targetFString)) { String message = PyPsiBundle.message("refactoring.inline.can.not.string.with.backslashes.or.quotes.to.f.string"); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HELP_ID); return false; } } char newQuote = PyStringLiteralUtil.flipQuote(targetFString.getQuote().charAt(0)); var quoteSafeValue = fStringCanContainArbitraryStrings ? value.copy() : replaceQuotesInExpression(value, newQuote); simpleReplacements.put(element, quoteSafeValue); return true; }