lua.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var TextMode = require("./text").Mode;
  34. var LuaHighlightRules = require("./lua_highlight_rules").LuaHighlightRules;
  35. var LuaFoldMode = require("./folding/lua").FoldMode;
  36. var Range = require("../range").Range;
  37. var WorkerClient = require("../worker/worker_client").WorkerClient;
  38. var Mode = function() {
  39. this.HighlightRules = LuaHighlightRules;
  40. this.foldingRules = new LuaFoldMode();
  41. };
  42. oop.inherits(Mode, TextMode);
  43. (function() {
  44. this.lineCommentStart = "--";
  45. this.blockComment = {start: "--[", end: "]--"};
  46. var indentKeywords = {
  47. "function": 1,
  48. "then": 1,
  49. "do": 1,
  50. "else": 1,
  51. "elseif": 1,
  52. "repeat": 1,
  53. "end": -1,
  54. "until": -1
  55. };
  56. var outdentKeywords = [
  57. "else",
  58. "elseif",
  59. "end",
  60. "until"
  61. ];
  62. function getNetIndentLevel(tokens) {
  63. var level = 0;
  64. // Support single-line blocks by decrementing the indent level if
  65. // an ending token is found
  66. for (var i = 0; i < tokens.length; i++) {
  67. var token = tokens[i];
  68. if (token.type == "keyword") {
  69. if (token.value in indentKeywords) {
  70. level += indentKeywords[token.value];
  71. }
  72. } else if (token.type == "paren.lparen") {
  73. level += token.value.length;
  74. } else if (token.type == "paren.rparen") {
  75. level -= token.value.length;
  76. }
  77. }
  78. // Limit the level to +/- 1 since usually users only indent one level
  79. // at a time regardless of the logical nesting level
  80. if (level < 0) {
  81. return -1;
  82. } else if (level > 0) {
  83. return 1;
  84. } else {
  85. return 0;
  86. }
  87. }
  88. this.getNextLineIndent = function(state, line, tab) {
  89. var indent = this.$getIndent(line);
  90. var level = 0;
  91. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  92. var tokens = tokenizedLine.tokens;
  93. if (state == "start") {
  94. level = getNetIndentLevel(tokens);
  95. }
  96. if (level > 0) {
  97. return indent + tab;
  98. } else if (level < 0 && indent.substr(indent.length - tab.length) == tab) {
  99. // Don't do a next-line outdent if we're going to do a real outdent of this line
  100. if (!this.checkOutdent(state, line, "\n")) {
  101. return indent.substr(0, indent.length - tab.length);
  102. }
  103. }
  104. return indent;
  105. };
  106. this.checkOutdent = function(state, line, input) {
  107. if (input != "\n" && input != "\r" && input != "\r\n")
  108. return false;
  109. if (line.match(/^\s*[\)\}\]]$/))
  110. return true;
  111. var tokens = this.getTokenizer().getLineTokens(line.trim(), state).tokens;
  112. if (!tokens || !tokens.length)
  113. return false;
  114. return (tokens[0].type == "keyword" && outdentKeywords.indexOf(tokens[0].value) != -1);
  115. };
  116. this.autoOutdent = function(state, session, row) {
  117. var prevLine = session.getLine(row - 1);
  118. var prevIndent = this.$getIndent(prevLine).length;
  119. var prevTokens = this.getTokenizer().getLineTokens(prevLine, "start").tokens;
  120. var tabLength = session.getTabString().length;
  121. var expectedIndent = prevIndent + tabLength * getNetIndentLevel(prevTokens);
  122. var curIndent = this.$getIndent(session.getLine(row)).length;
  123. if (curIndent < expectedIndent) {
  124. // User already outdented //
  125. return;
  126. }
  127. session.outdentRows(new Range(row, 0, row + 2, 0));
  128. };
  129. this.createWorker = function(session) {
  130. var worker = new WorkerClient(["ace"], "ace/mode/lua_worker", "Worker");
  131. worker.attachToDocument(session.getDocument());
  132. worker.on("annotate", function(e) {
  133. session.setAnnotations(e.data);
  134. });
  135. worker.on("terminate", function() {
  136. session.clearAnnotations();
  137. });
  138. return worker;
  139. };
  140. this.$id = "ace/mode/lua";
  141. }).call(Mode.prototype);
  142. exports.Mode = Mode;
  143. });