This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathfuzzy-finder-view.coffee
271 lines (217 loc) · 8.83 KB
/
fuzzy-finder-view.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
path = require 'path'
{Point, CompositeDisposable} = require 'atom'
{$, $$, SelectListView} = require 'atom-space-pen-views'
{repositoryForPath} = require './helpers'
fs = require 'fs-plus'
fuzzaldrin = require 'fuzzaldrin'
fuzzaldrinPlus = require 'fuzzaldrin-plus'
FileIcons = require './file-icons'
module.exports =
class FuzzyFinderView extends SelectListView
filePaths: null
projectRelativePaths: null
subscriptions: null
alternateScoring: false
initialize: ->
super
@addClass('fuzzy-finder')
@setMaxItems(10)
@subscriptions = new CompositeDisposable
splitLeft = => @splitOpenPath (pane) -> pane.splitLeft.bind(pane)
splitRight = => @splitOpenPath (pane) -> pane.splitRight.bind(pane)
splitUp = => @splitOpenPath (pane) -> pane.splitUp.bind(pane)
splitDown = => @splitOpenPath (pane) -> pane.splitDown.bind(pane)
atom.commands.add @element,
'pane:split-left': splitLeft
'pane:split-left-and-copy-active-item': splitLeft
'pane:split-left-and-move-active-item': splitLeft
'pane:split-right': splitRight
'pane:split-right-and-copy-active-item': splitRight
'pane:split-right-and-move-active-item': splitRight
'pane:split-up': splitUp
'pane:split-up-and-copy-active-item': splitUp
'pane:split-up-and-move-active-item': splitUp
'pane:split-down': splitDown
'pane:split-down-and-copy-active-item': splitDown
'pane:split-down-and-move-active-item': splitDown
'fuzzy-finder:invert-confirm': =>
@confirmInvertedSelection()
@alternateScoring = atom.config.get 'fuzzy-finder.useAlternateScoring'
@subscriptions.add atom.config.onDidChange 'fuzzy-finder.useAlternateScoring', ({newValue}) => @alternateScoring = newValue
getFilterKey: ->
'projectRelativePath'
cancel: ->
if atom.config.get('fuzzy-finder.preserveLastSearch')
lastSearch = @getFilterQuery()
super
@filterEditorView.setText(lastSearch)
@filterEditorView.getModel().selectAll()
else
super
destroy: ->
@cancel()
@panel?.destroy()
@subscriptions?.dispose()
@subscriptions = null
viewForItem: ({filePath, projectRelativePath}) ->
# Style matched characters in search results
filterQuery = @getFilterQuery()
if @alternateScoring
matches = fuzzaldrinPlus.match(projectRelativePath, filterQuery)
else
matches = fuzzaldrin.match(projectRelativePath, filterQuery)
$$ ->
highlighter = (path, matches, offsetIndex) =>
lastIndex = 0
matchedChars = [] # Build up a set of matched chars to be more semantic
for matchIndex in matches
matchIndex -= offsetIndex
continue if matchIndex < 0 # If marking up the basename, omit path matches
unmatched = path.substring(lastIndex, matchIndex)
if unmatched
@span matchedChars.join(''), class: 'character-match' if matchedChars.length
matchedChars = []
@text unmatched
matchedChars.push(path[matchIndex])
lastIndex = matchIndex + 1
@span matchedChars.join(''), class: 'character-match' if matchedChars.length
# Remaining characters are plain text
@text path.substring(lastIndex)
@li class: 'two-lines', =>
if (repo = repositoryForPath(filePath))?
id = encodeURIComponent("fuzzy-finder-#{filePath}")
@div class: 'status', id: id
repo.getCachedPathStatus(filePath).then (status) ->
statusNode = $(document.getElementById(id))
if statusNode? and repo.isStatusNew(status)
statusNode.addClass('status-added icon icon-diff-added')
else if statusNode? and repo.isStatusModified(status)
statusNode.addClass('status-modified icon icon-diff-modified')
typeClass = FileIcons.getService().iconClassForPath(filePath) or []
unless Array.isArray typeClass
typeClass = typeClass?.toString().split(/\s+/g)
fileBasename = path.basename(filePath)
baseOffset = projectRelativePath.length - fileBasename.length
@div class: "primary-line file icon #{typeClass.join(' ')}", 'data-name': fileBasename, 'data-path': projectRelativePath, -> highlighter(fileBasename, matches, baseOffset)
@div class: 'secondary-line path no-icon', -> highlighter(projectRelativePath, matches, 0)
openPath: (filePath, lineNumber, openOptions) ->
if filePath
atom.workspace.open(filePath, openOptions).then => @moveToLine(lineNumber)
moveToLine: (lineNumber=-1) ->
return unless lineNumber >= 0
if textEditor = atom.workspace.getActiveTextEditor()
position = new Point(lineNumber)
textEditor.scrollToBufferPosition(position, center: true)
textEditor.setCursorBufferPosition(position)
textEditor.moveToFirstCharacterOfLine()
splitOpenPath: (splitFn) ->
{filePath} = @getSelectedItem() ? {}
lineNumber = @getLineNumber()
if @isQueryALineJump() and editor = atom.workspace.getActiveTextEditor()
pane = atom.workspace.getActivePane()
splitFn(pane)(copyActiveItem: true)
@moveToLine(lineNumber)
else if not filePath
return
else if pane = atom.workspace.getActivePane()
splitFn(pane)()
@openPath(filePath, lineNumber)
else
@openPath(filePath, lineNumber)
populateList: ->
if @isQueryALineJump()
@list.empty()
@setError('Jump to line in active editor')
else if @alternateScoring
@populateAlternateList()
else
super
# Unfortunately SelectListView do not allow inheritor to handle their own filtering.
# That would be required to use external knowledge, for example: give a bonus to recent files.
#
# Or, in this case: test an alternate scoring algorithm.
#
# This is modified copy/paste from SelectListView#populateList, require jQuery!
# Should be temporary
populateAlternateList: ->
return unless @items?
filterQuery = @getFilterQuery()
if filterQuery.length
filteredItems = fuzzaldrinPlus.filter(@items, filterQuery, key: @getFilterKey())
else
filteredItems = @items
normalizedQuery = fs.normalize(filterQuery)
if fs.isFileSync(normalizedQuery)
filteredItems.push(filePath: normalizedQuery, projectRelativePath: normalizedQuery)
@list.empty()
if filteredItems.length
@setError(null)
for i in [0...Math.min(filteredItems.length, @maxItems)]
item = filteredItems[i]
itemView = $(@viewForItem(item))
itemView.data('select-list-item', item)
@list.append(itemView)
@selectItemView(@list.find('li:first'))
else
@setError(@getEmptyMessage(@items.length, filteredItems.length))
confirmSelection: ->
item = @getSelectedItem()
@confirmed(item, searchAllPanes: atom.config.get('fuzzy-finder.searchAllPanes'))
confirmInvertedSelection: ->
item = @getSelectedItem()
@confirmed(item, searchAllPanes: not atom.config.get('fuzzy-finder.searchAllPanes'))
confirmed: ({filePath}={}, openOptions) ->
if atom.workspace.getActiveTextEditor() and @isQueryALineJump()
lineNumber = @getLineNumber()
@cancel()
@moveToLine(lineNumber)
else if not filePath
@cancel()
else if fs.isDirectorySync(filePath)
@setError('Selected path is a directory')
setTimeout((=> @setError()), 2000)
else
lineNumber = @getLineNumber()
@cancel()
@openPath(filePath, lineNumber, openOptions)
isQueryALineJump: ->
query = @filterEditorView.getModel().getText()
colon = query.indexOf(':')
trimmedPath = @getFilterQuery().trim()
trimmedPath is '' and colon isnt -1
getFilterQuery: ->
query = super
colon = query.indexOf(':')
query = query[0...colon] if colon isnt -1
# Normalize to backslashes on Windows
query = query.replace(/\//g, '\\') if process.platform is 'win32'
query
getLineNumber: ->
query = @filterEditorView.getText()
colon = query.indexOf(':')
if colon is -1
-1
else
parseInt(query[colon+1..]) - 1
setItems: (filePaths) ->
super(@projectRelativePathsForFilePaths(filePaths))
projectRelativePathsForFilePaths: (filePaths) ->
# Don't regenerate project relative paths unless the file paths have changed
if filePaths isnt @filePaths
projectHasMultipleDirectories = atom.project.getDirectories().length > 1
@filePaths = filePaths
@projectRelativePaths = @filePaths.map (filePath) ->
[rootPath, projectRelativePath] = atom.project.relativizePath(filePath)
if rootPath and projectHasMultipleDirectories
projectRelativePath = path.join(path.basename(rootPath), projectRelativePath)
{filePath, projectRelativePath}
@projectRelativePaths
show: ->
@storeFocusedElement()
@panel ?= atom.workspace.addModalPanel(item: this)
@panel.show()
@focusFilterEditor()
hide: ->
@panel?.hide()
cancelled: ->
@hide()