Make TokenizedBuffer a telepath.Model subclass

There's a bunch of improvised code to make this work right now because
of the circularity of this refactoring. It will stabilize over time.
This commit is contained in:
Nathan Sobo
2013-12-07 02:25:47 -08:00
parent b61654b52f
commit a4d2b4d21a
9 changed files with 75 additions and 82 deletions

View File

@@ -1,16 +1,16 @@
_ = require 'underscore-plus'
TokenizedLine = require './tokenized-line'
{Emitter, Subscriber} = require 'emissary'
Token = require './token'
telepath = require 'telepath'
{Point, Range} = telepath
{Model, Point, Range} = require 'telepath'
### Internal ###
module.exports =
class TokenizedBuffer
Emitter.includeInto(this)
Subscriber.includeInto(this)
class TokenizedBuffer extends Model
@properties
bufferPath: null
tabLength: -> atom.config.get('editor.tabLength') ? 2
project: null
grammar: null
currentGrammarScore: null
@@ -20,24 +20,19 @@ class TokenizedBuffer
invalidRows: null
visible: false
@acceptsDocuments: true
atom.deserializers.add(this)
constructor: ->
super
@deserializing = @state?
@deserialize: (state) ->
new this(state)
created: ->
if @deserializing
@deserializing = false
return this
constructor: (optionsOrState) ->
if optionsOrState instanceof telepath.Document
@state = optionsOrState
# TODO: This needs to be made async, but should wait until the new Telepath changes land
@buffer = atom.project.bufferForPathSync(optionsOrState.get('bufferPath'))
if @buffer? and @buffer.isAlive()
@bufferPath = @buffer.getPath()
else
{ @buffer, tabLength } = optionsOrState
@state = atom.site.createDocument
deserializer: @constructor.name
bufferPath: @buffer.getPath()
tabLength: tabLength ? atom.config.get('editor.tabLength') ? 2
@buffer = @project.bufferForPathSync(@bufferPath)
@subscribe atom.syntax, 'grammar-added grammar-updated', (grammar) =>
if grammar.injectionSelector?
@@ -48,12 +43,22 @@ class TokenizedBuffer
@on 'grammar-changed grammar-updated', => @resetTokenizedLines()
@subscribe @buffer, "changed", (e) => @handleBufferChange(e)
@subscribe @buffer, "path-changed", => @state.set('bufferPath', @buffer.getPath())
@subscribe @buffer, "path-changed", => @bufferPath = @buffer.getPath()
@subscribe @$tabLength.changes.onValue (tabLength) =>
lastRow = @buffer.getLastRow()
@tokenizedLines = @buildPlaceholderTokenizedLinesForRows(0, lastRow)
@invalidateRow(0)
@emit "changed", { start: 0, end: lastRow, delta: 0 }
@reloadGrammar()
serialize: -> @state.clone()
getState: -> @state
# TODO: Remove when everything is a telepath model
destroy: ->
@destroyed()
destroyed: ->
@unsubscribe()
setGrammar: (grammar, score) ->
return if grammar is @grammar
@@ -87,24 +92,19 @@ class TokenizedBuffer
#
# Returns a {Number}.
getTabLength: ->
@state.get('tabLength')
@tabLength
# Specifies the tab length.
#
# tabLength - A {Number} that defines the new tab length.
setTabLength: (tabLength) ->
@state.set('tabLength', tabLength)
lastRow = @buffer.getLastRow()
@tokenizedLines = @buildPlaceholderTokenizedLinesForRows(0, lastRow)
@invalidateRow(0)
@emit "changed", { start: 0, end: lastRow, delta: 0 }
setTabLength: (@tabLength) ->
tokenizeInBackground: ->
return if not @visible or @pendingChunk or @destroyed
return if not @visible or @pendingChunk or not @isAlive()
@pendingChunk = true
_.defer =>
@pendingChunk = false
@tokenizeNextChunk() unless @destroyed
@tokenizeNextChunk() if @isAlive() and @buffer.isAlive()
tokenizeNextChunk: ->
rowsRemaining = @chunkSize
@@ -248,10 +248,6 @@ class TokenizedBuffer
endColumn = tokenizedLine.bufferColumnForToken(lastToken) + lastToken.bufferDelta
new Range([position.row, startColumn], [position.row, endColumn])
destroy: ->
@unsubscribe()
@destroyed = true
iterateTokensInBufferRange: (bufferRange, iterator) ->
bufferRange = Range.fromObject(bufferRange)
{ start, end } = bufferRange