PsiElement (@NotNull PyStringLiteralExpression oldExpression, @NotNull Pair<String, String> quotes, @NotNull String prefix, @NotNull String suffix, @NotNull String newText, boolean hasSubstitutions) { // 'foobar' + 'baz' -> s + 'bar' + 'baz' // 'foobar%s' -> s + 'bar%s' // 'f%soobar' % x -> (s + 'bar') % x final PyElementGenerator generator = PyElementGenerator.getInstance(oldExpression.getProject()); final LanguageLevel languageLevel = LanguageLevel.forElement(oldExpression); final String leftQuote = quotes.getFirst(); final String rightQuote = quotes.getSecond(); final StringBuilder builder = new StringBuilder(); if (hasSubstitutions) { builder.append("("); } if (!leftQuote.endsWith(prefix)) { builder.append(prefix).append(rightQuote).append(" + "); } final int pos = builder.toString().length(); builder.append(newText); if (!rightQuote.startsWith(suffix)) { builder.append(" + ").append(leftQuote).append(suffix); } if (hasSubstitutions) { builder.append(")"); } final PsiElement expression = generator.createExpressionFromText(languageLevel, builder.toString()); final PsiElement newElement = oldExpression.replace(expression); return newElement.findElementAt(pos); }