Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 8f09a2d

Browse files
authored
Merge pull request #1069 from atom/aw/accept-a-promise
Understand observeEmbeddedTextEditor to follow embedded TextEditor changes
2 parents c5d4cfb + 07bd683 commit 8f09a2d

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

lib/find.coffee

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports =
2121
new ResultsPaneView() if filePath is ResultsPaneView.URI
2222

2323
@subscriptions = new CompositeDisposable
24+
@currentItemSub = new Disposable
2425
@findHistory = new History(findHistory)
2526
@replaceHistory = new History(replaceHistory)
2627
@pathsHistory = new History(pathsHistory)
@@ -30,8 +31,16 @@ module.exports =
3031
@resultsModel = new ResultsModel(@findOptions)
3132

3233
@subscriptions.add atom.workspace.getCenter().observeActivePaneItem (paneItem) =>
34+
@subscriptions.delete @currentItemSub
35+
@currentItemSub.dispose()
36+
3337
if atom.workspace.isTextEditor(paneItem)
3438
@findModel.setEditor(paneItem)
39+
else if paneItem?.observeEmbeddedTextEditor?
40+
@currentItemSub = paneItem.observeEmbeddedTextEditor (editor) =>
41+
if atom.workspace.getCenter().getActivePaneItem() is paneItem
42+
@findModel.setEditor(editor)
43+
@subscriptions.add @currentItemSub
3544
else if paneItem?.getEmbeddedTextEditor?
3645
@findModel.setEditor(paneItem.getEmbeddedTextEditor())
3746
else

spec/find-spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ const {it, fit, ffit, beforeEach, afterEach} = require('./async-spec-helpers') /
22

33
const BufferSearch = require('../lib/buffer-search')
44
const EmbeddedEditorItem = require('./item/embedded-editor-item')
5+
const DeferredEditorItem = require('./item/deferred-editor-item');
56
const UnrecognizedItem = require('./item/unrecognized-item');
67

78
describe('Find', () => {
89
describe('updating the find model', () => {
910
beforeEach(async () => {
1011
atom.workspace.addOpener(EmbeddedEditorItem.opener)
1112
atom.workspace.addOpener(UnrecognizedItem.opener)
13+
atom.workspace.addOpener(DeferredEditorItem.opener)
1214

1315
const activationPromise = atom.packages.activatePackage('find-and-replace')
1416
atom.commands.dispatch(atom.views.getView(atom.workspace), 'find-and-replace:show')
@@ -30,6 +32,17 @@ describe('Find', () => {
3032
expect(BufferSearch.prototype.setEditor).toHaveBeenCalledWith(embedded.refs.theEditor)
3133
})
3234

35+
it("sets the find model's editor to an embedded text editor after activation", async () => {
36+
const deferred = await atom.workspace.open(DeferredEditorItem.uri)
37+
expect(BufferSearch.prototype.setEditor).not.toHaveBeenCalled()
38+
39+
await deferred.showEditor()
40+
expect(BufferSearch.prototype.setEditor).toHaveBeenCalledWith(deferred.refs.theEditor)
41+
42+
await deferred.hideEditor()
43+
expect(BufferSearch.prototype.setEditor).toHaveBeenCalledWith(null)
44+
})
45+
3346
it("sets the find model's editor to null if a non-editor is focused", async () => {
3447
await atom.workspace.open(UnrecognizedItem.uri)
3548
expect(BufferSearch.prototype.setEditor).toHaveBeenCalledWith(null)

spec/item/deferred-editor-item.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// An active workspace item that embeds an AtomTextEditor we wish to expose to Find and Replace that does not become
2+
// available until some time after the item is activated.
3+
4+
const etch = require('etch');
5+
const $ = etch.dom;
6+
7+
const { TextEditor, Emitter } = require('atom');
8+
9+
class DeferredEditorItem {
10+
static opener(u) {
11+
if (u === DeferredEditorItem.uri) {
12+
return new DeferredEditorItem();
13+
} else {
14+
return undefined;
15+
}
16+
}
17+
18+
constructor() {
19+
this.editorShown = false;
20+
this.emitter = new Emitter();
21+
22+
etch.initialize(this);
23+
}
24+
25+
render() {
26+
if (this.editorShown) {
27+
return (
28+
$.div({className: 'wrapper'},
29+
etch.dom(TextEditor, {ref: 'theEditor'})
30+
)
31+
)
32+
} else {
33+
return (
34+
$.div({className: 'wrapper'}, 'Empty')
35+
)
36+
}
37+
}
38+
39+
update() {
40+
return etch.update(this)
41+
}
42+
43+
observeEmbeddedTextEditor(cb) {
44+
if (this.editorShown) {
45+
cb(this.refs.theEditor)
46+
}
47+
return this.emitter.on('did-change-embedded-text-editor', cb)
48+
}
49+
50+
async showEditor() {
51+
const wasShown = this.editorShown
52+
this.editorShown = true
53+
await this.update()
54+
if (!wasShown) {
55+
this.emitter.emit('did-change-embedded-text-editor', this.refs.theEditor)
56+
}
57+
}
58+
59+
async hideEditor() {
60+
const wasShown = this.editorShown
61+
this.editorShown = false
62+
await this.update()
63+
if (wasShown) {
64+
this.emitter.emit('did-change-embedded-text-editor', null)
65+
}
66+
}
67+
}
68+
69+
DeferredEditorItem.uri = 'atom://find-and-replace/spec/deferred-editor'
70+
71+
module.exports = DeferredEditorItem

0 commit comments

Comments
 (0)