added format specifier quick fix for the python string format inspection

This commit is contained in:
Ekaterina Tuzova
2014-03-11 17:40:20 +04:00
parent 2ba4ece037
commit 378b22a23d
15 changed files with 183 additions and 13 deletions

View File

@@ -127,6 +127,8 @@ QFIX.NAME.move.except.up=Move except clause up
QFIX.NAME.remove.dict.key=Remove this key
QFIX.NAME.add.specifier=Add format specifier character
# Intentions: INTN
INTN.Family.convert.import.unqualify=Convert 'import module' to 'from module import'
INTN.Family.convert.import.qualify=Convert 'from module import' to 'import module'

View File

@@ -17,8 +17,8 @@ package com.jetbrains.python.inspections;
import com.google.common.collect.ImmutableMap;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
@@ -26,6 +26,7 @@ import com.intellij.util.containers.HashMap;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.inspections.quickfix.PyAddSpecifierToFormatQuickFix;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyBuiltinCache;
import com.jetbrains.python.psi.resolve.PyResolveContext;
@@ -45,7 +46,6 @@ import static com.jetbrains.python.inspections.PyStringFormatParser.parsePercent
* @author Alexey.Ivanov
*/
public class PyStringFormatInspection extends PyInspection {
private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.inspections.PyStringFormatInspection");
@Nls
@NotNull
@@ -110,7 +110,10 @@ public class PyStringFormatInspection extends PyInspection {
PyParenthesizedExpression parenthesizedExpression = (PyParenthesizedExpression)binaryExpression.getLeftExpression();
if (parenthesizedExpression.getContainedExpression() instanceof PyTupleExpression) {
PyExpression[] tupleElements = ((PyTupleExpression)parenthesizedExpression.getContainedExpression()).getElements();
return ((PyNumericLiteralExpression)((PyBinaryExpression)rightExpression).getRightExpression()).getBigIntegerValue().intValue() * tupleElements.length;
final PyExpression expression = ((PyBinaryExpression)rightExpression).getRightExpression();
if (expression != null) {
return ((PyNumericLiteralExpression)expression).getBigIntegerValue().intValue() * tupleElements.length;
}
}
}
}
@@ -150,11 +153,8 @@ public class PyStringFormatInspection extends PyInspection {
else if (rightExpression instanceof PyCallExpression) {
final Callable callable = ((PyCallExpression)rightExpression).resolveCalleeFunction(resolveContext);
// TODO: Switch to Callable.getCallType()
if (callable instanceof PyFunction && myTypeEvalContext.maySwitchToAST((PyFunction) callable)) {
if (callable instanceof PyFunction && myTypeEvalContext.maySwitchToAST(callable)) {
PyStatementList statementList = ((PyFunction)callable).getStatementList();
if (statementList == null) {
return -1;
}
PyReturnStatement[] returnStatements = PyUtil.getAllChildrenOfType(statementList, PyReturnStatement.class);
int expressionsSize = -1;
for (PyReturnStatement returnStatement : returnStatements) {
@@ -272,7 +272,7 @@ public class PyStringFormatInspection extends PyInspection {
additionalExpressions = new HashMap<PyExpression,PyExpression>();
pyElement = rightExpression;
}
if (pyElement == null) return 0;
final PyKeyValueExpression[] expressions = ((PyDictLiteralExpression)pyElement).getElements();
if (myUsedMappingKeys.isEmpty()) {
if (myExpectedArguments > 0) {
@@ -325,6 +325,11 @@ public class PyStringFormatInspection extends PyInspection {
return (expressions.length + additionalExpressions.size());
}
private void registerProblem(@NotNull PsiElement problemTarget, @NotNull final String message, @NotNull LocalQuickFix quickFix) {
myProblemRegister = true;
myVisitor.registerProblem(problemTarget, message, quickFix);
}
private void registerProblem(@NotNull PsiElement problemTarget, @NotNull final String message) {
myProblemRegister = true;
myVisitor.registerProblem(problemTarget, message);
@@ -385,7 +390,8 @@ public class PyStringFormatInspection extends PyInspection {
myFormatSpec.put(mappingKey, FORMAT_CONVERSIONS.get(chunk.getConversionType()));
continue;
}
registerProblem(formatExpression, PyBundle.message("INSP.no.format.specifier.char"));
registerProblem(formatExpression, PyBundle.message("INSP.no.format.specifier.char"), new PyAddSpecifierToFormatQuickFix());
return;
}
}

View File

@@ -181,10 +181,7 @@ public class PyStringFormatParser {
final String group = matcher.group();
final int start = matcher.start();
final int end = matcher.end();
if ("{{".equals(group)) {
results.add(new ConstantChunk(start, end));
}
else if ("}}".equals(group)) {
if ("{{".equals(group) || "}}".equals(group)) {
results.add(new ConstantChunk(start, end));
}
else if (group.startsWith("{") && group.endsWith("}")) {
@@ -303,6 +300,7 @@ public class PyStringFormatParser {
return results;
}
@SuppressWarnings("UnusedDeclaration")
@NotNull
public static List<SubstitutionChunk> getPositionalSubstitutions(@NotNull List<SubstitutionChunk> substitutions) {
final ArrayList<SubstitutionChunk> result = new ArrayList<SubstitutionChunk>();
@@ -314,6 +312,7 @@ public class PyStringFormatParser {
return result;
}
@SuppressWarnings("UnusedDeclaration")
@NotNull
public static Map<String, SubstitutionChunk> getKeywordSubstitutions(@NotNull List<SubstitutionChunk> substitutions) {
final Map<String, SubstitutionChunk> result = new HashMap<String, SubstitutionChunk>();

View File

@@ -0,0 +1,107 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.inspections.quickfix;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.inspections.PyStringFormatParser;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyBuiltinCache;
import com.jetbrains.python.psi.types.PyClassType;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.PyTypeChecker;
import com.jetbrains.python.psi.types.TypeEvalContext;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import static com.jetbrains.python.inspections.PyStringFormatParser.filterSubstitutions;
import static com.jetbrains.python.inspections.PyStringFormatParser.parsePercentFormat;
public class PyAddSpecifierToFormatQuickFix implements LocalQuickFix {
@NotNull
public String getName() {
return PyBundle.message("QFIX.NAME.add.specifier");
}
@NonNls
@NotNull
public String getFamilyName() {
return getName();
}
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PyBinaryExpression expression = PsiTreeUtil.getParentOfType(element, PyBinaryExpression.class);
if (expression == null) return;
PyExpression rightExpression = expression.getRightExpression();
if (rightExpression instanceof PyParenthesizedExpression) {
rightExpression = ((PyParenthesizedExpression)rightExpression).getContainedExpression();
}
if (rightExpression == null) return;
final PsiFile file = element.getContainingFile();
final Document document = FileDocumentManager.getInstance().getDocument(file.getVirtualFile());
if (document == null) return;
final int offset = element.getTextOffset();
final TypeEvalContext context = TypeEvalContext.userInitiated(file);
final PyClassType strType = PyBuiltinCache.getInstance(element).getStrType();
final PyClassType floatType = PyBuiltinCache.getInstance(element).getFloatType();
final PyClassType intType = PyBuiltinCache.getInstance(element).getIntType();
final PyExpression leftExpression = expression.getLeftExpression();
if (leftExpression instanceof PyStringLiteralExpression) {
final List<PyStringFormatParser.SubstitutionChunk> chunks =
filterSubstitutions(parsePercentFormat(((PyStringLiteralExpression)leftExpression).getStringValue()));
PyExpression[] elements;
if (rightExpression instanceof PyTupleExpression) {
elements = ((PyTupleExpression)rightExpression).getElements();
}
else {
elements = new PyExpression[]{rightExpression};
}
int shift = 2;
for (int i = 0; i < chunks.size(); i++) {
final PyStringFormatParser.SubstitutionChunk chunk = chunks.get(i);
if (elements.length <= i) return;
final PyType type = context.getType(elements[i]);
final char conversionType = chunk.getConversionType();
if (conversionType == '\u0000') {
final int insertOffset = offset + chunk.getStartIndex() + shift;
if (insertOffset > leftExpression.getTextRange().getEndOffset()) return;
if (PyTypeChecker.match(strType, type, context)) {
document.insertString(insertOffset, "s");
shift += 1;
}
if (PyTypeChecker.match(intType, type, context) || PyTypeChecker.match(floatType, type, context)) {
document.insertString(insertOffset, "d");
shift += 1;
}
}
}
}
}
}

View File

@@ -0,0 +1 @@
a = <warning descr="Format specifier character missing">"% <caret>test %"</warning> % ("x", 1)

View File

@@ -0,0 +1 @@
a = "%s test %d" % ("x", 1)

View File

@@ -0,0 +1 @@
a = <warning descr="Format specifier character missing">"t<caret>est %"</warning> % 1.

View File

@@ -0,0 +1 @@
a = "test %d" % 1.

View File

@@ -0,0 +1 @@
a = <warning descr="Format specifier character missing">"test<caret> %"</warning> % 1

View File

@@ -0,0 +1 @@
a = "test %d" % 1

View File

@@ -0,0 +1 @@
a = <warning descr="Format specifier character missing">"% tes<caret>t %"</warning> % "x"

View File

@@ -0,0 +1 @@
a = "%s test %" % "x"

View File

@@ -0,0 +1 @@
a = <warning descr="Format specifier character missing">"test<caret> %"</warning> % "x"

View File

@@ -0,0 +1 @@
a = "test %s" % "x"

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.quickFixes;
import com.intellij.testFramework.TestDataPath;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.PyQuickFixTestCase;
import com.jetbrains.python.inspections.PyStringFormatInspection;
@TestDataPath("$CONTENT_ROOT/../testData//quickFixes/PyAddSpecifierToFormatQuickFixTest/")
public class PyAddSpecifierToFormatQuickFixTest extends PyQuickFixTestCase {
public void testString() {
doQuickFixTest(PyStringFormatInspection.class, PyBundle.message("QFIX.NAME.add.specifier"));
}
public void testInt() {
doQuickFixTest(PyStringFormatInspection.class, PyBundle.message("QFIX.NAME.add.specifier"));
}
public void testFloat() {
doQuickFixTest(PyStringFormatInspection.class, PyBundle.message("QFIX.NAME.add.specifier"));
}
public void testDict() {
doQuickFixTest(PyStringFormatInspection.class, PyBundle.message("QFIX.NAME.add.specifier"));
}
public void testMissingValues() {
doQuickFixTest(PyStringFormatInspection.class, PyBundle.message("QFIX.NAME.add.specifier"));
}
}