Merge branch 'master' of github.com:github/atom

This commit is contained in:
Nathan Sobo
2012-06-12 15:21:06 -06:00
10 changed files with 138 additions and 18 deletions

View File

@@ -8,16 +8,19 @@ UndoManager = require 'undo-manager'
module.exports =
class Buffer
@idCounter = 1
modified: null
lines: null
path: null
@deserialize: (state, project) ->
if state.path
project.open(state.path)
buffer = project.open(state.path)
else
buffer = project.bufferWithId(state.id) ? project.open()
buffer.setText(state.text)
buffer
buffer.modified = state.modified
buffer
constructor: (path) ->
@id = @constructor.idCounter++
@@ -28,12 +31,13 @@ class Buffer
else
@setText('')
@undoManager = new UndoManager(this)
@modified = false
serialize: ->
if @getPath()
{ path: @path }
else
{ text: @getText(), id: @id }
{ text: @getText(), id: @id , modified: @modified}
getPath: ->
@path
@@ -139,6 +143,8 @@ class Buffer
newTextLines[lastLineIndex] += suffix
@lines[oldRange.start.row..oldRange.end.row] = newTextLines
@modified = true
@trigger 'change', { oldRange, newRange, oldText, newText }
newRange
@@ -155,15 +161,19 @@ class Buffer
@undoManager.redo()
save: ->
if not @getPath() then throw new Error("Tried to save buffer with no file path")
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: (path) ->
@setPath(path)
@save()
isModified: ->
@modified
getMode: ->
return @mode if @mode
extension = if @getPath() then @getPath().split('/').pop().split('.').pop() else null

View File

@@ -7,6 +7,8 @@ Range = require 'range'
EditSession = require 'edit-session'
CursorView = require 'cursor-view'
SelectionView = require 'selection-view'
Native = require 'native'
fs = require 'fs'
$ = require 'jquery'
_ = require 'underscore'
@@ -604,12 +606,14 @@ class Editor extends View
save: ->
if not @buffer.getPath()
path = $native.saveDialog()
return if not path
path = Native.saveDialog()
return false if not path
@buffer.saveAs(path)
else
@buffer.save()
true
clipScreenPosition: (screenPosition, options={}) ->
@renderer.clipScreenPosition(screenPosition, options)
@@ -778,7 +782,19 @@ class Editor extends View
close: ->
return if @mini
@removeActiveEditSession()
if @buffer.isModified()
filename = if @buffer.getPath() then fs.base(@buffer.getPath()) else "untitled buffer"
message = "'#{filename}' has changes, do you want to save them?"
detailedMessage = "Your changes will be lost if you don't save them"
buttons = [
["Save", => @save() and @removeActiveEditSession()]
["Cancel", =>]
["Don't save", => @removeActiveEditSession()]
]
Native.alert message, detailedMessage, buttons
else
@removeActiveEditSession()
unsubscribeFromBuffer: ->
@buffer.off ".editor#{@id}"

View File

@@ -123,6 +123,14 @@ class RootView extends View
editors: ->
@panes.find('.editor').map -> $(this).view()
modifiedBuffers: ->
modifiedBuffers = []
for editor in @editors()
for session in editor.editSessions
modifiedBuffers.push session.buffer if session.buffer.isModified()
modifiedBuffers
activeEditor: ->
if (editor = @panes.find('.editor.active')).length
editor.view()

View File

@@ -1,6 +1,7 @@
# This a weirdo file. We don't create a Window class, we just add stuff to
# the DOM window.
Native = require 'native'
fs = require 'fs'
_ = require 'underscore'
$ = require 'jquery'
@@ -60,6 +61,19 @@ windowAdditions =
return if $("head style[path='#{fullPath}']").length
$('head').append "<style path='#{fullPath}'>#{content}</style>"
reload: ->
if rootView.modifiedBuffers().length > 0
message = "There are unsaved buffers, reload anyway?"
detailedMessage = "You will lose all unsaved changes if you reload"
buttons = [
["Reload", -> Native.reload()]
["Cancel", ->]
]
Native.alert(message, detailedMessage, buttons)
else
Native.reload()
showConsole: ->
$native.showDevTools()