-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFuzzyRecord.rb
executable file
·427 lines (367 loc) · 13.2 KB
/
FuzzyRecord.rb
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# FuzzyRecord.rb
# FuzzyWindow
#
# Created by Geoffrey Grosenbach on 3/16/10.
# Copyright 2010 Topfunky Corporation. All rights reserved.
require 'Constants'
include Constants
class FuzzyRecord
attr_accessor *[:projectRoot, :filePath,
:scmStatus, :scmName,
:codeObjectName, :codeObjectNames,
:matchedRanges, :matchesOnFilenameScore, :longestMatch]
MAX_SCORE = 10_000
def self.discoverProjectRootForDirectoryOrFile(directoryOrFile)
normalizedPath = directoryOrFile.gsub(/[\/']$/, '') # Normalize: remove trailing slash
# If a directory was passed, assume it's the project root
# and don't do further searching.
if File.directory?(normalizedPath)
NSLog("Using Directory as Project Root: #{normalizedPath}") # DEBUG
return normalizedPath, true
end
projectRoot = ''
projectRootFoundFlag = false
projectRootRegex = Regexp.new(NSUserDefaults.standardUserDefaults.stringForKey("projectRootRegex"))
fileManager = NSFileManager.defaultManager
pathComponents = normalizedPath.pathComponents
NSLog("Finding Project Root with Path Components: #{pathComponents.inspect}") # DEBUG
(pathComponents.length - 1).downto(0) do |index|
path = NSString.pathWithComponents(pathComponents[0..index])
next if File.file?(path)
directoryContents = fileManager.contentsOfDirectoryAtPath(path, error:nil)
if directoryContents
directoryContents.map {|f|
if f.match(projectRootRegex)
projectRoot, projectRootFoundFlag = path.to_s, true
break
end
}
end
end
if projectRoot.empty?
projectRoot = File.directory?(normalizedPath) ? normalizedPath : File.dirname(normalizedPath)
end
NSLog("Using Project Root: #{projectRoot}") if projectRootFoundFlag # DEBUG
return projectRoot, projectRootFoundFlag
end
# Cache is a dictionary of project roots with arrays of recent files
# and records.
#
# {
# "/var/www/project" => {
# :recentlyOpenedRecords => [],
# :records => []
# }
# }
@@cache = {}
def self.recordsForProjectRoot(theProjectRoot, withFuzzyTableViewController:fuzzyTableViewController)
cacheScmStatus(theProjectRoot)
if records = cachedRecordsForProjectRoot(theProjectRoot)
# Get fresh scmStatus every time
records.each { |r| r.scmStatus = nil }
NSNotificationCenter.defaultCenter.postNotificationName(TFAllRecordsCreatedNotification, object:records)
return
end
loadRecordsWithProjectRoot(theProjectRoot, withFuzzyTableViewController:fuzzyTableViewController)
end
def self.cacheForProjectRoot(theProjectRoot)
return nil if @@cache.nil?
@@cache[theProjectRoot]
end
##
# Returns only the cached records for a project, or nil.
def self.cachedRecordsForProjectRoot(theProjectRoot)
if cacheHash = cacheForProjectRoot(theProjectRoot)
if records = cacheHash[:records]
return records
end
end
return nil
end
##
# Store records for faster launch.
def self.setCacheRecords(theRecords, forProjectRoot:theProjectRoot)
@@cache ||= {}
if @@cache[theProjectRoot].nil?
@@cache[theProjectRoot] = {}
end
@@cache[theProjectRoot][:records] = theRecords
end
##
# Add a single record to the existing cache for a project.
def self.addRecord(theRecord, toCacheForProjectRoot:theProjectRoot)
@@cache ||= {}
if @@cache[theProjectRoot].nil?
@@cache[theProjectRoot] = {
:records => []
}
end
@@cache[theProjectRoot][:records] << theRecord
end
def self.flushCache(theProjectRoot)
return if @@cache.nil?
if @@cache[theProjectRoot]
if @@cache[theProjectRoot][:records]
@@cache[theProjectRoot][:records] = nil
@@cache[theProjectRoot][:recentlyOpenedRecords] = nil
# TODO: Remove entries in :recentlyOpenedRecords if missing
end
end
end
##
# Keep last few opened records so the user can toggle between
# recent files.
def self.storeRecentlyOpenedRecord(theRecord)
cacheHash = cacheForProjectRoot(theRecord.projectRoot) || {}
if recentArray = cacheHash[:recentlyOpenedRecords]
if recentArray.include?(theRecord)
# Avoid a situation where one file occupies both slots in the
# recent files list.
if recentArray.first == theRecord
recentArray.reverse!
end
else
recentArray << theRecord
# Keep only two
while recentArray.length > 2
recentArray.shift
end
end
else
cacheHash[:recentlyOpenedRecords] = [theRecord]
end
@@cache[theRecord.projectRoot] = cacheHash
end
def self.cacheScmStatus(theProjectRoot)
@@scmStatusDictionary = {}
return unless NSUserDefaults.standardUserDefaults.boolForKey("scmShowMetadata")
gitDiffAgainst = NSUserDefaults.standardUserDefaults.stringForKey("scmGitDiffAgainst")
gitDiffAgainst = "" if (gitDiffAgainst == "Current")
# TODO: Run async
#
# Run "git diff --numstat" on projectRoot and save for all
# in an NSDictionary
#
# 3 1 Tests/run_suite.rb
# - - Foo/Bar.rb
projectDotGitPath =
NSString.pathWithComponents([theProjectRoot, ".git"])
unless NSFileManager.defaultManager.fileExistsAtPath(projectDotGitPath)
return
end
shellString = NSProcessInfo.processInfo.environment.objectForKey("SHELL") || "/bin/bash"
loginFlag = '-l'
if shellString.match(/tcsh/)
# NOTE: tcsh doesn't support -l unless it's the only option.
loginFlag = ''
end
# TODO: Read Git info asynchronously so it doesn't block the rest of the app.
if output = `#{shellString} #{loginFlag} -c "cd #{theProjectRoot} && git diff --numstat #{gitDiffAgainst}"`
output.split(/\n/).each do |outputLine|
added, deleted, filePath = outputLine.split
added = 30 if added.to_i > 30
deleted = 30 if deleted.to_i > 30
@@scmStatusDictionary[filePath] = [added.to_i, deleted.to_i]
end
end
end
##
# NOTE: NSPredicate
# p = NSPredicate.predicateWithFormat("name = 'john'")
# p.evaluateWithObject({"name" => "bert"}) # => false
def self.loadRecordsWithProjectRoot(theProjectRoot, withFuzzyTableViewController:fuzzyTableViewController)
fuzzyTableViewController.queue.cancelAllOperations
self.setCacheRecords([], forProjectRoot:theProjectRoot)
maximumDocumentCount = NSUserDefaults.standardUserDefaults.integerForKey("maximumDocumentCount")
pathOp = PathOperation.alloc.initWithProjectRoot(theProjectRoot,
maximumDocumentCount:maximumDocumentCount,
andFuzzyTableViewController:fuzzyTableViewController)
pathOp.setQueuePriority(2.0)
fuzzyTableViewController.queue.addOperation(pathOp)
end
def self.filterRecords(records, forString:searchString, whitespaceSearchCharacter:wildcardCharacter)
correctedSearchString = searchString.gsub(" ", wildcardCharacter).strip # Treat spaces as special
if correctedSearchString.length == 0
filteredRecords = records
else
filteredRecords = records.select { |r|
r.fuzzyInclude?(correctedSearchString)
}
end
sortedRecords =
filteredRecords.sort_by { |record| [ -record.matchesOnFilenameScore,
record.matchScore,
-record.longestMatch,
-record.modifiedAt.timeIntervalSince1970,
record.filePath.length ] }
if (correctedSearchString.length == 0) && (records.first != nil)
if cacheHash = self.cacheForProjectRoot(records.first.projectRoot)
if recentlyOpenedRecords = cacheHash[:recentlyOpenedRecords]
if recentlyOpenedRecords.length >= 2
recentRecord = recentlyOpenedRecords.first
sortedRecords.delete(recentRecord)
sortedRecords.unshift(recentlyOpenedRecords.first)
end
end
end
end
return sortedRecords
end
def self.resetMatchesForRecords!(records)
if records
records.map {|r| r.resetMatches! }
end
end
def initWithProjectRoot(theProjectRoot, filePath:theFilePath)
@projectRoot = theProjectRoot
@filePath = theFilePath
@matchedRanges = []
self
end
##
# Finds "a/b/c" in "armadillo/bacon/cheshire"
#
# Returns true if the string matches this record.
#
# Populates @matchedRanges with an Array of NSRange objects
# identifying the matches.
def fuzzyInclude?(searchString)
resetMatches!
# TODO: Search other things like date, classes, or SCM status
# Multiple strategies: filename, first occurrence, most
# contiguous.
filename = File.basename(filePath)
if filenameMatchRanges = searchText(filename,
forFirstOccurrenceOfString:searchString)
@matchesOnFilenameScore = 1
@matchScore = calculateMatchScoreForRanges(filenameMatchRanges)
dirname = File.dirname(filePath)
if dirname == "."
@matchedRanges = filenameMatchRanges
else
@matchedRanges = offsetRanges(filenameMatchRanges, by:dirname.length)
end
return true
elsif firstOccurrenceMatches = searchText(filePath,
forFirstOccurrenceOfString:searchString)
if reverseSearchMatches = searchInReverseForString(searchString)
if calculateMatchScoreForRanges(reverseSearchMatches) <
calculateMatchScoreForRanges(firstOccurrenceMatches)
@matchedRanges = reverseSearchMatches
else
@matchedRanges = firstOccurrenceMatches
end
return true
end
@matchedRanges = firstOccurrenceMatches
return true
end
return nil
end
def searchText(theText, forFirstOccurrenceOfString:searchString)
matchingRanges = []
upcaseText = theText.upcase
upcaseSearchString = searchString.upcase
upcaseSearchString.each_char do |c|
offset = 0
if matchingRanges.length > 0
offset =
matchingRanges.lastObject.location + matchingRanges.lastObject.length
end
if (foundIndex = upcaseText.index(c, offset))
if contiguousMatch?(foundIndex, matchingRanges)
lastRange = matchingRanges.lastObject
lastObjectIndex = matchingRanges.length - 1
matchingRanges[lastObjectIndex] = NSMakeRange(lastRange.location,
lastRange.length + 1)
else
matchingRanges << NSMakeRange(foundIndex, 1)
end
else
# No match or partial match
return nil
end
end
if matchingRanges.length > 0
return matchingRanges
end
nil
end
def searchInReverseForString(searchString)
reverseMatches = searchText(filePath.reverse,
forFirstOccurrenceOfString:searchString.reverse)
if reverseMatches.length > 0
forwardMatches = reverseMatches.reverse.map { |range|
location = filePath.length - range.location - range.length
NSMakeRange(location, range.length)
}
return forwardMatches
end
nil
end
def resetMatches!
@matchesOnFilenameScore = 0
@matchedRanges = nil
@matchScore = nil
@longestMatch = nil
end
def matchScore
return MAX_SCORE if @matchedRanges.nil? || @matchedRanges.length == 0
# TODO: Files with locations close together should rank higher
# than ones with locations farther apart.
# FuzzyRecord_test.rb
# FuzzyRecord.rb
# Search => fuzr.rb
@matchScore ||= calculateMatchScoreForRanges(@matchedRanges)
end
def calculateMatchScoreForRanges(ranges)
ranges.inject(0) {|memo, element|
memo + element.location }
end
def longestMatch
return 0 if @matchedRanges.nil? || @matchedRanges.length == 0
@longestMatch ||= calculateLongestMatch(@matchedRanges)
end
def calculateLongestMatch(ranges)
longest = 0
ranges.each do |range|
if range.length > longest
longest = range.length
end
end
return longest
end
def modifiedAt
@modifiedAt ||= begin
NSFileManager.defaultManager.
attributesOfItemAtPath(absFilePath,
error:nil)[NSFileModificationDate]
end
end
def scmStatus
return @scmStatus if @scmStatus
if statusCounts = @@scmStatusDictionary.objectForKey(filePath)
linesAdded, linesDeleted = statusCounts
@scmStatus = ("+" * linesAdded) + ("-" * linesDeleted)
end
return @scmStatus
end
def absFilePath
File.join(projectRoot, filePath)
end
def contiguousMatch?(foundIndex, matchingRanges)
return false if (matchingRanges.length == 0)
lastObject = matchingRanges.lastObject
return (lastObject.location + lastObject.length == foundIndex)
end
##
# Make a new array of ranges, moved forward by a number of
# characters.
#
# Used for switching filename-only match to a full path match.
def offsetRanges(theRanges, by:theOffset)
theRanges.map do |range|
NSMakeRange(range.location + theOffset + 1, range.length)
end
end
end