mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fixed PY-7152 Convert triple-quoted string to single-quoted string: missing intention for strings with prefixes
This commit is contained in:
+11
-2
@@ -9,6 +9,7 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
@@ -47,6 +48,8 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction {
|
||||
if (docStringOwner.getDocStringExpression() == string) return false;
|
||||
}
|
||||
String stringText = string.getText();
|
||||
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
|
||||
stringText = stringText.substring(prefixLength);
|
||||
if (stringText.length() >= 6) {
|
||||
if (stringText.startsWith("'''") && stringText.endsWith("'''") ||
|
||||
stringText.startsWith("\"\"\"") && stringText.endsWith("\"\"\"")) return true;
|
||||
@@ -59,15 +62,21 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction {
|
||||
PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
|
||||
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
|
||||
if (string != null) {
|
||||
String stringText = string.getStringValue();
|
||||
String stringText = string.getText();
|
||||
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
|
||||
String prefix = stringText.substring(0, prefixLength);
|
||||
Character firstQuote = stringText.substring(prefixLength).charAt(0);
|
||||
|
||||
stringText = string.getStringValue();
|
||||
List<String> subStrings = StringUtil.split(stringText, "\n", false, true);
|
||||
|
||||
Character firstQuote = string.getText().charAt(0);
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
if (subStrings.size() != 1)
|
||||
result.append("(");
|
||||
boolean lastString = false;
|
||||
for (String s : subStrings) {
|
||||
result.append(prefix);
|
||||
result.append(firstQuote);
|
||||
String validSubstring = convertToValidSubString(s, firstQuote);
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
s = u"string\n"
|
||||
@@ -0,0 +1,2 @@
|
||||
s = u"""<caret>string
|
||||
"""
|
||||
@@ -251,6 +251,10 @@ public class PyIntentionTest extends PyTestCase {
|
||||
doTest(PyBundle.message("INTN.triple.quoted.string"));
|
||||
}
|
||||
|
||||
public void testConvertTripleQuotedUnicodeString() { //PY-7152
|
||||
doTest(PyBundle.message("INTN.triple.quoted.string"));
|
||||
}
|
||||
|
||||
public void testTransformConditionalExpression() { //PY-3094
|
||||
doTest(PyBundle.message("INTN.transform.into.if.else.statement"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user