add moveToBeginningOf{Next,Previous}Paragraph for cursor and wrappers for editor

This commit is contained in:
karlin 2014-05-23 00:51:01 -04:00
parent a8538a1361
commit 64470a3c7d
3 changed files with 77 additions and 0 deletions

View File

@ -622,6 +622,38 @@ describe "Editor", ->
editor.moveCursorToBeginningOfNextWord()
expect(editor.getCursorBufferPosition()).toEqual [11, 9]
describe ".moveCursorToBeginningOfNextParagraph()", ->
it "moves the cursor before the first line of the next paragraph", ->
editor.setCursorBufferPosition [0,6]
cursor = editor.getCursor()
editor.moveCursorToBeginningOfNextParagraph()
expect(cursor.getBufferPosition()).toEqual { row : 10, column : 0 }
editor.setText("")
editor.setCursorBufferPosition [0,0]
cursor = editor.getCursor()
editor.moveCursorToBeginningOfNextParagraph()
expect(cursor.getBufferPosition()).toEqual [0, 0]
describe ".moveCursorToBeginningOfPreviousParagraph()", ->
it "moves the cursor before the first line of the pevious paragraph", ->
editor.setCursorBufferPosition [10,0]
cursor = editor.getCursor()
editor.moveCursorToBeginningOfPreviousParagraph()
expect(cursor.getBufferPosition()).toEqual { row : 0, column : 0 }
editor.setText("")
editor.setCursorBufferPosition [0,0]
cursor = editor.getCursor()
editor.moveCursorToBeginningOfPreviousParagraph()
expect(cursor.getBufferPosition()).toEqual [0, 0]
describe ".getCurrentParagraphBufferRange()", ->
it "returns the buffer range of the current paragraph, delimited by blank lines or the beginning / end of the file", ->
buffer.setText """

View File

@ -457,6 +457,43 @@ class Cursor extends Model
getCurrentLineBufferRange: (options) ->
@editor.bufferRangeForBufferRow(@getBufferRow(), options)
# Public: Moves the cursor to the beginning of the next paragraph
moveToBeginningOfNextParagraph: ->
if position = @getBeginningOfNextParagraphBufferPosition()
@setBufferPosition(position)
# Public: Moves the cursor to the beginning of the previous paragraph
moveToBeginningOfPreviousParagraph: ->
if position = @getBeginningOfPreviousParagraphBufferPosition()
@setBufferPosition(position)
getBeginningOfNextParagraphBufferPosition: (editor) ->
start = @getBufferPosition()
eof = @editor.getEofBufferPosition()
scanRange = [start, eof]
{row, column} = eof
position = new Point(row, column - 1)
@editor.scanInBufferRange /^\n*$/g, scanRange, ({range, stop}) =>
if !range.start.isEqual(start)
position = range.start
stop()
@editor.screenPositionForBufferPosition(position)
getBeginningOfPreviousParagraphBufferPosition: (editor) ->
start = @editor.getCursorBufferPosition()
{row, column} = start
scanRange = [[row-1, column], [0,0]]
position = new Point(0, 0)
zero = new Point(0,0)
@editor.backwardsScanInBufferRange /^\n*$/g, scanRange, ({range, stop}) =>
if !range.start.isEqual(zero)
position = range.start
stop()
@editor.screenPositionForBufferPosition(position)
# Public: Retrieves the range for the current paragraph.
#
# A paragraph is defined as a block of text surrounded by empty lines.

View File

@ -1488,6 +1488,14 @@ class Editor extends Model
moveCursorToNextWordBoundary: ->
@moveCursors (cursor) -> cursor.moveToNextWordBoundary()
# Public: Move every cursor to the beginning of the next paragraph.
moveCursorToBeginningOfNextParagraph: ->
@moveCursors (cursor) -> cursor.moveToBeginningOfNextParagraph()
# Public: Move every cursor to the beginning of the previous paragraph.
moveCursorToBeginningOfPreviousParagraph: ->
@moveCursors (cursor) -> cursor.moveToBeginningOfPreviousParagraph()
scrollToCursorPosition: ->
@getCursor().autoscroll()