@@ -34,6 +34,7 @@ define(function (require, exports, module) {
3434 require ( "utils/Global" ) ;
3535
3636 var FileSystemError = require ( "filesystem/FileSystemError" ) ,
37+ LanguageManager = require ( "language/LanguageManager" ) ,
3738 PerfUtils = require ( "utils/PerfUtils" ) ,
3839 Dialogs = require ( "widgets/Dialogs" ) ,
3940 DefaultDialogs = require ( "widgets/DefaultDialogs" ) ,
@@ -309,6 +310,41 @@ define(function (require, exports, module) {
309310 return baseName . substr ( idx + 1 ) ;
310311 }
311312
313+ /**
314+ * Get the file extension (excluding ".") given a path OR a bare filename.
315+ * Returns "" for names with no extension.
316+ * If the only `.` in the file is the first character,
317+ * returns "" as this is not considered an extension.
318+ * This method considers known extensions which include `.` in them.
319+ *
320+ * @param {string } fullPath full path to a file or directory
321+ * @return {string } Returns the extension of a filename or empty string if
322+ * the argument is a directory or a filename with no extension
323+ */
324+ function getSmartFileExtension ( fullPath ) {
325+ var baseName = getBaseName ( fullPath ) ,
326+ parts = baseName . split ( "." ) ;
327+
328+ // get rid of file name
329+ parts . shift ( ) ;
330+ if ( baseName [ 0 ] === "." ) {
331+ // if starts with a `.`, then still consider it as file name
332+ parts . shift ( ) ;
333+ }
334+
335+ // test all other parts of the baseName
336+ while ( parts . length > 1 ) {
337+ var ext = parts . join ( "." ) ;
338+ if ( LanguageManager . getLanguageForExtension ( ext ) ) {
339+ return ext ;
340+ } else {
341+ parts . shift ( ) ;
342+ }
343+ }
344+
345+ return parts [ 0 ] || "" ;
346+ }
347+
312348 /**
313349 * Computes filename as relative to the basePath. For example:
314350 * basePath: /foo/bar/, filename: /foo/bar/baz.txt
@@ -426,5 +462,6 @@ define(function (require, exports, module) {
426462 exports . getBaseName = getBaseName ;
427463 exports . getRelativeFilename = getRelativeFilename ;
428464 exports . getFileExtension = getFileExtension ;
465+ exports . getSmartFileExtension = getSmartFileExtension ;
429466 exports . compareFilenames = compareFilenames ;
430467} ) ;
0 commit comments