From 64470a3c7d20c1122d40ce358d6ebc938bf83fa7 Mon Sep 17 00:00:00 2001 From: karlin Date: Fri, 23 May 2014 00:51:01 -0400 Subject: [PATCH 1/2] add moveToBeginningOf{Next,Previous}Paragraph for cursor and wrappers for editor --- spec/editor-spec.coffee | 32 ++++++++++++++++++++++++++++++++ src/cursor.coffee | 37 +++++++++++++++++++++++++++++++++++++ src/editor.coffee | 8 ++++++++ 3 files changed, 77 insertions(+) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 6b37d6f86..277effef6 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -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 """ diff --git a/src/cursor.coffee b/src/cursor.coffee index 4a8895769..df9e5e06b 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -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. diff --git a/src/editor.coffee b/src/editor.coffee index bd714ecb1..25411c765 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -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() From 7f418e2de4766e4d3ba40a0db4cf2c14549a4a91 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Fri, 23 May 2014 09:13:44 -0700 Subject: [PATCH 2/2] Add move by paragraph commands --- src/editor-component.coffee | 2 ++ src/editor-view.coffee | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/editor-component.coffee b/src/editor-component.coffee index bf5904d53..cdc615d2b 100644 --- a/src/editor-component.coffee +++ b/src/editor-component.coffee @@ -188,6 +188,8 @@ EditorComponent = React.createClass 'editor:delete-to-end-of-word': => editor.deleteToEndOfWord() 'editor:delete-line': => editor.deleteLine() 'editor:cut-to-end-of-line': => editor.cutToEndOfLine() + 'editor:move-to-beginning-of-next-paragraph': => editor.moveCursorToBeginningOfNextParagraph() + 'editor:move-to-beginning-of-previous-paragraph': => editor.moveCursorToBeginningOfPreviousParagraph() 'editor:move-to-beginning-of-screen-line': => editor.moveCursorToBeginningOfScreenLine() 'editor:move-to-beginning-of-line': => editor.moveCursorToBeginningOfLine() 'editor:move-to-end-of-screen-line': => editor.moveCursorToEndOfScreenLine() diff --git a/src/editor-view.coffee b/src/editor-view.coffee index 2b69becd1..c4c8b8e42 100644 --- a/src/editor-view.coffee +++ b/src/editor-view.coffee @@ -159,6 +159,8 @@ class EditorView extends View 'editor:delete-to-end-of-word': => @editor.deleteToEndOfWord() 'editor:delete-line': => @editor.deleteLine() 'editor:cut-to-end-of-line': => @editor.cutToEndOfLine() + 'editor:move-to-beginning-of-next-paragraph': => @editor.moveCursorToBeginningOfNextParagraph() + 'editor:move-to-beginning-of-previous-paragraph': => @editor.moveCursorToBeginningOfPreviousParagraph() 'editor:move-to-beginning-of-screen-line': => @editor.moveCursorToBeginningOfScreenLine() 'editor:move-to-beginning-of-line': => @editor.moveCursorToBeginningOfLine() 'editor:move-to-end-of-screen-line': => @editor.moveCursorToEndOfScreenLine()