Added Python syntax support

This commit is contained in:
Colin Gourlay 2010-12-09 06:34:56 +08:00 • committed by Fabian Jakobs
commit 921bbe8949
2 changed files with 234 additions and 0 deletions

87
lib/ace/mode/Python.js Normal file
View file

@ -0,0 +1,87 @@
/**
* Ajax.org Code Editor (ACE)
*
* @copyright 2010, Ajax.org Services B.V.
* @license LGPLv3 <http://www.gnu.org/licenses/lgpl-3.0.txt>
* @author Fabian Jakobs <fabian AT ajax DOT org>
*/
require.def("ace/mode/Python",
[
"ace/lib/oop",
"ace/mode/Text",
"ace/Tokenizer",
"ace/mode/PythonHighlightRules",
"ace/mode/MatchingBraceOutdent",
"ace/Range"
], function(oop, TextMode, Tokenizer, PythonHighlightRules, MatchingBraceOutdent, Range) {
var Python = function() {
this.$tokenizer = new Tokenizer(new PythonHighlightRules().getRules());
this.$outdent = new MatchingBraceOutdent();
};
oop.inherits(Python, TextMode);
(function() {
this.toggleCommentLines = function(state, doc, range) {
var outdent = true;
var outentedRows = [];
var re = /^(\s*)#/;
for (var i=range.start.row; i<= range.end.row; i++) {
if (!re.test(doc.getLine(i))) {
outdent = false;
break;
}
}
if (outdent) {
var deleteRange = new Range(0, 0, 0, 0);
for (var i=range.start.row; i<= range.end.row; i++)
{
var line = doc.getLine(i).replace(re, "$1");
deleteRange.start.row = i;
deleteRange.end.row = i;
deleteRange.end.column = line.length + 2;
doc.replace(deleteRange, line);
}
return -2;
}
else {
return doc.indentRows(range, "#");
}
};
this.getNextLineIndent = function(state, line, tab) {
var indent = this.$getIndent(line);
var tokenizedLine = this.$tokenizer.getLineTokens(line, state);
var tokens = tokenizedLine.tokens;
var endState = tokenizedLine.state;
if (tokens.length && tokens[tokens.length-1].type == "comment") {
return indent;
}
if (state == "start") {
var match = line.match(/^.*[\{\(\[\:]\s*$/);
if (match) {
indent += tab;
}
}
return indent;
};
this.checkOutdent = function(state, line, input) {
return this.$outdent.checkOutdent(line, input);
};
this.autoOutdent = function(state, doc, row) {
return this.$outdent.autoOutdent(doc, row);
};
}).call(Python.prototype);
return Python;
});

View file

@ -0,0 +1,147 @@
/**
* Ajax.org Code Editor (ACE)
*
* @copyright 2010, Ajax.org Services B.V.
* @license LGPLv3 <http://www.gnu.org/licenses/lgpl-3.0.txt>
* @author Fabian Jakobs <fabian AT ajax DOT org>
*
* TODO: python delimiters
*/
require.def("ace/mode/PythonHighlightRules",
[
"ace/lib/oop",
"ace/lib/lang",
"ace/mode/TextHighlightRules"
], function(oop, lang, TextHighlightRules) {
PythonHighlightRules = function() {
var keywords = lang.arrayToMap(
("and|as|assert|break|class|continue|def|del|elif|else|except|exec|" +
"finally|for|from|global|if|import|in|is|lambda|not|or|pass|print|" +
"raise|return|try|while|with|yield").split("|")
);
var builtinConstants = lang.arrayToMap(
("True|False|None|NotImplemented|Ellipsis|__debug__").split("|")
);
var builtinFunctions = lang.arrayToMap(
("abs|divmod|input|open|staticmethod|all|enumerate|int|ord|str|any|" +
"eval|isinstance|pow|sum|basestring|execfile|issubclass|print|super|" +
"binfile|iter|property|tuple|bool|filter|len|range|type|bytearray|" +
"float|list|raw_input|unichr|callable|format|locals|reduce|unicode|" +
"chr|frozenset|long|reload|vars|classmethod|getattr|map|repr|xrange|" +
"cmp|globals|max|reversed|zip|compile|hasattr|memoryview|round|" +
"__import__|complex|hash|min|set|apply|delattr|help|next|setattr|" +
"buffer|dict|hex|object|slice|coerce|dir|id|oct|sorted|intern").split("|")
);
var futureReserved = lang.arrayToMap(
("").split("|")
);
var strPre = "(?:(?:[rubRUB])|(?:[ubUB][rR]))?";
var decimalInteger = "(?:(?:[1-9]\\d*)|(?:0))";
var octInteger = "(?:0[oO]?[0-7]+)";
var hexInteger = "(?:0[xX][\\dA-Fa-f]+)";
var binInteger = "(?:0[bB][01]+)";
var integer = "(?:" + decimalInteger + "|" + octInteger + "|" + hexInteger + "|" + binInteger + ")";
var exponent = "(?:[eE][+-]?\\d+)";
var fraction = "(?:\\.\\d+)";
var intPart = "(?:\\d+)";
var pointFloat = "(?:(?:" + intPart + "?" + fraction + ")|(?:" + intPart + "\\.))";
var exponentFloat = "(?:(?:" + pointFloat + "|" + intPart + ")" + exponent + ")";
var floatNumber = "(?:" + exponentFloat + "|" + pointFloat + ")";
this.$rules = {
"start" : [ {
token : "comment",
regex : "#.*$"
}, {
token : "string", // """ string
regex : strPre + '"{3}(?:(?:.)|(?:^"{3}))*?"{3}'
}, {
token : "string", // multi line """ string start
regex : strPre + '"{3}.*$',
next : "qqstring"
}, {
token : "string", // " string
regex : strPre + '"(?:(?:\\\\.)|(?:[^"\\\\]))*?"'
}, {
token : "string", // ''' string
regex : strPre + "'{3}(?:(?:.)|(?:^'{3}))*?'{3}"
}, {
token : "string", // multi line ''' string start
regex : strPre + "'{3}.*$",
next : "qstring"
}, {
token : "string", // ' string
regex : strPre + "'(?:(?:\\\\.)|(?:[^'\\\\]))*?'"
}, {
token : "constant.numeric", // imaginary
regex : "(?:" + floatNumber + "|\\d+)[jJ]\\b"
}, {
token : "constant.numeric", // float
regex : floatNumber
}, {
token : "constant.numeric", // long integer
regex : integer + "[lL]\\b"
}, {
token : "constant.numeric", // integer
regex : integer + "\\b"
}, {
token : function(value) {
if (keywords[value])
return "keyword";
else if (builtinConstants[value])
return "constant.language";
else if (futureReserved[value])
return "invalid.illegal";
else if (builtinFunctions[value])
return "support.function";
else if (value == "debugger")
return "invalid.deprecated";
else
return "identifier";
},
regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
}, {
token : "keyword.operator",
regex : "\\+|\\-|\\*|\\*\\*|\\/|\\/\\/|%|<<|>>|&|\\||\\^|~|<|>|<=|=>|==|!=|<>|="
}, {
token : "lparen",
regex : "[\\[\\(\\{]"
}, {
token : "rparen",
regex : "[\\]\\)\\}]"
}, {
token : "text",
regex : "\\s+"
} ],
"qqstring" : [ {
token : "string", // multi line """ string end
regex : '(?:^"{3})*?"{3}',
next : "start"
}, {
token : "string",
regex : '.+'
} ],
"qstring" : [ {
token : "string", // multi line ''' string end
regex : "(?:^'{3})*?'{3}",
next : "start"
}, {
token : "string",
regex : '.+'
} ]
};
};
oop.inherits(PythonHighlightRules, TextHighlightRules);
return PythonHighlightRules;
});