Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add indentation shortcuts and simple indentation for non-Python files #4567

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions spyder/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+]',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rlaverde, these shortcuts are used in the reversed, i.e.

  • Ctrl + ] -> Indent
  • Ctrl + [ -> Unindent

At least that's the case in Sublime: https://gist.github.com/eteanga/1736542

So please open a new PR to fix that.

'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 @@ -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")
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -1902,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
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()