Buffers are memoized on project by path

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-18 12:49:18 -07:00
parent 72750fd4a2
commit b959d5aa37
5 changed files with 54 additions and 34 deletions

View File

@@ -20,7 +20,7 @@ class Buffer
anchorRanges: null
refcount: 0
constructor: (path) ->
constructor: (path, @project) ->
@id = @constructor.idCounter++
@anchors = []
@anchorRanges = []
@@ -38,8 +38,9 @@ class Buffer
destroy: ->
throw new Error("Destroying buffer twice with path '#{@getPath()}'") if @destroyed
@destroyed = true
@file?.off()
@destroyed = true
@project?.removeBuffer(this)
retain: ->
@refcount++
@@ -50,6 +51,12 @@ class Buffer
@destroy() if @refcount <= 0
this
subscribeToFile: ->
@file?.on "contents-change", =>
unless @isModified()
@setText(fs.read(@file.getPath()))
@modified = false
getPath: ->
@file?.getPath()
@@ -58,10 +65,7 @@ class Buffer
@file?.off()
@file = new File(path)
@file.on "contents-change", =>
unless @isModified()
@setText(fs.read(@file.getPath()))
@modified = false
@subscribeToFile()
@trigger "path-change", this
getExtension: ->

View File

@@ -21,7 +21,7 @@ class Project
constructor: (path) ->
@setPath(path)
@editSessions = []
@buffer = []
@buffers = []
@setTabText(' ')
@setAutoIndent(true)
@setSoftTabs(true)
@@ -30,6 +30,9 @@ class Project
/(^|\/)\.git(\/|$)/
]
destroy: ->
editSession.destroy() for editSession in @getEditSessions()
getPath: ->
@rootDirectory?.path
@@ -106,9 +109,6 @@ class Project
getEditSessions: ->
new Array(@editSessions...)
destroy: ->
editSession.destroy() for editSession in @getEditSessions()
removeEditSession: (editSession) ->
_.remove(@editSessions, editSession)
@@ -122,16 +122,20 @@ class Project
bufferForPath: (filePath) ->
if filePath?
filePath = @resolve(filePath)
return editSession.buffer for editSession in @editSessions when editSession.buffer.getPath() == filePath
@buildBuffer(filePath)
buffer = _.find @buffers, (buffer) -> buffer.getPath() == filePath
buffer or @buildBuffer(filePath)
else
@buildBuffer()
buildBuffer: (filePath) ->
buffer = new Buffer(filePath)
buffer = new Buffer(filePath, this)
@buffers.push buffer
@trigger 'new-buffer', buffer
buffer
removeBuffer: (buffer) ->
_.remove(@buffers, buffer)
scan: (regex, iterator) ->
regex = new RegExp(regex.source, 'g')
commands = [