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: Some indentation fixes #4486

Merged
merged 2 commits into from
May 19, 2017
Merged
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions spyder/widgets/sourcecode/codeeditor.py
Original file line number Diff line number Diff line change
@@ -1925,6 +1925,11 @@ def fix_indent(self, forward=True, comment_or_string=False):
cursor.movePosition(QTextCursor.PreviousBlock)
prevtext = to_text_string(cursor.block().text()).rstrip()

# Remove inline comment
inline_comment = prevtext.find('#')
Copy link
Member

Choose a reason for hiding this comment

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

Is this enough to detect an inline comment? What if you have a line like

# foo

?

Copy link
Member Author

Choose a reason for hiding this comment

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

prevtext = "" and the indentation will be calculated to 0 as expected.

I was testing and It fails in expressions with # that isn't a comment, and the crop cause to drop a bracket, although it is pretty rare (e. g. a list assignment to a dict element that contains a "#"):

a_dict["#"] = (

a_dict["#"] = (
       |

if inline_comment != -1:
prevtext = prevtext[:inline_comment]

if ((self.is_python_like() and
not prevtext.strip().startswith('#') and prevtext) or
prevtext):
20 changes: 5 additions & 15 deletions spyder/widgets/sourcecode/tests/test_autoindent.py
Original file line number Diff line number Diff line change
@@ -160,19 +160,14 @@ def test_first_line():
"test_return"),
("def some_func():\n returns = 10\n", "def some_func():\n returns = 10\n ",
"test_return_not_keyword"),
pytest.mark.xfail(
("foo = 1 # Comment open parenthesis (\n",
("foo = 1 # Comment open parenthesis (\n",
"foo = 1 # Comment open parenthesis (\n",
"test_comment_with parenthesis")),
"test_comment_with parenthesis"),
])
def test_indentation_with_spaces(text_input, expected, test_text):
text = get_indent_fix(text_input)
assert text == expected, test_text

# --- Failing tests
# -----------------------------------------------------------------------------
@pytest.mark.xfail
def test_def_with_unindented_comment():
text = get_indent_fix("def function():\n# Comment\n")
assert text == "def function():\n# Comment\n ", repr(text)
@@ -202,14 +197,8 @@ def test_def_with_unindented_comment():
"test_return"),
("def some_func():\n\treturns = 10\n", "def some_func():\n\treturns = 10\n\t",
"test_return_not_keyword"),
# Failing test
pytest.mark.xfail(
("def function():\n# Comment\n", "def function():\n# Comment\n\t",
"test_def_with_unindented_comment")),
pytest.mark.xfail(
("a = (a # some comment\n", "a = (a # some comment\n\t ",
"test_inline_comment")),
("def function():\n# Comment\n", "def function():\n# Comment\n\t",
"test_def_with_unindented_comment"),
])
def test_indentation_with_tabs(text_input, expected, test_text,
tab_stop_width_spaces):
@@ -229,6 +218,7 @@ def test_indentation_with_tabs(text_input, expected, test_text,
("print(\n)", "print(\n\t)", 6),
("print(\n)", "print(\n )", 7),
("print(\n)", "print(\n )", 8),
("a = (a # some comment\n", "a = (a # some comment\n\t ", 4),
])
def test_indentation_with_tabs_parenthesis(text_input, expected,
tab_stop_width_spaces):