Skip to content

Commit 26ed59f

Browse files
authored
fix: only highlight when panel is visible (#37)
1 parent 6b62094 commit 26ed59f

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

lib/minimap-find-and-replace-binding.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
const { CompositeDisposable } = require('atom')
44

55
class MinimapFindAndReplaceBinding {
6-
constructor (minimap, fnrAPI) {
6+
constructor (minimap, fnrAPI, fnrVisible) {
77
this.minimap = minimap
88
this.fnrAPI = fnrAPI
9+
this.fnrVisible = fnrVisible
910
this.editor = this.minimap.getTextEditor()
1011
this.subscriptions = new CompositeDisposable()
1112
this.decorationsByMarkerId = {}
1213
this.subscriptionsByMarkerId = {}
1314

1415
this.layer = this.fnrAPI.resultsMarkerLayerForTextEditor(this.editor)
1516

16-
this.subscriptions.add(this.layer.onDidUpdate(() => this.discoverMarkers()))
17+
this.subscriptions.add(this.layer.onDidUpdate(() => {
18+
if (this.fnrVisible) {
19+
this.discoverMarkers()
20+
}
21+
}))
1722

1823
this.discoverMarkers()
1924
}
@@ -34,6 +39,15 @@ class MinimapFindAndReplaceBinding {
3439
this.subscriptionsByMarkerId = {}
3540
}
3641

42+
changeVisible (visible) {
43+
this.fnrVisible = visible
44+
if (visible) {
45+
this.discoverMarkers()
46+
} else {
47+
this.clear()
48+
}
49+
}
50+
3751
clear () {
3852
let id
3953
for (id in this.subscriptionsByMarkerId) {

lib/minimap-find-and-replace.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010

1111
activate (state) {
1212
this.active = false
13+
this.fnrVisible = false
1314
this.bindingsById = {}
1415
this.subscriptionsById = {}
1516
this.subscriptions = new CompositeDisposable()
@@ -48,7 +49,7 @@ module.exports = {
4849
const {
4950
id,
5051
} = minimap
51-
const binding = new MinimapFindAndReplaceBinding(minimap, fnr)
52+
const binding = new MinimapFindAndReplaceBinding(minimap, fnr, this.fnrVisible)
5253
this.bindingsById[id] = binding
5354

5455
this.subscriptionsById[id] = minimap.onDidDestroy(() => {
@@ -66,27 +67,31 @@ module.exports = {
6667
})
6768
},
6869

69-
setOnChangeVisibility (num = 1) {
70+
setOnChangeVisibility (retry = 0) {
7071
const [fnrPanel] = atom.workspace.getBottomPanels().filter(panel => panel.element.firstChild.classList.contains('find-and-replace'))
7172

7273
if (fnrPanel) {
73-
fnrPanel.onDidChangeVisible(visible => {
74-
if (visible) {
75-
this.discoverMarkers()
76-
} else {
77-
this.clearBindings()
78-
}
74+
this.changeVisible(true)
75+
fnrPanel.onDidChangeVisible((visible) => {
76+
this.changeVisible(visible)
7977
})
8078
} else {
81-
if (num < 10) {
82-
setTimeout(() => this.setOnChangeVisibility(num + 1), 100)
79+
if (retry < 10) {
80+
setTimeout(() => this.setOnChangeVisibility(retry + 1), 100)
8381
} else {
8482
// eslint-disable-next-line no-console
8583
console.error("Couldn't find find-and-replace")
8684
}
8785
}
8886
},
8987

88+
changeVisible (visible) {
89+
this.fnrVisible = visible
90+
for (const id in this.bindingsById) {
91+
this.bindingsById[id].changeVisible(visible)
92+
}
93+
},
94+
9095
deactivatePlugin () {
9196
let id
9297
if (!this.active) {
@@ -106,16 +111,4 @@ module.exports = {
106111
this.bindingsById = {}
107112
this.subscriptionsById = {}
108113
},
109-
110-
discoverMarkers () {
111-
for (const id in this.bindingsById) {
112-
this.bindingsById[id].discoverMarkers()
113-
}
114-
},
115-
116-
clearBindings () {
117-
for (const id in this.bindingsById) {
118-
this.bindingsById[id].clear()
119-
}
120-
},
121114
}

spec/minimap-find-and-replace-spec.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,50 @@ describe('MinimapFindAndReplace', () => {
3232
describe('when find-and-replace is closed', () => {
3333
beforeEach(() => {
3434
atom.commands.dispatch(workspace, 'find-and-replace:show')
35-
spyOn(MinimapFindAndReplace, 'clearBindings')
35+
spyOn(MinimapFindAndReplace, 'changeVisible')
3636
})
3737

3838
it('should clear on core:cancel', () => {
3939
atom.commands.dispatch(workspace, 'core:cancel')
40-
expect(MinimapFindAndReplace.clearBindings).toHaveBeenCalledTimes(1)
40+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledTimes(1)
41+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledWith(false)
4142
})
4243

4344
it('should clear on find-and-replace:toggle', () => {
4445
atom.commands.dispatch(workspace, 'find-and-replace:toggle')
45-
atom.commands.dispatch(workspace, 'find-and-replace:toggle')
46-
expect(MinimapFindAndReplace.clearBindings).toHaveBeenCalledTimes(1)
46+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledTimes(1)
47+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledWith(false)
4748
})
4849

4950
it('should clear on close button pressed', () => {
5051
document.querySelector('.find-and-replace .close-button').click()
51-
expect(MinimapFindAndReplace.clearBindings).toHaveBeenCalledTimes(1)
52+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledTimes(1)
53+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledWith(false)
5254
})
5355
})
5456

5557
describe('when find-and-replace is opened', () => {
5658
beforeEach(() => {
5759
atom.commands.dispatch(workspace, 'core:cancel')
58-
spyOn(MinimapFindAndReplace, 'discoverMarkers')
60+
spyOn(MinimapFindAndReplace, 'changeVisible')
5961
})
6062

6163
it('should display markers on find-and-replace:show', () => {
6264
atom.commands.dispatch(workspace, 'find-and-replace:show')
63-
expect(MinimapFindAndReplace.discoverMarkers).toHaveBeenCalledTimes(1)
65+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledTimes(1)
66+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledWith(true)
6467
})
6568

6669
it('should display markers on find-and-replace:show-replace', () => {
6770
atom.commands.dispatch(workspace, 'find-and-replace:show-replace')
68-
expect(MinimapFindAndReplace.discoverMarkers).toHaveBeenCalledTimes(1)
71+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledTimes(1)
72+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledWith(true)
6973
})
7074

7175
it('should display markers on find-and-replace:toggle', () => {
7276
atom.commands.dispatch(workspace, 'find-and-replace:toggle')
73-
expect(MinimapFindAndReplace.discoverMarkers).toHaveBeenCalledTimes(1)
77+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledTimes(1)
78+
expect(MinimapFindAndReplace.changeVisible).toHaveBeenCalledWith(true)
7479
})
7580
})
7681
})

0 commit comments

Comments
 (0)