PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/editor/EditorUtils.js

https://bitbucket.org/hanson/brackets
JavaScript | 151 lines | 90 code | 26 blank | 35 comment | 3 complexity | b316c4be9f8ddaabb353c033b157639a MD5 | raw file
  1. /*
  2. * Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. /*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
  24. /*global define, PathUtils, FileError, brackets */
  25. /**
  26. * Set of utilites for working with the code editor
  27. */
  28. define(function (require, exports, module) {
  29. 'use strict';
  30. require("thirdparty/path-utils/path-utils.min");
  31. require("thirdparty/CodeMirror2/mode/xml/xml");
  32. require("thirdparty/CodeMirror2/mode/javascript/javascript");
  33. require("thirdparty/CodeMirror2/mode/css/css");
  34. require("thirdparty/CodeMirror2/mode/less/less");
  35. require("thirdparty/CodeMirror2/mode/htmlmixed/htmlmixed");
  36. require("thirdparty/CodeMirror2/mode/clike/clike");
  37. require("thirdparty/CodeMirror2/mode/php/php");
  38. require("thirdparty/CodeMirror2/mode/coffeescript/coffeescript");
  39. require("thirdparty/CodeMirror2/mode/clojure/clojure");
  40. require("thirdparty/CodeMirror2/mode/perl/perl");
  41. require("thirdparty/CodeMirror2/mode/ruby/ruby");
  42. require("thirdparty/CodeMirror2/mode/mysql/mysql");
  43. require("thirdparty/CodeMirror2/mode/diff/diff");
  44. require("thirdparty/CodeMirror2/mode/markdown/markdown");
  45. /**
  46. * @private
  47. * Given a file URL, determines the mode to use based
  48. * off the file's extension.
  49. * @param {string} fileUrl A cannonical file URL to extract the extension from
  50. */
  51. function getModeFromFileExtension(fileUrl) {
  52. var ext = PathUtils.filenameExtension(fileUrl);
  53. //incase the arg is just the ext
  54. if (!ext) {
  55. ext = fileUrl;
  56. }
  57. if (ext.charAt(0) === '.') {
  58. ext = ext.substr(1);
  59. }
  60. switch (ext) {
  61. case "js":
  62. return "javascript";
  63. case "json":
  64. return {name: "javascript", json: true};
  65. case "css":
  66. return "css";
  67. case "less":
  68. return "less";
  69. case "html":
  70. case "htm":
  71. case "xhtml":
  72. case "cfm":
  73. case "cfc":
  74. return "htmlmixed";
  75. case "xml":
  76. return "xml";
  77. case "php":
  78. case "php3":
  79. case "php4":
  80. case "php5":
  81. case "phtm":
  82. case "phtml":
  83. return "php";
  84. case "cc":
  85. case "cp":
  86. case "cpp":
  87. case "c++":
  88. case "cxx":
  89. case "hh":
  90. case "hpp":
  91. case "hxx":
  92. case "h++":
  93. case "ii":
  94. return "text/x-c++src";
  95. case "c":
  96. case "h":
  97. case "i":
  98. return "text/x-csrc";
  99. case "cs":
  100. return "text/x-csharp";
  101. case "java":
  102. return "text/x-java";
  103. case "coffee":
  104. return "coffeescript";
  105. case "clj":
  106. return "clojure";
  107. case "pl":
  108. return "perl";
  109. case "rb":
  110. return "ruby";
  111. case "sql":
  112. return "mysql";
  113. case "diff":
  114. case "patch":
  115. return "diff";
  116. case "md":
  117. return "markdown";
  118. default:
  119. console.log("Called EditorUtils.js _getModeFromFileExtensions with an unhandled file extension: " + ext);
  120. return "";
  121. }
  122. }
  123. // Define public API
  124. exports.getModeFromFileExtension = getModeFromFileExtension;
  125. });