From 838a58f73355104a7a3fb1ddf0bf5b6ee70e7c50 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 14 Jan 2011 19:32:28 +0100 Subject: [PATCH] avoid inserting duplicate colon when adding 'self' (PY-2652) --- .../python/codeInsight/PyMethodNameTypedHandler.java | 9 +++++++-- .../testData/editing/firstParamDuplicateColon.after.py | 2 ++ python/testData/editing/firstParamDuplicateColon.py | 2 ++ python/testSrc/com/jetbrains/python/PyEditingTest.java | 4 ++++ 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 python/testData/editing/firstParamDuplicateColon.after.py create mode 100644 python/testData/editing/firstParamDuplicateColon.py diff --git a/python/src/com/jetbrains/python/codeInsight/PyMethodNameTypedHandler.java b/python/src/com/jetbrains/python/codeInsight/PyMethodNameTypedHandler.java index a16636998e1d..91486933e582 100644 --- a/python/src/com/jetbrains/python/codeInsight/PyMethodNameTypedHandler.java +++ b/python/src/com/jetbrains/python/codeInsight/PyMethodNameTypedHandler.java @@ -14,7 +14,6 @@ import com.jetbrains.python.PyNames; import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.PythonFileType; import com.jetbrains.python.psi.PyFunction; -import com.jetbrains.python.psi.PyStringLiteralExpression; import com.jetbrains.python.psi.PyUtil; /** @@ -55,7 +54,13 @@ public class PyMethodNameTypedHandler extends TypedHandlerDelegate { else if (flags.isStaticMethod()) pname=""; documentManager.commitDocument(document); // TODO: only print the ")" if Settings require it - EditorModificationUtil.typeInStringAtCaretHonorBlockSelection(editor, "("+pname+"):", true); + int caretOffset = editor.getCaretModel().getOffset(); + String textToType = "(" + pname + ")"; + CharSequence chars = editor.getDocument().getCharsSequence(); + if (caretOffset == chars.length() || chars.charAt(caretOffset) != ':') { + textToType += ':'; + } + EditorModificationUtil.typeInStringAtCaretHonorBlockSelection(editor, textToType, true); editor.getCaretModel().moveToOffset(offset + 1 + pname.length()); // right after param name return Result.STOP; } diff --git a/python/testData/editing/firstParamDuplicateColon.after.py b/python/testData/editing/firstParamDuplicateColon.after.py new file mode 100644 index 000000000000..902d48453bff --- /dev/null +++ b/python/testData/editing/firstParamDuplicateColon.after.py @@ -0,0 +1,2 @@ +class C: + def someDefName(self): \ No newline at end of file diff --git a/python/testData/editing/firstParamDuplicateColon.py b/python/testData/editing/firstParamDuplicateColon.py new file mode 100644 index 000000000000..2945e1464cf2 --- /dev/null +++ b/python/testData/editing/firstParamDuplicateColon.py @@ -0,0 +1,2 @@ +class C: + def someDefName: \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyEditingTest.java b/python/testSrc/com/jetbrains/python/PyEditingTest.java index 9e4bde1d236a..9333a685c1fb 100644 --- a/python/testSrc/com/jetbrains/python/PyEditingTest.java +++ b/python/testSrc/com/jetbrains/python/PyEditingTest.java @@ -233,4 +233,8 @@ public class PyEditingTest extends PyLightFixtureTestCase { public void testFirstParamStaticmethod() { doTypingTest('('); } + + public void testFirstParamDuplicateColon() { // PY-2652 + doTypingTest('('); + } }