##// END OF EJS Templates
Fix line_at_cursor for end of last line without trailing newline
Thomas Kluyver -
Show More
@@ -105,8 +105,8 b' def test_attrs():'
105 def test_line_at_cursor():
105 def test_line_at_cursor():
106 cell = ""
106 cell = ""
107 (line, offset) = line_at_cursor(cell, cursor_pos=11)
107 (line, offset) = line_at_cursor(cell, cursor_pos=11)
108 assert line == "", ("Expected '', got %r" % line)
108 nt.assert_equal(line, "")
109 assert offset == 0, ("Expected '', got %r" % line)
109 nt.assert_equal(offset, 0)
110
110
111 # The position after a newline should be the start of the following line.
111 # The position after a newline should be the start of the following line.
112 cell = "One\nTwo\n"
112 cell = "One\nTwo\n"
@@ -114,6 +114,12 b' def test_line_at_cursor():'
114 nt.assert_equal(line, "Two\n")
114 nt.assert_equal(line, "Two\n")
115 nt.assert_equal(offset, 4)
115 nt.assert_equal(offset, 4)
116
116
117 # The end of a cell should be on the last line
118 cell = "pri\npri"
119 (line, offset) = line_at_cursor(cell, cursor_pos=7)
120 nt.assert_equal(line, "pri")
121 nt.assert_equal(offset, 4)
122
117 def test_muliline_statement():
123 def test_muliline_statement():
118 cell = """a = (1,
124 cell = """a = (1,
119 3)
125 3)
@@ -44,6 +44,11 b' def line_at_cursor(cell, cursor_pos=0):'
44 lines = cell.splitlines(True)
44 lines = cell.splitlines(True)
45 for line in lines:
45 for line in lines:
46 next_offset = offset + len(line)
46 next_offset = offset + len(line)
47 if not line.endswith('\n'):
48 # If the last line doesn't have a trailing newline, treat it as if
49 # it does so that the cursor at the end of the line still counts
50 # as being on that line.
51 next_offset += 1
47 if next_offset > cursor_pos:
52 if next_offset > cursor_pos:
48 break
53 break
49 offset = next_offset
54 offset = next_offset
General Comments 0
You need to be logged in to leave comments. Login now