diff --git a/IPython/utils/tests/test_tokenutil.py b/IPython/utils/tests/test_tokenutil.py index d95d0fc..253d0a4 100644 --- a/IPython/utils/tests/test_tokenutil.py +++ b/IPython/utils/tests/test_tokenutil.py @@ -105,8 +105,8 @@ def test_attrs(): def test_line_at_cursor(): cell = "" (line, offset) = line_at_cursor(cell, cursor_pos=11) - assert line == "", ("Expected '', got %r" % line) - assert offset == 0, ("Expected '', got %r" % line) + nt.assert_equal(line, "") + nt.assert_equal(offset, 0) # The position after a newline should be the start of the following line. cell = "One\nTwo\n" @@ -114,6 +114,12 @@ def test_line_at_cursor(): nt.assert_equal(line, "Two\n") nt.assert_equal(offset, 4) + # The end of a cell should be on the last line + cell = "pri\npri" + (line, offset) = line_at_cursor(cell, cursor_pos=7) + nt.assert_equal(line, "pri") + nt.assert_equal(offset, 4) + def test_muliline_statement(): cell = """a = (1, 3) diff --git a/IPython/utils/tokenutil.py b/IPython/utils/tokenutil.py index 9db6e71..6e283a5 100644 --- a/IPython/utils/tokenutil.py +++ b/IPython/utils/tokenutil.py @@ -44,6 +44,11 @@ def line_at_cursor(cell, cursor_pos=0): lines = cell.splitlines(True) for line in lines: next_offset = offset + len(line) + if not line.endswith('\n'): + # If the last line doesn't have a trailing newline, treat it as if + # it does so that the cursor at the end of the line still counts + # as being on that line. + next_offset += 1 if next_offset > cursor_pos: break offset = next_offset