mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Deserialize display buffer when deserializing edit sessions
Previously, we kept display buffer attributes in the edit session's serialized state, then recreated a fresh display buffer each time when deserializing edit sessions. Now that DisplayBuffer and TokenizedBuffer are serializable, we can just include them directly when serializing the edit session.
This commit is contained in:
@@ -15,6 +15,21 @@ describe "EditSession", ->
|
||||
buffer = editSession.buffer
|
||||
lineLengths = buffer.getLines().map (line) -> line.length
|
||||
|
||||
describe "@deserialize(state)", ->
|
||||
it "restores selections and folds based on markers in the buffer", ->
|
||||
editSession.setSelectedBufferRange([[1, 2], [3, 4]])
|
||||
editSession.addSelectionForBufferRange([[5, 6], [7, 5]], isReversed: true)
|
||||
editSession.foldBufferRow(4)
|
||||
expect(editSession.isFoldedAtBufferRow(4)).toBeTruthy()
|
||||
|
||||
editSession2 = deserialize(editSession.serialize())
|
||||
|
||||
expect(editSession2.id).toBe editSession.id
|
||||
expect(editSession2.getBuffer().getPath()).toBe editSession.getBuffer().getPath()
|
||||
expect(editSession2.getSelectedBufferRanges()).toEqual [[[1, 2], [3, 4]], [[5, 6], [7, 5]]]
|
||||
expect(editSession2.getSelection(1).isReversed()).toBeTruthy()
|
||||
expect(editSession2.isFoldedAtBufferRow(4)).toBeTruthy()
|
||||
|
||||
describe ".copy()", ->
|
||||
it "returns a different edit session with the same initial state", ->
|
||||
editSession.setSelectedBufferRange([[1, 2], [3, 4]])
|
||||
@@ -1507,7 +1522,7 @@ describe "EditSession", ->
|
||||
|
||||
describe "if 'softTabs' is false", ->
|
||||
it "insert a \t into the buffer", ->
|
||||
editSession.softTabs = false
|
||||
editSession.setSoftTabs(false)
|
||||
expect(buffer.lineForRow(0)).not.toMatch(/^\t/)
|
||||
editSession.indent()
|
||||
expect(buffer.lineForRow(0)).toMatch(/^\t/)
|
||||
@@ -1526,7 +1541,7 @@ describe "EditSession", ->
|
||||
describe "when 'softTabs' is false", ->
|
||||
it "moves the cursor to the end of the leading whitespace and inserts enough tabs to bring the line to the suggested level of indentaion", ->
|
||||
convertToHardTabs(buffer)
|
||||
editSession.softTabs = false
|
||||
editSession.setSoftTabs(false)
|
||||
buffer.insert([5, 0], "\t\n")
|
||||
editSession.setCursorBufferPosition [5, 0]
|
||||
editSession.indent(autoIndent: true)
|
||||
@@ -1546,7 +1561,7 @@ describe "EditSession", ->
|
||||
describe "when 'softTabs' is false", ->
|
||||
it "moves the cursor to the end of the leading whitespace and inserts \t into the buffer", ->
|
||||
convertToHardTabs(buffer)
|
||||
editSession.softTabs = false
|
||||
editSession.setSoftTabs(false)
|
||||
buffer.insert([7, 0], "\t\t\t\n")
|
||||
editSession.setCursorBufferPosition [7, 1]
|
||||
editSession.indent(autoIndent: true)
|
||||
@@ -1633,7 +1648,7 @@ describe "EditSession", ->
|
||||
describe "when softTabs is disabled", ->
|
||||
it "indents line and retains selection", ->
|
||||
convertToHardTabs(buffer)
|
||||
editSession.softTabs = false
|
||||
editSession.setSoftTabs(false)
|
||||
editSession.setSelectedBufferRange([[0,3], [0,3]])
|
||||
editSession.indentSelectedRows()
|
||||
expect(buffer.lineForRow(0)).toBe "\tvar quicksort = function () {"
|
||||
@@ -1650,7 +1665,7 @@ describe "EditSession", ->
|
||||
describe "when softTabs is disabled", ->
|
||||
it "indents line and retains selection", ->
|
||||
convertToHardTabs(buffer)
|
||||
editSession.softTabs = false
|
||||
editSession.setSoftTabs(false)
|
||||
editSession.setSelectedBufferRange([[0,4], [0,14]])
|
||||
editSession.indentSelectedRows()
|
||||
expect(buffer.lineForRow(0)).toBe "\tvar quicksort = function () {"
|
||||
@@ -1677,7 +1692,7 @@ describe "EditSession", ->
|
||||
describe "when softTabs is disabled", ->
|
||||
it "indents selected lines (that are not empty) and retains selection", ->
|
||||
convertToHardTabs(buffer)
|
||||
editSession.softTabs = false
|
||||
editSession.setSoftTabs(false)
|
||||
editSession.setSelectedBufferRange([[9,1], [11,15]])
|
||||
editSession.indentSelectedRows()
|
||||
expect(buffer.lineForRow(9)).toBe "\t\t};"
|
||||
@@ -2214,15 +2229,15 @@ describe "EditSession", ->
|
||||
expect(editSession.getSelectedBufferRange()).toEqual [[0, 0], [0, 2]]
|
||||
|
||||
describe "soft-tabs detection", ->
|
||||
it "assign soft / hard tabs based on the contents of the buffer, or uses the default if unknown", ->
|
||||
it "assigns soft / hard tabs based on the contents of the buffer, or uses the default if unknown", ->
|
||||
editSession = project.open('sample.js', softTabs: false)
|
||||
expect(editSession.softTabs).toBeTruthy()
|
||||
expect(editSession.getSoftTabs()).toBeTruthy()
|
||||
|
||||
editSession = project.open('sample-with-tabs.coffee', softTabs: true)
|
||||
expect(editSession.softTabs).toBeFalsy()
|
||||
expect(editSession.getSoftTabs()).toBeFalsy()
|
||||
|
||||
editSession = project.open(null, softTabs: false)
|
||||
expect(editSession.softTabs).toBeFalsy()
|
||||
expect(editSession.getSoftTabs()).toBeFalsy()
|
||||
|
||||
describe ".indentLevelForLine(line)", ->
|
||||
it "returns the indent level when the line has only leading whitespace", ->
|
||||
|
||||
@@ -357,7 +357,7 @@ class Cursor
|
||||
#
|
||||
# Returns a {Number}.
|
||||
getIndentLevel: ->
|
||||
if @editSession.softTabs
|
||||
if @editSession.getSoftTabs()
|
||||
@getBufferColumn() / @editSession.getTabLength()
|
||||
else
|
||||
@getBufferColumn()
|
||||
|
||||
@@ -22,7 +22,7 @@ class EditSession
|
||||
|
||||
### Internal ###
|
||||
|
||||
@version: 3
|
||||
@version: 4
|
||||
|
||||
@deserialize: (state) ->
|
||||
new EditSession(state)
|
||||
@@ -32,39 +32,41 @@ class EditSession
|
||||
displayBuffer: null
|
||||
cursors: null
|
||||
selections: null
|
||||
softTabs: true
|
||||
softWrap: false
|
||||
suppressSelectionMerging: false
|
||||
|
||||
constructor: (optionsOrState) ->
|
||||
@cursors = []
|
||||
@selections = []
|
||||
|
||||
if optionsOrState instanceof telepath.Document
|
||||
project.editSessions.push(this)
|
||||
@state = optionsOrState
|
||||
{@id, tabLength, softTabs, @softWrap} = @state.toObject()
|
||||
@setBuffer(project.bufferForId(@state.get('bufferId')))
|
||||
@setDisplayBuffer(new DisplayBuffer({@buffer, tabLength}))
|
||||
@addSelection(marker) for marker in @findMarkers(@getSelectionMarkerAttributes())
|
||||
@id = @state.get('id')
|
||||
displayBuffer = deserialize(@state.get('displayBuffer'))
|
||||
@setBuffer(displayBuffer.buffer)
|
||||
@setDisplayBuffer(displayBuffer)
|
||||
for marker in @findMarkers(@getSelectionMarkerAttributes())
|
||||
marker.setAttributes(preserveFolds: true)
|
||||
@addSelection(marker)
|
||||
@setScrollTop(@state.get('scrollTop'))
|
||||
@setScrollLeft(@state.get('scrollLeft'))
|
||||
else
|
||||
{buffer, displayBuffer, tabLength, softTabs, @softWrap, suppressCursorCreation} = optionsOrState
|
||||
{buffer, displayBuffer, tabLength, softTabs, softWrap, suppressCursorCreation} = optionsOrState
|
||||
@id = guid.create().toString()
|
||||
displayBuffer ?= new DisplayBuffer({buffer, tabLength})
|
||||
@state = telepath.Document.create
|
||||
deserializer: 'EditSession'
|
||||
version: @constructor.version
|
||||
id: @id
|
||||
bufferId: buffer.id
|
||||
displayBuffer: displayBuffer.getState()
|
||||
softWrap: softWrap ? false
|
||||
softTabs: buffer.usesSoftTabs() ? softTabs ? true
|
||||
scrollTop: 0
|
||||
scrollLeft: 0
|
||||
@setBuffer(buffer)
|
||||
@setDisplayBuffer(displayBuffer ? new DisplayBuffer({@buffer, tabLength}))
|
||||
@setDisplayBuffer(displayBuffer)
|
||||
@addCursorAtScreenPosition([0, 0]) unless suppressCursorCreation
|
||||
|
||||
@languageMode = new LanguageMode(this, @buffer.getExtension())
|
||||
@softTabs = @buffer.usesSoftTabs() ? softTabs ? true
|
||||
|
||||
@state.on 'changed', ({key, newValue}) =>
|
||||
switch key
|
||||
when 'scrollTop'
|
||||
@@ -72,6 +74,8 @@ class EditSession
|
||||
when 'scrollLeft'
|
||||
@trigger 'scroll-left-changed', newValue
|
||||
|
||||
project.editSessions.push(this)
|
||||
|
||||
setBuffer: (@buffer) ->
|
||||
@buffer.retain()
|
||||
@subscribe @buffer, "path-changed", =>
|
||||
@@ -103,24 +107,16 @@ class EditSession
|
||||
@trigger 'destroyed'
|
||||
@off()
|
||||
|
||||
serialize: ->
|
||||
@state.set
|
||||
bufferId: @buffer.id
|
||||
scrollTop: @getScrollTop()
|
||||
scrollLeft: @getScrollLeft()
|
||||
tabLength: @getTabLength()
|
||||
softTabs: @softTabs
|
||||
softWrap: @softWrap
|
||||
cursorScreenPosition: @getCursorScreenPosition().serialize()
|
||||
@state
|
||||
|
||||
serialize: -> @state.clone()
|
||||
getState: -> @serialize()
|
||||
|
||||
# Creates an {EditSession} with the same initial state
|
||||
copy: ->
|
||||
tabLength = @getTabLength()
|
||||
displayBuffer = @displayBuffer.copy()
|
||||
newEditSession = new EditSession({@buffer, displayBuffer, tabLength, @softTabs, @softWrap, suppressCursorCreation: true})
|
||||
softTabs = @getSoftTabs()
|
||||
softWrap = @getSoftWrap()
|
||||
newEditSession = new EditSession({@buffer, displayBuffer, tabLength, softTabs, softWrap, suppressCursorCreation: true})
|
||||
newEditSession.setScrollTop(@getScrollTop())
|
||||
newEditSession.setScrollLeft(@getScrollLeft())
|
||||
for marker in @findMarkers(editSessionId: @id)
|
||||
@@ -197,20 +193,26 @@ class EditSession
|
||||
# softWrapColumn - A {Number} defining the soft wrap limit
|
||||
setSoftWrapColumn: (@softWrapColumn) -> @displayBuffer.setSoftWrapColumn(@softWrapColumn)
|
||||
|
||||
getSoftTabs: ->
|
||||
@state.get('softTabs')
|
||||
|
||||
# Defines whether to use soft tabs.
|
||||
#
|
||||
# softTabs - A {Boolean} which, if `true`, indicates that you want soft tabs.
|
||||
setSoftTabs: (@softTabs) ->
|
||||
setSoftTabs: (softTabs) ->
|
||||
@state.set('softTabs', softTabs)
|
||||
|
||||
# Retrieves whether soft tabs are enabled.
|
||||
#
|
||||
# Returns a {Boolean}.
|
||||
getSoftWrap: -> @softWrap
|
||||
getSoftWrap: ->
|
||||
@state.get('softWrap')
|
||||
|
||||
# Defines whether to use soft wrapping of text.
|
||||
#
|
||||
# softTabs - A {Boolean} which, if `true`, indicates that you want soft wraps.
|
||||
setSoftWrap: (@softWrap) ->
|
||||
setSoftWrap: (softWrap) ->
|
||||
@state.set('softWrap', softWrap)
|
||||
|
||||
# Retrieves that character used to indicate a tab.
|
||||
#
|
||||
@@ -286,7 +288,7 @@ class EditSession
|
||||
|
||||
# Constructs the string used for tabs.
|
||||
buildIndentString: (number) ->
|
||||
if @softTabs
|
||||
if @getSoftTabs()
|
||||
_.multiplyString(" ", number * @getTabLength())
|
||||
else
|
||||
_.multiplyString("\t", Math.floor(number))
|
||||
@@ -491,7 +493,7 @@ class EditSession
|
||||
#
|
||||
# bufferRange - The {Range} to perform the replace in
|
||||
normalizeTabsInBufferRange: (bufferRange) ->
|
||||
return unless @softTabs
|
||||
return unless @getSoftTabs()
|
||||
@scanInBufferRange /\t/, bufferRange, ({replace}) => replace(@getTabText())
|
||||
|
||||
# Performs a cut to the end of the current line.
|
||||
|
||||
@@ -333,7 +333,6 @@ class Project
|
||||
options = _.extend(@defaultEditSessionOptions(), editSessionOptions)
|
||||
options.buffer = buffer
|
||||
editSession = new EditSession(options)
|
||||
@editSessions.push editSession
|
||||
@trigger 'edit-session-created', editSession
|
||||
editSession
|
||||
|
||||
|
||||
Reference in New Issue
Block a user