From a1c39c9f38df4d387a0edce400cf716a5b62f7c8 Mon Sep 17 00:00:00 2001 From: probablycorey Date: Tue, 1 Oct 2013 17:31:08 -0700 Subject: [PATCH] Add RootView::openAsync --- spec/root-view-spec.coffee | 25 ++++++++++++++++- src/root-view.coffee | 50 ++++++++++++++++++++++++++------- src/window-event-handler.coffee | 3 +- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/spec/root-view-spec.coffee b/spec/root-view-spec.coffee index d9f58a061..7bdb65366 100644 --- a/spec/root-view-spec.coffee +++ b/spec/root-view-spec.coffee @@ -205,7 +205,7 @@ describe "RootView", -> rootView.trigger 'window:decrease-font-size' expect(config.get('editor.fontSize')).toBe 1 - describe ".open(path, options)", -> + describe ".open(filePath, options)", -> describe "when there is no active pane", -> beforeEach -> spyOn(Pane.prototype, 'focus') @@ -279,6 +279,29 @@ describe "RootView", -> editSession = rootView.open('b', changeFocus: false) expect(activePane.focus).not.toHaveBeenCalled() + describe ".openAsync(filePath)", -> + describe "when there is an active pane", -> + [activePane] = [] + beforeEach -> + activePane = rootView.getActivePane() + spyOn(activePane, 'focus') + + describe "when called with a path", -> + describe "when the active pane does not have an edit session item for the path being opened", -> + it "creates a new edit session for the given path in the active editor and returns a promise", -> + openHandler = jasmine.createSpy("Open Handler") + promise = rootView.openAsync('b', openHandler) + expect(activePane.items.length).toBe 1 + expect(activePane.focus).not.toHaveBeenCalled() + + waitsForPromise -> + promise.then (editSession) -> + expect(activePane.activeItem).toBe editSession + + runs -> + expect(activePane.items.length).toBe 2 + expect(activePane.focus).toHaveBeenCalled() + describe "window:toggle-invisibles event", -> it "shows/hides invisibles in all open and future editors", -> rootView.height(200) diff --git a/src/root-view.coffee b/src/root-view.coffee index aad82879f..dfba86c55 100644 --- a/src/root-view.coffee +++ b/src/root-view.coffee @@ -1,5 +1,6 @@ ipc = require 'ipc' path = require 'path' +Q = require 'q' $ = require './jquery-extensions' {$$, View} = require './space-pen-extensions' fsUtils = require './fs-utils' @@ -162,32 +163,61 @@ class RootView extends View confirmClose: -> @panes.confirmClose() - # Public: Opens a given a filepath in Atom. + # Public: Asynchronously opens a given a filepath in Atom. # - # * path: A file path + # * filePath: A file path # * options - # + initialLine: - # The buffer line number to open to. + # + initialLine: The buffer line number to open to. # - # Returns the {EditSession} for the file URI. - open: (path, options = {}) -> + # Returns a promise that resolves to the {EditSession} for the file URI. + openAsync: (filePath, options) -> + deferred = Q.defer() + + filePath = project.relativize(filePath) + initialLine = options.initialLine + if activePane = @getActivePane() + if filePath + if editSession = activePane.itemForUri(filePath) + deferred.resolve(editSession) + else + editSession = project.open(filePath, {initialLine}) + deferred.resolve(editSession) + else + editSession = project.open() + deferred.resolve(editSession) + else + editSession = project.open(filePath, {initialLine}) + deferred.resolve(editSession) + deferred.promise.done (editSession) => + activePane = new Pane(editSession) + @panes.setRoot(activePane) + + deferred.promise.done (editSession) -> + activePane.showItem(editSession) + activePane.focus() + + deferred.promise + + # Private: DEPRECATED Synchronously Opens a given a filepath in Atom. + open: (filePath, options = {}) -> changeFocus = options.changeFocus ? true initialLine = options.initialLine - path = project.relativize(path) + filePath = project.relativize(filePath) if activePane = @getActivePane() - if path - editSession = activePane.itemForUri(path) ? project.open(path, {initialLine}) + if filePath + editSession = activePane.itemForUri(filePath) ? project.open(filePath, {initialLine}) else editSession = project.open() activePane.showItem(editSession) else - editSession = project.open(path, {initialLine}) + editSession = project.open(filePath, {initialLine}) activePane = new Pane(editSession) @panes.setRoot(activePane) activePane.focus() if changeFocus editSession + # Public: Updates the application's title, based on whichever file is open. updateTitle: -> if projectPath = project.getPath() diff --git a/src/window-event-handler.coffee b/src/window-event-handler.coffee index 60e157b6d..a5ac852bf 100644 --- a/src/window-event-handler.coffee +++ b/src/window-event-handler.coffee @@ -24,7 +24,8 @@ class WindowEventHandler @subscribe $(window), 'blur', -> $("body").addClass('is-blurred') @subscribe $(window), 'window:open-path', (event, {pathToOpen, initialLine}) -> - atom.rootView?.open(pathToOpen, {initialLine}) unless fsUtils.isDirectorySync(pathToOpen) + unless fsUtils.isDirectorySync(pathToOpen) + atom.rootView?.openAsync(pathToOpen, initialLine) @subscribe $(window), 'beforeunload', => confirmed = atom.rootView?.confirmClose()