Make EditSession.copy return a copy with a unique id and markers

We want to be able to use the copy independently, which means we
should not use EditSession@deserialize to create it because that will
tie us to the same selection markers.
This commit is contained in:
Nathan Sobo
2013-07-18 06:23:43 -07:00
parent 804df73e8d
commit c54bb792b2
2 changed files with 23 additions and 2 deletions

View File

@@ -15,6 +15,18 @@ describe "EditSession", ->
buffer = editSession.buffer
lineLengths = buffer.getLines().map (line) -> line.length
describe ".copy()", ->
it "returns a different edit session with the same initial state", ->
editSession.setSelectedBufferRange([[1, 2], [3, 4]])
editSession.addSelectionForBufferRange([[5, 6], [7, 8]], isReversed: true)
editSession2 = editSession.copy()
expect(editSession2.id).not.toBe editSession.id
expect(editSession2.getSelectedBufferRanges()).toEqual editSession.getSelectedBufferRanges()
expect(editSession2.getSelection(1).isReversed()).toBeTruthy()
editSession2.getSelection().setBufferRange([[2, 1], [4, 3]])
expect(editSession2.getSelectedBufferRanges()).not.toEqual editSession.getSelectedBufferRanges()
describe "title", ->
describe ".getTitle()", ->
it "uses the basename of the buffer's path as its title, or 'untitled' if the path is undefined", ->

View File

@@ -117,9 +117,18 @@ class EditSession
getState: -> @serialize()
# Creates a copy of the current {EditSession}.Returns an identical `EditSession`.
# Creates an {EditSession} with the same initial state
copy: ->
EditSession.deserialize(@serialize())
tabLength = @getTabLength()
copy = new EditSession({@buffer, tabLength, @softTabs, @softWrap})
copy.setScrollTop(@getScrollTop())
copy.setScrollLeft(@getScrollLeft())
for selection, i in @getSelections()
if i is 0
copy.setSelectedBufferRange(selection.getBufferRange(), isReversed: selection.isReversed())
else
copy.addSelectionForBufferRange(selection.getBufferRange(), isReversed: selection.isReversed())
copy
### Public ###