@@ -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,40 @@ 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+ var extension = [ parts . pop ( ) ] , // last part is always an extension
336+ i = parts . length ;
337+ while ( i -- ) {
338+ if ( LanguageManager . getLanguageForExtension ( parts [ i ] ) ) {
339+ extension . unshift ( parts [ i ] ) ;
340+ } else {
341+ break ;
342+ }
343+ }
344+ return extension . join ( "." ) ;
345+ }
346+
312347 /**
313348 * Computes filename as relative to the basePath. For example:
314349 * basePath: /foo/bar/, filename: /foo/bar/baz.txt
@@ -426,5 +461,6 @@ define(function (require, exports, module) {
426461 exports . getBaseName = getBaseName ;
427462 exports . getRelativeFilename = getRelativeFilename ;
428463 exports . getFileExtension = getFileExtension ;
464+ exports . getSmartFileExtension = getSmartFileExtension ;
429465 exports . compareFilenames = compareFilenames ;
430466} ) ;
0 commit comments