@@ -12,18 +12,9 @@ define(function (require, exports, module) {
12
12
// Load brackets modules
13
13
var Async = brackets . getModule ( "utils/Async" ) ,
14
14
DocumentManager = brackets . getModule ( "document/DocumentManager" ) ,
15
- ChangedDocumentTracker = brackets . getModule ( "document/ChangedDocumentTracker" ) ,
16
15
FileUtils = brackets . getModule ( "file/FileUtils" ) ,
17
- NativeFileSystem = brackets . getModule ( "file/NativeFileSystem" ) . NativeFileSystem ,
18
16
CollectionUtils = brackets . getModule ( "utils/CollectionUtils" ) ,
19
- PerfUtils = brackets . getModule ( "utils/PerfUtils" ) ,
20
17
StringUtils = brackets . getModule ( "utils/StringUtils" ) ;
21
-
22
- /**
23
- * Tracks dirty documents between invocations of findMatches.
24
- * @type {ChangedDocumentTracker }
25
- */
26
- var _changedDocumentTracker = new ChangedDocumentTracker ( ) ;
27
18
28
19
/**
29
20
* Function matching regular expression. Recognizes the forms:
@@ -48,8 +39,6 @@ define(function (require, exports, module) {
48
39
functionName ,
49
40
match ;
50
41
51
- PerfUtils . markStart ( PerfUtils . JSUTILS_REGEXP ) ;
52
-
53
42
while ( ( match = _functionRegExp . exec ( text ) ) !== null ) {
54
43
functionName = match [ 1 ] . trim ( ) ;
55
44
@@ -60,8 +49,6 @@ define(function (require, exports, module) {
60
49
results [ functionName ] . push ( { offsetStart : match . index } ) ;
61
50
}
62
51
63
- PerfUtils . addMeasurement ( PerfUtils . JSUTILS_REGEXP ) ;
64
-
65
52
return results ;
66
53
}
67
54
@@ -149,13 +136,9 @@ define(function (require, exports, module) {
149
136
150
137
functions . forEach ( function ( funcEntry ) {
151
138
if ( ! funcEntry . offsetEnd ) {
152
- PerfUtils . markStart ( PerfUtils . JSUTILS_END_OFFSET ) ;
153
-
154
139
funcEntry . offsetEnd = _getFunctionEndOffset ( text , funcEntry . offsetStart ) ;
155
140
funcEntry . lineStart = StringUtils . offsetToLineNum ( lines , funcEntry . offsetStart ) ;
156
141
funcEntry . lineEnd = StringUtils . offsetToLineNum ( lines , funcEntry . offsetEnd ) ;
157
-
158
- PerfUtils . addMeasurement ( PerfUtils . JSUTILS_END_OFFSET ) ;
159
142
}
160
143
161
144
rangeResults . push ( {
@@ -173,8 +156,8 @@ define(function (require, exports, module) {
173
156
* @param {!FileInfo } fileInfo File to parse
174
157
* @param {!$.Deferred } result Deferred to resolve with all functions found and the document
175
158
*/
176
- function _readFile ( fileInfo , result ) {
177
- DocumentManager . getDocumentForPath ( fileInfo . fullPath )
159
+ function _readFile ( fileInfo ) {
160
+ return DocumentManager . getDocumentForPath ( fileInfo . fullPath )
178
161
. done ( function ( doc ) {
179
162
var allFunctions = _findAllFunctionsInText ( doc . getText ( ) ) ;
180
163
@@ -183,50 +166,10 @@ define(function (require, exports, module) {
183
166
fileInfo . JSUtils [ _functionRegExp ] = allFunctions ;
184
167
fileInfo . JSUtils . timestamp = doc . diskTimestamp ;
185
168
186
- result . resolve ( { doc : doc , functions : allFunctions } ) ;
187
- } )
188
- . fail ( function ( error ) {
189
- result . reject ( error ) ;
169
+ return { doc : doc , functions : allFunctions } ;
190
170
} ) ;
191
171
}
192
172
193
- /**
194
- * Determines if the document function cache is up to date.
195
- * @param {FileInfo } fileInfo
196
- * @return {$.Promise } A promise resolved with true with true when a function cache is available for the document. Resolves
197
- * with false when there is no cache or the cache is stale.
198
- */
199
- function _shouldGetFromCache ( fileInfo ) {
200
- var result = new $ . Deferred ( ) ,
201
- isChanged = _changedDocumentTracker . isPathChanged ( fileInfo . fullPath ) ;
202
-
203
- if ( isChanged && fileInfo . JSUtils && fileInfo . JSUtils [ _functionRegExp ] ) {
204
- // See if it's dirty and in the working set first
205
- var doc = DocumentManager . getOpenDocumentForPath ( fileInfo . fullPath ) ;
206
-
207
- if ( doc && doc . isDirty ) {
208
- result . resolve ( false ) ;
209
- } else {
210
- // If a cache exists, check the timestamp on disk
211
- var file = new NativeFileSystem . FileEntry ( fileInfo . fullPath ) ;
212
-
213
- file . getMetadata (
214
- function ( metadata ) {
215
- result . resolve ( fileInfo . JSUtils . timestamp === metadata . diskTimestamp ) ;
216
- } ,
217
- function ( error ) {
218
- result . reject ( error ) ;
219
- }
220
- ) ;
221
- }
222
- } else {
223
- // Use the cache if the file did not change and the cache exists
224
- result . resolve ( ! isChanged && fileInfo . JSUtils && fileInfo . JSUtils [ _functionRegExp ] ) ;
225
- }
226
-
227
- return result . promise ( ) ;
228
- }
229
-
230
173
/**
231
174
* @private
232
175
* Compute lineStart and lineEnd for each matched function
@@ -277,34 +220,6 @@ define(function (require, exports, module) {
277
220
return result . promise ( ) ;
278
221
}
279
222
280
- /**
281
- * Resolves with a record containing the Document or FileInfo and an Array of all
282
- * function names with offsets for the specified file. Results may be cached.
283
- * @param {FileInfo } fileInfo
284
- * @return {$.Promise } A promise resolved with a document info object that
285
- * contains a map of all function names from the document and each function's start offset.
286
- */
287
- function _getFunctionsForFile ( fileInfo ) {
288
- var result = new $ . Deferred ( ) ;
289
-
290
- _shouldGetFromCache ( fileInfo )
291
- . done ( function ( useCache ) {
292
- if ( useCache ) {
293
- // Return cached data. doc property is undefined since we hit the cache.
294
- // _getOffsets() will fetch the Document if necessary.
295
- var data = { /*doc: undefined,*/ fileInfo : fileInfo } ;
296
- data [ _functionRegExp ] = fileInfo . JSUtils [ _functionRegExp ] ;
297
- result . resolve ( data ) ;
298
- } else {
299
- _readFile ( fileInfo , result ) ;
300
- }
301
- } ) . fail ( function ( err ) {
302
- result . reject ( err ) ;
303
- } ) ;
304
-
305
- return result . promise ( ) ;
306
- }
307
-
308
223
/**
309
224
* @private
310
225
* Get all functions for each FileInfo.
@@ -313,33 +228,17 @@ define(function (require, exports, module) {
313
228
* contain a map of all function names from the document and each function's start offset.
314
229
*/
315
230
function _getFunctionsInFiles ( fileInfos ) {
316
- var result = new $ . Deferred ( ) ,
317
- docEntries = [ ] ;
231
+ var docEntries = [ ] ;
318
232
319
- PerfUtils . markStart ( PerfUtils . JSUTILS_GET_ALL_FUNCTIONS ) ;
320
-
321
- Async . doInParallel ( fileInfos , function ( fileInfo ) {
322
- var oneResult = new $ . Deferred ( ) ;
323
-
324
- _getFunctionsForFile ( fileInfo )
325
- . done ( function ( docInfo ) {
233
+ return Async . doInParallel ( fileInfos , function ( fileInfo ) {
234
+ return _readFile ( fileInfo )
235
+ . then ( function ( docInfo ) {
326
236
docEntries . push ( docInfo ) ;
327
- } )
328
- . always ( function ( error ) {
237
+ } , function ( error ) {
329
238
// If one file fails, continue to search
330
239
oneResult . resolve ( ) ;
331
240
} ) ;
332
-
333
- return oneResult . promise ( ) ;
334
- } ) . always ( function ( ) {
335
- // Reset ChangedDocumentTracker now that the cache is up to date.
336
- _changedDocumentTracker . reset ( ) ;
337
-
338
- PerfUtils . addMeasurement ( PerfUtils . JSUTILS_GET_ALL_FUNCTIONS ) ;
339
- result . resolve ( docEntries ) ;
340
- } ) ;
341
-
342
- return result . promise ( ) ;
241
+ } ) . promise ( ) ;
343
242
}
344
243
345
244
/**
@@ -354,8 +253,7 @@ define(function (require, exports, module) {
354
253
*/
355
254
function findMatches ( pattern , functionName , fileInfos , keepAllFiles ) {
356
255
_functionRegExp = pattern ;
357
- var result = new $ . Deferred ( ) ,
358
- jsFiles = [ ] ,
256
+ var jsFiles = [ ] ,
359
257
docEntries = [ ] ;
360
258
361
259
if ( ! keepAllFiles ) {
@@ -367,15 +265,11 @@ define(function (require, exports, module) {
367
265
jsFiles = fileInfos ;
368
266
}
369
267
370
- // RegExp search (or cache lookup) for all functions in the project
371
- _getFunctionsInFiles ( jsFiles ) . done ( function ( docEntries ) {
268
+ // RegExp search for all functions in the project
269
+ return _getFunctionsInFiles ( jsFiles ) . then ( function ( docEntries ) {
372
270
// Compute offsets for all matched functions
373
- _getOffsetsForFunction ( docEntries , functionName ) . done ( function ( rangeResults ) {
374
- result . resolve ( rangeResults ) ;
375
- } ) ;
271
+ return _getOffsetsForFunction ( docEntries , functionName ) ;
376
272
} ) ;
377
-
378
- return result . promise ( ) ;
379
273
}
380
274
381
275
/**
@@ -407,12 +301,7 @@ define(function (require, exports, module) {
407
301
408
302
return result ;
409
303
}
410
-
411
- PerfUtils . createPerfMeasurement ( "JSUTILS_GET_ALL_FUNCTIONS" , "Parallel file search across project" ) ;
412
- PerfUtils . createPerfMeasurement ( "JSUTILS_REGEXP" , "RegExp search for all functions" ) ;
413
- PerfUtils . createPerfMeasurement ( "JSUTILS_END_OFFSET" , "Find end offset for a single matched function" ) ;
414
304
415
305
exports . findAllMatchingDirectivesInText = findAllMatchingDirectivesInText ;
416
- exports . _getFunctionEndOffset = _getFunctionEndOffset ; // For testing only
417
306
exports . findMatches = findMatches ;
418
307
} ) ;
0 commit comments