Skip to content

Commit 517aaa4

Browse files
authored
Merge pull request #4567 from rlaverde/indentation-shortcuts-and-simple-indentation
PR: Add indentation shortcuts and simple indentation for non-Python files
2 parents 37f0c9c + 8c4a876 commit 517aaa4

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

Diff for: spyder/config/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@
367367
'editor/delete line': 'Ctrl+D',
368368
'editor/transform to uppercase': 'Ctrl+Shift+U',
369369
'editor/transform to lowercase': 'Ctrl+U',
370+
'editor/indent': 'Ctrl+[',
371+
'editor/unindent': 'Ctrl+]',
370372
'editor/move line up': "Alt+Up",
371373
'editor/move line down': "Alt+Down",
372374
'editor/go to definition': "Ctrl+G",

Diff for: spyder/widgets/sourcecode/codeeditor.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,12 @@ def create_shortcuts(self):
549549
name='Transform to lowercase',
550550
parent=self)
551551

552+
indent = config_shortcut(lambda: self.indent(force=True),
553+
context='Editor', name='Indent', parent=self)
554+
unindent = config_shortcut(lambda: self.unindent(force=True),
555+
context='Editor', name='Unindent',
556+
parent=self)
557+
552558
def cb_maker(attr):
553559
"""Make a callback for cursor move event type, (e.g. "Start")
554560
"""
@@ -632,7 +638,8 @@ def cursor_move_event():
632638
prev_char, next_char, prev_word, next_word, kill_line_end,
633639
kill_line_start, yank, kill_ring_rotate, kill_prev_word,
634640
kill_next_word, start_doc, end_doc, undo, redo, cut, copy,
635-
paste, delete, select_all, array_inline, array_table]
641+
paste, delete, select_all, array_inline, array_table, indent,
642+
unindent]
636643

637644
def get_shortcut_data(self):
638645
"""
@@ -1902,7 +1909,33 @@ def remove_prefix(self, prefix):
19021909
QTextCursor.KeepAnchor, len(prefix))
19031910
cursor.removeSelectedText()
19041911

1905-
def fix_indent(self, forward=True, comment_or_string=False):
1912+
1913+
def fix_indent(self, *args, **kwargs):
1914+
"""Indent line according to the preferences"""
1915+
if self.is_python_like():
1916+
self.fix_indent_smart(*args, **kwargs)
1917+
else:
1918+
self.simple_indentation(*args, **kwargs)
1919+
1920+
1921+
def simple_indentation(self, forward=True, **kwargs):
1922+
"""
1923+
Simply preserve the indentation-level of the previous line.
1924+
"""
1925+
cursor = self.textCursor()
1926+
block_nb = cursor.blockNumber()
1927+
prev_block = self.document().findBlockByLineNumber(block_nb-1)
1928+
prevline = to_text_string(prev_block.text())
1929+
1930+
indentation = re.match(r"\s*", prevline).group()
1931+
# Unident
1932+
if not forward:
1933+
indentation = indentation[len(self.indent_chars):]
1934+
1935+
cursor.insertText(indentation)
1936+
1937+
1938+
def fix_indent_smart(self, forward=True, comment_or_string=False):
19061939
"""
19071940
Fix indentation (Python only, no text selection)
19081941
forward=True: fix indent only if text is not enough indented

Diff for: spyder/widgets/sourcecode/tests/test_autoindent.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
# --- Fixtures
2222
# -----------------------------------------------------------------------------
2323
def get_indent_fix(text, indent_chars=" " * 4, tab_stop_width_spaces=4,
24-
sol=False, forward=True):
24+
sol=False, forward=True, language='Python'):
2525
"""Return text with last line's indentation fixed."""
2626
app = qapplication()
2727
editor = CodeEditor(parent=None)
28-
editor.setup_editor(language='Python', indent_chars=indent_chars,
28+
editor.setup_editor(language=language, indent_chars=indent_chars,
2929
tab_stop_width_spaces=tab_stop_width_spaces)
3030

3131
editor.set_text(text)
@@ -242,5 +242,23 @@ def test_unindentation_with_tabs(text_input, expected, test_text,
242242
assert text == expected, test_text
243243

244244

245+
# --- Simple indentation tests
246+
# -----------------------------------------------------------------------------
247+
248+
@pytest.mark.parametrize(
249+
"text_input, expected, test_text",
250+
[
251+
("hola\n", "hola\n", "witout indentation"),
252+
(" hola\n", " hola\n ", "some indentation"),
253+
("\thola\n", "\thola\n\t", "tab indentation"),
254+
(" hola(\n", " hola(\n ", "line with parenthesis"),
255+
])
256+
def test_simple_indentation(text_input, expected, test_text,):
257+
# language None deactivate smart indentation
258+
text = get_indent_fix(text_input, language=None)
259+
assert text == expected, test_text
260+
261+
262+
245263
if __name__ == "__main__":
246264
pytest.main()

0 commit comments

Comments
 (0)