From 0fd2e149dd70bc7ea63f494a6cd641ea8b20d16b Mon Sep 17 00:00:00 2001 From: Glenn Ruehle Date: Wed, 6 Nov 2013 16:32:15 -0800 Subject: [PATCH] Respond to more review comments. --- src/document/DocumentManager.js | 86 ++++++++++--------- src/document/InMemoryFile.js | 2 +- src/editor/ImageViewer.js | 2 +- src/extensions/default/DebugCommands/main.js | 4 +- .../default/JavaScriptCodeHints/unittests.js | 10 +-- .../default/JavaScriptQuickEdit/main.js | 26 +++--- src/extensions/default/RecentProjects/main.js | 8 +- src/language/CSSUtils.js | 4 +- src/project/ProjectManager.js | 17 ++-- src/search/FindInFiles.js | 7 ++ src/search/QuickOpen.js | 1 - src/utils/ExtensionLoader.js | 36 +++----- test/spec/CSSUtils-test.js | 2 +- test/spec/LiveDevelopment-test.js | 24 +++--- test/spec/ProjectManager-test.js | 2 +- test/spec/SpecRunnerUtils.js | 29 ++----- 16 files changed, 125 insertions(+), 135 deletions(-) diff --git a/src/document/DocumentManager.js b/src/document/DocumentManager.js index 4b50d1e73..f3afc93f9 100644 --- a/src/document/DocumentManager.js +++ b/src/document/DocumentManager.js @@ -630,8 +630,8 @@ define(function (require, exports, module) { var id; // Need to walk all open documents and check for matching path. We can't - // use getFileForPath(fullPath).id since that won't match untitled - // documents... + // use getFileForPath(fullPath).id since the file it returns won't match + // an Untitled document's InMemoryFile. for (id in _openDocuments) { if (_openDocuments.hasOwnProperty(id)) { if (_openDocuments[id].file.fullPath === fullPath) { @@ -658,56 +658,60 @@ define(function (require, exports, module) { * with a FileSystemError if the file is not yet open and can't be read from disk. */ function getDocumentForPath(fullPath) { - var file = FileSystem.getFileForPath(fullPath), - doc = getOpenDocumentForPath(fullPath), - pendingPromise = getDocumentForPath._pendingDocumentPromises[file.id]; + var doc = getOpenDocumentForPath(fullPath); if (doc) { // use existing document return new $.Deferred().resolve(doc).promise(); - } else if (pendingPromise) { - // wait for the result of a previous request - return pendingPromise; } else { - var result = new $.Deferred(), - promise = result.promise(); + // Should never get here if the fullPath refers to an Untitled document if (fullPath.indexOf(_untitledDocumentPath) === 0) { console.error("getDocumentForPath called for non-open untitled document: " + fullPath); - result.reject(); - return promise; + return new $.Deferred().reject().promise(); } - // log this document's Promise as pending - getDocumentForPath._pendingDocumentPromises[file.id] = promise; - - // create a new document - var perfTimerName = PerfUtils.markStart("getDocumentForPath:\t" + fullPath); - - result.done(function () { - PerfUtils.addMeasurement(perfTimerName); - }).fail(function () { - PerfUtils.finalizeMeasurement(perfTimerName); - }); - - FileUtils.readAsText(file) - .always(function () { - // document is no longer pending - delete getDocumentForPath._pendingDocumentPromises[file.id]; - }) - .done(function (rawText, readTimestamp) { - doc = new DocumentModule.Document(file, readTimestamp, rawText); - - // This is a good point to clean up any old dangling Documents - _gcDocuments(); - - result.resolve(doc); - }) - .fail(function (fileError) { - result.reject(fileError); - }); + var file = FileSystem.getFileForPath(fullPath), + pendingPromise = getDocumentForPath._pendingDocumentPromises[file.id]; - return promise; + if (pendingPromise) { + // wait for the result of a previous request + return pendingPromise; + } else { + var result = new $.Deferred(), + promise = result.promise(); + + // log this document's Promise as pending + getDocumentForPath._pendingDocumentPromises[file.id] = promise; + + // create a new document + var perfTimerName = PerfUtils.markStart("getDocumentForPath:\t" + fullPath); + + result.done(function () { + PerfUtils.addMeasurement(perfTimerName); + }).fail(function () { + PerfUtils.finalizeMeasurement(perfTimerName); + }); + + FileUtils.readAsText(file) + .always(function () { + // document is no longer pending + delete getDocumentForPath._pendingDocumentPromises[file.id]; + }) + .done(function (rawText, readTimestamp) { + doc = new DocumentModule.Document(file, readTimestamp, rawText); + + // This is a good point to clean up any old dangling Documents + _gcDocuments(); + + result.resolve(doc); + }) + .fail(function (fileError) { + result.reject(fileError); + }); + + return promise; + } } } diff --git a/src/document/InMemoryFile.js b/src/document/InMemoryFile.js index c38e54a38..5de570651 100644 --- a/src/document/InMemoryFile.js +++ b/src/document/InMemoryFile.js @@ -27,7 +27,7 @@ /** * Represents a file that will never exist on disk - a placeholder backing file for untitled Documents. NO ONE - * other than DocumentManager should create insteances of InMemoryFile. It is valid to test for one (`instanceof + * other than DocumentManager should create instances of InMemoryFile. It is valid to test for one (`instanceof * InMemoryFile`), but it's better to check `doc.isUntitled` where possible. * * Attempts to read/write an InMemoryFile will always fail, and exists() always yields false. InMemoryFile.fullPath diff --git a/src/editor/ImageViewer.js b/src/editor/ImageViewer.js index b1a5b06aa..a32fff31c 100644 --- a/src/editor/ImageViewer.js +++ b/src/editor/ImageViewer.js @@ -106,7 +106,7 @@ define(function (require, exports, module) { $("#img-data").text(dimensionString); } else { var sizeString = ""; - if (stat && stat.size) { + if (stat.size) { sizeString = " — " + StringUtils.prettyPrintBytes(stat.size, 2); } $("#img-data").html(dimensionString + sizeString); diff --git a/src/extensions/default/DebugCommands/main.js b/src/extensions/default/DebugCommands/main.js index 126379171..8fefab7d7 100644 --- a/src/extensions/default/DebugCommands/main.js +++ b/src/extensions/default/DebugCommands/main.js @@ -142,7 +142,7 @@ define(function (require, exports, module) { function _handleSwitchLanguage() { var stringsPath = FileUtils.getNativeBracketsDirectoryPath() + "/nls"; - FileSystem.getDirectoryForPath(stringsPath).getContents(function (err, contents) { + FileSystem.getDirectoryForPath(stringsPath).getContents(function (err, entries) { if (!err) { var $dialog, $submit, @@ -172,7 +172,7 @@ define(function (require, exports, module) { languages.push({label: getLocalizedLabel("en"), language: "en"}); // inspect all children of dirEntry - contents.forEach(function (entry) { + entries.forEach(function (entry) { if (entry.isDirectory) { var match = entry.name.match(/^([a-z]{2})(-[a-z]{2})?$/); diff --git a/src/extensions/default/JavaScriptCodeHints/unittests.js b/src/extensions/default/JavaScriptCodeHints/unittests.js index abf36208b..5cd4501da 100644 --- a/src/extensions/default/JavaScriptCodeHints/unittests.js +++ b/src/extensions/default/JavaScriptCodeHints/unittests.js @@ -744,12 +744,10 @@ define(function (require, exports, module) { end = { line: 6, ch: 8 }, endplus = { line: 6, ch: 12 }; - runs(function () { - testDoc.replaceRange("A1.prop", start, start); - testEditor.setCursorPos(middle); - var hintObj = expectHints(JSCodeHints.jsHintProvider); - selectHint(JSCodeHints.jsHintProvider, hintObj, "propA"); - }); + testDoc.replaceRange("A1.prop", start, start); + testEditor.setCursorPos(middle); + var hintObj = expectHints(JSCodeHints.jsHintProvider); + selectHint(JSCodeHints.jsHintProvider, hintObj, "propA"); runs(function () { expect(testEditor.getCursorPos()).toEqual(end); diff --git a/src/extensions/default/JavaScriptQuickEdit/main.js b/src/extensions/default/JavaScriptQuickEdit/main.js index bb1cf5010..0634c8bc6 100644 --- a/src/extensions/default/JavaScriptQuickEdit/main.js +++ b/src/extensions/default/JavaScriptQuickEdit/main.js @@ -75,17 +75,21 @@ define(function (require, exports, module) { PerfUtils.markStart(PerfUtils.JAVASCRIPT_FIND_FUNCTION); - ProjectManager.getAllFiles().done(function (files) { - JSUtils.findMatchingFunctions(functionName, files) - .done(function (functions) { - PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_FIND_FUNCTION); - result.resolve(functions); - }) - .fail(function () { - PerfUtils.finalizeMeasurement(PerfUtils.JAVASCRIPT_FIND_FUNCTION); - result.reject(); - }); - }); + ProjectManager.getAllFiles() + .done(function (files) { + JSUtils.findMatchingFunctions(functionName, files) + .done(function (functions) { + PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_FIND_FUNCTION); + result.resolve(functions); + }) + .fail(function () { + PerfUtils.finalizeMeasurement(PerfUtils.JAVASCRIPT_FIND_FUNCTION); + result.reject(); + }); + }) + .fail(function () { + result.reject(); + }); return result.promise(); } diff --git a/src/extensions/default/RecentProjects/main.js b/src/extensions/default/RecentProjects/main.js index c6f6ded1b..e204b3c0f 100644 --- a/src/extensions/default/RecentProjects/main.js +++ b/src/extensions/default/RecentProjects/main.js @@ -280,10 +280,10 @@ define(function (require, exports, module) { */ function _handleListEvents() { $dropdown - .on("click", "a", function (e) { - var $link = $(e.target).closest("a"), - id = $link.attr("id"), - path = $link.data("path"); + .on("click", "a", function () { + var $link = $(this), + id = $link.attr("id"), + path = $link.data("path"); if (path) { ProjectManager.openProject(path) diff --git a/src/language/CSSUtils.js b/src/language/CSSUtils.js index eca317edd..9466e8979 100644 --- a/src/language/CSSUtils.js +++ b/src/language/CSSUtils.js @@ -22,8 +22,8 @@ */ -/*jslint vars: true, plusplus: true, regexp: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ -/*global define, $, CodeMirror, _parseRuleList: true, PathUtils */ +/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, regexp: true */ +/*global define, $, CodeMirror, _parseRuleList: true */ // JSLint Note: _parseRuleList() is cyclical dependency, not a global function. // It was added to this list to prevent JSLint warning about being used before being defined. diff --git a/src/project/ProjectManager.js b/src/project/ProjectManager.js index 2095be13d..6ac0db313 100644 --- a/src/project/ProjectManager.js +++ b/src/project/ProjectManager.js @@ -90,7 +90,6 @@ define(function (require, exports, module) { * http://www.gnu.org/software/tar/manual/html_section/exclude.html * @type {RegExp} */ - var _exclusionListRegEx = /\.pyc$|^\.git$|^\.gitignore$|^\.gitmodules$|^\.svn$|^\.DS_Store$|^Thumbs\.db$|^\.hg$|^CVS$|^\.cvsignore$|^\.gitattributes$|^\.hgtags$|^\.c9revisions|^\.SyncArchive|^\.SyncID|^\.SyncIgnore|^\.hgignore$/; /** @@ -771,7 +770,7 @@ define(function (require, exports, module) { } // Fetch dirEntry's contents - dirEntry.getContents(function (err, contents) { + dirEntry.getContents(function (err, contents, statsErrs) { if (err) { Dialogs.showModalDialog( DefaultDialogs.DIALOG_ID_ERROR, @@ -785,6 +784,10 @@ define(function (require, exports, module) { // Reject the render promise so we can move on. deferred.reject(); } else { + if (statsErrs) { + // some but not all entries failed to load, so render what we can + console.warn("Error reading a subset of folder " + dirEntry); + } processEntries(contents); } }); @@ -879,7 +882,7 @@ define(function (require, exports, module) { FileSystem.watch(FileSystem.getDirectoryForPath(rootPath), _shouldShowName, function (err) { if (err) { - console.log("Error watching project root: ", rootPath, err); + console.error("Error watching project root: ", rootPath, err); } }); } @@ -896,7 +899,7 @@ define(function (require, exports, module) { FileSystem.unwatch(_projectRoot, function (err) { if (err) { - console.log("Error unwatching project root: ", _projectRoot.fullPath, err); + console.error("Error unwatching project root: ", _projectRoot.fullPath, err); } }); } @@ -1367,10 +1370,10 @@ define(function (require, exports, module) { var file = FileSystem.getFileForPath(newItemPath); file.write("", function (err) { - if (!err) { - successCallback(file); - } else { + if (err) { errorCallback(err); + } else { + successCallback(file); } }); } diff --git a/src/search/FindInFiles.js b/src/search/FindInFiles.js index 9265a6f26..2e8811d2a 100644 --- a/src/search/FindInFiles.js +++ b/src/search/FindInFiles.js @@ -875,6 +875,13 @@ define(function (require, exports, module) { if (entry && entry.isDirectory) { var resultsChanged = false; + // This is a temporary watcher implementation that needs to be updated + // once we have our final watcher API. Specifically, we will be adding + // 'added' and 'removed' parameters to this function to easily determine + // which files/folders have been added or removed. + // + // In the meantime, do a quick check for directory changed events to see + // if any of the search results files have been deleted. if (searchResultsPanel.isVisible()) { entry.getContents(function (err, contents) { if (!err) { diff --git a/src/search/QuickOpen.js b/src/search/QuickOpen.js index 05693ba2e..79594fb13 100644 --- a/src/search/QuickOpen.js +++ b/src/search/QuickOpen.js @@ -841,7 +841,6 @@ define(function (require, exports, module) { // Start fetching the file list, which will be needed the first time the user enters an un-prefixed query. If file index // caches are out of date, this list might take some time to asynchronously build. See searchFileList() for how this is handled. - this._filenameMatcher.reset(); fileListPromise = ProjectManager.getAllFiles(true) .done(function (files) { fileList = files; diff --git a/src/utils/ExtensionLoader.js b/src/utils/ExtensionLoader.js index 743bc010c..53d257b81 100644 --- a/src/utils/ExtensionLoader.js +++ b/src/utils/ExtensionLoader.js @@ -192,29 +192,21 @@ define(function (require, exports, module) { function testExtension(name, config, entryPoint) { var result = new $.Deferred(), extensionPath = config.baseUrl + "/" + entryPoint + ".js"; - - var fileExists = false, statComplete = false; - var file = FileSystem.getFileForPath(extensionPath); - file.stat(function (err, stat) { - if (!err) { - statComplete = true; - if (stat.isFile) { - // unit test file exists - var extensionRequire = brackets.libRequire.config({ - context: name, - baseUrl: config.baseUrl, - paths: $.extend({}, config.paths, globalConfig) - }); - - // console.log("[Extension] loading unit test " + config.baseUrl); - extensionRequire([entryPoint], function () { - // console.log("[Extension] loaded unit tests " + config.baseUrl); - result.resolve(); - }); - } else { - result.reject(); - } + FileSystem.resolve(extensionPath, function (err, entry) { + if (!err && entry.isFile) { + // unit test file exists + var extensionRequire = brackets.libRequire.config({ + context: name, + baseUrl: config.baseUrl, + paths: $.extend({}, config.paths, globalConfig) + }); + + // console.log("[Extension] loading unit test " + config.baseUrl); + extensionRequire([entryPoint], function () { + // console.log("[Extension] loaded unit tests " + config.baseUrl); + result.resolve(); + }); } else { result.reject(); } diff --git a/test/spec/CSSUtils-test.js b/test/spec/CSSUtils-test.js index 39c1bd4ca..b67be3f90 100644 --- a/test/spec/CSSUtils-test.js +++ b/test/spec/CSSUtils-test.js @@ -22,7 +22,7 @@ */ /*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */ -/*global brackets, define, describe, xdescribe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, $, CodeMirror, beforeFirst, afterLast */ +/*global define, describe, xdescribe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, $, CodeMirror, beforeFirst, afterLast */ define(function (require, exports, module) { "use strict"; diff --git a/test/spec/LiveDevelopment-test.js b/test/spec/LiveDevelopment-test.js index 935cf1ce4..b1af49eba 100644 --- a/test/spec/LiveDevelopment-test.js +++ b/test/spec/LiveDevelopment-test.js @@ -999,7 +999,7 @@ define(function (require, exports, module) { SpecRunnerUtils.closeTestWindow(); }); - function loadFileAndUpdateFileIndex(fileToLoadIntoEditor) { + function loadFile(fileToLoadIntoEditor) { runs(function () { waitsForDone(SpecRunnerUtils.openProjectFiles([fileToLoadIntoEditor]), "SpecRunnerUtils.openProjectFiles " + fileToLoadIntoEditor); }); @@ -1039,7 +1039,7 @@ define(function (require, exports, module) { indexFile = "sub/sub2/index.html"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/static-project-1"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { promise = LiveDevelopment._getInitialDocFromCurrent(); @@ -1064,7 +1064,7 @@ define(function (require, exports, module) { SpecRunnerUtils.loadProjectInTestWindow(testPath + "/static-project-2"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { promise = LiveDevelopment._getInitialDocFromCurrent(); @@ -1088,7 +1088,7 @@ define(function (require, exports, module) { indexFile = "index.html"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/static-project-3"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { promise = LiveDevelopment._getInitialDocFromCurrent(); @@ -1112,7 +1112,7 @@ define(function (require, exports, module) { indexFile = "sub/sub2/index.html"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/static-project-4"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { promise = LiveDevelopment._getInitialDocFromCurrent(); @@ -1136,7 +1136,7 @@ define(function (require, exports, module) { indexFile = "sub/index.html"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/static-project-5"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { promise = LiveDevelopment._getInitialDocFromCurrent(); @@ -1159,7 +1159,7 @@ define(function (require, exports, module) { var cssFile = "top2/test.css"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/static-project-6"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { promise = LiveDevelopment._getInitialDocFromCurrent(); @@ -1211,7 +1211,7 @@ define(function (require, exports, module) { indexFile = "sub/sub2/index.php"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/dynamic-project-1"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { ProjectManager.setBaseUrl("http://localhost:1111/"); @@ -1236,7 +1236,7 @@ define(function (require, exports, module) { indexFile = "sub/index.php"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/dynamic-project-2"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { ProjectManager.setBaseUrl("http://localhost:2222/"); @@ -1261,7 +1261,7 @@ define(function (require, exports, module) { indexFile = "index.php"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/dynamic-project-3"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { ProjectManager.setBaseUrl("http://localhost:3333/"); @@ -1286,7 +1286,7 @@ define(function (require, exports, module) { indexFile = "sub/index.php"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/dynamic-project-5"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { ProjectManager.setBaseUrl("http://localhost:5555/"); @@ -1310,7 +1310,7 @@ define(function (require, exports, module) { var cssFile = "top2/test.css"; SpecRunnerUtils.loadProjectInTestWindow(testPath + "/dynamic-project-6"); - loadFileAndUpdateFileIndex(cssFile); + loadFile(cssFile); runs(function () { ProjectManager.setBaseUrl("http://localhost:6666/"); diff --git a/test/spec/ProjectManager-test.js b/test/spec/ProjectManager-test.js index 20a6c6e65..12f3499d8 100644 --- a/test/spec/ProjectManager-test.js +++ b/test/spec/ProjectManager-test.js @@ -23,7 +23,7 @@ /*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */ -/*global $, define, require, describe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, waitsForDone, beforeFirst, afterLast */ +/*global $, define, require, describe, it, expect, beforeEach, afterEach, waitsFor, runs, waitsForDone, beforeFirst, afterLast */ define(function (require, exports, module) { "use strict"; diff --git a/test/spec/SpecRunnerUtils.js b/test/spec/SpecRunnerUtils.js index fe490288c..ba397d760 100644 --- a/test/spec/SpecRunnerUtils.js +++ b/test/spec/SpecRunnerUtils.js @@ -81,6 +81,7 @@ define(function (require, exports, module) { if (err === FileSystemError.NOT_FOUND && silent) { result.resolve(); } else { + console.error("Unable to remove " + fullPath, err); result.reject(err); } } @@ -241,24 +242,6 @@ define(function (require, exports, module) { waitsForDone(deferred, "Create temp directory", 500); } - /** - * @private - */ - function _stat(pathname) { - var deferred = new $.Deferred(); - - FileSystem.resolve(pathname, function (err, entry) { - if (err) { - deferred.reject(err); - return; - } - - deferred.resolve(entry); - }); - - return deferred; - } - function _resetPermissionsOnSpecialTempFolders() { var i, folders = [], @@ -271,19 +254,19 @@ define(function (require, exports, module) { promise = Async.doSequentially(folders, function (folder) { var deferred = new $.Deferred(); - _stat(folder) - .done(function () { + FileSystem.resolve(folder, function (err, entry) { + if (!err) { // Change permissions if the directory exists chmod(folder, "777").then(deferred.resolve, deferred.reject); - }) - .fail(function (err) { + } else { if (err === FileSystemError.NOT_FOUND) { // Resolve the promise since the folder to reset doesn't exist deferred.resolve(); } else { deferred.reject(); } - }); + } + }); return deferred.promise(); }, true);