Methods to implement will be printed in the same order as shown in dialog (PY-25906)

Every of them was inserted after statement list so the last method becomes the first.
The problem is fixed by reverting writing order.
This commit is contained in:
Semyon Proshev
2017-09-28 20:00:23 +03:00
parent 0900fea8ed
commit b0f3e6c20b
4 changed files with 57 additions and 6 deletions

View File

@@ -146,9 +146,7 @@ public class PyOverrideImplementUtil {
}.execute();
}
private static void write(@NotNull final PyClass pyClass,
@NotNull final List<PyMethodMember> newMembers,
@NotNull final Editor editor, boolean implement) {
private static void write(@NotNull PyClass pyClass, @NotNull List<PyMethodMember> newMembers, @NotNull Editor editor, boolean implement) {
final PyStatementList statementList = pyClass.getStatementList();
final int offset = editor.getCaretModel().getOffset();
PsiElement anchor = null;
@@ -161,10 +159,11 @@ public class PyOverrideImplementUtil {
}
PyFunction element = null;
for (PyMethodMember newMember : newMembers) {
PyFunction baseFunction = (PyFunction)newMember.getPsiElement();
final LanguageLevel languageLevel = LanguageLevel.forElement(statementList);
for (PyMethodMember newMember : Lists.reverse(newMembers)) {
final PyFunction baseFunction = (PyFunction)newMember.getPsiElement();
final PyFunctionBuilder builder = buildOverriddenFunction(pyClass, baseFunction, implement);
PyFunction function = builder.addFunctionAfter(statementList, anchor, LanguageLevel.forElement(statementList));
final PyFunction function = builder.addFunctionAfter(statementList, anchor, languageLevel);
element = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(function);
}

View File

@@ -0,0 +1,15 @@
from abc import abstractmethod
class Abstract:
@abstractmethod
def foo(self):
pass
@abstractmethod
def bar(self):
pass
class Impl(Abstract):
pass

View File

@@ -0,0 +1,20 @@
from abc import abstractmethod
class Abstract:
@abstractmethod
def foo(self):
pass
@abstractmethod
def bar(self):
pass
class Impl(Abstract):
def foo(self):
pass
def bar(self):
pass

View File

@@ -5,6 +5,7 @@ package com.jetbrains.python;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.python.codeInsight.override.PyMethodMember;
import com.jetbrains.python.codeInsight.override.PyOverrideImplementUtil;
import com.jetbrains.python.fixtures.PyTestCase;
@@ -14,6 +15,7 @@ import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.stubs.PyClassNameIndex;
import java.util.Arrays;
import java.util.Collections;
/**
@@ -119,6 +121,21 @@ public class PyOverrideTest extends PyTestCase {
doTest();
}
// PY-25906
public void testImplementationOrder() {
myFixture.configureByFile("override/" + getTestName(true) + ".py");
final PyFunction[] toImplement = getTopLevelClass(0).getMethods();
assertEquals(Arrays.asList("foo", "bar"), ContainerUtil.map(toImplement, PyFunction::getName));
PyOverrideImplementUtil.overrideMethods(myFixture.getEditor(),
getTopLevelClass(1),
ContainerUtil.map(toImplement, PyMethodMember::new),
true);
myFixture.checkResultByFile("override/" + getTestName(true) + "_after.py", true);
}
public void testPy3k() {
doTest3k();
}