From 4b1c3bccfbc3061e49ad0f023b652a7a2e37b78c Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Fri, 25 Jan 2019 14:08:29 -0500 Subject: [PATCH 1/3] Example components for tests about custom pane items --- spec/item/embedded-editor-item.js | 38 +++++++++++++++++++++++++++++++ spec/item/unrecognized-item.js | 30 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 spec/item/embedded-editor-item.js create mode 100644 spec/item/unrecognized-item.js diff --git a/spec/item/embedded-editor-item.js b/spec/item/embedded-editor-item.js new file mode 100644 index 00000000..fd07072f --- /dev/null +++ b/spec/item/embedded-editor-item.js @@ -0,0 +1,38 @@ +// An active workspace item that embeds an AtomTextEditor we wish to expose to Find and Replace. + +const etch = require('etch'); +const $ = etch.dom; + +const { TextEditor } = require('atom'); + +class EmbeddedEditorItem { + static opener(u) { + if (u === EmbeddedEditorItem.uri) { + return new EmbeddedEditorItem(); + } else { + return undefined; + } + } + + constructor() { + etch.initialize(this); + } + + render() { + return ( + $.div({className: 'wrapper'}, + etch.dom(TextEditor, {ref: 'theEditor'}) + ) + ) + } + + update() {} + + getEmbeddedTextEditor() { + return this.refs.theEditor + } +} + +EmbeddedEditorItem.uri = 'atom://find-and-replace/spec/embedded-editor' + +module.exports = EmbeddedEditorItem diff --git a/spec/item/unrecognized-item.js b/spec/item/unrecognized-item.js new file mode 100644 index 00000000..6feb2b08 --- /dev/null +++ b/spec/item/unrecognized-item.js @@ -0,0 +1,30 @@ +// An active workspace item that doesn't contain a TextEditor. + +const etch = require('etch'); +const $ = etch.dom; + +class UnrecognizedItem { + static opener(u) { + if (u === UnrecognizedItem.uri) { + return new UnrecognizedItem(); + } else { + return undefined; + } + } + + constructor() { + etch.initialize(this); + } + + render() { + return ( + $.div({className: 'wrapper'}, 'Some text') + ) + } + + update() {} +} + +UnrecognizedItem.uri = 'atom://find-and-replace/spec/unrecognized' + +module.exports = UnrecognizedItem From 82a8e22f248e9c24c0198bb6ea3d629f5be8d342 Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Fri, 25 Jan 2019 14:09:57 -0500 Subject: [PATCH 2/3] Unit tests for finding TextEditors embedded within other components --- spec/find-spec.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/spec/find-spec.js b/spec/find-spec.js index 685900ac..24d535ab 100644 --- a/spec/find-spec.js +++ b/spec/find-spec.js @@ -1,10 +1,15 @@ const {it, fit, ffit, beforeEach, afterEach} = require('./async-spec-helpers') // eslint-disable-line no-unused-vars const BufferSearch = require('../lib/buffer-search') +const EmbeddedEditorItem = require('./item/embedded-editor-item') +const UnrecognizedItem = require('./item/unrecognized-item'); describe('Find', () => { describe('updating the find model', () => { beforeEach(async () => { + atom.workspace.addOpener(EmbeddedEditorItem.opener) + atom.workspace.addOpener(UnrecognizedItem.opener) + const activationPromise = atom.packages.activatePackage('find-and-replace') atom.commands.dispatch(atom.views.getView(atom.workspace), 'find-and-replace:show') await activationPromise @@ -20,10 +25,13 @@ describe('Find', () => { expect(BufferSearch.prototype.setEditor).toHaveBeenCalledWith(editor) }) - it("sets the find model's editor to null if a non-editor is focused", async () => { - spyOn(atom.workspace, 'isTextEditor').andReturn(false) + it("sets the find model's editor to an embedded text editor", async () => { + const embedded = await atom.workspace.open(EmbeddedEditorItem.uri) + expect(BufferSearch.prototype.setEditor).toHaveBeenCalledWith(embedded.refs.theEditor) + }) - await atom.workspace.open() + it("sets the find model's editor to null if a non-editor is focused", async () => { + await atom.workspace.open(UnrecognizedItem.uri) expect(BufferSearch.prototype.setEditor).toHaveBeenCalledWith(null) }) }) From f8ef9b327fc29c153978f9c0fd6c256c2b1c9c49 Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Fri, 25 Jan 2019 14:13:12 -0500 Subject: [PATCH 3/3] Discover an embedded TextEditor within a non-TextEditor pane item --- lib/find.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/find.coffee b/lib/find.coffee index 03480531..602cb277 100644 --- a/lib/find.coffee +++ b/lib/find.coffee @@ -32,6 +32,8 @@ module.exports = @subscriptions.add atom.workspace.getCenter().observeActivePaneItem (paneItem) => if atom.workspace.isTextEditor(paneItem) @findModel.setEditor(paneItem) + else if paneItem?.getEmbeddedTextEditor? + @findModel.setEditor(paneItem.getEmbeddedTextEditor()) else @findModel.setEditor(null)