[PY-346] fix formatting

This commit is contained in:
Dennis Ushakov
2010-01-25 18:49:20 +03:00
parent 837ef22f60
commit 77bafe85df
4 changed files with 69 additions and 14 deletions
@@ -1,17 +1,18 @@
package com.jetbrains.python.refactoring.classes;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.PythonLanguage;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyElement;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyUtil;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import org.jetbrains.annotations.Nullable;
@@ -108,7 +109,10 @@ public class PyClassRefactoringUtil {
final PyClass newClass = PythonLanguage.getInstance().getElementGenerator().createFromText(project, PyClass.class, text);
if (superClass.getMethods().length != 0) {
final PyFunction previousLastMethod = superClass.getMethods()[0];
PyPsiUtils.addBeforeInParent(previousLastMethod, newClass.getLastChild().getChildren());
final ASTNode node = newClass.getLastChild().getNode();
for (ASTNode child : node.getChildren(null)) {
PyPsiUtils.addBeforeInParent(previousLastMethod, child.getPsi());
}
PyPsiUtils.addBeforeInParent(previousLastMethod, newClass.getLastChild().getPrevSibling());
PyPsiUtils.addBeforeInParent(previousLastMethod, newClass.getLastChild());
} else {
@@ -127,7 +131,7 @@ public class PyClassRefactoringUtil {
for (PyElement element : elements) {
final String name = element.getName();
if (name != null && (up || superClass.findMethodByName(name, false) == null)) {
builder.append(white).append(element.getText()).append("\n\n");
builder.append(white).append(element.getText()).append("\n");
hasChanges = true;
}
}
@@ -0,0 +1,25 @@
class A:
class B(A):
def meth_a1(self, name = {}):
pass
def meth_a2(self):
pass
def meth_b1(self):
pass
def meth_b2(self):
pass
class D(A):
def meth_a1(self, name = {}):
pass
def meth_a2(self):
pass
def meth_d1(self):
pass
@@ -0,0 +1,15 @@
class A:
def meth_a1(self, name = {}):
pass
def meth_a2(self):
pass
class B(A):
def meth_b1(self):
pass
def meth_b2(self):
pass
class D(A):
def meth_d1(self):
pass
@@ -5,35 +5,46 @@ import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyElement;
import com.jetbrains.python.refactoring.classes.pushDown.PyPushDownProcessor;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
/**
* @author Dennis.Ushakov
*/
public class PyPushDownTest extends PyClassRefactoringTest {
public void testSimple() throws Exception {
doProcessorTest("Foo", ".foo", null);
doProcessorTest("Foo", null, ".foo");
}
public void testSuperclass() throws Exception {
doProcessorTest("Zope", "Foo", null);
doProcessorTest("Zope", null, "Foo");
}
public void testMultiple() throws Exception {
doProcessorTest("Foo", ".foo", null);
doProcessorTest("Foo", null, ".foo");
}
public void testPy346() throws Exception {
doProcessorTest("A", null, ".meth_a1", ".meth_a2");
}
public void testExistingmethod() throws Exception {
doProcessorTest("Foo", ".foo", "function <b><code>foo</code></b> is already overridden in class <b><code>Boo</code></b>. Method will not be pushed down to that class.");
doProcessorTest("Foo", "function <b><code>foo</code></b> is already overridden in class <b><code>Boo</code></b>. Method will not be pushed down to that class.", ".foo");
}
private void doProcessorTest(final String className, final String memberName, final String expectedError) throws Exception {
private void doProcessorTest(final String className, final String expectedError, final String... membersName) throws Exception {
try {
String baseName = "/refactoring/pushdown/" + getTestName(true);
myFixture.configureByFile(baseName + ".before.py");
final PyClass clazz = findClass(className);
final PyElement member = findMember(className, memberName);
final PyPushDownProcessor processor = new PyPushDownProcessor(myFixture.getProject(), clazz, Collections.singleton(new PyMemberInfo(member)));
final List<PyMemberInfo> members = new ArrayList<PyMemberInfo>();
for (String memberName : membersName) {
final PyElement member = findMember(className, memberName);
assertNotNull(member);
members.add(new PyMemberInfo(member));
}
final PyPushDownProcessor processor = new PyPushDownProcessor(myFixture.getProject(), clazz, members);
new WriteCommandAction.Simple(myFixture.getProject()) {
@Override
protected void run() throws Throwable {