Skip to content

Commit

Permalink
Fix commenting/uncommenting uneven indents
Browse files Browse the repository at this point in the history
  • Loading branch information
remisalmon committed Feb 16, 2021
1 parent b7f19e8 commit 653d6ae
Showing 1 changed file with 7 additions and 39 deletions.
46 changes: 7 additions & 39 deletions spyder/plugins/editor/widgets/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3303,54 +3303,22 @@ 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)
# Handle prefix remove for comments with spaces
cursor.movePosition(QTextCursor.Right,
QTextCursor.MoveAnchor,
line_text.find(prefix))
if (prefix.strip() and line_text.lstrip().startswith(prefix + ' ')
or line_text.startswith(prefix + ' ') and '#' in prefix):
# Handle prefix for comments with spaces
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()
# Check for prefix without space
QTextCursor.KeepAnchor, len(prefix + ' '))
elif (prefix.strip() and line_text.lstrip().startswith(prefix)
or line_text.startswith(prefix)):
cursor.movePosition(QTextCursor.Right,
QTextCursor.MoveAnchor,
line_text.find(prefix))
# Handle prefix without space
cursor.movePosition(QTextCursor.Right,
QTextCursor.KeepAnchor, len(prefix))
cursor.removeSelectedText()

cursor.removeSelectedText()
self.document_did_change()

def __even_number_of_spaces(self, line_text, group=0):
"""
Get if there is a correct indentation from a group of spaces of a line.
"""
spaces = re.findall(r'\s+', line_text)
if len(spaces) - 1 >= group:
return len(spaces[group]) % len(self.indent_chars) == 0

def __number_of_spaces(self, line_text, group=0):
"""Get the number of spaces from a group of spaces in a line."""
spaces = re.findall(r'\s+', line_text)
Expand Down

0 comments on commit 653d6ae

Please sign in to comment.