Create a selection automatically as needed if the last one was destroyed

Fixes atom/bracket-matcher#102
This commit is contained in:
Nathan Sobo
2015-08-26 17:59:19 -06:00
parent 6bfa228060
commit 4bbc1d806e
2 changed files with 29 additions and 2 deletions

View File

@@ -207,6 +207,15 @@ describe "TextEditor", ->
lastCursor = editor.addCursorAtScreenPosition([2, 0])
expect(editor.getLastCursor()).toBe lastCursor
it "creates a new cursor at (0, 0) if the last cursor has been destroyed", ->
editor.getLastCursor().destroy()
expect(editor.getLastCursor().getBufferPosition()).toEqual([0, 0])
describe ".getCursors()", ->
it "creates a new cursor at (0, 0) if the last cursor has been destroyed", ->
editor.getLastCursor().destroy()
expect(editor.getCursors()[0].getBufferPosition()).toEqual([0, 0])
describe "when the cursor moves", ->
it "clears a goal column established by vertical movement", ->
editor.setText('b')
@@ -1027,6 +1036,16 @@ describe "TextEditor", ->
beforeEach ->
selection = editor.getLastSelection()
describe ".getLastSelection()", ->
it "creates a new selection at (0, 0) if the last selection has been destroyed", ->
editor.getLastSelection().destroy()
expect(editor.getLastSelection().getBufferRange()).toEqual([[0, 0], [0, 0]])
describe ".getSelections()", ->
it "creates a new selection at (0, 0) if the last selection has been destroyed", ->
editor.getLastSelection().destroy()
expect(editor.getSelections()[0].getBufferRange()).toEqual([[0, 0], [0, 0]])
describe "when the selection range changes", ->
it "emits an event with the old range, new range, and the selection that moved", ->
editor.setSelectedBufferRange([[3, 0], [4, 5]])

View File

@@ -95,7 +95,7 @@ class TextEditor extends Model
@subscribeToBuffer()
@subscribeToDisplayBuffer()
if @getCursors().length is 0 and not suppressCursorCreation
if @cursors.length is 0 and not suppressCursorCreation
initialLine = Math.max(parseInt(initialLine) or 0, 0)
initialColumn = Math.max(parseInt(initialColumn) or 0, 0)
@addCursorAtBufferPosition([initialLine, initialColumn])
@@ -185,7 +185,7 @@ class TextEditor extends Model
@unsubscribe() if includeDeprecatedAPIs
@disposables.dispose()
@tabTypeSubscription.dispose()
selection.destroy() for selection in @getSelections()
selection.destroy() for selection in @selections.slice()
@buffer.release()
@displayBuffer.destroy()
@languageMode.destroy()
@@ -1744,6 +1744,7 @@ class TextEditor extends Model
# Extended: Returns the most recently added {Cursor}
getLastCursor: ->
@createLastSelectionIfNeeded()
_.last(@cursors)
# Extended: Returns the word surrounding the most recently added cursor.
@@ -1754,6 +1755,7 @@ class TextEditor extends Model
# Extended: Get an Array of all {Cursor}s.
getCursors: ->
@createLastSelectionIfNeeded()
@cursors.slice()
# Extended: Get all {Cursors}s, ordered by their position in the buffer
@@ -2133,12 +2135,14 @@ class TextEditor extends Model
#
# Returns a {Selection}.
getLastSelection: ->
@createLastSelectionIfNeeded()
_.last(@selections)
# Extended: Get current {Selection}s.
#
# Returns: An {Array} of {Selection}s.
getSelections: ->
@createLastSelectionIfNeeded()
@selections.slice()
# Extended: Get all {Selection}s, ordered by their position in the buffer
@@ -2295,6 +2299,10 @@ class TextEditor extends Model
@emit 'selection-screen-range-changed', event if includeDeprecatedAPIs
@emitter.emit 'did-change-selection-range', event
createLastSelectionIfNeeded: ->
if @selections.length is 0
@addSelectionForBufferRange([[0, 0], [0, 0]], autoscroll: false, preserveFolds: true)
###
Section: Searching and Replacing
###