From 99f4933e7087433bcf755260b2f2589e9a65c0ff Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 18 Nov 2014 01:18:16 +0300 Subject: [PATCH] PY-9209 Insert a space after '#' sign when using smart enter in comment --- .../PyCommentBreakerEnterProcessor.java | 10 +++++++--- python/testData/codeInsight/smartEnter/comment.py | 2 +- .../testData/codeInsight/smartEnter/comment_after.py | 4 ++-- .../smartEnter/spaceInsertedAfterHashSignInComment.py | 2 ++ .../spaceInsertedAfterHashSignInComment_after.py | 3 +++ .../testSrc/com/jetbrains/python/PySmartEnterTest.java | 5 +++++ 6 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment.py create mode 100644 python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment_after.py diff --git a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/enterProcessors/PyCommentBreakerEnterProcessor.java b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/enterProcessors/PyCommentBreakerEnterProcessor.java index ee8394defe9e..0a6abd4b084b 100644 --- a/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/enterProcessors/PyCommentBreakerEnterProcessor.java +++ b/python/src/com/jetbrains/python/codeInsight/editorActions/smartEnter/enterProcessors/PyCommentBreakerEnterProcessor.java @@ -19,6 +19,7 @@ import com.intellij.openapi.editor.CaretModel; import com.intellij.openapi.editor.Editor; import com.intellij.psi.PsiComment; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiWhiteSpace; import com.intellij.psi.util.PsiTreeUtil; import com.jetbrains.python.codeInsight.editorActions.smartEnter.SmartEnterUtil; @@ -34,12 +35,15 @@ public class PyCommentBreakerEnterProcessor implements EnterProcessor { return false; } final CaretModel caretModel = editor.getCaretModel(); - final PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset()); + PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset()); + if (atCaret instanceof PsiWhiteSpace) { + atCaret = atCaret.getPrevSibling(); + } final PsiElement comment = PsiTreeUtil.getParentOfType(atCaret, PsiComment.class, false); if (comment != null) { SmartEnterUtil.plainEnter(editor); - editor.getDocument().insertString(caretModel.getOffset(), "#"); - caretModel.moveToOffset(caretModel.getOffset() + 1); + editor.getDocument().insertString(caretModel.getOffset(), "# "); + caretModel.moveToOffset(caretModel.getOffset() + 2); return true; } return false; diff --git a/python/testData/codeInsight/smartEnter/comment.py b/python/testData/codeInsight/smartEnter/comment.py index c776b2a05afb..edf9e80daaf9 100644 --- a/python/testData/codeInsight/smartEnter/comment.py +++ b/python/testData/codeInsight/smartEnter/comment.py @@ -1 +1 @@ -#comment \ No newline at end of file +# comment \ No newline at end of file diff --git a/python/testData/codeInsight/smartEnter/comment_after.py b/python/testData/codeInsight/smartEnter/comment_after.py index 374c22d6a10a..129e53e20712 100644 --- a/python/testData/codeInsight/smartEnter/comment_after.py +++ b/python/testData/codeInsight/smartEnter/comment_after.py @@ -1,2 +1,2 @@ -#comment -# \ No newline at end of file +# comment +# \ No newline at end of file diff --git a/python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment.py b/python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment.py new file mode 100644 index 000000000000..d2f1f9bdb19d --- /dev/null +++ b/python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment.py @@ -0,0 +1,2 @@ +# foo +pass \ No newline at end of file diff --git a/python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment_after.py b/python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment_after.py new file mode 100644 index 000000000000..9e0b9db075d8 --- /dev/null +++ b/python/testData/codeInsight/smartEnter/spaceInsertedAfterHashSignInComment_after.py @@ -0,0 +1,3 @@ +# foo +# +pass \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PySmartEnterTest.java b/python/testSrc/com/jetbrains/python/PySmartEnterTest.java index e58988010645..a4db79654658 100644 --- a/python/testSrc/com/jetbrains/python/PySmartEnterTest.java +++ b/python/testSrc/com/jetbrains/python/PySmartEnterTest.java @@ -189,4 +189,9 @@ public class PySmartEnterTest extends PyTestCase { public void testWithOnlyColonMissing() { doTest(); } + + // PY-9209 + public void testSpaceInsertedAfterHashSignInComment() { + doTest(); + } }