Skip to content

Commit

Permalink
Merge pull request #14768 from remisalmon/fix_uncomments
Browse files Browse the repository at this point in the history
PR: Fix commenting/uncommenting to not change leading whitespaces
  • Loading branch information
ccordoba12 authored Nov 30, 2023
2 parents 4749cb2 + 4ae32bf commit 19a3979
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 45 deletions.
33 changes: 5 additions & 28 deletions spyder/plugins/editor/widgets/codeeditor/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
34 changes: 17 additions & 17 deletions spyder/plugins/editor/widgets/codeeditor/tests/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,30 @@ 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"
)
# Toggle comment with space at the right and left of prefix
# 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"
)

Expand All @@ -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"
Expand All @@ -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"
)


Expand Down

0 comments on commit 19a3979

Please sign in to comment.