From f972db4a88a3b8a6bb6b2d89cb6a9692b136e044 Mon Sep 17 00:00:00 2001 From: Rafael Laverde Date: Tue, 6 Jun 2017 09:23:38 -0500 Subject: [PATCH 1/3] Add shortcuts for indent and unident in the editor. --- spyder/config/main.py | 2 ++ spyder/widgets/sourcecode/codeeditor.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spyder/config/main.py b/spyder/config/main.py index 3f7c559e33b..d35ec543cbd 100755 --- a/spyder/config/main.py +++ b/spyder/config/main.py @@ -367,6 +367,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", diff --git a/spyder/widgets/sourcecode/codeeditor.py b/spyder/widgets/sourcecode/codeeditor.py index 705be9b53fb..aaeaeab22c3 100644 --- a/spyder/widgets/sourcecode/codeeditor.py +++ b/spyder/widgets/sourcecode/codeeditor.py @@ -549,6 +549,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") """ @@ -632,7 +638,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): """ From 4ef50f6a76441f75e54500d8ed3195987166cc34 Mon Sep 17 00:00:00 2001 From: Rafael Laverde Date: Tue, 6 Jun 2017 10:59:23 -0500 Subject: [PATCH 2/3] Add simple indentation for non python files. --- spyder/widgets/sourcecode/codeeditor.py | 28 ++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/spyder/widgets/sourcecode/codeeditor.py b/spyder/widgets/sourcecode/codeeditor.py index aaeaeab22c3..0f20b804318 100644 --- a/spyder/widgets/sourcecode/codeeditor.py +++ b/spyder/widgets/sourcecode/codeeditor.py @@ -1909,7 +1909,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 From 8c4a8766fa55232183b8f48e4e1736670ffee9bf Mon Sep 17 00:00:00 2001 From: Rafael Laverde Date: Tue, 6 Jun 2017 10:54:17 -0500 Subject: [PATCH 3/3] Add tests for simple indentation. --- .../sourcecode/tests/test_autoindent.py | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/spyder/widgets/sourcecode/tests/test_autoindent.py b/spyder/widgets/sourcecode/tests/test_autoindent.py index 4775f0e20e7..7976887cf05 100644 --- a/spyder/widgets/sourcecode/tests/test_autoindent.py +++ b/spyder/widgets/sourcecode/tests/test_autoindent.py @@ -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) @@ -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()