Positioning the cursor beyond the bottom of the buffer will clip it to the last column of the last row

This commit is contained in:
Corey Johnson 2012-02-03 16:07:54 -08:00
parent b45c00d90e
commit 88cf574bc6
2 changed files with 8 additions and 3 deletions

View File

@ -632,7 +632,7 @@ describe "Editor", ->
describe ".clipPosition(point)", ->
it "selects the nearest valid position to the given point", ->
expect(editor.clipPosition(row: 1000, column: 0)).toEqual(row: buffer.numLines() - 1, column: 0)
expect(editor.clipPosition(row: 1000, column: 0)).toEqual(row: buffer.lastRow(), column: buffer.getLine(buffer.lastRow()).length)
expect(editor.clipPosition(row: -5, column: 0)).toEqual(row: 0, column: 0)
expect(editor.clipPosition(row: 1, column: 10000)).toEqual(row: 1, column: buffer.getLine(1).length)
expect(editor.clipPosition(row: 1, column: -5)).toEqual(row: 1, column: 0)

View File

@ -161,8 +161,13 @@ class Editor extends Template
@lines.find("pre.line:eq(#{row})")
clipPosition: ({row, column}) ->
row = Math.min(Math.max(0, row), @buffer.numLines() - 1)
column = Math.min(Math.max(0, column), @buffer.getLine(row).length)
if row > @buffer.lastRow()
row = @buffer.lastRow()
column = @buffer.getLine(row).length
else
row = Math.min(Math.max(0, row), @buffer.numLines() - 1)
column = Math.min(Math.max(0, column), @buffer.getLine(row).length)
new Point(row, column)
pixelPositionFromPoint: ({row, column}) ->