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: Allow undo/redo on Source > Fix indentation #5657

Merged
merged 2 commits into from
Dec 26, 2017
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
8 changes: 5 additions & 3 deletions spyder/widgets/sourcecode/codeeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,12 +907,14 @@ def remove_trailing_spaces(self):
cursor.endEditBlock()

def fix_indentation(self):
"""Replace tabs by spaces"""
"""Replace tabs by 4 spaces."""
Copy link
Member

@goanpeca goanpeca Nov 6, 2017

Choose a reason for hiding this comment

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

@csabella isn't this fix dependent on the preference setting for what a tab should represent? Python is 4 but a user could change this to 3 or 2 if they follow google's ugly style guide.

Not sure this is the case but if it is then.

"""Replace tabs by number of spaces (for tabs) defined in the preferences."""

Copy link
Member

Choose a reason for hiding this comment

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

Yes, @goanpeca is right about this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't use the settings now. It's hard-coded as 4, which is why I made the comment. It's in sourcecode/utils.py.

def fix_indentation(text):
    """Replace tabs by spaces"""
    return text.replace('\t', ' '*4)

Copy link
Member

@goanpeca goanpeca Nov 6, 2017

Choose a reason for hiding this comment

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

@csabella so then we need to fix fix_indentation perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@goanpeca Sure. Should it be in this PR or a separate issue?

Copy link
Member

Choose a reason for hiding this comment

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

I think @dalthviz fixed this one. @dalthviz?

Copy link
Member

Choose a reason for hiding this comment

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

I fixed something regarding the use of preferences but in the comment/uncomment functionality in #5470, is that related with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's similar, but not the same.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, let's leave this for another PR then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened issue #5678 for the number of spaces.

text_before = to_text_string(self.toPlainText())
text_after = sourcecode.fix_indentation(text_before)
if text_before != text_after:
self.setPlainText(text_after)
self.document().setModified(True)
# We do the following rather than using self.setPlainText
# to benefit from QTextEdit's undo/redo feature.
self.selectAll()
self.insertPlainText(text_after)
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch!


def get_current_object(self):
"""Return current object (string) """
Expand Down
32 changes: 31 additions & 1 deletion spyder/widgets/sourcecode/tests/test_indentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_single_line_indent(code_editor_indent_bot):


def test_selection_indent(code_editor_indent_bot):
"""Test indentation with selection of more tha one line."""
"""Test indentation with selection of more than one line."""
editor, qtbot = code_editor_indent_bot
text = ("class a():\n"
"self.b = 1\n"
Expand All @@ -84,5 +84,35 @@ def test_selection_indent(code_editor_indent_bot):
)


def test_fix_indentation(code_editor_indent_bot):
"""Test fix_indentation() method."""
editor, qtbot = code_editor_indent_bot
# Contains tabs.
original = ("\t\n"
"class a():\t\n"
"\tself.b = 1\n"
"\tprint(self.b)\n"
"\n"
)
# Fix indentation replaces tabs with 4 spaces and not indent_chars spaces.
fixed = (" \n"
"class a(): \n"
" self.b = 1\n"
" print(self.b)\n"
"\n"
)
editor.set_text(original)
editor.fix_indentation()
assert to_text_string(editor.toPlainText()) == fixed
assert editor.document().isModified()
# Test that undo/redo works - issue 1754.
editor.undo()
assert to_text_string(editor.toPlainText()) == original
assert not editor.document().isModified()
editor.redo()
assert to_text_string(editor.toPlainText()) == fixed
assert editor.document().isModified()


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