Merge pull request #1008 from ajaxorg/tmp

Make Editing Syntax Highlighters Over 111.1(1) Times More Efficient
This commit is contained in:
Garen Torikian 2012-10-04 13:31:04 -07:00
commit 7208932909
24 changed files with 7917 additions and 566 deletions

View file

@ -55,205 +55,18 @@ var Editor = require("ace/editor").Editor;
var MultiSelect = require("ace/multi_select").MultiSelect;
// workers do not work for file:
if (location.protocol == "file:")
EditSession.prototype.$useWorker = false;
/* if (location.protocol == "file:")
EditSession.prototype.$useWorker = false; */
/************** modes ***********************/
var modes = [];
function getModeFromPath(path) {
var mode = modesByName.text;
for (var i = 0; i < modes.length; i++) {
if (modes[i].supportsFile(path)) {
mode = modes[i];
break;
}
}
return mode;
};
var Mode = function(name, desc, extensions) {
this.name = name;
this.desc = desc;
this.mode = "ace/mode/" + name;
this.extRe = new RegExp("^.*\\.(" + extensions + ")$", "g");
};
Mode.prototype.supportsFile = function(filename) {
return filename.match(this.extRe);
};
var modesByName = {
c9search: ["C9Search" , "c9search_results"],
coffee: ["CoffeeScript" , "coffee|^Cakefile"],
coldfusion: ["ColdFusion" , "cfm"],
csharp: ["C#" , "cs"],
css: ["CSS" , "css"],
diff: ["Diff" , "diff|patch"],
glsl: ["Glsl" , "glsl|frag|vert"],
golang: ["Go" , "go"],
groovy: ["Groovy" , "groovy"],
haxe: ["haXe" , "hx"],
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"],
liquid: ["Liquid" , "liquid"],
lua: ["Lua" , "lua"],
luapage: ["LuaPage" , "lp"], // http://keplerproject.github.com/cgilua/manual.html#templates
markdown: ["Markdown" , "md|markdown"],
ocaml: ["OCaml" , "ml|mli"],
perl: ["Perl" , "pl|pm"],
pgsql: ["pgSQL" , "pgsql"],
php: ["PHP" , "php|phtml"],
powershell: ["Powershell" , "ps1"],
python: ["Python" , "py"],
ruby: ["Ruby" , "ru|gemspec|rake|rb"],
scad: ["OpenSCAD" , "scad"],
scala: ["Scala" , "scala"],
scss: ["SCSS" , "scss|sass"],
sh: ["SH" , "sh|bash|bat"],
sql: ["SQL" , "sql"],
svg: ["SVG" , "svg"],
tcl: ["Tcl" , "tcl"],
text: ["Text" , "txt"],
textile: ["Textile" , "textile"],
typescript: ["Typescript" , "typescript|ts|str"],
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);
}
/*********** demo documents ***************************/
var fileCache = {};
function initDoc(file, path, doc) {
if (doc.prepare)
file = doc.prepare(file);
var session = new EditSession(file);
session.setUndoManager(new UndoManager());
doc.session = session;
doc.path = path;
if (doc.wrapped) {
session.setUseWrapMode(true);
session.setWrapLimitRange(80, 80);
}
var mode = getModeFromPath(path)
session.modeName = mode.name;
session.setMode(mode.mode);
}
function makeHuge(txt) {
for (var i = 0; i < 5; i++)
txt += txt;
return txt
}
var docs = {
"docs/javascript.js": "JavaScript",
"docs/clojure.clj": "Clojure",
"docs/coffeescript.coffee": "Coffeescript",
"docs/coldfusion.cfm": "ColdFusion",
"docs/cpp.cpp": "C/C++",
"docs/csharp.cs": "C#",
"docs/css.css": "CSS",
"docs/diff.diff": "Diff",
"docs/glsl.glsl": "Glsl",
"docs/golang.go": "Go",
"docs/groovy.groovy": "Groovy",
"docs/Haxe.hx": "haXe",
"docs/html.html": "HTML",
"docs/jade.jade": "Jade",
"docs/java.java": "Java",
"docs/jsp.jsp": "JSP",
"docs/json.json": "JSON",
"docs/jsx.jsx": "JSX",
"docs/latex.tex": {name: "LaTeX", wrapped: true},
"docs/less.less": "LESS",
"docs/liquid.liquid": "Liquid",
"docs/lua.lua": "Lua",
"docs/luapage.lp": "LuaPage",
"docs/markdown.md": {name: "Markdown", wrapped: true},
"docs/ocaml.ml": "OCaml",
"docs/OpenSCAD.scad": "OpenSCAD",
"docs/perl.pl": "Perl",
"docs/pgsql.pgsql": {name: "pgSQL", wrapped: true},
"docs/php.php": "PHP",
"docs/plaintext.txt": {name: "Plain Text", prepare: makeHuge, wrapped: true},
"docs/powershell.ps1": "Powershell",
"docs/python.py": "Python",
"docs/ruby.rb": "Ruby",
"docs/scala.scala": "Scala",
"docs/scss.scss": "SCSS",
"docs/sh.sh": "SH",
"docs/sql.sql": {name: "SQL", wrapped: true},
"docs/svg.svg": "SVG",
"docs/tcl.tcl": "Tcl",
"docs/textile.textile": {name: "Textile", wrapped: true},
"docs/typescript.ts": "Typescript",
"docs/xml.xml": "XML",
"docs/xquery.xq": "XQuery",
"docs/yaml.yaml": "YAML",
"docs/c9search.c9search_results": "C9 Search Results"
}
var ownSource = {
/* filled from require*/
};
var hugeDocs = {
"build/src/ace.js": "",
"build/src-min/ace.js": ""
};
if (window.require && window.require.s) try {
for (var path in window.require.s.contexts._.loaded) {
if (path.indexOf("!") != -1)
path = path.split("!").pop();
else
path = path + ".js";
ownSource[path] = ""
}
} catch(e) {}
function prepareDocList(docs) {
var list = []
for (var path in docs) {
var doc = docs[path];
if (typeof doc != "object")
doc = {name: doc || path};
doc.path = path;
doc.desc = doc.name.replace(/^(ace|docs|demo|build)\//, "");
if (doc.desc.length > 18)
doc.desc = doc.desc.slice(0, 7) + ".." + doc.desc.slice(-9)
fileCache[doc.name] = doc;
list.push(doc);
};
return list;
}
docs = prepareDocList(docs);
ownSource = prepareDocList(ownSource);
hugeDocs = prepareDocList(hugeDocs);
var doclist = require("./doclist");
var modelist = require("./modelist");
var layout = require("./layout");
var TokenTooltip = require("./token_tooltip").TokenTooltip;
var util = require("./util");
var saveOption = util.saveOption;
var fillDropdown = util.fillDropdown;
var bindCheckbox = util.bindCheckbox;
var bindDropdown = util.bindDropdown;
/*********** create editor ***************************/
var container = document.getElementById("editor");
@ -271,27 +84,30 @@ window.env = env;
window.ace = env.editor;
env.editor.setAnimatedScroll(true);
// add multiple cursor support to editor
require("ace/multi_select").MultiSelect(env.editor);
var consoleEl = dom.createElement("div");
container.parentNode.appendChild(consoleEl);
consoleEl.style.position="fixed"
consoleEl.style.bottom = "1px"
consoleEl.style.right = 0
consoleEl.style.background = "white"
consoleEl.style.border = "1px solid #baf"
consoleEl.style.zIndex = "100"
var cmdLine = new singleLineEditor(consoleEl);
consoleEl.style.cssText = "position:fixed; bottom:1px; right:0;\
border:1px solid #baf; zIndex:100";
var cmdLine = new layout.singleLineEditor(consoleEl);
cmdLine.editor = env.editor;
env.editor.cmdLine = cmdLine;
/**
* This demonstrates how you can define commands and bind shortcuts to them.
*/
env.editor.commands.addCommands([{
name: "gotoline",
bindKey: {win: "Ctrl-L", mac: "Command-L"},
exec: function(editor, line) {
if (typeof line == "object") {
var arg = this.name + " " + editor.getCursorPosition().row;
editor.cmdLine.setValue(arg, 1)
editor.cmdLine.focus()
return
editor.cmdLine.setValue(arg, 1);
editor.cmdLine.focus();
return;
}
line = parseInt(line, 10);
if (!isNaN(line))
@ -303,10 +119,10 @@ env.editor.commands.addCommands([{
bindKey: {win: "Ctrl-F", mac: "Command-F"},
exec: function(editor, needle) {
if (typeof needle == "object") {
var arg = this.name + " " + editor.getCopyText()
editor.cmdLine.setValue(arg, 1)
editor.cmdLine.focus()
return
var arg = this.name + " " + editor.getCopyText();
editor.cmdLine.setValue(arg, 1);
editor.cmdLine.focus();
return;
}
editor.find(needle);
},
@ -316,24 +132,32 @@ env.editor.commands.addCommands([{
bindKey: "shift-esc",
exec: function(editor, needle) { editor.cmdLine.focus(); },
readOnly: true
}])
}, {
name: "execute",
bindKey: "ctrl+enter",
exec: function(editor) {
try {
var r = eval(editor.getCopyText()||editor.getValue());
} catch(e) {
r = e;
}
editor.cmdLine.setValue(r + "")
},
readOnly: true
}]);
cmdLine.commands.bindKeys({
"Shift-Return|Ctrl-Return|Alt-Return": function(cmdLine) { cmdLine.insert("\n")},
"Shift-Return|Ctrl-Return|Alt-Return": function(cmdLine) { cmdLine.insert("\n"); },
"Esc|Shift-Esc": function(cmdLine){ cmdLine.editor.focus(); },
"Return": function(cmdLine){
var command = cmdLine.getValue().split(/\s+/);
var editor = cmdLine.editor;
editor.commands.exec(command[0], editor, command[1]);
editor.focus();
},
})
}
});
cmdLine.commands.removeCommands(["find", "gotoline", "findall", "replace", "replaceall"])
/**
* This demonstrates how you can define commands and bind shortcuts to them.
*/
cmdLine.commands.removeCommands(["find", "gotoline", "findall", "replace", "replaceall"]);
var commands = env.editor.commands;
commands.addCommand({
@ -354,28 +178,28 @@ var keybindings = {
"outdent": "[",
"gotolinestart": "^",
"gotolineend": "$"
})
})
};
/*********** manage layout ***************************/
var consoleHight = 20;
var consoleHeight = 20;
function onResize() {
var left = env.split.$container.offsetLeft;
var width = document.documentElement.clientWidth - left;
container.style.width = width + "px";
container.style.height = document.documentElement.clientHeight - consoleHight + "px";
container.style.height = document.documentElement.clientHeight - consoleHeight + "px";
env.split.resize();
consoleEl.style.width = width + "px";
cmdLine.resize()
cmdLine.resize();
}
window.onresize = onResize;
onResize();
/*********** options pane ***************************/
/*********** options panel ***************************/
var docEl = document.getElementById("doc");
var modeEl = document.getElementById("mode");
var wrapModeEl = document.getElementById("soft_wrap");
@ -392,54 +216,23 @@ var animateScrollEl = document.getElementById("animate_scroll");
var softTabEl = document.getElementById("soft_tab");
var behavioursEl = document.getElementById("enable_behaviours");
var group = document.createElement("optgroup");
group.setAttribute("label", "Mode Examples");
fillDropdown(docs, group);
docEl.appendChild(group);
var group = document.createElement("optgroup");
group.setAttribute("label", "Huge documents");
fillDropdown(hugeDocs, group);
docEl.appendChild(group);
var group = document.createElement("optgroup");
group.setAttribute("label", "own source");
fillDropdown(ownSource, group);
docEl.appendChild(group);
fillDropdown(modes, modeEl);
fillDropdown(docEl, doclist.all);
fillDropdown(modeEl, modelist.modes);
var modesByName = modelist.modesByName;
bindDropdown("mode", function(value) {
env.editor.getSession().setMode(modesByName[value].mode || modesByName.text.mode);
env.editor.getSession().modeName = value;
env.editor.session.setMode(modesByName[value].mode || modesByName.text.mode);
env.editor.session.modeName = value;
});
bindDropdown("doc", function(name) {
var doc = fileCache[name];
if (!doc)
return;
if (doc.session)
return setSession(doc.session)
//@todo do something while waiting
// env.editor.setSession(emptySession || (emptySession = new EditSession("")))
var path = doc.path;
var parts = path.split("/");
if (parts[0] == "docs")
path = "demo/kitchen-sink/" + path;
else if (parts[0] == "ace")
path = "lib/" + path;
net.get(path, function(x) {
initDoc(x, path, doc);
setSession(doc.session)
})
function setSession(session) {
var session = env.split.setSession(session);
doclist.loadDoc(name, function(session) {
if (!session)
return;
session = env.split.setSession(session);
updateUIEditorOptions();
env.editor.focus();
}
});
});
function updateUIEditorOptions() {
@ -465,40 +258,22 @@ function updateUIEditorOptions() {
saveOption(behavioursEl, editor.getBehavioursEnabled());
}
function saveOption(el, val) {
if (!el.onchange && !el.onclick)
return;
if ("checked" in el) {
if (val !== undefined)
el.checked = val;
localStorage && localStorage.setItem(el.id, el.checked ? 1 : 0);
}
else {
if (val !== undefined)
el.value = val;
localStorage && localStorage.setItem(el.id, el.value);
}
}
event.addListener(themeEl, "mouseover", function(e){
this.desiredValue = e.target.value;
if (!this.$timer)
this.$timer = setTimeout(this.updateTheme);
})
});
event.addListener(themeEl, "mouseout", function(e){
this.desiredValue = null;
if (!this.$timer)
this.$timer = setTimeout(this.updateTheme, 20);
})
});
themeEl.updateTheme = function(){
env.split.setTheme(themeEl.desiredValue || themeEl.selectedValue);
themeEl.$timer = null;
}
};
bindDropdown("theme", function(value) {
if (!value)
@ -516,33 +291,28 @@ bindDropdown("fontsize", function(value) {
});
bindDropdown("folding", function(value) {
env.editor.getSession().setFoldStyle(value);
env.editor.session.setFoldStyle(value);
env.editor.setShowFoldWidgets(value !== "manual");
});
bindDropdown("soft_wrap", function(value) {
var session = env.editor.getSession();
var session = env.editor.session;
var renderer = env.editor.renderer;
switch (value) {
case "off":
session.setUseWrapMode(false);
renderer.setPrintMarginColumn(80);
break;
case "40":
session.setUseWrapMode(true);
session.setWrapLimitRange(40, 40);
renderer.setPrintMarginColumn(40);
break;
case "80":
session.setUseWrapMode(true);
session.setWrapLimitRange(80, 80);
renderer.setPrintMarginColumn(80);
break;
case "free":
session.setUseWrapMode(true);
session.setWrapLimitRange(null, null);
renderer.setPrintMarginColumn(80);
break;
default:
session.setUseWrapMode(true);
var col = parseInt(value, 10);
session.setWrapLimitRange(col, col);
renderer.setPrintMarginColumn(col);
}
});
@ -583,7 +353,7 @@ bindCheckbox("animate_scroll", function(checked) {
});
bindCheckbox("soft_tab", function(checked) {
env.editor.getSession().setUseSoftTabs(checked);
env.editor.session.setUseSoftTabs(checked);
});
bindCheckbox("enable_behaviours", function(checked) {
@ -619,42 +389,15 @@ bindDropdown("split", function(value) {
}
});
function bindCheckbox(id, callback) {
var el = document.getElementById(id);
if (localStorage && localStorage.getItem(id))
el.checked = localStorage.getItem(id) == "1";
var onCheck = function() {
callback(!!el.checked);
saveOption(el);
};
el.onclick = onCheck;
onCheck();
}
function bindDropdown(id, callback) {
var el = document.getElementById(id);
if (localStorage && localStorage.getItem(id))
el.value = localStorage.getItem(id);
var onChange = function() {
callback(el.value);
saveOption(el);
};
el.onchange = onChange;
onChange();
}
function fillDropdown(list, el) {
list.forEach(function(item) {
var option = document.createElement("option");
option.setAttribute("value", item.name);
option.innerHTML = item.desc;
el.appendChild(option);
});
}
bindCheckbox("highlight_token", function(checked) {
var editor = env.editor;
if (editor.tokenTooltip && !checked) {
editor.tokenTooltip.destroy();
delete editor.tokenTooltip;
} else if (checked) {
editor.tokenTooltip = new TokenTooltip(editor);
}
});
/************** dragover ***************************/
event.addListener(container, "dragover", function(e) {
@ -683,96 +426,10 @@ event.addListener(container, "drop", function(e) {
}
});
// add multiple cursor support to editor
require("ace/multi_select").MultiSelect(env.editor);
function singleLineEditor(el) {
var renderer = new Renderer(el);
renderer.scrollBar.element.style.display = "none";
renderer.scrollBar.width = 0;
renderer.content.style.height = "auto";
renderer.screenToTextCoordinates = function(x, y) {
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
// todo size change event
renderer.$computeLayerConfig = function() {
var longestLine = this.$getLongestLine();
var firstRow = 0;
var lastRow = this.session.getLength();
var height = this.session.getScreenLength() * this.lineHeight;
this.scrollTop = 0;
var config = this.layerConfig;
config.width = longestLine;
config.padding = this.$padding;
config.firstRow = 0;
config.firstRowScreen = 0;
config.lastRow = lastRow;
config.lineHeight = this.lineHeight;
config.characterWidth = this.characterWidth;
config.minHeight = height;
config.maxHeight = height;
config.offset = 0;
config.height = height;
this.$gutterLayer.element.style.marginTop = 0 + "px";
this.content.style.marginTop = 0 + "px";
this.content.style.width = longestLine + 2 * this.$padding + "px";
this.content.style.height = height + "px";
this.scroller.style.height = height + "px";
this.container.style.height = height + "px";
};
renderer.isScrollableBy=function(){return false};
var editor = new Editor(renderer);
new MultiSelect(editor);
editor.session.setUndoManager(new UndoManager());
editor.setHighlightActiveLine(false);
editor.setShowPrintMargin(false);
editor.renderer.setShowGutter(false);
editor.renderer.setHighlightGutterLine(false);
return editor;
};
/** simple statusbar **/
var editor = env.editor;
var statusBarEl = dom.createElement("div");
statusBarEl.style.cssText = "color:gray;position:absolute;right:0;border-left:1px solid";
cmdLine.container.appendChild(statusBarEl);
var statusUpdate = lang.deferredCall(function() {
var status = [];
function add(s, sep) {s && status.push(s, sep || "|")}
if (editor.$vimModeHandler)
add(editor.$vimModeHandler.getStatusText());
else if (editor.commands.recording)
add("REC");
var c = editor.selection.lead;
add(c.row + ":" + c.column, " ");
if (!editor.selection.isEmpty()) {
var r = editor.getSelectionRange()
add("(" + (r.end.row - r.start.row) + ":" +(r.end.column - r.start.column) + ")");
}
status.pop();
statusBarEl.textContent = status.join("");
});
env.editor.on("changeStatus", function() {
statusUpdate.schedule(50);
});
env.editor.on("changeSelection", function() {
statusUpdate.schedule(50);
});
var StatusBar = require("./statusbar").StatusBar;
new StatusBar(env.editor, cmdLine.container);
});

View file

@ -0,0 +1,190 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var EditSession = require("ace/edit_session").EditSession;
var UndoManager = require("ace/undomanager").UndoManager;
var net = require("ace/lib/net");
var modelist = require("./modelist");
/*********** demo documents ***************************/
var fileCache = {};
function initDoc(file, path, doc) {
if (doc.prepare)
file = doc.prepare(file);
var session = new EditSession(file);
session.setUndoManager(new UndoManager());
doc.session = session;
doc.path = path;
if (doc.wrapped) {
session.setUseWrapMode(true);
session.setWrapLimitRange(80, 80);
}
var mode = modelist.getModeFromPath(path);
session.modeName = mode.name;
session.setMode(mode.mode);
}
function makeHuge(txt) {
for (var i = 0; i < 5; i++)
txt += txt;
return txt;
}
var docs = {
"docs/AsciiDoc.asciidoc": "AsciiDoc",
"docs/javascript.js": "JavaScript",
"docs/clojure.clj": "Clojure",
"docs/coffeescript.coffee": "Coffeescript",
"docs/coldfusion.cfm": "ColdFusion",
"docs/cpp.cpp": "C/C++",
"docs/csharp.cs": "C#",
"docs/css.css": "CSS",
"docs/diff.diff": "Diff",
"docs/glsl.glsl": "Glsl",
"docs/golang.go": "Go",
"docs/groovy.groovy": "Groovy",
"docs/Haxe.hx": "haXe",
"docs/html.html": "HTML",
"docs/jade.jade": "Jade",
"docs/java.java": "Java",
"docs/jsp.jsp": "JSP",
"docs/json.json": "JSON",
"docs/jsx.jsx": "JSX",
"docs/latex.tex": {name: "LaTeX", wrapped: true},
"docs/less.less": "LESS",
"docs/liquid.liquid": "Liquid",
"docs/lua.lua": "Lua",
"docs/luapage.lp": "LuaPage",
"docs/markdown.md": {name: "Markdown", wrapped: true},
"docs/ocaml.ml": "OCaml",
"docs/OpenSCAD.scad": "OpenSCAD",
"docs/perl.pl": "Perl",
"docs/pgsql.pgsql": {name: "pgSQL", wrapped: true},
"docs/php.php": "PHP",
"docs/plaintext.txt": {name: "Plain Text", prepare: makeHuge, wrapped: true},
"docs/powershell.ps1": "Powershell",
"docs/python.py": "Python",
"docs/ruby.rb": "Ruby",
"docs/scala.scala": "Scala",
"docs/scss.scss": "SCSS",
"docs/sh.sh": "SH",
"docs/sql.sql": {name: "SQL", wrapped: true},
"docs/svg.svg": "SVG",
"docs/tcl.tcl": "Tcl",
"docs/textile.textile": {name: "Textile", wrapped: true},
"docs/typescript.ts": "Typescript",
"docs/xml.xml": "XML",
"docs/xquery.xq": "XQuery",
"docs/yaml.yaml": "YAML",
"docs/c9search.c9search_results": "C9 Search Results"
};
var ownSource = {
/* filled from require*/
};
var hugeDocs = {
"build/src/ace.js": "",
"build/src-min/ace.js": ""
};
if (window.require && window.require.s) try {
for (var path in window.require.s.contexts._.defined) {
if (path.indexOf("!") != -1)
path = path.split("!").pop();
else
path = path + ".js";
ownSource[path] = "";
}
} catch(e) {}
function prepareDocList(docs) {
var list = [];
for (var path in docs) {
var doc = docs[path];
if (typeof doc != "object")
doc = {name: doc || path};
doc.path = path;
doc.desc = doc.name.replace(/^(ace|docs|demo|build)\//, "");
if (doc.desc.length > 18)
doc.desc = doc.desc.slice(0, 7) + ".." + doc.desc.slice(-9);
fileCache[doc.name] = doc;
list.push(doc);
}
return list;
}
function loadDoc(name, callback) {
var doc = fileCache[name];
if (!doc)
return callback(null);
if (doc.session)
return callback(doc.session);
// TODO: show load screen while waiting
var path = doc.path;
var parts = path.split("/");
if (parts[0] == "docs")
path = "demo/kitchen-sink/" + path;
else if (parts[0] == "ace")
path = "lib/" + path;
net.get(path, function(x) {
initDoc(x, path, doc);
callback(doc.session);
});
}
module.exports = {
fileCache: fileCache,
docs: prepareDocList(docs),
ownSource: prepareDocList(ownSource),
hugeDocs: prepareDocList(hugeDocs),
initDoc: initDoc,
loadDoc: loadDoc
};
module.exports.all = {
"Mode Examples": module.exports.docs,
"Huge documents": module.exports.hugeDocs,
"own source": module.exports.ownSource
};
});

File diff suppressed because it is too large Load diff

194
demo/kitchen-sink/layout.js Normal file
View file

@ -0,0 +1,194 @@
define(function(require, exports, module) {
"use strict";
var dom = require("ace/lib/dom");
var event = require("ace/lib/event");
var EditSession = require("ace/edit_session").EditSession;
var UndoManager = require("ace/undomanager").UndoManager;
var Renderer = require("ace/virtual_renderer").VirtualRenderer;
var Editor = require("ace/editor").Editor;
var MultiSelect = require("ace/multi_select").MultiSelect;
dom.importCssString("\
splitter {\
border: 1px solid #C6C6D2;\
width: 0px;\
cursor: ew-resize;\
z-index:10}\
splitter:hover {\
margin-left: -2px;\
width:3px;\
border-color: #B5B4E0;\
}\
", "splitEditor");
exports.edit = function(el) {
if (typeof(el) == "string")
el = document.getElementById(el);
var editor = new Editor(new Renderer(el, require("ace/theme/textmate")));
editor.resize();
event.addListener(window, "resize", function() {
editor.resize();
});
return editor;
};
var SplitRoot = function(el, theme, position, getSize) {
el.style.position = position || "relative";
this.container = el;
this.getSize = getSize || this.getSize;
this.resize = this.$resize.bind(this);
event.addListener(el.ownerDocument.defaultView, "resize", this.resize);
this.editor = this.createEditor();
};
(function(){
this.createEditor = function() {
var el = document.createElement("div");
el.className = this.$editorCSS;
el.style.cssText = "position: absolute; top:0px; bottom:0px";
this.$container.appendChild(el);
var session = new EditSession("");
var editor = new Editor(new Renderer(el, this.$theme));
/*editor.on("focus", function() {
this._emit("focus", editor);
}.bind(this));*/
this.$editors.push(editor);
editor.setFontSize(this.$fontSize);
return editor;
};
this.$resize = function() {
var size = this.getSize(this.container);
this.rect = {
x: size.left,
y: size.top,
w: size.width,
h: size.height
};
this.item.resize(this.rect);
};
this.getSize = function(el) {
return el.getBoundingClientRect();
};
this.destroy = function() {
var win = this.container.ownerDocument.defaultView;
event.removeListener(win, "resize", this.resize);
};
}).call(SplitRoot.prototype);
var Split = function(){
};
(function(){
this.execute = function(options) {
this.$u.execute(options);
};
}).call(Split.prototype);
exports.singleLineEditor = function(el) {
var renderer = new Renderer(el);
el.style.overflow = "hidden";
renderer.scrollBar.element.style.top = "0";
renderer.scrollBar.element.style.display = "none";
renderer.scrollBar.orginalWidth = renderer.scrollBar.width;
renderer.scrollBar.width = 0;
renderer.content.style.height = "auto";
renderer.screenToTextCoordinates = function(x, y) {
var pos = this.pixelToScreenCoordinates(x, y);
return this.session.screenToDocumentPosition(
Math.min(this.session.getScreenLength() - 1, Math.max(pos.row, 0)),
Math.max(pos.column, 0)
);
};
renderer.maxLines = 4;
renderer.$computeLayerConfigWithScroll = renderer.$computeLayerConfig;
renderer.$computeLayerConfig = function() {
var config = this.layerConfig;
var height = this.session.getScreenLength() * this.lineHeight;
if (config.height != height) {
var vScroll = height > this.maxLines * this.lineHeight;
if (vScroll != this.$vScroll) {
if (vScroll) {
this.scrollBar.element.style.display = "";
this.scrollBar.width = this.scrollBar.orginalWidth;
this.container.style.height = config.height + "px";
height = config.height;
this.scrollTop = height - this.maxLines * this.lineHeight;
} else {
this.scrollBar.element.style.display = "none";
this.scrollBar.width = 0;
}
this.onResize();
this.$vScroll = vScroll;
}
if (this.$vScroll)
return renderer.$computeLayerConfigWithScroll();
this.container.style.height = height + "px";
this.scroller.style.height = height + "px";
this.content.style.height = height + "px";
this._emit("resize");
}
var longestLine = this.$getLongestLine();
var firstRow = 0;
var lastRow = this.session.getLength();
this.scrollTop = 0;
config.width = longestLine;
config.padding = this.$padding;
config.firstRow = 0;
config.firstRowScreen = 0;
config.lastRow = lastRow;
config.lineHeight = this.lineHeight;
config.characterWidth = this.characterWidth;
config.minHeight = height;
config.maxHeight = height;
config.offset = 0;
config.height = height;
this.$gutterLayer.element.style.marginTop = 0 + "px";
this.content.style.marginTop = 0 + "px";
this.content.style.width = longestLine + 2 * this.$padding + "px";
};
renderer.isScrollableBy=function(){return false};
renderer.setStyle("ace_one-line");
var editor = new Editor(renderer);
new MultiSelect(editor);
editor.session.setUndoManager(new UndoManager());
editor.setHighlightActiveLine(false);
editor.setShowPrintMargin(false);
editor.renderer.setShowGutter(false);
editor.renderer.setHighlightGutterLine(false);
editor.$mouseHandler.$focusWaitTimout = 0;
return editor;
};
});

View file

@ -1,52 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Editor</title>
<style type="text/css" media="screen">
body {
overflow: hidden;
margin: 0;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#editor {
}
</style>
</head>
<body>
define(function(require, exports, module) {
"use strict";
<div id="toolbar"></div>
<pre id="editor"></pre>
<script src="../build/src/ace.js" type="text/javascript"></script>
<script src="../build/kitchen-sink/demo.js" type="text/javascript"></script>
<script>
var $ = document.getElementById.bind(document);
function bindDropdown(el, callback) {
var onChange = function() {
callback(el.value);
};
el.onchange = onChange;
onChange();
}
function fillDropdown(list, el) {
list.forEach(function(item) {
var option = document.createElement("option");
option.setAttribute("value", item.name);
option.innerHTML = item.desc;
el.appendChild(option);
});
}
/************** modes ***********************/
var modes = [];
function getModeFromPath(path) {
@ -58,7 +12,7 @@ function getModeFromPath(path) {
}
}
return mode;
};
}
var Mode = function(name, desc, extensions) {
this.name = name;
@ -72,19 +26,23 @@ Mode.prototype.supportsFile = function(filename) {
};
var modesByName = {
asciidoc: ["AsciiDoc" , "asciidoc"],
c9search: ["C9Search" , "c9search_results"],
coffee: ["CoffeeScript" , "coffee|^Cakefile"],
coldfusion: ["ColdFusion" , "cfm"],
csharp: ["C#" , "cs"],
css: ["CSS" , "css"],
diff: ["Diff" , "diff|patch"],
glsl: ["Glsl" , "glsl|frag|vert"],
golang: ["Go" , "go"],
groovy: ["Groovy" , "groovy"],
haxe: ["haXe" , "hx"],
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"],
@ -107,8 +65,10 @@ var modesByName = {
sh: ["SH" , "sh|bash|bat"],
sql: ["SQL" , "sql"],
svg: ["SVG" , "svg"],
tcl: ["Tcl" , "tcl"],
text: ["Text" , "txt"],
textile: ["Textile" , "textile"],
typescript: ["Typescript" , "typescript|ts|str"],
xml: ["XML" , "xml|rdf|rss|wsdl|xslt|atom|mathml|mml|xul|xbl"],
xquery: ["XQuery" , "xq"],
yaml: ["YAML" , "yaml"]
@ -116,69 +76,16 @@ var modesByName = {
for (var name in modesByName) {
var mode = modesByName[name];
mode = new Mode(name, mode[0], mode[1])
mode = new Mode(name, mode[0], mode[1]);
modesByName[name] = mode;
modes.push(mode);
}
module.exports = {
getModeFromPath: getModeFromPath,
modes: modes,
modesByName: modesByName
};
var container = document.getElementById("editor");
container.style.position = "absolute";
// Splitting.
var Split = require("ace/split").Split;
var split = new Split(container, null, 1);
split.setOrientation(split.BESIDE);
split.setSplits(2);
var editor1 = split.getEditor(0);
var editor2 = split.getEditor(1);
var toolbar = $("toolbar");
var modeEl = document.createElement("select");
fillDropdown(modes, modeEl);
bindDropdown(modeEl, function(value) {
editor1.session.setMode((modesByName[value] || modesByName.text).mode);
});
toolbar.appendChild(modeEl);
var button = document.createElement("input");
button.setAttribute("type", "button");
button.value = "generate test";
toolbar.appendChild(button)
function onResize() {
var top = toolbar.clientHeight;
var width = document.documentElement.clientWidth;
container.style.top = top + "px";
container.style.width = width + "px";
container.style.height = document.documentElement.clientHeight - top + "px";
split.resize();
}
window.onresize = onResize;
onResize();
button.onclick = function() {
var s = editor1.session
tok = s.bgTokenizer
var data = []
for (var i = 0; i < s.getLength(); i++) {
data.push({
text: s.getLine(i),
state: [tok.getState(i - 1), tok.getState(i)],
tokens: tok.getTokens(i)
})
}
data = JSON.stringify(data, null, 4);
data = data.replace(/(\n\s*)"(\w+)":/g, "$1$2:");
editor2.insert(data);
}
</script>
</body>
</html>

View file

@ -0,0 +1,48 @@
define(function(require, exports, module) {
"use strict";
/** simple statusbar **/
var dom = require("ace/lib/dom");
var lang = require("ace/lib/lang");
var StatusBar = function(editor, parentNode) {
this.element = dom.createElement("div");
this.element.style.cssText = "color: gray; position:absolute; right:0; border-left:1px solid";
parentNode.appendChild(this.element);
var statusUpdate = lang.deferredCall(function(){
this.updateStatus(editor)
}.bind(this));
editor.on("changeStatus", function() {
statusUpdate.schedule(50);
});
editor.on("changeSelection", function() {
statusUpdate.schedule(50);
});
};
(function(){
this.updateStatus = function(editor) {
var status = [];
function add(str, separator) {
str && status.push(str, separator || "|");
}
if (editor.$vimModeHandler)
add(editor.$vimModeHandler.getStatusText());
else if (editor.commands.recording)
add("REC");
var c = editor.selection.lead;
add(c.row + ":" + c.column, " ");
if (!editor.selection.isEmpty()) {
var r = editor.getSelectionRange();
add("(" + (r.end.row - r.start.row) + ":" +(r.end.column - r.start.column) + ")");
}
status.pop();
this.element.textContent = status.join("");
};
}).call(StatusBar.prototype);
exports.StatusBar = StatusBar;
});

View file

@ -0,0 +1,183 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var dom = require("ace/lib/dom");
var event = require("ace/lib/event");
var Range = require("ace/range").Range;
var tooltipNode;
var TokenTooltip = function(editor) {
if (editor.tokenTooltip)
return;
editor.tokenTooltip = this;
this.editor = editor;
editor.tooltip = tooltipNode || this.$init();
this.update = this.update.bind(this);
this.onMouseMove = this.onMouseMove.bind(this);
this.onMouseOut = this.onMouseOut.bind(this);
event.addListener(editor.renderer.scroller, "mousemove", this.onMouseMove);
event.addListener(editor.renderer.content, "mouseout", this.onMouseOut);
};
(function(){
this.token = {};
this.range = new Range();
this.update = function() {
this.$timer = null;
var r = this.editor.renderer;
if (this.lastT - (r.timeStamp || 0) > 1000) {
r.rect = null;
r.timeStamp = this.lastT;
this.maxHeight = innerHeight;
this.maxWidth = innerWidth;
}
var canvasPos = r.rect || (r.rect = r.scroller.getBoundingClientRect());
var offset = (this.x + r.scrollLeft - canvasPos.left - r.$padding) / r.characterWidth;
var row = Math.floor((this.y + r.scrollTop - canvasPos.top) / r.lineHeight);
var col = Math.round(offset);
var screenPos = {row: row, column: col, side: offset - col > 0 ? 1 : -1};
var session = this.editor.session;
var docPos = session.screenToDocumentPosition(screenPos.row, screenPos.column);
var token = session.getTokenAt(docPos.row, docPos.column);
if (!token && !session.getLine(docPos.row)) {
token = {
type: "",
value: "",
state: session.bgTokenizer.getState(0)
};
}
if (!token) {
session.removeMarker(this.marker);
tooltipNode.style.display = "none";
this.isOpen = false;
return;
}
if (!this.isOpen) {
tooltipNode.style.display = "";
this.isOpen = true;
}
var tokenText = token.type;
if (token.state)
tokenText += "|" + token.state;
if (token.merge)
tokenText += "\n merge";
if (token.stateTransitions)
tokenText += "\n " + token.stateTransitions.join("\n ");
if (this.tokenText != tokenText) {
tooltipNode.textContent = tokenText;
this.tooltipWidth = tooltipNode.offsetWidth;
this.tooltipHeight = tooltipNode.offsetHeight;
this.tokenText = tokenText;
}
this.updateTooltipPosition(this.x, this.y);
this.token = token;
session.removeMarker(this.marker);
this.range = new Range(docPos.row, token.start, docPos.row, token.start + token.value.length);
this.marker = session.addMarker(this.range, "ace_bracket", "text");
};
this.onMouseMove = function(e) {
this.x = e.clientX;
this.y = e.clientY;
if (this.isOpen) {
this.lastT = e.timeStamp;
this.updateTooltipPosition(this.x, this.y);
}
if (!this.$timer)
this.$timer = setTimeout(this.update, 100);
};
this.onMouseOut = function(e) {
var t = e && e.relatedTarget;
var ct = e && e.currentTarget;
while(t && (t = t.parentNode)) {
if (t == ct)
return;
}
tooltipNode.style.display = "none";
this.editor.session.removeMarker(this.marker);
this.$timer = clearTimeout(this.$timer);
this.isOpen = false;
};
this.updateTooltipPosition = function(x, y) {
var st = tooltipNode.style;
if (x + 10 + this.tooltipWidth > this.maxWidth)
x = innerWidth - this.tooltipWidth - 10;
if (y > innerHeight * 0.75 || y + 20 + this.tooltipHeight > this.maxHeight);
y = y - this.tooltipHeight - 30;
st.left = x + 10 + "px";
st.top = y + 20 + "px";
};
this.$init = function() {
tooltipNode = document.documentElement.appendChild(dom.createElement("div"));
var st = tooltipNode.style;
st.position = "fixed";
st.display = "none";
st.background = "lightyellow";
st.borderRadius = "";
st.border = "1px solid gray";
st.padding = "1px";
st.zIndex = 1000;
st.fontFamily = "monospace";
st.whiteSpace = "pre-line";
return tooltipNode;
};
this.destroy = function() {
this.onMouseOut();
event.removeListener(this.editor.renderer.scroller, "mousemove", this.onMouseMove);
event.removeListener(this.editor.renderer.content, "mouseout", this.onMouseOut);
delete this.editor.tokenTooltip;
};
}).call(TokenTooltip.prototype);
exports.TokenTooltip = TokenTooltip;
});

234
demo/kitchen-sink/util.js Normal file
View file

@ -0,0 +1,234 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var dom = require("ace/lib/dom");
var event = require("ace/lib/event");
var EditSession = require("ace/edit_session").EditSession;
var UndoManager = require("ace/undomanager").UndoManager;
var Renderer = require("ace/virtual_renderer").VirtualRenderer;
var Editor = require("ace/editor").Editor;
var MultiSelect = require("ace/multi_select").MultiSelect;
exports.createSplitEditor = function(el) {
if (typeof(el) == "string")
el = document.getElementById(el);
var e0 = document.createElement("div");
var s = document.createElement("splitter");
var e1 = document.createElement("div");
el.appendChild(e0);
el.appendChild(e1);
el.appendChild(s);
e0.style.position = e1.style.position = s.style.position = "absolute";
el.style.position = "relative";
var split = {$container: el};
split.editor0 = split[0] = new Editor(new Renderer(e0, require("ace/theme/textmate")));
split.editor1 = split[1] = new Editor(new Renderer(e1, require("ace/theme/textmate")));
split.splitter = s;
MultiSelect(split.editor0);
MultiSelect(split.editor1);
s.ratio = 0.5;
split.resize = function resize(){
var height = el.parentNode.clientHeight - el.offsetTop;
var total = el.clientWidth;
var w1 = total * s.ratio;
var w2 = total * (1- s.ratio);
s.style.left = w1 - 1 + "px";
s.style.height = el.style.height = height + "px";
var st0 = split[0].container.style;
var st1 = split[1].container.style;
st0.width = w1 + "px";
st1.width = w2 + "px";
st0.left = 0 + "px";
st1.left = w1 + "px";
st0.top = st1.top = "0px";
st0.height = st1.height = height + "px";
split[0].resize();
split[1].resize();
};
split.onMouseDown = function(e) {
var rect = el.getBoundingClientRect();
var x = e.clientX;
var y = e.clientY;
var button = e.button;
if (button !== 0) {
return;
}
var onMouseMove = function(e) {
x = e.clientX;
y = e.clientY;
};
var onResizeEnd = function(e) {
clearInterval(timerId);
};
var onResizeInterval = function() {
s.ratio = (x - rect.left) / rect.width
split.resize()
};
event.capture(s, onMouseMove, onResizeEnd);
var timerId = setInterval(onResizeInterval, 40);
return e.preventDefault();
};
event.addListener(s, "mousedown", split.onMouseDown);
event.addListener(window, "resize", split.resize);
split.resize();
return split;
};
/***************************/
exports.stripLeadingComments = function(str) {
if(str.slice(0,2)=='/*') {
var j = str.indexOf('*/')+2;
str = str.substr(j);
}
return str.trim() + "\n";
};
/***************************/
exports.saveOption = function(el, val) {
if (!el.onchange && !el.onclick)
return;
if ("checked" in el) {
if (val !== undefined)
el.checked = val;
localStorage && localStorage.setItem(el.id, el.checked ? 1 : 0);
}
else {
if (val !== undefined)
el.value = val;
localStorage && localStorage.setItem(el.id, el.value);
}
};
exports.bindCheckbox = function(id, callback, noInit) {
if (typeof id == "string")
var el = document.getElementById(id);
else {
var el = id;
id = el.id;
}
var el = document.getElementById(id);
if (localStorage && localStorage.getItem(id))
el.checked = localStorage.getItem(id) == "1";
var onCheck = function() {
callback(!!el.checked);
exports.saveOption(el);
};
el.onclick = onCheck;
noInit || onCheck();
};
exports.bindDropdown = function(id, callback, noInit) {
if (typeof id == "string")
var el = document.getElementById(id);
else {
var el = id;
id = el.id;
}
if (localStorage && localStorage.getItem(id))
el.value = localStorage.getItem(id);
var onChange = function() {
callback(el.value);
exports.saveOption(el);
};
el.onchange = onChange;
noInit || onChange();
};
exports.fillDropdown = function(el, values) {
if (typeof el == "string")
el = document.getElementById(el);
dropdown(values).forEach(function(e) {
el.appendChild(e);
});
};
function elt(tag, attributes, content) {
var el = dom.createElement(tag);
if (typeof content == "string") {
el.textContent = content;
} else if (content) {
content.forEach(function(ch) {
el.appendChild(ch);
});
}
for (var i in attributes)
el.setAttribute(i, attributes[i]);
return el;
}
function optgroup(values) {
return values.map(function(item) {
if (typeof item == "string")
item = {name: item, desc: item};
return elt("option", {value: item.name}, item.desc);
});
}
function dropdown(values) {
if (Array.isArray(values))
return optgroup(values);
return Object.keys(values).map(function(i) {
return elt("optgroup", {"label": i}, optgroup(values[i]));
});
}
});

View file

@ -1,5 +1,4 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<!DOCTYPE html>
<html lang="en">
<head>
@ -7,11 +6,9 @@
<title>Ace Kitchen Sink</title>
<meta name="author" content="Fabian Jakobs">
<!--
Ace
version %version%
commit %commit%
-->
<!--DEVEL-->
@ -25,9 +22,13 @@
PACKAGE-->
</head>
<body>
<div id="optionsPanel" style="position:absolute;height:100%">
<a href="http://ajaxorg.github.com/ace/" >
<img id="logo" src="demo/kitchen-sink/logo.png">
</a>
<div style="position: absolute; overflow: hidden; top:80px; bottom:0">
<div style="width: 120%; height:100%; overflow-y: scroll">
<table id="controls">
<tr>
<td>
@ -236,9 +237,20 @@
<input type="checkbox" id="fade_fold_widgets">
</td>
</tr>
<tr>
<td >
<label for="highlight_token">Show token info</label>
</td>
<td>
<input type="checkbox" id="highlight_token">
</td>
</tr>
</table>
</td></tr>
</table>
</div>
</div>
</div>
<div id="editor"></div>

View file

@ -4,6 +4,8 @@ function foo(items, nada) {
} // Real Tab.
}
regexp = /p|p/ // ends here
r = /d{1,2}?f{e}++r*?\d+?[]r[^r-o\f\f[\f]?r{7}+r\{7}+rr--rr$^(?:d|s)(?=a|)(?!y)[]|$?|^*/ o
a=/a/ jk = / / / / /
/************************************/

View file

@ -20,9 +20,9 @@
{
"state": "start",
"data": [
[ "keyword", "using" ],
[ "keyword.control", "using" ],
[ "text", " " ],
[ "keyword", "namespace" ],
[ "keyword.operator", "namespace" ],
[ "text", " " ],
[ "identifier", "std" ],
[ "punctuation.operator", ";" ]
@ -35,9 +35,9 @@
{
"state": "start",
"data": [
[ "keyword", "int" ],
[ "storage.type", "int" ],
[ "text", " " ],
[ "identifier", "main" ],
[ "entity.name.function", "main" ],
[ "text", " " ],
[ "paren.lparen", "(" ],
[ "paren.rparen", ")" ]
@ -53,7 +53,7 @@
"state": "start",
"data": [
[ "text", " " ],
[ "keyword", "int" ],
[ "storage.type", "int" ],
[ "text", " " ],
[ "identifier", "a" ],
[ "punctuation.operator", "," ],
@ -109,7 +109,7 @@
"state": "start",
"data": [
[ "text", " " ],
[ "keyword", "return" ],
[ "keyword.control", "return" ],
[ "text", " " ],
[ "constant.numeric", "0" ],
[ "punctuation.operator", ";" ]

View file

@ -41,7 +41,7 @@
"data": [
[ "keyword", "void" ],
[ "text", " " ],
[ "identifier", "main" ],
[ "entity.name.function", "main" ],
[ "paren.lparen", "(" ],
[ "paren.rparen", ")" ],
[ "text", " " ],

View file

@ -81,6 +81,24 @@
"state": "start",
"data": []
},
{
"state": "start",
"data": [
[ "identifier", "regexp" ],
[ "text", " " ],
[ "keyword.operator", "=" ],
[ "text", " " ],
[ "string.regexp", "/p" ],
[ "constant.language.escape", "|" ],
[ "string.regexp", "p/" ],
[ "text", " " ],
[ "comment", "// ends here" ]
]
},
{
"state": "start",
"data": []
},
{
"state": "start",
"data": [
@ -118,10 +136,13 @@
[ "constant.language.escape", "$" ],
[ "constant.language.escape", "^" ],
[ "constant.language.escape", "(?:" ],
[ "string.regexp", "d|s" ],
[ "string.regexp", "d" ],
[ "constant.language.escape", "|" ],
[ "string.regexp", "s" ],
[ "constant.language.escape", ")" ],
[ "constant.language.escape", "(?=" ],
[ "string.regexp", "a|" ],
[ "string.regexp", "a" ],
[ "constant.language.escape", "|" ],
[ "constant.language.escape", ")" ],
[ "constant.language.escape", "(?!" ],
[ "string.regexp", "y" ],

64
lib/ace/mode/asciidoc.js Normal file
View file

@ -0,0 +1,64 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var Tokenizer = require("../tokenizer").Tokenizer;
var AsciidocHighlightRules = require("./asciidoc_highlight_rules").AsciidocHighlightRules;
var AsciidocFoldMode = require("./folding/asciidoc").FoldMode;
var Mode = function() {
var highlighter = new AsciidocHighlightRules();
this.$tokenizer = new Tokenizer(highlighter.getRules());
this.foldingRules = new AsciidocFoldMode();
};
oop.inherits(Mode, TextMode);
(function() {
this.getNextLineIndent = function(state, line, tab) {
if (state == "listblock") {
var match = /^((?:.+)?)([-+*][ ]+)/.exec(line);
if (match) {
return new Array(match[1].length + 1).join(" ") + match[2];
} else {
return "";
}
} else {
return this.$getIndent(line);
}
};
}).call(Mode.prototype);
exports.Mode = Mode;
});

View file

@ -0,0 +1,233 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
var AsciidocHighlightRules = function() {
var identifierRe = "[a-zA-Z\u00a1-\uffff]+\\b";
this.$rules = {
"start": [
{token: "empty", regex: /$/},
{token: "literal", regex: /^\.{4,}\s*$/, next: "listingBlock"},
{token: "literal", regex: /^-{4,}\s*$/, next: "literalBlock"},
{token: "string", regex: /^\+{4,}\s*$/, next: "passthroughBlock"},
{token: "keyword", regex: /^={4,}\s*$/},
{token: "text", regex: /^\s*$/},
// immediately return to the start mode without matching anything
{token: "empty", regex: "", next: "dissallowDelimitedBlock"}
],
"dissallowDelimitedBlock": [
{include: "paragraphEnd"},
{token: "comment", regex: '^//.+$'},
{token: "keyword", regex: "^(?:NOTE|TIP|IMPORTANT|WARNING|CAUTION):"},
{include: "listStart"},
{token: "literal", regex: /^\s+.+$/, next: "indentedBlock"},
{token: "empty", regex: "", next: "text"}
],
"paragraphEnd": [
{token: "doc.comment", regex: /^\/{4,}\s*$/, next: "commentBlock"},
{token: "tableBlock", regex: /^\s*[|!]=+\s*$/, next: "tableBlock"},
// open block, ruller
{token: "keyword", regex: /^(?:--|''')\s*$/, next: "start"},
{token: "option", regex: /^\[.*\]\s*$/, next: "start"},
{token: "pageBreak", regex: /^>{3,}$/, next: "start"},
{token: "literal", regex: /^\.{4,}\s*$/, next: "listingBlock"},
{token: "titleUnderline", regex: /^(?:={2,}|-{2,}|~{2,}|\^{2,}|\+{2,})\s*$/, next: "start"},
{token: "singleLineTitle", regex: /^={1,5}\s+\S.*$/, next: "start"},
{token: "otherBlock", regex: /^(?:\*{2,}|_{2,})\s*$/, next: "start"},
// .optional title
{token: "optionalTitle", regex: /^\.[^.\s].+$/, next: "start"}
],
"listStart": [
{token: "keyword", regex: /^\s*(?:\d+\.|[a-zA-Z]\.|[ixvmIXVM]+\)|\*{1,5}|-|\.{1,5})\s/, next: "listText"},
{token: "meta.tag", regex: /^.+(?::{2,4}|;;)(?: |$)/, next: "listText"},
{token: "support.function.list.callout", regex: /^(?:<\d+>|\d+>|>) /, next: "text"},
// continuation
{token: "keyword", regex: /^\+\s*$/, next: "start"}
],
"text": [
{token: ["link", "variable.language"], regex: /((?:https?:\/\/|ftp:\/\/|file:\/\/|mailto:|callto:)[^\s\[]+)(\[.*?\])/},
{token: "link", regex: /(?:https?:\/\/|ftp:\/\/|file:\/\/|mailto:|callto:)[^\s\[]+/},
{token: "link", regex: /\b[\w\.\/\-]+@[\w\.\/\-]+\b/},
{include: "macros"},
{include: "paragraphEnd"},
{token: "literal", regex:/\+{3,}/, next:"smallPassthrough"},
{token: "escape", regex: /\((?:C|TM|R)\)|\.{3}|->|<-|=>|<=|&#(?:\d+|x[a-fA-F\d]+);|(?: |^)--(?=\s+\S)/},
{token: "escape", regex: /\\[_*'`+#]|\\{2}[_*'`+#]{2}/},
{token: "keyword", regex: /\s\+$/},
// any word
{token: "text", regex: identifierRe},
{token: ["keyword", "string", "keyword"],
regex: /(<<[\w\d\-$]+,)(.*?)(>>|$)/, merge: true},
{token: "keyword", regex: /<<[\w\d\-$]+,?|>>/, merge: true},
{token: "constant.character", regex: /\({2,3}.*?\){2,3}/, merge: true},
// Anchor
{token: "keyword", regex: /\[\[.+?\]\]/},
// bibliography
{token: "support", regex: /^\[{3}[\w\d =\-]+\]{3}/},
{include: "quotes"},
// text block end
{token: "empty", regex: /^\s*$/, next: "start"}
],
"listText": [
{include: "listStart"},
{include: "text"}
],
"indentedBlock": [
{token: "literal", regex: /^[\s\w].+$/, next: "indentedBlock"},
{token: "literal", regex: "", next: "start"}
],
"listingBlock": [
{token: "literal", regex: /^\.{4,}\s*$/, next: "dissallowDelimitedBlock"},
{token: "constant.numeric", regex: '<\\d+>'},
{token: "literal", regex: '[^<]+', merge: true},
{token: "literal", regex: '<', merge: true}
],
"literalBlock": [
{token: "literal", regex: /^-{4,}\s*$/, next: "dissallowDelimitedBlock"},
{token: "constant.numeric", regex: '<\\d+>'},
{token: "literal", regex: '[^<]+', merge: true},
{token: "literal", regex: '<', merge: true}
],
"passthroughBlock": [
{token: "literal", regex: /^\+{4,}\s*$/, next: "dissallowDelimitedBlock"},
{token: "literal", regex: identifierRe + "|\\d+", merge: true},
{include: "macros"},
{token: "literal", regex: ".", merge: true}
],
"smallPassthrough": [
{token: "literal", regex: /[+]{3,}/, next: "dissallowDelimitedBlock"},
{token: "literal", regex: /^\s*$/, next: "dissallowDelimitedBlock", merge: true},
{token: "literal", regex: identifierRe + "|\\d+", merge: true},
{include: "macros"}
],
"commentBlock": [
{token: "doc.comment", regex: /^\/{4,}\s*$/, next: "dissallowDelimitedBlock"},
{token: "doc.comment", regex: '^.*$'}
],
"tableBlock": [
{token: "tableBlock", regex: /^\s*\|={3,}\s*$/, next: "dissallowDelimitedBlock"},
{token: "tableBlock", regex: /^\s*!={3,}\s*$/, next: "innerTableBlock"},
{token: "tableBlock", regex: /\|/},
{include: "text", noEscape: true}
],
"innerTableBlock": [
{token: "tableBlock", regex: /^\s*!={3,}\s*$/, next: "tableBlock"},
{token: "tableBlock", regex: /^\s*|={3,}\s*$/, next: "dissallowDelimitedBlock"},
{token: "tableBlock", regex: /\!/}
],
"macros": [
{token: "macro", regex: /{[\w\-$]+}/},
{token: ["text", "string", "text", "constant.character", "text"], regex: /({)([\w\-$]+)(:)?(.+)?(})/},
{token: ["markup.list.macro", "keyword", "string"], regex: /([\w\.\/\-]+::?)([^\s\[]+)(\[.*?\])?/},
{token: ["markup.list.macro", "keyword"], regex: /([\w\.\/\-]+::?)(\[.*?\])/},
{token: "keyword", regex: /^:.+?:(?= |$)/}
],
"quotes": [
{token: "string.italic", regex: /__[^_\s].*?__/},
{token: "string.italic", regex: quoteRule("_")},
{token: "keyword.bold", regex: /\*\*[^*\s].*?\*\*/},
{token: "keyword.bold", regex: quoteRule("\\*")},
{token: "literal", regex: quoteRule("\\+")},
{token: "literal", regex: /\+\+[^+\s].*?\+\+/},
{token: "literal", regex: /\$\$.+?\$\$/},
{token: "literal", regex: quoteRule("`")},
{token: "keyword", regex: quoteRule("^")},
{token: "keyword", regex: quoteRule("~")},
{token: "keyword", regex: /##?/},
{token: "keyword", regex: /(?:\B|^)``|\b''/}
]
};
function quoteRule(ch) {
var prefix = /\w/.test(ch) ? "\\b" : "(?:\\B|^)";
return prefix + ch + "[^" + ch + "].*?" + ch + "(?![\\w*])";
}
//addQuoteBlock("text")
var tokenMap = {
macro: "constant.character",
tableBlock: "doc.comment",
titleUnderline: "markup.heading",
singleLineTitle: "markup.heading",
pageBreak: "string",
option: "string.regexp",
otherBlock: "markup.list",
literal: "support.function",
optionalTitle: "constant.numeric",
escape: "constant.language.escape",
link: "markup.underline.list"
};
for (var state in this.$rules) {
var stateRules = this.$rules[state];
for (var i = stateRules.length; i--; ) {
var rule = stateRules[i];
if (rule.include || typeof rule == "string") {
var args = [i, 1].concat(this.$rules[rule.include || rule]);
if (rule.noEscape) {
args = args.filter(function(x) {
return !x.next;
});
}
stateRules.splice.apply(stateRules, args);
} else if (rule.token in tokenMap) {
rule.token = tokenMap[rule.token];
}
}
}
};
oop.inherits(AsciidocHighlightRules, TextHighlightRules);
exports.AsciidocHighlightRules = AsciidocHighlightRules;
});

View file

@ -143,7 +143,7 @@ define(function(require, exports, module) {
next : "heregex"
}, {
token : "string.regex",
regex : "/(?!\\s)[^[/\\n\\\\]*(?: (?:\\\\.|\\[[^\\]\\n\\\\]*(?:\\\\.[^\\]\\n\\\\]*)*\\])[^[/\\n\\\\]*)*/[imgy]{0,4}(?!\\w)"
regex : /(?:\/(?![\s=])[^[\/\n\\]*(?:(?:\\[\s\S]|\[[^\]\n\\]*(?:\\[\s\S][^\]\n\\]*)*])[^[\/\n\\]*)*\/)(?:[imgy]{0,4})(?!\w)/
}, {
token : "comment",
merge : true,

View file

@ -54,7 +54,13 @@ module.exports = {
assert.equal(tokens.length, 1);
assert.equal(tokens[0].type, "keyword");
},
"test: tokenize regexp": function() {
var tokens = this.tokenizer.getLineTokens('/"[a]/', "start").tokens;
assert.equal(tokens.length, 1);
assert.equal(tokens[0].type, "string.regex");
},
"test: tokenize function: 'foo = ({args}) ->'": function() {
var tokens = this.tokenizer.getLineTokens("foo = ({args}) ->", "start").tokens;
var correct = [

View file

@ -0,0 +1,142 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
"use strict";
var oop = require("../../lib/oop");
var BaseFoldMode = require("./fold_mode").FoldMode;
var Range = require("../../range").Range;
var FoldMode = exports.FoldMode = function() {};
oop.inherits(FoldMode, BaseFoldMode);
(function() {
this.foldingStartMarker = /^(?:\|={10,}|[\.\/=\-~^+]{4,}|={1,5} )/;
this.singleLineHeadingRe = /^={1,5}(?=\s+\S)/;
this.getFoldWidget = function(session, foldStyle, row) {
var line = session.getLine(row);
if (!this.foldingStartMarker.test(line))
return ""
if (line[0] == "=") {
if (this.singleLineHeadingRe.test(line))
return "start";
if (session.getLine(row - 1).length != session.getLine(row).length)
return "";
return "start";
}
if (session.bgTokenizer.getState(row) == "dissallowDelimitedBlock")
return "end";
return "start";
};
this.getFoldWidgetRange = function(session, foldStyle, row) {
var line = session.getLine(row);
var startColumn = line.length;
var maxRow = session.getLength();
var startRow = row;
var endRow = row;
if (!line.match(this.foldingStartMarker))
return;
var token;
function getTokenType(row) {
token = session.getTokens(row)[0];
return token && token.type;
}
var levels = ["=","-","~","^","+"];
var heading = "markup.heading";
var singleLineHeadingRe = this.singleLineHeadingRe;
function getLevel() {
var match = token.value.match(singleLineHeadingRe);
if (match)
return match[0].length;
var level = levels.indexOf(token.value[0]) + 1;
if (level == 1) {
if (session.getLine(row - 1).length != session.getLine(row).length)
return Infinity;
}
return level;
}
if (getTokenType(row) == heading) {
var startHeadingLevel = getLevel();
while (++row < maxRow) {
if (getTokenType(row) != heading)
continue;
var level = getLevel();
if (level <= startHeadingLevel)
break;
}
var isSingleLineHeading = token && token.value.match(this.singleLineHeadingRe);
endRow = isSingleLineHeading ? row - 1 : row - 2;
if (endRow > startRow) {
while (endRow > startRow && (!getTokenType(endRow) || token.value[0] == "["))
endRow--;
}
if (endRow > startRow) {
var endColumn = session.getLine(endRow).length;
return new Range(startRow, startColumn, endRow, endColumn);
}
} else {
var state = session.bgTokenizer.getState(row);
if (state == "dissallowDelimitedBlock") {
while (row -- > 0) {
if (session.bgTokenizer.getState(row).lastIndexOf("Block") == -1)
break;
}
endRow = row + 1;
if (endRow < startRow) {
var endColumn = session.getLine(row).length;
return new Range(endRow, 5, startRow, startColumn - 5);
}
} else {
while (++row < maxRow) {
if (session.bgTokenizer.getState(row) == "dissallowDelimitedBlock")
break;
}
endRow = row;
if (endRow > startRow) {
var endColumn = session.getLine(row).length;
return new Range(startRow, 5, endRow, endColumn - 5);
}
}
}
};
}).call(FoldMode.prototype);
});

View file

@ -32,7 +32,6 @@ define(function(require, exports, module) {
"use strict";
var oop = require("../lib/oop");
var unicode = require("../unicode");
var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
@ -64,7 +63,7 @@ var JavaScriptHighlightRules = function() {
"constant.language":
"null|Infinity|NaN|undefined",
"support.function":
"alert",
"alert"
}, "identifier");
// keywords which can be followed by regular expressions
@ -256,14 +255,14 @@ var JavaScriptHighlightRules = function() {
}, {
// invalid operators
token : "invalid",
regex: /\{\d+,?(?:\d+)?}[+*]|[+*^$?][+*]|\?\?/ // |[^$][?]
regex: /\{\d+,?(?:\d+)?}[+*]|[+*$^?][+*]|[$^][?]|\?{3,}/
}, {
// operators
token : "constant.language.escape",
regex: /\(\?[:=!]|\)|\{\d+,?(?:\d+)?}|[+*]\?|[(|)$^+*?]/
regex: /\(\?[:=!]|\)|{\d+,?(?:\d+)?}|{,\d+}|[+*]\?|[(|)$^+*?]/
}, {
token: "string.regexp",
regex: /{|[^\[\\{()$^+*?\/]+/,
regex: /{|[^{\[\/\\(|)$^+*?]+/,
merge: true
}, {
token: "constant.language.escape",

View file

@ -36,14 +36,9 @@ var TextMode = require("./text").Mode;
var Tokenizer = require("../tokenizer").Tokenizer;
var JspHighlightRules = require("./jsp_highlight_rules").JspHighlightRules;
var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
var Range = require("../range").Range;
var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
var CStyleFoldMode = require("./folding/cstyle").FoldMode;
var JavaScriptMode = require("./javascript").Mode;
var CssMode = require("./css").Mode;
var Mode = function() {
var highlighter = new JspHighlightRules();
this.$tokenizer = new Tokenizer(highlighter.getRules());

View file

@ -44,14 +44,23 @@ var YamlHighlightRules = function() {
token : "comment",
regex : "#.*$"
}, {
token : "comment",
regex : "^---"
token : "list.markup",
regex : /^(?:-{3}|\.{3})\s*(?=#|$)/
}, {
token : "list.markup",
regex : /^\s*[\-?](?:$|\s)/
}, {
token: "variable",
token: "constant",
regex: "!![\\w//]+"
}, {
token: "constant.language",
regex: "[&\\*][a-zA-Z0-9-_]+"
}, {
token: ["identifier", "text"],
regex: "(\\w+\\s*:)(\\w*)"
token: ["meta.tag", "keyword"],
regex: /^(\s*\w.*?)(\:(?:\s+|$))/
},{
token: ["meta.tag", "keyword"],
regex: /(\w+?)(\s*\:(?:\s+|$))/
}, {
token : "keyword.operator",
regex : "<<\\w*:\\w*"
@ -71,10 +80,13 @@ var YamlHighlightRules = function() {
regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
}, {
token : "constant.numeric", // float
regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
regex : /[+\-]?[\d_]+(?:(?:\.[\d_]*)?(?:[eE][+\-]?[\d_]+)?)?\b/
}, {
token : "constant.numeric", // other number
regex : /[+\-]?\.inf\b|NaN\b|0x[\dA-Fa-f_]+|0b[10_]+/
}, {
token : "constant.language.boolean",
regex : "(?:true|false|yes|no)\\b"
regex : "(?:true|false|TRUE|FALSE|True|False|yes|no)\\b"
}, {
token : "invalid.illegal", // comments are not allowed
regex : "\\/\\/.*$"
@ -87,6 +99,9 @@ var YamlHighlightRules = function() {
}, {
token : "text",
regex : "\\s+"
}, {
token : "text",
regex : "\\w+"
}
],
"qqstring" : [
@ -99,7 +114,7 @@ var YamlHighlightRules = function() {
merge : true,
regex : '.+'
}
]}
]};
};

205
lib/ace/tokenizer_dev.js Normal file
View file

@ -0,0 +1,205 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
/**
* version of Tokenizer with additional logging
* and infinite loop checks
* can be used for developing/testing new modes
**/
var Tokenizer = function(rules, flag) {
flag = flag ? "g" + flag : "g";
this.rules = rules;
this.regExps = {};
this.matchMappings = {};
for ( var key in this.rules) {
var rule = this.rules[key];
var state = rule;
var ruleRegExps = [];
var matchTotal = 0;
var mapping = this.matchMappings[key] = {};
for ( var i = 0; i < state.length; i++) {
if (state[i].regex instanceof RegExp)
state[i].regex = state[i].regex.toString().slice(1, -1);
// Count number of matching groups. 2 extra groups from the full match
// And the catch-all on the end (used to force a match);
var matchcount = new RegExp("(?:(" + state[i].regex + ")|(.))").exec("a").length - 2;
// Replace any backreferences and offset appropriately.
var adjustedregex = state[i].regex.replace(/\\([0-9]+)/g, function (match, digit) {
return "\\" + (parseInt(digit, 10) + matchTotal + 1);
});
if (matchcount > 1 && state[i].token.length !== matchcount-1)
throw new Error("For " + state[i].regex + " the matching groups and length of the token array don't match (rule #" + i + " of state " + key + ")");
mapping[matchTotal] = {
rule: i,
len: matchcount
};
matchTotal += matchcount;
ruleRegExps.push(adjustedregex);
}
this.regExps[key] = new RegExp("(?:(" + ruleRegExps.join(")|(") + ")|(.))", flag);
}
};
(function() {
this.getLineTokens = function(line, startState) {
var currentState = startState || "start";
var state = this.rules[currentState];
var mapping = this.matchMappings[currentState];
var re = this.regExps[currentState];
re.lastIndex = 0;
var match, tokens = [];
var lastIndex = 0;
var stateTransitions = [];
function onStateChange() {
stateTransitions.push(startState+"@"+lastIndex);
}
function initState() {
onStateChange();
stateTransitions = [];
onStateChange();
}
var token = {
type: null,
value: "",
state: currentState,
};
initState();
var maxRecur = 10000;
while (match = re.exec(line)) {
var type = "default.text";
var rule = null;
var value = [match[0]];
for (var i = 0; i < match.length-2; i++) {
if (match[i + 1] === undefined)
continue;
if (!maxRecur--) {
throw "infinite" + mapping[i].rule + currentState
}
rule = state[mapping[i].rule];
if (mapping[i].len > 1)
value = match.slice(i+2, i+1+mapping[i].len);
// compute token type
if (typeof rule.token == "function")
type = rule.token.apply(this, value);
else
type = rule.token;
if (rule.next) {
currentState = rule.next;
state = this.rules[currentState];
mapping = this.matchMappings[currentState];
lastIndex = re.lastIndex;
re = this.regExps[currentState];
if (re === undefined) {
throw new Error("You indicated a state of " + rule.next + " to go to, but it doesn't exist!");
}
re.lastIndex = lastIndex;
onStateChange();
}
break;
}
if (value[0]) {
if (typeof type == "string") {
value = [value.join("")];
type = [type];
}
for (var i = 0; i < value.length; i++) {
if (!value[i])
continue;
var mergeable = (!rule || rule.merge || type[i] === "text") && token.type === type[i];
if (false && mergeable) {
token.value += value[i];
} else {
if (token.type) {
token.stateTransitions = stateTransitions;
tokens.push(token);
initState()
}
token = {
type: type[i],
value: value[i],
state: currentState,
mergeable: mergeable
};
}
}
}
if (lastIndex == line.length)
break;
lastIndex = re.lastIndex;
}
if (token.type) {
token.stateTransitions = stateTransitions;
tokens.push(token);
}
return {
tokens : tokens,
state : currentState
};
};
}).call(Tokenizer.prototype);
exports.Tokenizer = Tokenizer;
});

64
tool/mode_creator.html Normal file
View file

@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="en">
<head>
<base href="../">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Ace Mode Creator</title>
<meta name="author" content="Harutyun Amirjanyan">
<style type="text/css" >
body, html {
overflow: hidden;
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#header {
border-bottom: solid 1px;
}
.separator-h {
padding: 0 20px;
}
</style>
</head>
<body>
<div id="header">
<label for="modeEl">mode</label>
<select id="modeEl" size="1"></select>
<span id="tablist"></span>
<span class="separator-h"></span>
<label for="autorunEl">live preview</label>
<input type="checkbox" label="autorun" id="autorunEl" checked>
<div style='float:right'>
<label for="themeEl">Theme</label>
<select id="themeEl" size="1" value="textmate"></select>
<span class="separator-h"></span>
<label for="doc">Document</label>
<select id="doc" size="1"></select>
</div>
</div>
<div id="editor"></div>
<script type="text/javascript">
var require = {
baseUrl: window.location.protocol + "//" + window.location.host + window.location.pathname.split("/").slice(0, -1).join("/"),
paths: {
ace: "../lib/ace",
demo: "../demo",
tool: "../tool"
}
};
</script>
<script src="./demo/kitchen-sink/require.js" data-main="mode_creator"></script>
</body>
</html>

140
tool/mode_creator.js Normal file
View file

@ -0,0 +1,140 @@
define(function(require, exports, module) {
/** creates globals intentionally to make things easily accessible from console **/
var net = require("ace/lib/net");
var Range = require("ace/range").Range;
var util = require("demo/kitchen-sink/util");
var layout = require("demo/kitchen-sink/layout");
var modelist = require("demo/kitchen-sink/modelist");
var doclist = require("demo/kitchen-sink/doclist");
var TokenTooltip = require("demo/kitchen-sink/token_tooltip").TokenTooltip;
var EditSession = require("ace/edit_session").EditSession;
var UndoManager = require("ace/undomanager").UndoManager;
var DebugTokenizer = require("ace/tokenizer_dev").Tokenizer;
// createEditor
var splitEditor = window.splitEditor = util.createSplitEditor("editor");
var editor1 = window.editor1 = splitEditor.editor0;
var editor2 = window.editor2 = splitEditor.editor1;
new TokenTooltip(editor2);
var timeout = null;
var schedule = function() {
if (timeout != null) {
clearTimeout(timeout);
}
timeout = setTimeout(run, 800);
};
var setAutorunEnabled = function(val) {
if (val)
editor1.on('change', schedule);
else
editor1.removeEventListener('change', schedule);
};
util.bindCheckbox("autorunEl", setAutorunEnabled);
var docEl = document.getElementById("doc");
util.fillDropdown(docEl, doclist.docs);
util.bindDropdown("doc", function(value) {
doclist.loadDoc(value, function(session) {
if (session) {
editor2.setSession(session);
}
});
});
var modeEl = document.getElementById("modeEl");
util.fillDropdown(modeEl, modelist.modes);
var modeSessions = {};
util.bindDropdown(modeEl, function(value) {
if (modeSessions[value]) {
editor1.setSession(modeSessions[value]);
schedule();
return;
}
var hp = "./lib/ace/mode/" + value + "_highlight_rules.js";
net.get(hp, function(text) {
text = util.stripLeadingComments(text);
var session = new EditSession(text);
session.setUndoManager(new UndoManager());
modeSessions[value] = session;
session.setMode("ace/mode/javascript");
editor1.setSession(modeSessions[value]);
schedule();
});
});
util.fillDropdown("themeEl", {
bright: [
"chrome", "clouds", "crimson_editor", "dawn", "dreamweaver", "eclipse", "github",
"solarized_light", "textmate", "tomorrow", "xcode"],
dark: [ "clouds_midnight", "cobalt", "idle_fingers", "kr_theme", "merbivore", "merbivore_soft",
"mono_industrial", "monokai", "pastel_on_dark", "solarized_dark", "tomorrow_night",
"tomorrow_night_blue", "tomorrow_night_bright", "tomorrow_night_eighties", "twilight", "vibrant_ink"]
});
util.bindDropdown("themeEl", function(value) {
if (!value)
return;
editor1.setTheme("ace/theme/" + value);
editor2.setTheme("ace/theme/" + value);
});
function getDeps(src, path) {
var deps = [];
src.replace(/require\((['"])(.*?)\1/g, function(a,b,c){
if (c[0] == ".") {
var base = path.split("/");
c.split("/").forEach(function(part) {
if (part == ".") {
base.pop();
} else if (part == "..") {
base.pop();
base.pop();
} else {
base.push(part);
}
});
c = base.join("/");
}
deps.push('"' + c + '"');
});
return deps;
}
function run() {
var src = editor1.getValue();
var path = "ace/mode/new";
var deps = getDeps(src, path);
src = src.replace("define(", 'define("' + path +'", ["require","exports","module",' + deps +'],');
src += ';require(["ace/mode/new"], continueRun, function(e){console.log(e);require.undef("ace/mode/new")})';
try {
eval(src);
} catch(e) {
console.log(e);
}
}
var continueRun = function(rules) {
rules = rules[Object.keys(rules)[0]];
var Tokenizer = DebugTokenizer;
var tk = new Tokenizer(new rules().getRules());
editor2.session.bgTokenizer.setTokenizer(tk);
editor2.renderer.updateText();
};
editor1.commands.bindKey("ctrl-Return", run);
});