diff --git a/src/atom/browser.coffee b/src/atom/browser.coffee index 01e4653b2..b0a650400 100644 --- a/src/atom/browser.coffee +++ b/src/atom/browser.coffee @@ -6,22 +6,13 @@ module.exports = class Browser extends Document @register (path) -> /^https?:/.test path - buffers: {} - + path: null html: $ "
" + iframe: -> + $ "" - @isPathUrl: (path) -> - /^https?:\/\//.test path + constructor: (@path) -> + @html.html @iframe() + @show() - constructor: -> - atom.on "window:open", (e) => - path = e.details - return unless @constructor.isPathUrl path - - @buffers[path] ?= $ "" - - @html.html @buffers[path] - - @show() - - atom.trigger "browser:focus", path + atom.trigger 'browser:load', this diff --git a/src/atom/editor.coffee b/src/atom/editor.coffee index 995f130e5..f04094fae 100644 --- a/src/atom/editor.coffee +++ b/src/atom/editor.coffee @@ -11,13 +11,12 @@ module.exports = class Editor extends Document @register (path) -> fs.isFile path - activePath: null - - buffers: {} - + dirty: false + path: null html: $ "
" - constructor: -> + constructor: (@path) -> + super() atom.keybinder.register "editor", @ @show() @@ -31,13 +30,6 @@ class Editor extends Document @ace.setShowInvisibles(true) @ace.setPrintMarginColumn 78 - atom.on 'window:open', (e) => - path = e.details - @addBuffer e.details if fs.isFile path - - atom.on 'window:close', (e) => @removeBuffer e.details - atom.on 'editor:bufferFocus', (e) => @resize() - # Resize editor when panes are added/removed el = document.body el.addEventListener 'DOMNodeInsertedIntoDocument', => @resize() @@ -46,6 +38,19 @@ class Editor extends Document @resize() , 500 + # Setup the session + code = if @path then fs.read @path else '' + session = new EditSession code + session.setUndoManager new UndoManager + session.setUseSoftTabs useSoftTabs = @usesSoftTabs code + session.setTabSize if useSoftTabs then @guessTabSize code else 8 + mode = @modeForPath() + session.setMode new mode if mode + session.on 'change', => @dirty = true + @ace.setSession session + + atom.trigger 'editor:load', this + modeMap: js: 'javascript' c: 'c_cpp' @@ -62,7 +67,7 @@ class Editor extends Document Gemfile: 'ruby' Rakefile: 'ruby' - modeForPath: (path) -> + modeForPath: (path=@path) -> return null if not path if not modeName = @modeFileMap[ _.last path.split '/' ] @@ -75,93 +80,36 @@ class Editor extends Document catch e null - addBuffer: (path) -> - throw "#{@constructor.name}: Cannot create buffer from a directory `#{path}`" if fs.isDirectory path - - buffer = @buffers[path] - if not buffer - code = if path then fs.read path else '' - buffer = new EditSession code - buffer.setUndoManager new UndoManager - buffer.setUseSoftTabs useSoftTabs = @usesSoftTabs code - buffer.setTabSize if useSoftTabs then @guessTabSize code else 8 - - mode = @modeForPath path - buffer.setMode new mode if mode - - @buffers[path] = buffer - - buffer.on 'change', -> buffer.$atom_dirty = true - atom.trigger "editor:bufferAdd", path - - @focusBuffer path - - removeBuffer: (path) -> - path ?= @activePath - - return if not path - - buffer = @buffers[path] - return if not buffer - - if buffer.$atom_dirty - # This should be thrown into it's own method, but I can't think of a good - # name. - detailedMessage = if @activePath - "#{@activePath} has changes." + close: -> + if @dirty + detailedMessage = if @path + "#{@path} has changes." else "An untitled file has changes." - canceled = atom.native.alert "Do you want to save the changes you made?", detailedMessage, + canceled = atom.native.alert "Do you want to save your changes?", + detailedMessage, "Save": => - path = @save() - not path # if save modal fails/cancels, consider it canceled + # if save modal fails/cancels, consider it cancelled + not @save() "Cancel": => true "Don't Save": => false return if canceled - delete @buffers[path] - - atom.trigger "editor:bufferRemove", path - - if path is @activePath - newActivePath = Object.keys(@buffers)[0] - if newActivePath - @focusBuffer newActivePath - else - @ace.setSession new EditSession '' - - focusBuffer: (path) -> - return if not path - - @show() - @activePath = path - - buffer = @buffers[path] or @addBuffer path - @ace.setSession buffer - - atom.trigger "editor:bufferFocus", path - - save: (path) -> - path ?= @activePath - - return @saveAs() if not path + save: -> + return @saveAs() if not @path @removeTrailingWhitespace() - fs.write path, @code() - if @buffers[path] - @buffers[path].$atom_dirty = false + fs.write @path, @code() + @dirty = false - path + @path saveAs: -> - path = atom.native.savePanel()?.toString() - if path + if path = atom.native.savePanel()?.toString() + @path = path @save path - @addBuffer path - - path code: -> @ace.getSession().getValue() diff --git a/src/atom/window.coffee b/src/atom/window.coffee index 6a7e932e9..943795764 100644 --- a/src/atom/window.coffee +++ b/src/atom/window.coffee @@ -1,16 +1,9 @@ -Browser = require 'browser' -Editor = require 'editor' - fs = require 'fs' _ = require 'underscore' # This a weirdo file. We don't create a Window class, we just add stuff to # the DOM window. windowAdditions = - editor: null - - browser: null - appRoot: OSX.NSBundle.mainBundle.resourcePath path: null @@ -18,8 +11,8 @@ windowAdditions = startup: -> atom.keybinder.register "window", window - @path = $atomController.path - @setTitle _.last @path.split '/' + @path = $atomController.path.toString() + @setTitle (_.last @path.split '/') or 'Untitled Document' # Remember sizing! defaultFrame = x: 0, y: 0, width: 600, height: 800 @@ -27,9 +20,6 @@ windowAdditions = rect = OSX.CGRectMake(frame.x, frame.y, frame.width, frame.height) $atomController.window.setFrame_display rect, true - @editor = new Editor - @browser = new Browser - $atomController.window.makeKeyWindow atom.trigger 'window:load' diff --git a/src/startup.coffee b/src/startup.coffee index 5eb623d72..cf16e7901 100644 --- a/src/startup.coffee +++ b/src/startup.coffee @@ -2,6 +2,9 @@ window.atom = {} App = require 'app' +Browser = require 'browser' +Document = require 'document' +Editor = require 'editor' Event = require 'event' ExtensionManager = require 'extension-manager' KeyBinder = require 'key-binder' @@ -26,5 +29,8 @@ atom.app = new App for name, method of atom.app atom[name] = atom.app[name] +atom.path = $atomController.path.toString() +atom.document = new Document.handler atom.path + require 'window' -window.startup() \ No newline at end of file +window.startup() diff --git a/src/stdlib/key-binder.coffee b/src/stdlib/key-binder.coffee index de5e74bd8..47ce157be 100644 --- a/src/stdlib/key-binder.coffee +++ b/src/stdlib/key-binder.coffee @@ -21,9 +21,10 @@ class KeyBinder load: (path) -> try Watcher.watch path, => - # Should we keep track of which file bindings are associated with? That - # way we could clear bindings when the file is changed or deleted. I - # think the answer is yes, but I don't want to do this right now. + # Should we keep track of which file bindings are associated with? + # That way we could clear bindings when the file is changed + # or deleted. I think the answer is yes, but I don't want to + # do this right now. console.log "#{@name}: Reloading #{path}" @load path