tweak PSI generation logic in AddFieldQuickFix

This commit is contained in:
Dmitry Jemerov
2010-02-03 19:25:38 +03:00
parent d767e18d06
commit 58005b8c69
4 changed files with 28 additions and 35 deletions
@@ -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<String, PyStatement> {
@@ -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<String> 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);
@@ -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 {
}
@@ -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();
}
}
}