Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Fix commenting/uncommenting to not change leading whitespaces #14768

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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