Buffer listens for changes to file and triggers 'contents-change' event

This commit is contained in:
Corey Johnson
2012-06-29 10:20:19 -07:00
parent 9907751fd9
commit c3d7d3566f
16 changed files with 354 additions and 89 deletions

View File

@@ -13,20 +13,34 @@ class Buffer
lines: null
file: null
constructor: (path) ->
@id = @constructor.idCounter++
@setPath(path)
@lines = ['']
if fs.exists(@getPath())
@undoManager = new UndoManager(this)
if path
throw "Path '#{path}' does not exist" unless fs.exists(path)
@setPath(path)
@setText(fs.read(@getPath()))
else
@setText('')
@undoManager = new UndoManager(this)
@modified = false
destroy: ->
@file?.off()
getPath: ->
@file.getPath()
@file?.getPath()
setPath: (path) ->
return if path == @getPath()
@file?.off()
@file = new File(path)
@file.on "contents-change", =>
@setText(fs.read(@file.getPath())) unless @isModified()
@trigger "path-change", this
getExtension: ->
if @getPath()
@@ -34,13 +48,6 @@ class Buffer
else
null
setPath: (path) ->
@file?.off()
@file = new File(path)
@file.on "contents-change", =>
@setText(fs.read(@file.getPath())) unless @isModified()
@trigger "path-change", this
getText: ->
@lines.join('\n')
@@ -156,15 +163,16 @@ class Buffer
@undoManager.redo()
save: ->
if not @getPath() then throw new Error("Can't save buffer with no file path")
@trigger 'before-save'
fs.write @getPath(), @getText()
@modified = false
@trigger 'after-save'
@saveAs(@getPath())
saveAs: (path) ->
if not path then throw new Error("Can't save buffer with no file path")
@trigger 'before-save'
fs.write path, @getText()
@modified = false
@setPath(path)
@save()
@trigger 'after-save'
isModified: ->
@modified

View File

@@ -54,6 +54,7 @@ class EditSession
@buffer.off ".edit-session-#{@id}"
@displayBuffer.off ".edit-session-#{@id}"
@displayBuffer.destroy()
@trigger "destroy"
serialize: ->
buffer: @buffer.getPath()

View File

@@ -572,7 +572,8 @@ class Editor extends View
@buffer.off ".editor#{@id}"
destroyEditSessions: ->
session.destroy() for session in @editSessions
for session in @editSessions
session.destroy()
renderWhenAttached: ->
return unless @attached

View File

@@ -89,6 +89,11 @@ class Project
softTabs: @getSoftTabs()
softWrap: @getSoftWrap()
editSession.on 'destroy', =>
@editSessions = _.without(@editSessions, editSession)
bufferIsOrphaned = not _.find @editSessions, (e) -> e.buffer == editSession.buffer
editSession.buffer.destroy() if bufferIsOrphaned
@editSessions.push editSession
@trigger 'new-edit-session', editSession
editSession

View File

@@ -132,9 +132,9 @@ class RootView extends View
if not editor.mini
editor.on 'editor-path-change.root-view', =>
@trigger 'active-editor-path-change', editor.buffer.path
if not previousActiveEditor or editor.buffer.path != previousActiveEditor.buffer.path
@trigger 'active-editor-path-change', editor.buffer.path
@trigger 'active-editor-path-change', editor.buffer.getPath()
if not previousActiveEditor or editor.buffer.getPath() != previousActiveEditor.buffer.getPath()
@trigger 'active-editor-path-change', editor.buffer.getPath()
activeKeybindings: ->
keymap.bindingsForElement(document.activeElement)

View File

@@ -28,7 +28,7 @@ class StatusBar extends View
@editor.on 'cursor-move', => @updateCursorPositionText()
updatePathText: ->
path = @editor.buffer.path
path = @editor.buffer.getPath()
if path
@currentPath.text(@rootView.project.relativize(path))
else