diff --git a/python/src/com/jetbrains/python/actions/AddFieldQuickFix.java b/python/src/com/jetbrains/python/actions/AddFieldQuickFix.java index e54eeba3c4dc..f409d6f7744c 100644 --- a/python/src/com/jetbrains/python/actions/AddFieldQuickFix.java +++ b/python/src/com/jetbrains/python/actions/AddFieldQuickFix.java @@ -12,6 +12,7 @@ import com.jetbrains.python.PyBundle; import com.jetbrains.python.PyNames; import com.jetbrains.python.PythonLanguage; import com.jetbrains.python.psi.*; +import com.jetbrains.python.psi.impl.PyPsiUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -54,7 +55,9 @@ public class AddFieldQuickFix implements LocalQuickFix { } PyStatement new_stmt = callback.fun(self_name); PyUtil.ensureWritable(stmt_list); - return stmt_list.addAfter(new_stmt, last_stmt); + final PsiElement result = stmt_list.addAfter(new_stmt, last_stmt); + PyPsiUtils.removeRedundantPass(stmt_list); + return result; } public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { @@ -109,11 +112,8 @@ public class AddFieldQuickFix implements LocalQuickFix { // found it; copy its param list and make a call to it. PyUtil.ensureWritable(cls); String paramList = ancestorInit != null ? ancestorInit.getParameterList().getText() : "(self)"; - PyFunction new_init = generator.createFromText( - project, PyFunction.class, - "def "+ PyNames.INIT + paramList + ":\n", - new int[]{0} - ); // NOTE: this results in a parsing error, but the StatementList gets created ok + + String functionText = "def " + PyNames.INIT + paramList + ":\n"; if (cls.isNewStyleClass() && ancestorInit != null) { // form the super() call StringBuffer sb = new StringBuffer("super("); @@ -129,10 +129,16 @@ public class AddFieldQuickFix implements LocalQuickFix { sb.append(params[i].getText()); } sb.append(")"); - PyStatement new_stmt = generator.createFromText(project, PyStatement.class, sb.toString()); - new_init.getStatementList().add(new_stmt); + functionText += " " + sb.toString(); } - return new_init; + else { + functionText += " pass"; + } + + return generator.createFromText( + project, PyFunction.class, functionText, + new int[]{0} + ); } public abstract static class FieldCallback implements Function { diff --git a/python/src/com/jetbrains/python/codeInsight/override/PyOverrideImplementUtil.java b/python/src/com/jetbrains/python/codeInsight/override/PyOverrideImplementUtil.java index d272a78dcafd..776f5ba24ae8 100644 --- a/python/src/com/jetbrains/python/codeInsight/override/PyOverrideImplementUtil.java +++ b/python/src/com/jetbrains/python/codeInsight/override/PyOverrideImplementUtil.java @@ -18,7 +18,10 @@ import com.intellij.psi.PsiWhiteSpace; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.jetbrains.python.PythonLanguage; -import com.jetbrains.python.psi.*; +import com.jetbrains.python.psi.PyClass; +import com.jetbrains.python.psi.PyFunction; +import com.jetbrains.python.psi.PyUtil; +import com.jetbrains.python.psi.impl.PyPsiUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -105,10 +108,6 @@ public class PyOverrideImplementUtil { @NotNull final List newMembers, @NotNull final Project project, @NotNull final Editor editor) { - final PyStatement[] statements = pyClass.getStatementList().getStatements(); - if ((statements.length == 1) && (statements[0] instanceof PyPassStatement)) { - statements[0].delete(); - } PyFunction element = null; for (String newMember : newMembers) { element = PythonLanguage.getInstance().getElementGenerator().createFromText(project, PyFunction.class, newMember + "\n pass"); @@ -120,6 +119,7 @@ public class PyOverrideImplementUtil { LOG.error(e); } } + PyPsiUtils.removeRedundantPass(pyClass.getStatementList()); final int start = element.getStatementList().getTextRange().getStartOffset(); editor.getCaretModel().moveToOffset(start); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); diff --git a/python/src/com/jetbrains/python/psi/PyPassStatement.java b/python/src/com/jetbrains/python/psi/PyPassStatement.java index 0ba7a26f02cc..e1896ca16ee6 100644 --- a/python/src/com/jetbrains/python/psi/PyPassStatement.java +++ b/python/src/com/jetbrains/python/psi/PyPassStatement.java @@ -1,27 +1,7 @@ -/* - * Copyright 2005 Pythonid Project - * - * 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.psi; /** - * Created by IntelliJ IDEA. - * User: yole - * Date: 29.05.2005 - * Time: 21:45:15 - * To change this template use File | Settings | File Templates. + * @author yole */ public interface PyPassStatement extends PyStatement { } diff --git a/python/src/com/jetbrains/python/psi/impl/PyPsiUtils.java b/python/src/com/jetbrains/python/psi/impl/PyPsiUtils.java index b8cb7212489d..4200008a5c0d 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyPsiUtils.java +++ b/python/src/com/jetbrains/python/psi/impl/PyPsiUtils.java @@ -199,4 +199,11 @@ public class PyPsiUtils { } return result; } + + public static void removeRedundantPass(final PyStatementList statementList) { + final PyStatement[] statements = statementList.getStatements(); + if ((statements.length > 1) && (statements[0] instanceof PyPassStatement)) { + statements[0].delete(); + } + } } \ No newline at end of file