-
Notifications
You must be signed in to change notification settings - Fork 7.5k
[OPEN] Fix #4332 and #4409: Sort issues fixed on the Working Set and Project Tree #4463
Changes from 1 commit
a81d8c3
9331daf
f65acfe
49c48ba
121ab4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -359,13 +359,26 @@ define(function (require, exports, module) { | |
|
|
||
| /** | ||
| * Get the parent directory of a file. If a directory is passed in the directory is returned. | ||
| * @param {string} full path to a file or directory | ||
| * @return {string} Returns the path to the parent directory of a file or the path of a directory | ||
| * @param {string} fullPath full path to a file or directory | ||
| * @return {string} Returns the path to the parent directory of a file or the path of a directory | ||
| */ | ||
| function getDirectoryPath(fullPath) { | ||
| return fullPath.substr(0, fullPath.lastIndexOf("/") + 1); | ||
| } | ||
|
|
||
| /** | ||
| * Get the file name without the extension and the file extension from a file name. | ||
| * @param {string} filename file name of a file or directory | ||
| * @return {{name: string, extension: string}} Returns the file real name and extension | ||
| */ | ||
| function getFileNameExtension(filename) { | ||
| var extension = _getFileExtension(filename), | ||
| name = filename.replace(new RegExp("." + extension + "$"), ""); | ||
|
|
||
| return {name: name, extension: extension}; | ||
| } | ||
|
|
||
|
|
||
| // Define public API | ||
| exports.LINE_ENDINGS_CRLF = LINE_ENDINGS_CRLF; | ||
| exports.LINE_ENDINGS_LF = LINE_ENDINGS_LF; | ||
|
|
@@ -386,4 +399,5 @@ define(function (require, exports, module) { | |
| exports.isStaticHtmlFileExt = isStaticHtmlFileExt; | ||
| exports.isServerHtmlFileExt = isServerHtmlFileExt; | ||
| exports.getDirectoryPath = getDirectoryPath; | ||
| exports.getFileNameExtension = getFileNameExtension; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This api is already taken by this pull request #4379 that I landed in master yesterday.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just saw it. Although it doesn't do what I need here. I want to retrieve from a file name (not a path), both the file name without the extension and the extension. So I still need that function, but will need to rename it. By checking #4379, it looks like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @TomMalbran #4379 was my pull request. You're right, getFilenameExtension could re-use _getFileExtension. Haven't noticed it. getFilenameExtension should return extension including . (See the usage). It's due to how PathUtils behaved. It has to be fixed. On your FileEntry comment: that one I actually considered. However, getting the base name is a string operation (see the usage included in the same pull request). Creating an object each time one needs to parse a string is not a good idea.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, didn't noticed that one returned the "." and the other didn't. But yes, merging them or having one use the other might be good. About the other one, I actually checked the uses, and in several places you already have a FileEntry to work with, but there is a place where you don't. Could be made possible to receive a FileEntry, but anyway, i guess it can be useful to have it. |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
|
|
||
|
|
||
| /*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ | ||
| /*global define, $, window */ | ||
| /*global define, $, window, brackets */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need |
||
|
|
||
| /** | ||
| * Manages the workingSet sort methods. | ||
|
|
@@ -35,6 +35,7 @@ define(function (require, exports, module) { | |
| CommandManager = require("command/CommandManager"), | ||
| DocumentManager = require("document/DocumentManager"), | ||
| PreferencesManager = require("preferences/PreferencesManager"), | ||
| FileUtils = require("file/FileUtils"), | ||
| AppInit = require("utils/AppInit"), | ||
| Strings = require("strings"); | ||
|
|
||
|
|
@@ -292,28 +293,40 @@ define(function (require, exports, module) { | |
| function (file1, file2) { | ||
| var index1 = DocumentManager.findInWorkingSetAddedOrder(file1.fullPath), | ||
| index2 = DocumentManager.findInWorkingSetAddedOrder(file2.fullPath); | ||
|
|
||
| return index1 - index2; | ||
| }, | ||
| "workingSetAdd workingSetAddList" | ||
| ); | ||
| register( | ||
| Commands.SORT_WORKINGSET_BY_NAME, | ||
| function (file1, file2) { | ||
| return file1.name.toLocaleLowerCase().localeCompare(file2.name.toLocaleLowerCase()); | ||
| var name1, name2; | ||
| if (brackets.platform === "win") { | ||
| name1 = FileUtils.getFileNameExtension(file1.name).name; | ||
| name2 = FileUtils.getFileNameExtension(file2.name).name; | ||
| } else { | ||
| name1 = file1.name; | ||
| name2 = file2.name; | ||
| } | ||
| return name1.toLocaleLowerCase().localeCompare(name2.toLocaleLowerCase()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know these three sorting functions have some differences, but I still think we can at least refactor the code here to a function as follows and share it here and the other one below. How about moving your code here to a local function And then replace the anonymous function here with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I am working on doing something like this. But passing just the name and not the files, and making it a utils function in |
||
| }, | ||
| "workingSetAdd workingSetAddList" | ||
| ); | ||
| register( | ||
| Commands.SORT_WORKINGSET_BY_TYPE, | ||
| function (file1, file2) { | ||
| var ext1 = file1.name.split('.').pop(), | ||
| ext2 = file2.name.split('.').pop(), | ||
| cmp = ext1.localeCompare(ext2); | ||
| var name1 = FileUtils.getFileNameExtension(file1.name), | ||
| name2 = FileUtils.getFileNameExtension(file2.name), | ||
| cmpExt = name1.extension.localeCompare(name2.extension); | ||
|
|
||
| name1 = brackets.platform === "win" ? name1.name : file1.name; | ||
| name2 = brackets.platform === "win" ? name2.name : file2.name; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you need to conditionally assign name1 and name2, I think it is better to avoid using ternary assignments. Instead, just wrap these two assignments with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the reuse of the variables I can change it to an if, but I still need to assign the names in both branches.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I just notice that you need to compare extensions first and therefore you need to call FileUtils.getFilenameAndExtension() in name1 and name2 initialization. So no need to make any changes.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To share the refactored code, change the anonymous function here as follows:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction: Don't use the above |
||
|
|
||
| if (cmp === 0) { | ||
| return file1.name.toLocaleLowerCase().localeCompare(file2.name.toLocaleLowerCase()); | ||
| if (cmpExt === 0) { | ||
| return name1.toLocaleLowerCase().localeCompare(name2.toLocaleLowerCase()); | ||
| } else { | ||
| return cmp; | ||
| return cmpExt; | ||
| } | ||
| }, | ||
| "workingSetAdd workingSetAddList" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may need to rename this api since this is already taken by pull request #4379. Renaming it to getFilenameAndExtension or splitFilenameAndExtension may be better.