PY-15640 Ignore smart enter fixers in case of empty PyFunction created only to wrap list of decorators

This commit is contained in:
Mikhail Golubev
2015-04-21 15:14:25 +03:00
parent b7695fb678
commit c1ed100ca8
2 changed files with 14 additions and 2 deletions
@@ -41,10 +41,14 @@ public class PyFunctionFixer extends PyFixer<PyFunction> {
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyFunction function)
throws IncorrectOperationException {
final PsiElement colon = PyUtil.getFirstChildOfType(function, PyTokenTypes.COLON);
if (colon == null) {
if (!isFakeFunction(function) && colon == null) {
final PyParameterList parameterList = function.getParameterList();
final Document document = editor.getDocument();
document.insertString(parameterList.getTextRange().getEndOffset(), ":");
}
}
private static boolean isFakeFunction(PyFunction function) {
return function.getNode().findChildByType(PyTokenTypes.DEF_KEYWORD) == null;
}
}
@@ -21,10 +21,13 @@ import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyParameterList;
import com.jetbrains.python.psi.PyUtil;
import org.jetbrains.annotations.NotNull;
import static com.jetbrains.python.psi.PyUtil.as;
/**
* Created by IntelliJ IDEA.
* Author: Alexey.Ivanov
@@ -40,7 +43,7 @@ public class PyParameterListFixer extends PyFixer<PyParameterList> {
public void doApply(@NotNull Editor editor, @NotNull PySmartEnterProcessor processor, @NotNull PyParameterList psiElement) throws IncorrectOperationException {
final PsiElement lBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.OPEN_BRACES, 0);
final PsiElement rBrace = PyUtil.getChildByFilter(psiElement, PyTokenTypes.CLOSE_BRACES, 0);
if (lBrace == null || rBrace == null) {
if (!isFakeParameterList(psiElement) && (lBrace == null || rBrace == null)) {
final Document document = editor.getDocument();
if (lBrace == null) {
document.insertString(psiElement.getTextRange().getStartOffset(), "(");
@@ -50,4 +53,9 @@ public class PyParameterListFixer extends PyFixer<PyParameterList> {
}
}
}
private static boolean isFakeParameterList(@NotNull PyParameterList parameterList) {
final PyFunction pyFunction = as(parameterList.getParent(), PyFunction.class);
return pyFunction != null && pyFunction.getNode().findChildByType(PyTokenTypes.DEF_KEYWORD) == null;
}
}