Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit 603b324

Browse files
author
Dean Sofer
committed
Assorted code cleanup
1 parent 4a454a1 commit 603b324

File tree

2 files changed

+14
-125
lines changed

2 files changed

+14
-125
lines changed

‎NGUtils.js

Lines changed: 13 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,9 @@ define(function (require, exports, module) {
1212
// Load brackets modules
1313
var Async = brackets.getModule("utils/Async"),
1414
DocumentManager = brackets.getModule("document/DocumentManager"),
15-
ChangedDocumentTracker = brackets.getModule("document/ChangedDocumentTracker"),
1615
FileUtils = brackets.getModule("file/FileUtils"),
17-
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem,
1816
CollectionUtils = brackets.getModule("utils/CollectionUtils"),
19-
PerfUtils = brackets.getModule("utils/PerfUtils"),
2017
StringUtils = brackets.getModule("utils/StringUtils");
21-
22-
/**
23-
* Tracks dirty documents between invocations of findMatches.
24-
* @type {ChangedDocumentTracker}
25-
*/
26-
var _changedDocumentTracker = new ChangedDocumentTracker();
2718

2819
/**
2920
* Function matching regular expression. Recognizes the forms:
@@ -48,8 +39,6 @@ define(function (require, exports, module) {
4839
functionName,
4940
match;
5041

51-
PerfUtils.markStart(PerfUtils.JSUTILS_REGEXP);
52-
5342
while ((match = _functionRegExp.exec(text)) !== null) {
5443
functionName = match[1].trim();
5544

@@ -60,8 +49,6 @@ define(function (require, exports, module) {
6049
results[functionName].push({offsetStart: match.index});
6150
}
6251

63-
PerfUtils.addMeasurement(PerfUtils.JSUTILS_REGEXP);
64-
6552
return results;
6653
}
6754

@@ -149,13 +136,9 @@ define(function (require, exports, module) {
149136

150137
functions.forEach(function (funcEntry) {
151138
if (!funcEntry.offsetEnd) {
152-
PerfUtils.markStart(PerfUtils.JSUTILS_END_OFFSET);
153-
154139
funcEntry.offsetEnd = _getFunctionEndOffset(text, funcEntry.offsetStart);
155140
funcEntry.lineStart = StringUtils.offsetToLineNum(lines, funcEntry.offsetStart);
156141
funcEntry.lineEnd = StringUtils.offsetToLineNum(lines, funcEntry.offsetEnd);
157-
158-
PerfUtils.addMeasurement(PerfUtils.JSUTILS_END_OFFSET);
159142
}
160143

161144
rangeResults.push({
@@ -173,8 +156,8 @@ define(function (require, exports, module) {
173156
* @param {!FileInfo} fileInfo File to parse
174157
* @param {!$.Deferred} result Deferred to resolve with all functions found and the document
175158
*/
176-
function _readFile(fileInfo,result) {
177-
DocumentManager.getDocumentForPath(fileInfo.fullPath)
159+
function _readFile(fileInfo) {
160+
returnDocumentManager.getDocumentForPath(fileInfo.fullPath)
178161
.done(function (doc) {
179162
var allFunctions = _findAllFunctionsInText(doc.getText());
180163

@@ -183,50 +166,10 @@ define(function (require, exports, module) {
183166
fileInfo.JSUtils[_functionRegExp] = allFunctions;
184167
fileInfo.JSUtils.timestamp = doc.diskTimestamp;
185168

186-
result.resolve({doc: doc, functions: allFunctions});
187-
})
188-
.fail(function (error) {
189-
result.reject(error);
169+
return {doc: doc, functions: allFunctions};
190170
});
191171
}
192172

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-
230173
/**
231174
* @private
232175
* Compute lineStart and lineEnd for each matched function
@@ -277,34 +220,6 @@ define(function (require, exports, module) {
277220
return result.promise();
278221
}
279222

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-
308223
/**
309224
* @private
310225
* Get all functions for each FileInfo.
@@ -313,33 +228,17 @@ define(function (require, exports, module) {
313228
* contain a map of all function names from the document and each function's start offset.
314229
*/
315230
function _getFunctionsInFiles(fileInfos) {
316-
var result = new $.Deferred(),
317-
docEntries = [];
231+
var docEntries = [];
318232

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) {
326236
docEntries.push(docInfo);
327-
})
328-
.always(function (error) {
237+
}, function (error) {
329238
// If one file fails, continue to search
330239
oneResult.resolve();
331240
});
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();
343242
}
344243

345244
/**
@@ -354,8 +253,7 @@ define(function (require, exports, module) {
354253
*/
355254
function findMatches(pattern, functionName, fileInfos, keepAllFiles) {
356255
_functionRegExp = pattern;
357-
var result = new $.Deferred(),
358-
jsFiles = [],
256+
var jsFiles = [],
359257
docEntries = [];
360258

361259
if (!keepAllFiles) {
@@ -367,15 +265,11 @@ define(function (require, exports, module) {
367265
jsFiles = fileInfos;
368266
}
369267

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) {
372270
// Compute offsets for all matched functions
373-
_getOffsetsForFunction(docEntries, functionName).done(function (rangeResults) {
374-
result.resolve(rangeResults);
375-
});
271+
return _getOffsetsForFunction(docEntries, functionName);
376272
});
377-
378-
return result.promise();
379273
}
380274

381275
/**
@@ -407,12 +301,7 @@ define(function (require, exports, module) {
407301

408302
return result;
409303
}
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");
414304

415305
exports.findAllMatchingDirectivesInText = findAllMatchingDirectivesInText;
416-
exports._getFunctionEndOffset = _getFunctionEndOffset; // For testing only
417306
exports.findMatches = findMatches;
418307
});

‎main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ define(function (require, exports, module) {
7373
*/
7474
function _findInProject(directiveName, pattern) {
7575
return ProjectManager.getAllFiles()
76-
.done(function (files) {
76+
.then(function (files) {
7777
return NGUtils.findMatches(pattern, directiveName, files, true);
7878
});
7979
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /