| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /* ***** 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 LuaHighlightRules = require("./lua_highlight_rules").LuaHighlightRules;
- var LuaFoldMode = require("./folding/lua").FoldMode;
- var Range = require("../range").Range;
- var WorkerClient = require("../worker/worker_client").WorkerClient;
- var Mode = function() {
- this.HighlightRules = LuaHighlightRules;
-
- this.foldingRules = new LuaFoldMode();
- };
- oop.inherits(Mode, TextMode);
- (function() {
-
- this.lineCommentStart = "--";
- this.blockComment = {start: "--[", end: "]--"};
-
- var indentKeywords = {
- "function": 1,
- "then": 1,
- "do": 1,
- "else": 1,
- "elseif": 1,
- "repeat": 1,
- "end": -1,
- "until": -1
- };
- var outdentKeywords = [
- "else",
- "elseif",
- "end",
- "until"
- ];
- function getNetIndentLevel(tokens) {
- var level = 0;
- // Support single-line blocks by decrementing the indent level if
- // an ending token is found
- for (var i = 0; i < tokens.length; i++) {
- var token = tokens[i];
- if (token.type == "keyword") {
- if (token.value in indentKeywords) {
- level += indentKeywords[token.value];
- }
- } else if (token.type == "paren.lparen") {
- level += token.value.length;
- } else if (token.type == "paren.rparen") {
- level -= token.value.length;
- }
- }
- // Limit the level to +/- 1 since usually users only indent one level
- // at a time regardless of the logical nesting level
- if (level < 0) {
- return -1;
- } else if (level > 0) {
- return 1;
- } else {
- return 0;
- }
- }
- this.getNextLineIndent = function(state, line, tab) {
- var indent = this.$getIndent(line);
- var level = 0;
- var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
- var tokens = tokenizedLine.tokens;
- if (state == "start") {
- level = getNetIndentLevel(tokens);
- }
- if (level > 0) {
- return indent + tab;
- } else if (level < 0 && indent.substr(indent.length - tab.length) == tab) {
- // Don't do a next-line outdent if we're going to do a real outdent of this line
- if (!this.checkOutdent(state, line, "\n")) {
- return indent.substr(0, indent.length - tab.length);
- }
- }
- return indent;
- };
- this.checkOutdent = function(state, line, input) {
- if (input != "\n" && input != "\r" && input != "\r\n")
- return false;
- if (line.match(/^\s*[\)\}\]]$/))
- return true;
- var tokens = this.getTokenizer().getLineTokens(line.trim(), state).tokens;
- if (!tokens || !tokens.length)
- return false;
- return (tokens[0].type == "keyword" && outdentKeywords.indexOf(tokens[0].value) != -1);
- };
- this.autoOutdent = function(state, session, row) {
- var prevLine = session.getLine(row - 1);
- var prevIndent = this.$getIndent(prevLine).length;
- var prevTokens = this.getTokenizer().getLineTokens(prevLine, "start").tokens;
- var tabLength = session.getTabString().length;
- var expectedIndent = prevIndent + tabLength * getNetIndentLevel(prevTokens);
- var curIndent = this.$getIndent(session.getLine(row)).length;
- if (curIndent < expectedIndent) {
- // User already outdented //
- return;
- }
- session.outdentRows(new Range(row, 0, row + 2, 0));
- };
- this.createWorker = function(session) {
- var worker = new WorkerClient(["ace"], "ace/mode/lua_worker", "Worker");
- worker.attachToDocument(session.getDocument());
-
- worker.on("annotate", function(e) {
- session.setAnnotations(e.data);
- });
-
- worker.on("terminate", function() {
- session.clearAnnotations();
- });
-
- return worker;
- };
- this.$id = "ace/mode/lua";
- }).call(Mode.prototype);
- exports.Mode = Mode;
- });
|