When the active edit session is modified, and its file is then modified by the file system, it displays an alert.

This commit is contained in:
Corey Johnson
2012-07-26 10:37:32 -07:00
parent 8b743b90a2
commit 42299c6a71
4 changed files with 69 additions and 2 deletions

View File

@@ -110,6 +110,27 @@ describe "Editor", ->
expect(editor.isFocused).toBeFalsy()
expect(editor).not.toHaveClass('focused')
describe "when the activeEditSession's file is modified on disk", ->
it "triggers an alert", ->
path = "/tmp/atom-changed-file.txt"
fs.write(path, "")
editSession = project.buildEditSessionForPath(path)
editor.edit(editSession)
editor.insertText("now the buffer is modified")
fileChangeHandler = jasmine.createSpy('fileChange')
editSession.buffer.file.on 'contents-change', fileChangeHandler
spyOn($native, "alert")
fs.write(path, "a file change")
waitsFor "file to trigger contents-change event", ->
fileChangeHandler.callCount > 0
runs ->
expect($native.alert).toHaveBeenCalled()
describe ".remove()", ->
it "removes subscriptions from all edit session buffers", ->
previousEditSession = editor.activeEditSession
@@ -227,6 +248,26 @@ describe "Editor", ->
editor.activeEditSession.selectToEndOfLine()
expect(editor.getSelectionView().find('.selection')).toExist()
it "triggers alert if edit session's file changed on disk", ->
path = "/tmp/atom-changed-file.txt"
fs.write(path, "")
editSession = project.buildEditSessionForPath(path)
editSession.insertText("a buffer change")
fileChangeHandler = jasmine.createSpy('fileChange')
editSession.buffer.file.on 'contents-change', fileChangeHandler
spyOn($native, "alert")
fs.write(path, "a file change")
waitsFor "file to trigger contents-change event", ->
fileChangeHandler.callCount > 0
runs ->
editor.edit(editSession)
expect($native.alert).toHaveBeenCalled()
describe ".loadNextEditSession()", ->
it "loads the next editor state and wraps to beginning when end is reached", ->
expect(editor.activeEditSession).toBe session2

View File

@@ -81,6 +81,13 @@ class Buffer
@file?.off()
@file = new File(path)
@subscribeToFile()
@file.on "contents-change", =>
if @isModified()
@modifiedOnDisk = true
@trigger "contents-change-on-disk"
else
@setText(fs.read(@file.getPath()))
@modified = false
@trigger "path-change", this
getExtension: ->

View File

@@ -44,7 +44,10 @@ class EditSession
@buffer.retain()
@buffer.on "path-change.edit-session-#{@id}", =>
@trigger 'buffer-path-change'
@trigger "buffer-path-change"
@buffer.on "contents-change-on-disk.edit-session-#{@id}", =>
@trigger "buffer-contents-change-on-disk"
@buffer.on "update-anchors-after-change.edit-session-#{@id}", =>
@mergeCursors()

View File

@@ -386,10 +386,26 @@ class Editor extends View
@activeEditSession = @editSessions[index]
@activeEditSession.on "buffer-path-change", => @trigger 'editor-path-change'
if @activeEditSession.buffer.isModifiedOnDisk() and @activeEditSession.buffer.isModified()
@alertEditSessionChangedOnDisk(@activeEditSession)
@activeEditSession.on "buffer-contents-change-on-disk", =>
@alertEditSessionChangedOnDisk(@activeEditSession)
@activeEditSession.on "buffer-path-change", =>
@trigger 'editor-path-change'
@trigger 'editor-path-change'
@renderWhenAttached()
alertEditSessionChangedOnDisk: (editSession) ->
message = editSession.getPath()
detailedMessage = "Has changed on disk. Do you want to reload it?"
Native.alert message, detailedMessage, [
["Reload", => editSession.buffer.reload()]
["Cancel", => ],
]
activateEditSessionForPath: (path) ->
for editSession, index in @editSessions
if editSession.buffer.getPath() == path