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

Commit

Permalink
Bugfix: renaming file "foo" should not affect file "foobar/baz" even …
Browse files Browse the repository at this point in the history
…though "foo" is a prefix of "foobar".
  • Loading branch information
DennisKehrig committed Feb 21, 2013
1 parent 40dee60 commit 59cf95f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/document/DocumentManager.js
Expand Up @@ -1163,16 +1163,16 @@ define(function (require, exports, module) {
var keysToDelete = [];
for (path in _openDocuments) {
if (_openDocuments.hasOwnProperty(path)) {
if (path.indexOf(oldName) === 0) {
if (FileUtils.isAffectedWhenRenaming(path, oldName, newName, isFolder)) {
// Copy value to new key
var newKey = path.replace(oldName, newName);

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

// Update document file
FileUtils.updateFileEntryPath(_openDocuments[newKey].file, oldName, newName);
FileUtils.updateFileEntryPath(_openDocuments[newKey].file, oldName, newName, isFolder);

if (!isFolder) {
// If the path name is a file, there can only be one matched entry in the open document
// list, which we just updated. Break out of the for .. in loop.
Expand All @@ -1188,7 +1188,7 @@ define(function (require, exports, module) {

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

// Send a "fileNameChanged" event. This will trigger the views to update.
Expand Down
23 changes: 20 additions & 3 deletions src/file/FileUtils.js
Expand Up @@ -248,14 +248,30 @@ define(function (require, exports, module) {
}

/**
* Checks wheter a path is affected by a rename operation.
* A path is affected if the object being renamed is a file and the given path refers
* to that file or if the object being renamed is a directory and a prefix of the path.
* Always checking for prefixes can create conflicts:
* renaming file "foo" should not affect file "foobar/baz" even though "foo" is a prefix of "foobar".
* @param {!string} path The path potentially affected
* @param {!string} oldName An object's name before renaming
* @param {!string} newName An object's name after renaming
* @param {?boolean} isFolder Whether the renamed object is a folder or not
*/
function isAffectedWhenRenaming(path, oldName, newName, isFolder) {
isFolder = isFolder || oldName.slice(-1) === "/";
return (isFolder && path.indexOf(oldName) === 0) || (!isFolder && path === oldName);
}

/**
* Update a file entry path after a file/folder name change.
* @param {FileEntry} entry The FileEntry or DirectoryEntry to update
* @param {string} oldName The full path of the old name
* @param {string} newName The full path of the new name
* @return {boolean} Returns true if the file entry was updated
*/
function updateFileEntryPath(entry, oldName, newName) {
if (entry.fullPath.indexOf(oldName) === 0) {
function updateFileEntryPath(entry, oldName, newName, isFolder) {
if (isAffectedWhenRenaming(entry.fullPath, oldName, newName, isFolder)) {
var fullPath = entry.fullPath.replace(oldName, newName);

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

return false;
}

/** @const - hard-coded for now, but may want to make these preferences */
var _staticHtmlFileExts = ["htm", "html"],
_serverHtmlFileExts = ["php", "php3", "php4", "php5", "phtm", "phtml", "cfm", "cfml", "asp", "aspx", "jsp", "jspx", "shtm", "shtml"];
Expand Down Expand Up @@ -327,6 +343,7 @@ define(function (require, exports, module) {
exports.getNativeBracketsDirectoryPath = getNativeBracketsDirectoryPath;
exports.getNativeModuleDirectoryPath = getNativeModuleDirectoryPath;
exports.canonicalizeFolderPath = canonicalizeFolderPath;
exports.isAffectedWhenRenaming = isAffectedWhenRenaming;
exports.updateFileEntryPath = updateFileEntryPath;
exports.isStaticHtmlFileExt = isStaticHtmlFileExt;
exports.isServerHtmlFileExt = isServerHtmlFileExt;
Expand Down
2 changes: 1 addition & 1 deletion src/project/ProjectManager.js
Expand Up @@ -1226,7 +1226,7 @@ define(function (require, exports, module) {

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

// Notify that one of the project files has changed
Expand Down

0 comments on commit 59cf95f

Please sign in to comment.