mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-30 10:20:15 +07:00
IDEA-CR-51590: added self parameter for methods on smart enter (PY-35163)
self is inserted for methods that are not class-/staticmethod cls is inserted for classmethod empty parameter list for staticmethod GitOrigin-RevId: 9743ae50c89c069b8123b8b6eb26fb841dd1f5b2
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b2875bdf04
commit
3c203f5382
@@ -19,6 +19,7 @@ import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.jetbrains.python.PyNames;
|
||||
import com.jetbrains.python.PyTokenTypes;
|
||||
import com.jetbrains.python.codeInsight.editorActions.smartEnter.PySmartEnterProcessor;
|
||||
import com.jetbrains.python.psi.PyFunction;
|
||||
@@ -48,13 +49,31 @@ public class PyParameterListFixer extends PyFixer<PyParameterList> {
|
||||
if (lBrace == null) {
|
||||
final String textToInsert = pyFunction.getNameNode() == null ? " (" : "(";
|
||||
document.insertString(parameters.getTextOffset(), textToInsert);
|
||||
insertParametersForMethod(pyFunction, document, parameters.getTextOffset() + textToInsert.length());
|
||||
}
|
||||
else if (parameters.getParameters().length == 0) {
|
||||
document.insertString(lBrace.getTextRange().getEndOffset(), ")");
|
||||
final int lBraceOffset = lBrace.getTextRange().getEndOffset();
|
||||
final int offsetWithParam = insertParametersForMethod(pyFunction, document, lBraceOffset);
|
||||
document.insertString(offsetWithParam, ")");
|
||||
}
|
||||
else {
|
||||
document.insertString(parameters.getTextRange().getEndOffset(), ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int insertParametersForMethod(@NotNull PyFunction pyFunction, @NotNull Document document, int offset) {
|
||||
if (pyFunction.getContainingClass() != null) {
|
||||
final PyFunction.Modifier modifier = pyFunction.getModifier();
|
||||
String parameterName = null;
|
||||
if (modifier == null) parameterName = PyNames.CANONICAL_SELF;
|
||||
else if (modifier == PyFunction.Modifier.CLASSMETHOD) parameterName = PyNames.CANONICAL_CLS;
|
||||
|
||||
if (parameterName != null) {
|
||||
document.insertString(offset, parameterName);
|
||||
return offset + parameterName.length();
|
||||
}
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
class MyClass:
|
||||
|
||||
@classmethod
|
||||
def method<caret>
|
||||
@@ -0,0 +1,5 @@
|
||||
class MyClass:
|
||||
|
||||
@classmethod
|
||||
def method(cls):
|
||||
<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
class MyClass:
|
||||
|
||||
def method<caret>
|
||||
@@ -0,0 +1,4 @@
|
||||
class MyClass:
|
||||
|
||||
def method(self):
|
||||
<caret>
|
||||
@@ -0,0 +1,4 @@
|
||||
class MyClass:
|
||||
|
||||
@staticmethod
|
||||
def method<caret>
|
||||
@@ -0,0 +1,5 @@
|
||||
class MyClass:
|
||||
|
||||
@staticmethod
|
||||
def method():
|
||||
<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
class MyClass:
|
||||
|
||||
def method(self, x, y<caret>
|
||||
@@ -0,0 +1,4 @@
|
||||
class MyClass:
|
||||
|
||||
def method(self, x, y):
|
||||
<caret>
|
||||
@@ -0,0 +1,3 @@
|
||||
class MyClass:
|
||||
|
||||
def method(<caret>
|
||||
@@ -0,0 +1,4 @@
|
||||
class MyClass:
|
||||
|
||||
def method(self):
|
||||
<caret>
|
||||
@@ -188,6 +188,31 @@ public class PySmartEnterTest extends PyTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-35163
|
||||
public void testMethodParameterNoDecorators() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-35163
|
||||
public void testMethodParameterClassMethod() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-35163
|
||||
public void testMethodParameterStaticMethod() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-35163
|
||||
public void testMethodParameterWithExistingParameters() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-35163
|
||||
public void testMethodParameterWithOpenBracket() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-12877
|
||||
public void testWithTargetOmitted() {
|
||||
doTest();
|
||||
|
||||
Reference in New Issue
Block a user