diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 4b92c7ba4..f2cf94142 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -750,6 +750,26 @@ describe "Editor", -> buffer.insert([8, 0], '...') expect(cursorMovedHandler).not.toHaveBeenCalled() + describe "::getCursorBufferPositions()", -> + it "returns the existing cursor", -> + cursor1 = editor.addCursorAtBufferPosition([8, 5]) + cursor2 = editor.addCursorAtBufferPosition([4, 5]) + expect(editor.getCursorBufferPositions()).toEqual [[0, 0], [4, 5], [8, 5]] + + describe "::getCursorScreenPositions()", -> + it "returns the existing cursor", -> + editor.foldBufferRow(4) + cursor1 = editor.addCursorAtBufferPosition([8, 5]) + cursor2 = editor.addCursorAtBufferPosition([3, 5]) + expect(editor.getCursorScreenPositions()).toEqual [[0, 0], [3, 5], [5, 5]] + + describe "::getCursorsOrderedByBufferPosition()", -> + it "returns all cursors ordered by buffer positions", -> + originalCursor = editor.getCursor() + cursor1 = editor.addCursorAtBufferPosition([8, 5]) + cursor2 = editor.addCursorAtBufferPosition([4, 5]) + expect(editor.getCursorsOrderedByBufferPosition()).toEqual [originalCursor, cursor2, cursor1] + describe "addCursorAtScreenPosition(screenPosition)", -> describe "when a cursor already exists at the position", -> it "returns the existing cursor", -> diff --git a/src/cursor.coffee b/src/cursor.coffee index 968b965dd..7cc5e6484 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -569,3 +569,11 @@ class Cursor extends Model false else bufferPosition.column > firstCharacterColumn + + # Public: Compare this cursor's buffer position to another cursor's buffer position. + # + # See {Point::compare} for more details. + # + # * `otherCursor`{Cursor} to compare against + compare: (otherCursor) -> + @getBufferPosition().compare(otherCursor.getBufferPosition()) diff --git a/src/editor.coffee b/src/editor.coffee index b6a9054a0..2939df441 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1513,10 +1513,16 @@ class Editor extends Model # Essential: Get the position of the most recently added cursor in buffer # coordinates. # - # Returns a {Point}. + # Returns a {Point} getCursorBufferPosition: -> @getCursor().getBufferPosition() + # Essential: Get the position of all the cursor positions in buffer coordinates. + # + # Returns {Array} of {Point}s + getCursorBufferPositions: -> + cursor.getBufferPosition() for cursor in @getCursorsOrderedByBufferPosition() + # Essential: Move the cursor to the given position in buffer coordinates. # # If there are multiple cursors, they will be consolidated to a single cursor. @@ -1535,6 +1541,12 @@ class Editor extends Model getCursorScreenPosition: -> @getCursor().getScreenPosition() + # Essential: Get the position of all the cursor positions in screen coordinates. + # + # Returns {Array} of {Point}s + getCursorScreenPositions: -> + cursor.getScreenPosition() for cursor in @getCursorsOrderedByBufferPosition() + # Get the row of the most recently added cursor in screen coordinates. # # Returns the screen row {Number}. @@ -1717,13 +1729,20 @@ class Editor extends Model getWordUnderCursor: (options) -> @getTextInBufferRange(@getCursor().getCurrentWordBufferRange(options)) - # Extended: Get an Array of all {Cursor}s. - getCursors: -> new Array(@cursors...) - # Extended: Get the most recently added {Cursor}. getCursor: -> _.last(@cursors) + # Extended: Get an Array of all {Cursor}s. + getCursors: -> new Array(@cursors...) + + # Extended: Get all {Cursors}s, ordered by their position in the buffer + # instead of the order in which they were added. + # + # Returns an {Array} of {Selection}s. + getCursorsOrderedByBufferPosition: -> + @getCursors().sort (a, b) -> a.compare(b) + # Add a cursor based on the given {DisplayBufferMarker}. addCursor: (marker) -> cursor = new Cursor(editor: this, marker: marker)