From 6c2bb26e77054fe43041effd2fbe2633097b6c7e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 28 Aug 2014 18:28:10 -0600 Subject: [PATCH] Add Workspace::observeTextEditors and ::onDidAddTextEditor --- spec/workspace-spec.coffee | 15 +++++++++++++++ src/workspace.coffee | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index a75896582..64ba379b2 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -284,6 +284,21 @@ describe "Workspace", -> waitsForPromise -> workspace.openLicense() runs -> expect(workspace.activePaneItem.getText()).toMatch /Copyright/ + describe "::observeTextEditors()", -> + it "invokes the observer with current and future text editors", -> + observed = [] + + waitsForPromise -> workspace.open() + waitsForPromise -> workspace.open() + waitsForPromise -> workspace.openLicense() + + runs -> + workspace.observeTextEditors (editor) -> observed.push(editor) + + waitsForPromise -> workspace.open() + + expect(observed).toEqual workspace.getTextEditors() + describe "when an editor is destroyed", -> it "removes the editor", -> editor = null diff --git a/src/workspace.coffee b/src/workspace.coffee index 4581d0099..84e3ff4a8 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -127,7 +127,32 @@ class Workspace extends Model # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. observePaneItems: (callback) -> @paneContainer.observePaneItems(callback) - # Public: Register a function to be called for every current and future + # Public: Invoke the given callback when a text editor is added to the + # workspace. + # + # * `callback` {Function} to be called panes are added. + # * `event` {Object} with the following keys: + # * `textEditor` {Editor} that was added. + # * `pane` {Pane} containing the added text editor. + # * `index` {Number} indicating the index of the added text editor in its + # pane. + # + # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onDidAddTextEditor: (callback) -> + @onDidAddPaneItem ({item, pane, index}) -> + callback({textEditor: item, pane, index}) if item instanceof Editor + + # Public: Invoke the given callback with all current and future text editors + # in the workspace. + # + # * `callback` {Function} to be called with current and future text editors. + # + # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + observeTextEditors: (callback) -> + callback(textEditor) for textEditor in @getTextEditors() + @onDidAddTextEditor ({textEditor}) -> callback(textEditor) + + # Deprecated: Register a function to be called for every current and future # {Editor} in the workspace. # # * `callback` A {Function} with an {Editor} as its only argument. @@ -138,7 +163,7 @@ class Workspace extends Model callback(editor) for editor in @getEditors() @subscribe this, 'editor-created', (editor) -> callback(editor) - # Public: Get all current editors in the workspace. + # Deprecated: Get all current editors in the workspace. # # Returns an {Array} of {Editor}s. getEditors: -> @@ -148,6 +173,12 @@ class Workspace extends Model editors + # Public: Get all text editors in the workspace. + # + # Returns an {Array} of {Editor}s. + getTextEditors: -> + @getPaneItems().filter (item) -> item instanceof Editor + # Public: Open a given a URI in Atom asynchronously. # # * `uri` A {String} containing a URI.