From 92d0f60e6c408b65ac4d6b22a63256c93eae6c3f Mon Sep 17 00:00:00 2001 From: Jason Rudolph Date: Thu, 25 May 2017 17:10:56 -0400 Subject: [PATCH 1/2] :bug: Add missing onDidAddTextEditor method to Dock Dock's observeTextEditors() method calls this.onDidAddTextEditor(), but Dock didn't have an onDidAddTextEditor method. :scream_cat: Dock's observeTextEditors() method also calls this.getTextEditors(), and Dock's getTextEditors() method calls this.paneContainer.getTextEditors(), but there is no getTextEditors() method on this.paneContainer. :see_no_evil: This commit adds a test for observeTextEditors, and it adds the missing onDidAddTextEditor method, and it fixes the getTextEditors method to make the new test pass. --- spec/dock-spec.js | 26 ++++++++++++++++++++++++++ src/dock.js | 21 ++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/spec/dock-spec.js b/spec/dock-spec.js index f9b8a5326..ba15b9339 100644 --- a/spec/dock-spec.js +++ b/spec/dock-spec.js @@ -1,5 +1,7 @@ /** @babel */ +const TextEditor = require('../src/text-editor') + import {it, fit, ffit, fffit, beforeEach, afterEach} from './async-spec-helpers' describe('Dock', () => { @@ -329,4 +331,28 @@ describe('Dock', () => { expect(() => atom.workspace.getElement().handleDragStart(dragEvent)).not.toThrow() }) }) + + describe('.observeTextEditors()', () => { + it('invokes the observer with current and future text editors', () => { + const dock = atom.workspace.getLeftDock() + const dockPane = dock.getActivePane() + const observed = [] + + const editorAddedBeforeRegisteringObserver = new TextEditor() + const nonEditorItemAddedBeforeRegisteringObserver = document.createElement('div') + dockPane.activateItem(editorAddedBeforeRegisteringObserver) + dockPane.activateItem(nonEditorItemAddedBeforeRegisteringObserver) + + dock.observeTextEditors(editor => observed.push(editor)) + + const editorAddedAfterRegisteringObserver = new TextEditor() + const nonEditorItemAddedAfterRegisteringObserver = document.createElement('div') + dockPane.activateItem(editorAddedAfterRegisteringObserver) + dockPane.activateItem(nonEditorItemAddedAfterRegisteringObserver) + + expect(observed).toEqual( + [editorAddedBeforeRegisteringObserver, editorAddedAfterRegisteringObserver] + ) + }) + }) }) diff --git a/src/dock.js b/src/dock.js index d531e8627..f196f4827 100644 --- a/src/dock.js +++ b/src/dock.js @@ -565,6 +565,25 @@ module.exports = class Dock { return this.paneContainer.onDidDestroyPaneItem(callback) } + // Extended: Invoke the given callback when a text editor is added to the + // dock. + // + // * `callback` {Function} to be called when panes are added. + // * `event` {Object} with the following keys: + // * `textEditor` {TextEditor} 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) { + return this.onDidAddPaneItem(({item, pane, index}) => { + if (item instanceof TextEditor) { + callback({textEditor: item, pane, index}) + } + }) + } + /* Section: Pane Items */ @@ -587,7 +606,7 @@ module.exports = class Dock { // // Returns an {Array} of {TextEditor}s. getTextEditors () { - return this.paneContainer.getTextEditors() + return this.getPaneItems().filter(item => item instanceof TextEditor) } // Essential: Get the active item if it is an {TextEditor}. From a49203504c262fd5380d8a81611d9c6826cf2590 Mon Sep 17 00:00:00 2001 From: Jason Rudolph Date: Thu, 25 May 2017 17:23:02 -0400 Subject: [PATCH 2/2] :bug: Add missing onDidAddTextEditor method to WorkspaceCenter WorkspaceCenter's observeTextEditors method calls this.onDidAddTextEditor, but WorkspaceCenter didn't have an onDidAddTextEditor method. This commit adds a test for observeTextEditors and it adds the missing onDidAddTextEditor method to make the test pass. --- spec/workspace-center-spec.js | 31 +++++++++++++++++++++++++++++++ src/workspace-center.js | 19 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 spec/workspace-center-spec.js diff --git a/spec/workspace-center-spec.js b/spec/workspace-center-spec.js new file mode 100644 index 000000000..7c01078c3 --- /dev/null +++ b/spec/workspace-center-spec.js @@ -0,0 +1,31 @@ +/** @babel */ + +const TextEditor = require('../src/text-editor') + +import {it, fit, ffit, fffit, beforeEach, afterEach} from './async-spec-helpers' + +describe('WorkspaceCenter', () => { + describe('.observeTextEditors()', () => { + it('invokes the observer with current and future text editors', () => { + const workspaceCenter = atom.workspace.getCenter() + const pane = workspaceCenter.getActivePane() + const observed = [] + + const editorAddedBeforeRegisteringObserver = new TextEditor() + const nonEditorItemAddedBeforeRegisteringObserver = document.createElement('div') + pane.activateItem(editorAddedBeforeRegisteringObserver) + pane.activateItem(nonEditorItemAddedBeforeRegisteringObserver) + + workspaceCenter.observeTextEditors(editor => observed.push(editor)) + + const editorAddedAfterRegisteringObserver = new TextEditor() + const nonEditorItemAddedAfterRegisteringObserver = document.createElement('div') + pane.activateItem(editorAddedAfterRegisteringObserver) + pane.activateItem(nonEditorItemAddedAfterRegisteringObserver) + + expect(observed).toEqual( + [editorAddedBeforeRegisteringObserver, editorAddedAfterRegisteringObserver] + ) + }) + }) +}) diff --git a/src/workspace-center.js b/src/workspace-center.js index 56d821265..1972c5c59 100644 --- a/src/workspace-center.js +++ b/src/workspace-center.js @@ -226,6 +226,25 @@ module.exports = class WorkspaceCenter { return this.paneContainer.onDidDestroyPaneItem(callback) } + // Extended: Invoke the given callback when a text editor is added to the + // workspace center. + // + // * `callback` {Function} to be called when panes are added. + // * `event` {Object} with the following keys: + // * `textEditor` {TextEditor} 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) { + return this.onDidAddPaneItem(({item, pane, index}) => { + if (item instanceof TextEditor) { + callback({textEditor: item, pane, index}) + } + }) + } + /* Section: Pane Items */