Skip to content

Commit

Permalink
Add moveCursorToEnd/BeginningOfBufferLine
Browse files Browse the repository at this point in the history
Fixes atom#1123
  • Loading branch information
probablycorey committed Dec 31, 2013
1 parent 97aed1f commit c37b884
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion keymaps/base.cson
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'alt-shift-left': 'editor:select-to-beginning-of-word'
'alt-shift-right': 'editor:select-to-end-of-word'
'home': 'editor:move-to-first-character-of-line'
'end': 'editor:move-to-end-of-line'
'end': 'editor:move-to-end-of-screen-line'
'shift-home': 'editor:select-to-first-character-of-line'
'shift-end': 'editor:select-to-end-of-line'

Expand Down
2 changes: 1 addition & 1 deletion keymaps/darwin.cson
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
'ctrl-A': 'editor:select-to-first-character-of-line'
'ctrl-E': 'editor:select-to-end-of-line'
'cmd-left': 'editor:move-to-first-character-of-line'
'cmd-right': 'editor:move-to-end-of-line'
'cmd-right': 'editor:move-to-end-of-screen-line'
'cmd-shift-left': 'editor:select-to-first-character-of-line'
'cmd-shift-right': 'editor:select-to-end-of-line'
'alt-backspace': 'editor:backspace-to-beginning-of-word'
Expand Down
30 changes: 24 additions & 6 deletions spec/editor-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -361,46 +361,64 @@ describe "Editor", ->
expect(editor.getCursors().length).toBe 1
expect(editor.getCursorBufferPosition()).toEqual [12,2]

describe ".moveCursorToBeginningOfLine()", ->
describe ".moveCursorToBeginningOfScreenLine()", ->
describe "when soft wrap is on", ->
it "moves cursor to the beginning of the screen line", ->
editor.setSoftWrap(true)
editor.setEditorWidthInChars(10)
editor.setCursorScreenPosition([1, 2])
editor.moveCursorToBeginningOfLine()
editor.moveCursorToBeginningOfScreenLine()
cursor = editor.getCursor()
expect(cursor.getScreenPosition()).toEqual [1, 0]

describe "when soft wrap is off", ->
it "moves cursor to the beginning of then line", ->
editor.setCursorScreenPosition [0,5]
editor.addCursorAtScreenPosition [1,7]
editor.moveCursorToBeginningOfLine()
editor.moveCursorToBeginningOfScreenLine()
expect(editor.getCursors().length).toBe 2
[cursor1, cursor2] = editor.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,0]
expect(cursor2.getBufferPosition()).toEqual [1,0]

describe ".moveCursorToEndOfLine()", ->
describe ".moveCursorToEndOfScreenLine()", ->
describe "when soft wrap is on", ->
it "moves cursor to the beginning of the screen line", ->
editor.setSoftWrap(true)
editor.setEditorWidthInChars(10)
editor.setCursorScreenPosition([1, 2])
editor.moveCursorToEndOfLine()
editor.moveCursorToEndOfScreenLine()
cursor = editor.getCursor()
expect(cursor.getScreenPosition()).toEqual [1, 9]

describe "when soft wrap is off", ->
it "moves cursor to the end of line", ->
editor.setCursorScreenPosition [0,0]
editor.addCursorAtScreenPosition [1,0]
editor.moveCursorToEndOfLine()
editor.moveCursorToEndOfScreenLine()
expect(editor.getCursors().length).toBe 2
[cursor1, cursor2] = editor.getCursors()
expect(cursor1.getBufferPosition()).toEqual [0,29]
expect(cursor2.getBufferPosition()).toEqual [1,30]

describe ".moveCursorToBeginningOfLine()", ->
it "moves cursor to the beginning of the buffer line", ->
editor.setSoftWrap(true)
editor.setEditorWidthInChars(10)
editor.setCursorScreenPosition([1, 2])
editor.moveCursorToBeginningOfLine()
cursor = editor.getCursor()
expect(cursor.getScreenPosition()).toEqual [0, 0]

describe ".moveCursorToEndOfLine()", ->
it "moves cursor to the end of the buffer line", ->
editor.setSoftWrap(true)
editor.setEditorWidthInChars(10)
editor.setCursorScreenPosition([0, 2])
editor.moveCursorToEndOfLine()
cursor = editor.getCursor()
expect(cursor.getScreenPosition()).toEqual [3, 4]

describe ".moveCursorToFirstCharacterOfLine()", ->
describe "when soft wrap is on", ->
it "moves to the first character of the current screen line or the beginning of the screen line if it's already on the first character", ->
Expand Down
14 changes: 11 additions & 3 deletions src/cursor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,14 @@ class Cursor
moveToBottom: ->
@setBufferPosition(@editor.getEofBufferPosition())

# Public: Moves the cursor to the beginning of the screen line.
moveToBeginningOfLine: ->
# Public: Moves the cursor to the beginning of the line.
moveToBeginningOfScreenLine: ->
@setScreenPosition([@getScreenRow(), 0])

# Public: Moves the cursor to the beginning of the buffer line.
moveToBeginningOfLine: ->
@setBufferPosition([@getBufferRow(), 0])

# Public: Moves the cursor to the beginning of the first character in the
# line.
moveToFirstCharacterOfLine: ->
Expand All @@ -275,9 +279,13 @@ class Cursor

@setBufferPosition(endOfLeadingWhitespace) if endOfLeadingWhitespace.isGreaterThan(position)

# Public: Moves the cursor to the end of the line.
moveToEndOfScreenLine: ->
@setScreenPosition([@getScreenRow(), Infinity])

# Public: Moves the cursor to the end of the buffer line.
moveToEndOfLine: ->
@setScreenPosition([@getScreenRow(), Infinity])
@setBufferPosition([@getBufferRow(), Infinity])

# Public: Moves the cursor to the beginning of the word.
moveToBeginningOfWord: ->
Expand Down
2 changes: 2 additions & 0 deletions src/editor-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class EditorView extends View
'editor:delete-to-end-of-word': @deleteToEndOfWord
'editor:delete-line': @deleteLine
'editor:cut-to-end-of-line': @cutToEndOfLine
'editor:move-to-beginning-of-screen-line': => @editor.moveCursorToBeginningOfScreenLine()
'editor:move-to-beginning-of-line': @moveCursorToBeginningOfLine
'editor:move-to-end-of-screen-line': => @editor.moveCursorToEndOfScreenLine()
'editor:move-to-end-of-line': @moveCursorToEndOfLine
'editor:move-to-first-character-of-line': @moveCursorToFirstCharacterOfLine
'editor:move-to-beginning-of-word': @moveCursorToBeginningOfWord
Expand Down
8 changes: 8 additions & 0 deletions src/editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,10 @@ class Editor extends Model
@moveCursors (cursor) -> cursor.moveToBottom()

# Public: Moves every local cursor to the beginning of the line.
moveCursorToBeginningOfScreenLine: ->
@moveCursors (cursor) -> cursor.moveToBeginningOfScreenLine()

# Public: Moves every local cursor to the beginning of the buffer line.
moveCursorToBeginningOfLine: ->
@moveCursors (cursor) -> cursor.moveToBeginningOfLine()

Expand All @@ -1128,6 +1132,10 @@ class Editor extends Model
@moveCursors (cursor) -> cursor.moveToFirstCharacterOfLine()

# Public: Moves every local cursor to the end of the line.
moveCursorToEndOfScreenLine: ->
@moveCursors (cursor) -> cursor.moveToEndOfScreenLine()

# Public: Moves every local cursor to the end of the buffer line.
moveCursorToEndOfLine: ->
@moveCursors (cursor) -> cursor.moveToEndOfLine()

Expand Down

0 comments on commit c37b884

Please sign in to comment.