From a37b9ac53acefda9e4ab17c69c535563228e61b3 Mon Sep 17 00:00:00 2001 From: Matthew Kastor Date: Thu, 4 Apr 2013 19:40:13 -0400 Subject: [PATCH 1/6] adds modelist to extensions I copied the modelist.js file from the kitchen sink demo and added doc comments to the exported methods. This module would be extremely helpful to anyone implementing a menu system or who wanted to programatically switch rendering modes based on the file extension of the file loaded into ace. I can think of several use cases for it already. (1) in the settingsMenu branch. (2) in supporting theme switching for anyone implementing drag and drop support for loading files into the editor (3) in supporting theme switching for sites implementing their own file picker dialogues and loading content into ace through xmlhttprequest (4) in supporting filebrowser / editor applications where a server could be set up to display file contents in an ace editor as users browsed to arbitrary files. (5) etc. etc. Had I known about it when I began writing the settingsMenu branch I would have used it. --- lib/ace/ext/modelist.js | 140 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 lib/ace/ext/modelist.js diff --git a/lib/ace/ext/modelist.js b/lib/ace/ext/modelist.js new file mode 100644 index 00000000..f3bf695b --- /dev/null +++ b/lib/ace/ext/modelist.js @@ -0,0 +1,140 @@ +define(function(require, exports, module) { +"use strict"; + +/************** modes ***********************/ +/** + * An array containing information about rendering modes. + */ +var modes = []; +/** + * Suggests a mode based on the file extension present in the given path + * @param {string} path The path to the file + * @returns {object} Returns an object containing information about the + * suggested mode. + */ +function getModeFromPath(path) { + var mode = modesByName.text; + var fileName = path.split(/[\/\\]/).pop(); + for (var i = 0; i < modes.length; i++) { + if (modes[i].supportsFile(fileName)) { + mode = modes[i]; + break; + } + } + return mode; +} + +var Mode = function(name, desc, extensions) { + this.name = name; + this.desc = desc; + this.mode = "ace/mode/" + name; + if (/\^/.test(extensions)) { + var re = extensions.replace(/\|(\^)?/g, function(a, b){ + return "$|" + (b ? "^" : "^.*\\."); + }) + "$"; + } else { + var re = "^.*\\.(" + extensions + ")$"; + } + + this.extRe = new RegExp(re, "gi"); +}; + +Mode.prototype.supportsFile = function(filename) { + return filename.match(this.extRe); +}; + +/** + * An object containing properties that map to rendering modes. Each property + * contains an array where element 0 is the name of the mode and element 1 + * contains information about the file extensions where this mode is + * applicable. + */ +var modesByName = { + abap: ["ABAP" , "abap"], + asciidoc: ["AsciiDoc" , "asciidoc"], + c9search: ["C9Search" , "c9search_results"], + coffee: ["CoffeeScript" , "^Cakefile|coffee|cf|cson"], + coldfusion: ["ColdFusion" , "cfm"], + csharp: ["C#" , "cs"], + css: ["CSS" , "css"], + curly: ["Curly" , "curly"], + dart: ["Dart" , "dart"], + diff: ["Diff" , "diff|patch"], + dot: ["Dot" , "dot"], + ftl: ["FreeMarker" , "ftl"], + glsl: ["Glsl" , "glsl|frag|vert"], + golang: ["Go" , "go"], + groovy: ["Groovy" , "groovy"], + haxe: ["haXe" , "hx"], + haml: ["HAML" , "haml"], + html: ["HTML" , "htm|html|xhtml"], + c_cpp: ["C/C++" , "c|cc|cpp|cxx|h|hh|hpp"], + clojure: ["Clojure" , "clj"], + jade: ["Jade" , "jade"], + java: ["Java" , "java"], + jsp: ["JSP" , "jsp"], + javascript: ["JavaScript" , "js"], + json: ["JSON" , "json"], + jsx: ["JSX" , "jsx"], + latex: ["LaTeX" , "latex|tex|ltx|bib"], + less: ["LESS" , "less"], + lisp: ["Lisp" , "lisp"], + scheme: ["Scheme" , "scm|rkt"], + liquid: ["Liquid" , "liquid"], + livescript: ["LiveScript" , "ls"], + logiql: ["LogiQL" , "logic|lql"], + lua: ["Lua" , "lua"], + luapage: ["LuaPage" , "lp"], // http://keplerproject.github.com/cgilua/manual.html#templates + lucene: ["Lucene" , "lucene"], + lsl: ["LSL" , "lsl"], + makefile: ["Makefile" , "^GNUmakefile|^makefile|^Makefile|^OCamlMakefile|make"], + markdown: ["Markdown" , "md|markdown"], + mushcode: ["TinyMUSH" , "mc|mush"], + objectivec: ["Objective-C" , "m"], + ocaml: ["OCaml" , "ml|mli"], + pascal: ["Pascal" , "pas|p"], + perl: ["Perl" , "pl|pm"], + pgsql: ["pgSQL" , "pgsql"], + php: ["PHP" , "php|phtml"], + powershell: ["Powershell" , "ps1"], + python: ["Python" , "py"], + r: ["R" , "r"], + rdoc: ["RDoc" , "Rd"], + rhtml: ["RHTML" , "Rhtml"], + ruby: ["Ruby" , "ru|gemspec|rake|rb"], + scad: ["OpenSCAD" , "scad"], + scala: ["Scala" , "scala"], + scss: ["SCSS" , "scss"], + sass: ["SASS" , "sass"], + sh: ["SH" , "sh|bash|bat"], + sql: ["SQL" , "sql"], + stylus: ["Stylus" , "styl|stylus"], + svg: ["SVG" , "svg"], + tcl: ["Tcl" , "tcl"], + tex: ["Tex" , "tex"], + text: ["Text" , "txt"], + textile: ["Textile" , "textile"], + tmsnippet: ["tmSnippet" , "tmSnippet"], + toml: ["toml" , "toml"], + typescript: ["Typescript" , "typescript|ts|str"], + vbscript: ["VBScript" , "vbs"], + xml: ["XML" , "xml|rdf|rss|wsdl|xslt|atom|mathml|mml|xul|xbl"], + xquery: ["XQuery" , "xq"], + yaml: ["YAML" , "yaml"] +}; + +for (var name in modesByName) { + var mode = modesByName[name]; + mode = new Mode(name, mode[0], mode[1]); + modesByName[name] = mode; + modes.push(mode); +} + +module.exports = { + getModeFromPath: getModeFromPath, + modes: modes, + modesByName: modesByName +}; + +}); + From e306b09232d213fe37ea1c57883e60f71d64a429 Mon Sep 17 00:00:00 2001 From: Matthew Kastor Date: Thu, 4 Apr 2013 22:07:51 -0400 Subject: [PATCH 2/6] adds modelist demo --- demo/mode_list.html | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 demo/mode_list.html diff --git a/demo/mode_list.html b/demo/mode_list.html new file mode 100644 index 00000000..9aa3f295 --- /dev/null +++ b/demo/mode_list.html @@ -0,0 +1,57 @@ + + + + + + Editor + + + + +
Editor.prototype.showKeyboardShortcuts = function () {
+    showKeyboardShortcuts(this);
+};
+editor.commands.addCommands([{
+    name: "showKeyboardShortcuts",
+    bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
+    exec: function(editor, line) {
+        editor.showKeyboardShortcuts();
+    }
+}]);
+
+ + + + + + + From 3d222a773ac0c66b664b8c00e974507eea70e6ed Mon Sep 17 00:00:00 2001 From: Matthew Kastor Date: Thu, 4 Apr 2013 22:09:21 -0400 Subject: [PATCH 3/6] better example code for modelist demo --- demo/mode_list.html | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/demo/mode_list.html b/demo/mode_list.html index 9aa3f295..7285c77d 100644 --- a/demo/mode_list.html +++ b/demo/mode_list.html @@ -21,16 +21,20 @@ -
Editor.prototype.showKeyboardShortcuts = function () {
-    showKeyboardShortcuts(this);
-};
-editor.commands.addCommands([{
-    name: "showKeyboardShortcuts",
-    bindKey: {win: "Ctrl-Alt-h", mac: "Command-Alt-h"},
-    exec: function(editor, line) {
-        editor.showKeyboardShortcuts();
-    }
-}]);
+
var editor = ace.edit("editor");
+editor.setTheme("ace/theme/twilight");
+(function () {
+    var modelist = ace.require('ace/ext/modelist');
+    // the file path could come from an xmlhttp request, a drop event,
+    // or any other scriptable file loading process.
+    // Extensions could consume the modelist and use it to dynamically
+    // set the editor mode. Webmasters could use it in their scripts
+    // for site specific purposes as well.
+    var filePath = 'blahblah/weee/some.js';
+    var mode = modelist.getModeFromPath(filePath).mode;
+    console.log(mode);
+    editor.getSession().setMode(mode);
+}());
 
From 75a55d5efaeedfef34e867249d0d2f482a4ff5a4 Mon Sep 17 00:00:00 2001 From: Matthew Kastor Date: Thu, 4 Apr 2013 22:11:16 -0400 Subject: [PATCH 4/6] rename modelist demo --- demo/{mode_list.html => modelist.html} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename demo/{mode_list.html => modelist.html} (100%) diff --git a/demo/mode_list.html b/demo/modelist.html similarity index 100% rename from demo/mode_list.html rename to demo/modelist.html From 9702e8d4d3e559cce29f476d92456bccaf90f09e Mon Sep 17 00:00:00 2001 From: Matthew Kastor Date: Fri, 5 Apr 2013 01:59:28 -0400 Subject: [PATCH 5/6] change title of modelist demo page --- demo/modelist.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/modelist.html b/demo/modelist.html index 7285c77d..da46acf6 100644 --- a/demo/modelist.html +++ b/demo/modelist.html @@ -3,7 +3,7 @@ - Editor + ACE Editor Modelist Demo