Skip to content

Commit

Permalink
Merge from 3.x: PR #4567
Browse files Browse the repository at this point in the history
Fixes #3406
Fixes #4157
  • Loading branch information
ccordoba12 committed Jun 7, 2017
2 parents 92869e3 + 517aaa4 commit ae42c6e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
2 changes: 2 additions & 0 deletions spyder/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@
'editor/delete line': 'Ctrl+D',
'editor/transform to uppercase': 'Ctrl+Shift+U',
'editor/transform to lowercase': 'Ctrl+U',
'editor/indent': 'Ctrl+[',
'editor/unindent': 'Ctrl+]',
'editor/move line up': "Alt+Up",
'editor/move line down': "Alt+Down",
'editor/go to definition': "Ctrl+G",
Expand Down
37 changes: 35 additions & 2 deletions spyder/widgets/sourcecode/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,12 @@ def create_shortcuts(self):
name='Transform to lowercase',
parent=self)

indent = config_shortcut(lambda: self.indent(force=True),
context='Editor', name='Indent', parent=self)
unindent = config_shortcut(lambda: self.unindent(force=True),
context='Editor', name='Unindent',
parent=self)

def cb_maker(attr):
"""Make a callback for cursor move event type, (e.g. "Start")
"""
Expand Down Expand Up @@ -517,7 +523,8 @@ def cursor_move_event():
prev_char, next_char, prev_word, next_word, kill_line_end,
kill_line_start, yank, kill_ring_rotate, kill_prev_word,
kill_next_word, start_doc, end_doc, undo, redo, cut, copy,
paste, delete, select_all, array_inline, array_table]
paste, delete, select_all, array_inline, array_table, indent,
unindent]

def get_shortcut_data(self):
"""
Expand Down Expand Up @@ -1564,7 +1571,33 @@ def remove_prefix(self, prefix):
QTextCursor.KeepAnchor, len(prefix))
cursor.removeSelectedText()

def fix_indent(self, forward=True, comment_or_string=False):

def fix_indent(self, *args, **kwargs):
"""Indent line according to the preferences"""
if self.is_python_like():
self.fix_indent_smart(*args, **kwargs)
else:
self.simple_indentation(*args, **kwargs)


def simple_indentation(self, forward=True, **kwargs):
"""
Simply preserve the indentation-level of the previous line.
"""
cursor = self.textCursor()
block_nb = cursor.blockNumber()
prev_block = self.document().findBlockByLineNumber(block_nb-1)
prevline = to_text_string(prev_block.text())

indentation = re.match(r"\s*", prevline).group()
# Unident
if not forward:
indentation = indentation[len(self.indent_chars):]

cursor.insertText(indentation)


def fix_indent_smart(self, forward=True, comment_or_string=False):
"""
Fix indentation (Python only, no text selection)
forward=True: fix indent only if text is not enough indented
Expand Down
22 changes: 20 additions & 2 deletions spyder/widgets/sourcecode/tests/test_autoindent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
# --- Fixtures
# -----------------------------------------------------------------------------
def get_indent_fix(text, indent_chars=" " * 4, tab_stop_width_spaces=4,
sol=False, forward=True):
sol=False, forward=True, language='Python'):
"""Return text with last line's indentation fixed."""
app = qapplication()
editor = CodeEditor(parent=None)
editor.setup_editor(language='Python', indent_chars=indent_chars,
editor.setup_editor(language=language, indent_chars=indent_chars,
tab_stop_width_spaces=tab_stop_width_spaces)

editor.set_text(text)
Expand Down Expand Up @@ -242,5 +242,23 @@ def test_unindentation_with_tabs(text_input, expected, test_text,
assert text == expected, test_text


# --- Simple indentation tests
# -----------------------------------------------------------------------------

@pytest.mark.parametrize(
"text_input, expected, test_text",
[
("hola\n", "hola\n", "witout indentation"),
(" hola\n", " hola\n ", "some indentation"),
("\thola\n", "\thola\n\t", "tab indentation"),
(" hola(\n", " hola(\n ", "line with parenthesis"),
])
def test_simple_indentation(text_input, expected, test_text,):
# language None deactivate smart indentation
text = get_indent_fix(text_input, language=None)
assert text == expected, test_text



if __name__ == "__main__":
pytest.main()

0 comments on commit ae42c6e

Please sign in to comment.