Skip to content

Cbank/initial code coloring #55

Merged
merged 6 commits into from Jan 10, 2012
View
3 .gitmodules
@@ -1,3 +1,6 @@
[submodule "src/thirdparty/CodeMirror2"]
path = src/thirdparty/CodeMirror2
url = git@github.com:adobe/CodeMirror2.git
+[submodule "src/thirdparty/path-utils"]
+ path = src/thirdparty/path-utils
+ url = git@github.com:jblas/path-utils.git
View
67 src/EditorUtils.js
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2011 Adobe Systems Incorporated. All Rights Reserved.
+ */
+
+/**
+ * Set of utilites for working with the code editor
+ */
+define(function(require, exports, module) {
+
+ require("thirdparty/path-utils/path-utils.min");
+ require("thirdparty/CodeMirror2/mode/xml/xml");
+ require("thirdparty/CodeMirror2/mode/javascript/javascript");
+ require("thirdparty/CodeMirror2/mode/css/css");
+ require("thirdparty/CodeMirror2/mode/less/less");
+ require("thirdparty/CodeMirror2/mode/htmlmixed/htmlmixed");
+
+ /**
+ * Change the current mode of the editor based on file extension
+ * @param {object} editor An instance of a CodeMirror editor
+ * @param {string} fileUrl A cannonical file URL to extract the extension from
+ */
+ function setModeFromFileExtension( editor, fileUrl ) {
+ var mode = _getModeFromFileExtensions( fileUrl );
+ editor.setOption("mode", mode);
+ }
+
+ /**
+ * @private
+ * Given a file URL, determines the mode to use based
+ * off the files extensions.
+ * @param {string} fileUrl A cannonical file URL to extract the extension from
+ */
+ function _getModeFromFileExtensions( fileUrl ) {
+ var ext = PathUtils.filenameExtension( fileUrl );
+ //incase the arg is just the ext
+ if( !ext )
+ ext = fileUrl;
+ if( ext.charAt(0) == '.' )
+ ext = ext.substr(1);
+
+ switch( ext ) {
+
+ case "js":
+ return "javascript";
+
+ case "json":
+ return {name: "javascript", json: true};
+
+ case "css":
+ return "css";
+
+ case "less":
+ return "less";
+
+ case "html":
+ case "htm":
+ return "htmlmixed";
+
+ default:
+ console.log("Called EditorUtils.js _getModeFromFileExtensions with an unhandled file extension: " + ext);
+ return "";
+ }
+ }
+
+ // Define public API
+ exports.setModeFromFileExtension = setModeFromFileExtension;
+});
View
5 src/FileCommandHandlers.js
@@ -7,7 +7,8 @@ define(function(require, exports, module) {
, Commands = require("Commands")
, NativeFileSystem = require("NativeFileSystem").NativeFileSystem
, ProjectManager = require("ProjectManager")
- , Strings = require("strings");
+ , Strings = require("strings")
+ , EditorUtils = require("EditorUtils")
;
/**
@@ -151,6 +152,8 @@ define(function(require, exports, module) {
}
}
+ EditorUtils.setModeFromFileExtension(_editor, _currentFilePath);
+
// TODO: have a real controller object for the editor
_editor.setValue(event.target.result);
View
1 src/brackets.js
@@ -5,7 +5,6 @@
// TODO: break out the definition of brackets into a separate module from the application controller logic
define(function(require, exports, module) {
// Load dependent non-module scripts
- require("thirdparty/CodeMirror2/mode/javascript/javascript");
require("widgets/bootstrap-dropdown");
require("widgets/bootstrap-modal");
1 src/thirdparty/path-utils
@@ -0,0 +1 @@
+Subproject commit 08dba4f8724edeede95660dbd90d8ad2ad13640d
View
66 test/spec/Editor-test.js
@@ -1,28 +1,56 @@
define(function(require, exports, module) {
+ var EditorUtils = require("EditorUtils");
+
describe("Editor", function(){
- var content = 'Brackets is going to be awesome!\n';
+ var content = 'Brackets is going to be awesome!\n';
+
+ describe("CodeMirror", function() {
+ var myCodeMirror;
- describe("CodeMirror", function() {
- var myCodeMirror;
+ beforeEach(function() {
+ // init CodeMirror instance
+ $("body").append("<div id='editor'/>");
+ myCodeMirror = CodeMirror($("#editor").get(0), {
+ value: content
+ });
+ });
- beforeEach(function() {
- // init CodeMirror instance
- $("body").append("<div id='editor'/>");
- myCodeMirror = CodeMirror($("#editor").get(0), {
- value: content
- });
- });
+ afterEach(function() {
+ $("#editor").remove();
+ myCodeMirror = null;
+ });
- afterEach(function() {
- $("#editor").remove();
- myCodeMirror = null;
- });
+ it("should initialize with content", function() {
+ // verify editor content
+ expect(myCodeMirror.getValue()).toEqual(content);
+ });
+
+ describe("Modes", function() {
+ it("should switch to the HTML mode for files ending in .html", function() {
+ // verify editor content
+ EditorUtils.setModeFromFileExtension(myCodeMirror, "file:///only/testing/the/path.html");
+ expect(myCodeMirror.getOption("mode")).toEqual("htmlmixed");
+ });
+
+ it("should switch modes even if the url has a query string", function() {
+ // verify editor content
+ EditorUtils.setModeFromFileExtension(myCodeMirror, "http://only.org/testing/the/path.css?v=2");
+ expect(myCodeMirror.getOption("mode")).toEqual("css");
+ });
+
+ it("should accecpt just a file name too", function() {
+ // verify editor content
+ EditorUtils.setModeFromFileExtension(myCodeMirror, "path.js");
+ expect(myCodeMirror.getOption("mode")).toEqual("javascript");
+ });
- it("should initialize with content", function() {
- // verify editor content
- expect(myCodeMirror.getValue()).toEqual(content);
- });
- });
+ it("should default to plaintext for unknown file extensions", function() {
+ // verify editor content
+ EditorUtils.setModeFromFileExtension(myCodeMirror, "test.foo");
+ expect(myCodeMirror.getOption("mode")).toEqual("");
+ });
+ });
+ });
});
});
Something went wrong with that request. Please try again.