PY-467: Do not treat self as method parameter in call in method case

This commit is contained in:
Oleg Shpynov
2010-02-04 12:45:18 +03:00
parent fae1fc8983
commit eded2a9c86
5 changed files with 25 additions and 20 deletions
@@ -8,6 +8,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyImportStatementNavigator;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import java.util.*;
@@ -61,6 +62,10 @@ public class PyCodeFragmentBuilder extends PyRecursiveElementVisitor {
processDeclaration(element);
return;
}
// Ignore self as local variable usage in case of method context
if (PyPsiUtils.isMethodContext(element) && "self".equals(element.getName())){
return;
}
final Position position = CodeFragmentUtil.getPosition(element, startOffset, endOffset);
final String name = element.getName();
@@ -24,8 +24,8 @@ public class PyCodeFragmentUtil {
}
public static CodeFragment createCodeFragment(@NotNull final ScopeOwner owner,
@NotNull final PsiElement startInScope,
@NotNull final PsiElement endInScope) throws CannotCreateCodeFragmentException {
@NotNull final PsiElement startInScope,
@NotNull final PsiElement endInScope) throws CannotCreateCodeFragmentException {
final int start = startInScope.getTextOffset();
final int end = endInScope.getTextOffset() + endInScope.getTextLength();
@@ -209,4 +209,8 @@ public class PyPsiUtils {
statements[0].delete();
}
}
public static boolean isMethodContext(final PsiElement element) {
return PsiTreeUtil.getParentOfType(element, PyFile.class, PyClass.class) instanceof PyClass;
}
}
@@ -68,7 +68,7 @@ public class PyExtractMethodUtil {
generatedMethod = insertGeneratedMethod(statement1, generatedMethod);
// Process parameters
final boolean isMethod = isMethodContext(generatedMethod);
final boolean isMethod = PyPsiUtils.isMethodContext(generatedMethod);
processParameters(project, generatedMethod, variableData, isMethod);
// Generating call element
@@ -116,7 +116,7 @@ public class PyExtractMethodUtil {
generatedMethod = insertGeneratedMethod(statement1, generatedMethod);
// Process parameters
final boolean isMethod = isMethodContext(generatedMethod);
final boolean isMethod = PyPsiUtils.isMethodContext(generatedMethod);
processParameters(project, generatedMethod, variableData, isMethod);
// Generate call element
@@ -140,10 +140,6 @@ public class PyExtractMethodUtil {
}
}
private static boolean isMethodContext(PyFunction generatedMethod) {
return PsiTreeUtil.getParentOfType(generatedMethod, PyFile.class, PyClass.class) instanceof PyClass;
}
public static void extractFromExpression(final Project project,
final Editor editor,
final CodeFragment fragment,
@@ -179,7 +175,7 @@ public class PyExtractMethodUtil {
generatedMethod = insertGeneratedMethod(expression, generatedMethod);
// Process parameters
final boolean isMethod = isMethodContext(generatedMethod);
final boolean isMethod = PyPsiUtils.isMethodContext(generatedMethod);
processParameters(project, generatedMethod, variableData, isMethod);
// Generating call element
@@ -372,24 +368,24 @@ public class PyExtractMethodUtil {
return LanguageNamesValidation.INSTANCE.forLanguage(PythonLanguage.getInstance()).isIdentifier(name, project);
}
};
final boolean isMethod = PyPsiUtils.isMethodContext(element);
final ExtractMethodDecorator decorator = new ExtractMethodDecorator() {
public String createMethodPreview(final String methodName, final AbstractVariableData[] variableDatas) {
final StringBuilder builder = new StringBuilder();
builder.append("def ").append(methodName);
builder.append("(");
boolean first = true;
if (isMethod) {
builder.append("self");
}
for (AbstractVariableData variableData : variableDatas) {
if (variableData.passAsParameter) {
if (first) {
first = false;
}
else {
if (builder.length() != 0) {
builder.append(", ");
}
builder.append(variableData.name);
}
}
builder.insert(0, "(");
builder.insert(0, methodName);
builder.insert(0, "def ");
builder.append(")");
return builder.toString();
}
@@ -101,7 +101,7 @@ public class PyExtractMethodTest extends LightMarkedTestCase {
doTest("context/methodindent.before.py", "bar", "context/methodindent.after.py");
}
//public void testMethodReturn() throws Throwable {
// doTest("context/methodreturn.before.py", "bar", "context/methodreturn.after.py");
//}
public void testMethodReturn() throws Throwable {
doTest("context/methodreturn.before.py", "bar", "context/methodreturn.after.py");
}
}