Fix PY-19749 'Replace with str.format method call' should not add positional id

This commit is contained in:
Valentina Kiryushkina
2016-09-08 12:55:53 +03:00
parent 1ad15a702c
commit 6ed9d3a73d
6 changed files with 16 additions and 17 deletions

View File

@@ -123,7 +123,6 @@ public class ConvertFormatOperatorToMethodIntention extends BaseIntentionAction
int index = openPos + 1; // from quote to first in-string char
StringBuilder out = new StringBuilder(text.subSequence(0, openPos+1));
if (!hasPrefix) out.insert(0, prefix);
int position_count = 0;
Matcher scanner = FORMAT_PATTERN.matcher(text);
while (scanner.find(index)) {
// store previous non-format part
@@ -147,10 +146,6 @@ public class ConvertFormatOperatorToMethodIntention extends BaseIntentionAction
out.append(f_key);
usesNamedFormat = true;
}
else {
out.append(position_count);
position_count += 1;
}
if ("r".equals(f_conversion)) out.append("!r");
// don't convert %s -> !s, for %s is the normal way to output the default representation
out.append(":");
@@ -172,8 +167,12 @@ public class ConvertFormatOperatorToMethodIntention extends BaseIntentionAction
}
if ("i".equals(f_conversion) || "u".equals(f_conversion)) out.append("d");
else if ("r".equals(f_conversion)) out.append("s"); // we want our raw string as a string
else out.append(f_conversion);
//
else if (!"s".equals(f_conversion)) out.append(f_conversion);
final int lastIndexOf = out.lastIndexOf(":");
if (lastIndexOf == out.length() - 1) {
out.deleteCharAt(lastIndexOf);
}
out.append("}");
}
index = scanner.end();

View File

@@ -1,2 +1,2 @@
print('{quantity:s} pounds of '
'{type:s}'.format(quantity=42, type='spam'))
print('{quantity} pounds of '
'{type}'.format(quantity=42, type='spam'))

View File

@@ -1,2 +1,2 @@
print u"\N{LATIN SMALL LETTER B}{0:s}\N{NUMBER SIGN}\
{1:s}\N{LATIN SMALL LETTER B}".format(str1, str2)
print u"\N{LATIN SMALL LETTER B}{}\N{NUMBER SIGN}\
{}\N{LATIN SMALL LETTER B}".format(str1, str2)

View File

@@ -1,13 +1,13 @@
n = 8
a = "{n:d} bottles of {what:s} on the {where:s}" \
a = "{n:d} bottles of {what} on the {where}" \
"""
{n:d} bottles
of {what:s}
of {what}
""" \
r'\n/ take {howmuch!r:s} down \n/' \
ur"pass it {how:>8s}" \
ur"pass it {how:>8}" \
"{new_n:#d} {{that is, {percent:+3.2f}% less}} " \
"bottles of {what:>6s} on the {where:s}" \
"bottles of {what:>6} on the {where}" \
.format(n=n, where="wall", howmuch=u'one', how="'round\t", new_n=(n - 1), percent=100 * (1 - ((n - 1.0) / n)),
what="beer")
print a

View File

@@ -1 +1 @@
print("|\077{0:>20s}FOO! {1: 5.3F}%\n|{2:<15s} {3:<10d}-torn-off, {4:+#05d}".format("right-->", 12, "<--left", -10, 20))
print("|\077{:>20}FOO! {: 5.3F}%\n|{:<15} {:<10d}-torn-off, {:+#05d}".format("right-->", 12, "<--left", -10, 20))

View File

@@ -1,3 +1,3 @@
# coding=utf-8
x = u'привет'
y = u"{0:s}".format(x)
y = u"{}".format(x)