lua.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2012, 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 BaseFoldMode = require("./fold_mode").FoldMode;
  34. var Range = require("../../range").Range;
  35. var TokenIterator = require("../../token_iterator").TokenIterator;
  36. var FoldMode = exports.FoldMode = function() {};
  37. oop.inherits(FoldMode, BaseFoldMode);
  38. (function() {
  39. this.foldingStartMarker = /\b(function|then|do|repeat)\b|{\s*$|(\[=*\[)/;
  40. this.foldingStopMarker = /\bend\b|^\s*}|\]=*\]/;
  41. this.getFoldWidget = function(session, foldStyle, row) {
  42. var line = session.getLine(row);
  43. var isStart = this.foldingStartMarker.test(line);
  44. var isEnd = this.foldingStopMarker.test(line);
  45. if (isStart && !isEnd) {
  46. var match = line.match(this.foldingStartMarker);
  47. if (match[1] == "then" && /\belseif\b/.test(line))
  48. return;
  49. if (match[1]) {
  50. if (session.getTokenAt(row, match.index + 1).type === "keyword")
  51. return "start";
  52. } else if (match[2]) {
  53. var type = session.bgTokenizer.getState(row) || "";
  54. if (type[0] == "bracketedComment" || type[0] == "bracketedString")
  55. return "start";
  56. } else {
  57. return "start";
  58. }
  59. }
  60. if (foldStyle != "markbeginend" || !isEnd || isStart && isEnd)
  61. return "";
  62. var match = line.match(this.foldingStopMarker);
  63. if (match[0] === "end") {
  64. if (session.getTokenAt(row, match.index + 1).type === "keyword")
  65. return "end";
  66. } else if (match[0][0] === "]") {
  67. var type = session.bgTokenizer.getState(row - 1) || "";
  68. if (type[0] == "bracketedComment" || type[0] == "bracketedString")
  69. return "end";
  70. } else
  71. return "end";
  72. };
  73. this.getFoldWidgetRange = function(session, foldStyle, row) {
  74. var line = session.doc.getLine(row);
  75. var match = this.foldingStartMarker.exec(line);
  76. if (match) {
  77. if (match[1])
  78. return this.luaBlock(session, row, match.index + 1);
  79. if (match[2])
  80. return session.getCommentFoldRange(row, match.index + 1);
  81. return this.openingBracketBlock(session, "{", row, match.index);
  82. }
  83. var match = this.foldingStopMarker.exec(line);
  84. if (match) {
  85. if (match[0] === "end") {
  86. if (session.getTokenAt(row, match.index + 1).type === "keyword")
  87. return this.luaBlock(session, row, match.index + 1);
  88. }
  89. if (match[0][0] === "]")
  90. return session.getCommentFoldRange(row, match.index + 1);
  91. return this.closingBracketBlock(session, "}", row, match.index + match[0].length);
  92. }
  93. };
  94. this.luaBlock = function(session, row, column) {
  95. var stream = new TokenIterator(session, row, column);
  96. var indentKeywords = {
  97. "function": 1,
  98. "do": 1,
  99. "then": 1,
  100. "elseif": -1,
  101. "end": -1,
  102. "repeat": 1,
  103. "until": -1
  104. };
  105. var token = stream.getCurrentToken();
  106. if (!token || token.type != "keyword")
  107. return;
  108. var val = token.value;
  109. var stack = [val];
  110. var dir = indentKeywords[val];
  111. if (!dir)
  112. return;
  113. var startColumn = dir === -1 ? stream.getCurrentTokenColumn() : session.getLine(row).length;
  114. var startRow = row;
  115. stream.step = dir === -1 ? stream.stepBackward : stream.stepForward;
  116. while(token = stream.step()) {
  117. if (token.type !== "keyword")
  118. continue;
  119. var level = dir * indentKeywords[token.value];
  120. if (level > 0) {
  121. stack.unshift(token.value);
  122. } else if (level <= 0) {
  123. stack.shift();
  124. if (!stack.length && token.value != "elseif")
  125. break;
  126. if (level === 0)
  127. stack.unshift(token.value);
  128. }
  129. }
  130. var row = stream.getCurrentTokenRow();
  131. if (dir === -1)
  132. return new Range(row, session.getLine(row).length, startRow, startColumn);
  133. else
  134. return new Range(startRow, startColumn, row, stream.getCurrentTokenColumn());
  135. };
  136. }).call(FoldMode.prototype);
  137. });