Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 59cf95f

Browse files
committed
Bugfix: renaming file "foo" should not affect file "foobar/baz" even though "foo" is a prefix of "foobar".
1 parent 40dee60 commit 59cf95f

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/document/DocumentManager.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,16 +1163,16 @@ define(function (require, exports, module) {
11631163
var keysToDelete = [];
11641164
for (path in _openDocuments) {
11651165
if (_openDocuments.hasOwnProperty(path)) {
1166-
if (path.indexOf(oldName) === 0) {
1166+
if (FileUtils.isAffectedWhenRenaming(path, oldName, newName, isFolder)) {
11671167
// Copy value to new key
11681168
var newKey = path.replace(oldName, newName);
11691169

11701170
_openDocuments[newKey] = _openDocuments[path];
11711171
keysToDelete.push(path);
11721172

11731173
// Update document file
1174-
FileUtils.updateFileEntryPath(_openDocuments[newKey].file, oldName, newName);
1175-
1174+
FileUtils.updateFileEntryPath(_openDocuments[newKey].file, oldName, newName, isFolder);
1175+
11761176
if (!isFolder) {
11771177
// If the path name is a file, there can only be one matched entry in the open document
11781178
// list, which we just updated. Break out of the for .. in loop.
@@ -1188,7 +1188,7 @@ define(function (require, exports, module) {
11881188

11891189
// Update working set
11901190
for (i = 0; i < _workingSet.length; i++) {
1191-
FileUtils.updateFileEntryPath(_workingSet[i], oldName, newName);
1191+
FileUtils.updateFileEntryPath(_workingSet[i], oldName, newName, isFolder);
11921192
}
11931193

11941194
// Send a "fileNameChanged" event. This will trigger the views to update.

src/file/FileUtils.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,30 @@ define(function (require, exports, module) {
248248
}
249249

250250
/**
251+
* Checks wheter a path is affected by a rename operation.
252+
* A path is affected if the object being renamed is a file and the given path refers
253+
* to that file or if the object being renamed is a directory and a prefix of the path.
254+
* Always checking for prefixes can create conflicts:
255+
* renaming file "foo" should not affect file "foobar/baz" even though "foo" is a prefix of "foobar".
256+
* @param {!string} path The path potentially affected
257+
* @param {!string} oldName An object's name before renaming
258+
* @param {!string} newName An object's name after renaming
259+
* @param {?boolean} isFolder Whether the renamed object is a folder or not
260+
*/
261+
function isAffectedWhenRenaming(path, oldName, newName, isFolder) {
262+
isFolder = isFolder || oldName.slice(-1) === "/";
263+
return (isFolder && path.indexOf(oldName) === 0) || (!isFolder && path === oldName);
264+
}
265+
266+
/**
251267
* Update a file entry path after a file/folder name change.
252268
* @param {FileEntry} entry The FileEntry or DirectoryEntry to update
253269
* @param {string} oldName The full path of the old name
254270
* @param {string} newName The full path of the new name
255271
* @return {boolean} Returns true if the file entry was updated
256272
*/
257-
function updateFileEntryPath(entry, oldName, newName) {
258-
if (entry.fullPath.indexOf(oldName) === 0) {
273+
function updateFileEntryPath(entry, oldName, newName, isFolder) {
274+
if (isAffectedWhenRenaming(entry.fullPath, oldName, newName, isFolder)) {
259275
var fullPath = entry.fullPath.replace(oldName, newName);
260276

261277
entry.fullPath = fullPath;
@@ -276,7 +292,7 @@ define(function (require, exports, module) {
276292

277293
return false;
278294
}
279-
295+
280296
/** @const - hard-coded for now, but may want to make these preferences */
281297
var _staticHtmlFileExts = ["htm", "html"],
282298
_serverHtmlFileExts = ["php", "php3", "php4", "php5", "phtm", "phtml", "cfm", "cfml", "asp", "aspx", "jsp", "jspx", "shtm", "shtml"];
@@ -327,6 +343,7 @@ define(function (require, exports, module) {
327343
exports.getNativeBracketsDirectoryPath = getNativeBracketsDirectoryPath;
328344
exports.getNativeModuleDirectoryPath = getNativeModuleDirectoryPath;
329345
exports.canonicalizeFolderPath = canonicalizeFolderPath;
346+
exports.isAffectedWhenRenaming = isAffectedWhenRenaming;
330347
exports.updateFileEntryPath = updateFileEntryPath;
331348
exports.isStaticHtmlFileExt = isStaticHtmlFileExt;
332349
exports.isServerHtmlFileExt = isServerHtmlFileExt;

src/project/ProjectManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ define(function (require, exports, module) {
12261226

12271227
for (i = 0; i < nodes.length; i++) {
12281228
var node = $(nodes[i]);
1229-
FileUtils.updateFileEntryPath(node.data("entry"), oldName, newName);
1229+
FileUtils.updateFileEntryPath(node.data("entry"), oldName, newName, isFolder);
12301230
}
12311231

12321232
// Notify that one of the project files has changed

0 commit comments

Comments
 (0)