diff --git a/spyder/plugins/editor/widgets/codeeditor/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor/codeeditor.py index bf350325348..b544ac6ee32 100644 --- a/spyder/plugins/editor/widgets/codeeditor/codeeditor.py +++ b/spyder/plugins/editor/widgets/codeeditor/codeeditor.py @@ -2374,43 +2374,20 @@ def remove_prefix(self, prefix): def __remove_prefix(self, prefix, cursor, line_text): """Handle the removal of the prefix for a single line.""" - start_with_space = line_text.startswith(' ') - if start_with_space: - left_spaces = self.__even_number_of_spaces(line_text) - else: - left_spaces = False - if start_with_space: - right_number_spaces = self.__number_of_spaces(line_text, group=1) - else: - right_number_spaces = self.__number_of_spaces(line_text) + cursor.movePosition(QTextCursor.Right, + QTextCursor.MoveAnchor, + line_text.find(prefix)) # Handle prefix remove for comments with spaces if (prefix.strip() and line_text.lstrip().startswith(prefix + ' ') or line_text.startswith(prefix + ' ') and '#' in prefix): cursor.movePosition(QTextCursor.Right, - QTextCursor.MoveAnchor, - line_text.find(prefix)) - if (right_number_spaces == 1 - and (left_spaces or not start_with_space) - or (not start_with_space and right_number_spaces % 2 != 0) - or (left_spaces and right_number_spaces % 2 != 0)): - # Handle inserted '# ' with the count of the number of spaces - # at the right and left of the prefix. - cursor.movePosition(QTextCursor.Right, - QTextCursor.KeepAnchor, len(prefix + ' ')) - else: - # Handle manual insertion of '#' - cursor.movePosition(QTextCursor.Right, - QTextCursor.KeepAnchor, len(prefix)) - cursor.removeSelectedText() + QTextCursor.KeepAnchor, len(prefix + ' ')) # Check for prefix without space elif (prefix.strip() and line_text.lstrip().startswith(prefix) or line_text.startswith(prefix)): - cursor.movePosition(QTextCursor.Right, - QTextCursor.MoveAnchor, - line_text.find(prefix)) cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor, len(prefix)) - cursor.removeSelectedText() + cursor.removeSelectedText() def __even_number_of_spaces(self, line_text, group=0): """ diff --git a/spyder/plugins/editor/widgets/codeeditor/tests/test_comments.py b/spyder/plugins/editor/widgets/codeeditor/tests/test_comments.py index b390af19cd4..9a28c8c2c1b 100644 --- a/spyder/plugins/editor/widgets/codeeditor/tests/test_comments.py +++ b/spyder/plugins/editor/widgets/codeeditor/tests/test_comments.py @@ -62,21 +62,21 @@ def test_single_line_comment(codeeditor): # Toggle comment with space at the right of prefix but manually inserted text = toggle_comment(editor, start_line=2) assert text == ("class a():\n" - " self.b = 1\n" + " self.b = 1\n" " # print(self.b)\n" "# \n" ) # Toggle comment with space insertion text = toggle_comment(editor, start_line=2) assert text == ("class a():\n" - " # self.b = 1\n" + " # self.b = 1\n" " # print(self.b)\n" "# \n" ) # Toggle comment deleting inserted space text = toggle_comment(editor, start_line=2) assert text == ("class a():\n" - " self.b = 1\n" + " self.b = 1\n" " # print(self.b)\n" "# \n" ) @@ -84,8 +84,8 @@ def test_single_line_comment(codeeditor): # but manually inserted text = toggle_comment(editor, start_line=3) assert text == ("class a():\n" - " self.b = 1\n" - " print(self.b)\n" + " self.b = 1\n" + " print(self.b)\n" "# \n" ) @@ -102,23 +102,23 @@ def test_selection_comment(codeeditor): # Toggle manually commented code text = toggle_comment(editor, single_line=False) assert text == ("class a():\n" - " self.b = 1\n" - " print(self.b)\n" - " \n" + " self.b = 1\n" + " print(self.b)\n" + " \n" ) # Toggle comment inserting prefix and space text = toggle_comment(editor, single_line=False) assert text == ("# class a():\n" - "# self.b = 1\n" - "# print(self.b)\n" - " \n" + "# self.b = 1\n" + "# print(self.b)\n" + " \n" ) # Toggle comment deleting inserted prefix and space text = toggle_comment(editor, single_line=False) assert text == ("class a():\n" - " self.b = 1\n" - " print(self.b)\n" - " \n" + " self.b = 1\n" + " print(self.b)\n" + " \n" ) # Test compatibility with Spyder 3 commenting structure text = ("#class a():\n" @@ -130,9 +130,9 @@ def test_selection_comment(codeeditor): # Toggle comment deleting inserted prefix (without space) text = toggle_comment(editor, single_line=False) assert text == ("class a():\n" - " self.b = 1\n" - " print(self.b)\n" - " \n" + " self.b = 1\n" + " print(self.b)\n" + " \n" )