This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathfind.coffee
216 lines (174 loc) · 8.22 KB
/
find.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
{CompositeDisposable, Disposable, TextBuffer} = require 'atom'
SelectNext = require './select-next'
{History, HistoryCycler} = require './history'
FindOptions = require './find-options'
BufferSearch = require './buffer-search'
getIconServices = require './get-icon-services'
FindView = require './find-view'
ProjectFindView = require './project-find-view'
ResultsModel = require './project/results-model'
ResultsPaneView = require './project/results-pane'
module.exports =
activate: ({findOptions, findHistory, replaceHistory, pathsHistory}={}) ->
# Convert old config setting for backward compatibility.
if atom.config.get('find-and-replace.openProjectFindResultsInRightPane')
atom.config.set('find-and-replace.projectSearchResultsPaneSplitDirection', 'right')
atom.config.unset('find-and-replace.openProjectFindResultsInRightPane')
atom.workspace.addOpener (filePath) ->
new ResultsPaneView() if filePath is ResultsPaneView.URI
@subscriptions = new CompositeDisposable
@findHistory = new History(findHistory)
@replaceHistory = new History(replaceHistory)
@pathsHistory = new History(pathsHistory)
@findOptions = new FindOptions(findOptions)
@findModel = new BufferSearch(@findOptions)
@resultsModel = new ResultsModel(@findOptions)
@subscriptions.add atom.workspace.getCenter().observeActivePaneItem (paneItem) =>
if paneItem?.getBuffer?()
@findModel.setEditor(paneItem)
else
@findModel.setEditor(null)
@subscriptions.add atom.commands.add '.find-and-replace, .project-find', 'window:focus-next-pane', ->
atom.views.getView(atom.workspace).focus()
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:show', =>
@createViews()
showPanel @projectFindPanel, @findPanel, => @projectFindView.focusFindElement()
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:toggle', =>
@createViews()
togglePanel @projectFindPanel, @findPanel, => @projectFindView.focusFindElement()
@subscriptions.add atom.commands.add 'atom-workspace', 'project-find:show-in-current-directory', ({target}) =>
@createViews()
@findPanel.hide()
@projectFindPanel.show()
@projectFindView.focusFindElement()
@projectFindView.findInCurrentlySelectedDirectory(target)
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:use-selection-as-find-pattern', =>
return if @projectFindPanel?.isVisible() or @findPanel?.isVisible()
@createViews()
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:toggle', =>
@createViews()
togglePanel @findPanel, @projectFindPanel, => @findView.focusFindEditor()
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:show', =>
@createViews()
showPanel @findPanel, @projectFindPanel, => @findView.focusFindEditor()
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:show-replace', =>
@createViews()
showPanel @findPanel, @projectFindPanel, => @findView.focusReplaceEditor()
@subscriptions.add atom.commands.add 'atom-workspace', 'find-and-replace:clear-history', =>
@findHistory.clear()
@replaceHistory.clear()
# Handling cancel in the workspace + code editors
handleEditorCancel = ({target}) =>
isMiniEditor = target.tagName is 'ATOM-TEXT-EDITOR' and target.hasAttribute('mini')
unless isMiniEditor
@findPanel?.hide()
@projectFindPanel?.hide()
@subscriptions.add atom.commands.add 'atom-workspace',
'core:cancel': handleEditorCancel
'core:close': handleEditorCancel
selectNextObjectForEditorElement = (editorElement) =>
@selectNextObjects ?= new WeakMap()
editor = editorElement.getModel()
selectNext = @selectNextObjects.get(editor)
unless selectNext?
selectNext = new SelectNext(editor)
@selectNextObjects.set(editor, selectNext)
selectNext
showPanel = (panelToShow, panelToHide, postShowAction) ->
panelToHide.hide()
panelToShow.show()
postShowAction?()
togglePanel = (panelToToggle, panelToHide, postToggleAction) ->
panelToHide.hide()
if panelToToggle.isVisible()
panelToToggle.hide()
else
panelToToggle.show()
postToggleAction?()
@subscriptions.add atom.commands.add '.editor:not(.mini)',
'find-and-replace:select-next': (event) ->
selectNextObjectForEditorElement(this).findAndSelectNext()
'find-and-replace:select-all': (event) ->
selectNextObjectForEditorElement(this).findAndSelectAll()
'find-and-replace:select-undo': (event) ->
selectNextObjectForEditorElement(this).undoLastSelection()
'find-and-replace:select-skip': (event) ->
selectNextObjectForEditorElement(this).skipCurrentSelection()
consumeElementIcons: (service) ->
getIconServices().setElementIcons service
new Disposable ->
getIconServices().resetElementIcons()
consumeFileIcons: (service) ->
getIconServices().setFileIcons service
new Disposable ->
getIconServices().resetFileIcons()
toggleAutocompletions: (value) ->
if not @findView?
return
if value
@autocompleteSubscriptions = new CompositeDisposable
disposable = @autocompleteWatchEditor?(@findView.findEditor, ['default'])
if disposable?
@autocompleteSubscriptions.add(disposable)
else
@autocompleteSubscriptions?.dispose()
consumeAutocompleteWatchEditor: (watchEditor) ->
@autocompleteWatchEditor = watchEditor
atom.config.observe(
'find-and-replace.autocompleteSearches',
(value) => @toggleAutocompletions(value))
new Disposable =>
@autocompleteSubscriptions?.dispose()
@autocompleteWatchEditor = null
provideService: ->
resultsMarkerLayerForTextEditor: @findModel.resultsMarkerLayerForTextEditor.bind(@findModel)
createViews: ->
return if @findView?
findBuffer = new TextBuffer
replaceBuffer = new TextBuffer
pathsBuffer = new TextBuffer
findHistoryCycler = new HistoryCycler(findBuffer, @findHistory)
replaceHistoryCycler = new HistoryCycler(replaceBuffer, @replaceHistory)
pathsHistoryCycler = new HistoryCycler(pathsBuffer, @pathsHistory)
options = {findBuffer, replaceBuffer, pathsBuffer, findHistoryCycler, replaceHistoryCycler, pathsHistoryCycler}
@findView = new FindView(@findModel, options)
@projectFindView = new ProjectFindView(@resultsModel, options)
@findPanel = atom.workspace.addBottomPanel(item: @findView, visible: false, className: 'tool-panel panel-bottom')
@projectFindPanel = atom.workspace.addBottomPanel(item: @projectFindView, visible: false, className: 'tool-panel panel-bottom')
@findView.setPanel(@findPanel)
@projectFindView.setPanel(@projectFindPanel)
# HACK: Soooo, we need to get the model to the pane view whenever it is
# created. Creation could come from the opener below, or, more problematic,
# from a deserialize call when splitting panes. For now, all pane views will
# use this same model. This needs to be improved! I dont know the best way
# to deal with this:
# 1. How should serialization work in the case of a shared model.
# 2. Or maybe we create the model each time a new pane is created? Then
# ProjectFindView needs to know about each model so it can invoke a search.
# And on each new model, it will run the search again.
#
# See https://github.com/atom/find-and-replace/issues/63
ResultsPaneView.model = @resultsModel
@toggleAutocompletions atom.config.get('find-and-replace.autocompleteSearches')
deactivate: ->
@findPanel?.destroy()
@findPanel = null
@findView?.destroy()
@findView = null
@findModel?.destroy()
@findModel = null
@projectFindPanel?.destroy()
@projectFindPanel = null
@projectFindView?.destroy()
@projectFindView = null
ResultsPaneView.model = null
@resultsModel = null
@autocompleteSubscriptions?.dispose()
@autocompleteManagerService = null
@subscriptions?.dispose()
@subscriptions = null
serialize: ->
findOptions: @findOptions.serialize()
findHistory: @findHistory.serialize()
replaceHistory: @replaceHistory.serialize()
pathsHistory: @pathsHistory.serialize()