mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Buffer listens for changes to file and triggers 'contents-change' event
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -54,6 +54,7 @@ class EditSession
|
||||
@buffer.off ".edit-session-#{@id}"
|
||||
@displayBuffer.off ".edit-session-#{@id}"
|
||||
@displayBuffer.destroy()
|
||||
@trigger "destroy"
|
||||
|
||||
serialize: ->
|
||||
buffer: @buffer.getPath()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user