Skip to content

Commit

Permalink
Still attempts to fix spyder-ide#1785.
Browse files Browse the repository at this point in the history
Comment function :
Adds the comment string (# for Python) plus a space, after the indent,
just before the text

Uncomment function :
Removes the comment string and removes one space if there is one after
the comment string and if the indent length is not an even number std
indent length (to avoid breaking code)
  • Loading branch information
Ericvulpi committed Jul 8, 2017
1 parent bd99672 commit 668f85e
Showing 1 changed file with 62 additions and 32 deletions.
94 changes: 62 additions & 32 deletions spyder/widgets/sourcecode/codeeditor.py
Expand Up @@ -645,7 +645,7 @@ def cursor_move_event():

return [codecomp, duplicate_line, copyline, deleteline, movelineup,
movelinedown, gotodef, toggle_comment, blockcomment,
unblockcomment, transform_uppercase, transform_lowercase,
unblockcomment, transform_uppercase, transform_lowercase,
line_start, line_end, prev_line, next_line,
prev_char, next_char, prev_word, next_word, kill_line_end,
kill_line_start, yank, kill_ring_rotate, kill_prev_word,
Expand Down Expand Up @@ -694,7 +694,7 @@ def setup_editor(self, linenumbers=True, language=None, markers=False,
add_colons=True, auto_unindent=True, indent_chars=" "*4,
tab_stop_width_spaces=4, cloned_from=None, filename=None,
occurrence_timeout=1500):

# Code completion and calltips
self.set_codecompletion_auto(codecompletion_auto)
self.set_codecompletion_case(codecompletion_case)
Expand Down Expand Up @@ -1400,7 +1400,7 @@ def set_edge_line_column(self, column):
"""Set edge line column value"""
self.edge_line.column = column
self.edge_line.update()

# -----blank spaces
def set_blanks_enabled(self, state):
"""Toggle blanks visibility"""
Expand Down Expand Up @@ -1806,7 +1806,7 @@ def process_todo(self, todo_results):


#------Comments/Indentation
def add_prefix(self, prefix):
def add_prefix(self, prefix, **kwargs):
"""Add prefix to current line or selected line(s)"""
cursor = self.textCursor()
if self.has_selected_text():
Expand All @@ -1829,8 +1829,7 @@ def add_prefix(self, prefix):
cursor.setPosition(start_pos)

while cursor.position() >= start_pos:
cursor.movePosition(QTextCursor.StartOfBlock)
cursor.insertText(prefix)
self.add_prefix_to_line(cursor, prefix, **kwargs)
if start_pos == 0 and cursor.blockNumber() == 0:
# Avoid infinite loop when indenting the very first line
break
Expand All @@ -1852,10 +1851,21 @@ def add_prefix(self, prefix):
else:
# Add prefix to current line
cursor.beginEditBlock()
cursor.movePosition(QTextCursor.StartOfBlock)
cursor.insertText(prefix)
self.add_prefix_to_line(cursor, prefix, **kwargs)
cursor.endEditBlock()

def add_prefix_to_line(self, cursor, prefix, after_indent=False, **kwargs):
"""Add prefix to current line"""
cursor.movePosition(QTextCursor.StartOfBlock)
# If after_indent option is true, adds the prefix at the indent
# position instead of at the beginning of the line
if after_indent:
indent = self.get_block_indentation(cursor.blockNumber())
cursor.movePosition(QTextCursor.Right,
QTextCursor.MoveAnchor,
indent)
cursor.insertText(prefix)

def __is_cursor_at_start_of_block(self, cursor):
cursor.movePosition(QTextCursor.StartOfBlock)

Expand All @@ -1869,9 +1879,10 @@ def remove_suffix(self, suffix):
if to_text_string(cursor.selectedText()) == suffix:
cursor.removeSelectedText()

def remove_prefix(self, prefix):
def remove_prefix(self, *args, **kwargs):
"""Remove prefix from current line or selected line(s)"""
cursor = self.textCursor()
cursor.beginEditBlock()
if self.has_selected_text():
# Remove prefix from selected line(s)
start_pos, end_pos = sorted([cursor.selectionStart(),
Expand All @@ -1880,7 +1891,6 @@ def remove_prefix(self, prefix):
if not cursor.atBlockStart():
cursor.movePosition(QTextCursor.StartOfBlock)
start_pos = cursor.position()
cursor.beginEditBlock()
cursor.setPosition(end_pos)
# Check if end_pos is at the start of a block: if so, starting
# changes from the previous block
Expand All @@ -1897,31 +1907,50 @@ def remove_prefix(self, prefix):
break
else:
old_pos = new_pos
line_text = to_text_string(cursor.block().text())
if (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()
self.remove_prefix_from_line(cursor, *args, **kwargs)
cursor.movePosition(QTextCursor.PreviousBlock)
cursor.endEditBlock()
else:
# Remove prefix from current line
cursor.movePosition(QTextCursor.StartOfBlock)
line_text = to_text_string(cursor.block().text())
if (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()
self.remove_prefix_from_line(cursor, *args, **kwargs)
cursor.endEditBlock()


def remove_prefix_from_line(self, cursor, prefix, with_trailing_space=False,
check_indent=False, **kwargs):
"""Remove prefix from current line"""
if with_trailing_space:
initial_indent = self.get_block_indentation(cursor.blockNumber())
line_text = to_text_string(cursor.block().text())
if (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()
# with_trailing_space is an option to remove one space after the
# prefix when applicable (see conditions below)
if with_trailing_space:
remove_space = True
cursor.movePosition(QTextCursor.StartOfBlock)
indent = self.get_block_indentation(cursor.blockNumber())
indent_std = len(self.indent_chars)
# Condition 1 : there is a trailing space to remove
if not indent > initial_indent:
remove_space = False
# Condition 2 : if check_indent is true, check that the indent
# level is not a multiple of std indent length to avoid breaking
# code
if check_indent and indent%indent_std == 0:
remove_space = False
if remove_space:
cursor.movePosition(QTextCursor.StartOfBlock)
cursor.movePosition(QTextCursor.Right,
QTextCursor.KeepAnchor, 1)
cursor.removeSelectedText()

def fix_indent(self, *args, **kwargs):
"""Indent line according to the preferences"""
if self.is_python_like():
Expand Down Expand Up @@ -2258,11 +2287,12 @@ def toggle_comment(self):

def comment(self):
"""Comment current line or selection."""
self.add_prefix(self.comment_string)
self.add_prefix(self.comment_string + " ", after_indent=True)

def uncomment(self):
"""Uncomment current line or selection."""
self.remove_prefix(self.comment_string)
self.remove_prefix(self.comment_string, with_trailing_space=True,
check_indent=True)

def __blockcomment_bar(self):
return self.comment_string + ' ' + '=' * (78 - len(self.comment_string))
Expand Down Expand Up @@ -3176,7 +3206,7 @@ def __init__(self, parent):
self.setStretchFactor(0, 4)
self.setStretchFactor(1, 1)
self.setWindowIcon(ima.icon('spyder'))

def load(self, filename):
self.editor.set_text_from_file(filename)
self.setWindowTitle("%s - %s (%s)" % (_("Editor"),
Expand Down

0 comments on commit 668f85e

Please sign in to comment.