latex.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 = /^\s*\\(begin)|(section|subsection|paragraph)\b|{\s*$/;
  40. this.foldingStopMarker = /^\s*\\(end)\b|^\s*}/;
  41. this.getFoldWidgetRange = function(session, foldStyle, row) {
  42. var line = session.doc.getLine(row);
  43. var match = this.foldingStartMarker.exec(line);
  44. if (match) {
  45. if (match[1])
  46. return this.latexBlock(session, row, match[0].length - 1);
  47. if (match[2])
  48. return this.latexSection(session, row, match[0].length - 1);
  49. return this.openingBracketBlock(session, "{", row, match.index);
  50. }
  51. var match = this.foldingStopMarker.exec(line);
  52. if (match) {
  53. if (match[1])
  54. return this.latexBlock(session, row, match[0].length - 1);
  55. return this.closingBracketBlock(session, "}", row, match.index + match[0].length);
  56. }
  57. };
  58. this.latexBlock = function(session, row, column) {
  59. var keywords = {
  60. "\\begin": 1,
  61. "\\end": -1
  62. };
  63. var stream = new TokenIterator(session, row, column);
  64. var token = stream.getCurrentToken();
  65. if (!token || !(token.type == "storage.type" || token.type == "constant.character.escape"))
  66. return;
  67. var val = token.value;
  68. var dir = keywords[val];
  69. var getType = function() {
  70. var token = stream.stepForward();
  71. var type = token.type == "lparen" ?stream.stepForward().value : "";
  72. if (dir === -1) {
  73. stream.stepBackward();
  74. if (type)
  75. stream.stepBackward();
  76. }
  77. return type;
  78. };
  79. var stack = [getType()];
  80. var startColumn = dir === -1 ? stream.getCurrentTokenColumn() : session.getLine(row).length;
  81. var startRow = row;
  82. stream.step = dir === -1 ? stream.stepBackward : stream.stepForward;
  83. while(token = stream.step()) {
  84. if (!token || !(token.type == "storage.type" || token.type == "constant.character.escape"))
  85. continue;
  86. var level = keywords[token.value];
  87. if (!level)
  88. continue;
  89. var type = getType();
  90. if (level === dir)
  91. stack.unshift(type);
  92. else if (stack.shift() !== type || !stack.length)
  93. break;
  94. }
  95. if (stack.length)
  96. return;
  97. var row = stream.getCurrentTokenRow();
  98. if (dir === -1)
  99. return new Range(row, session.getLine(row).length, startRow, startColumn);
  100. stream.stepBackward();
  101. return new Range(startRow, startColumn, row, stream.getCurrentTokenColumn());
  102. };
  103. this.latexSection = function(session, row, column) {
  104. var keywords = ["\\subsection", "\\section", "\\begin", "\\end", "\\paragraph"];
  105. var stream = new TokenIterator(session, row, column);
  106. var token = stream.getCurrentToken();
  107. if (!token || token.type != "storage.type")
  108. return;
  109. var startLevel = keywords.indexOf(token.value);
  110. var stackDepth = 0
  111. var endRow = row;
  112. while(token = stream.stepForward()) {
  113. if (token.type !== "storage.type")
  114. continue;
  115. var level = keywords.indexOf(token.value);
  116. if (level >= 2) {
  117. if (!stackDepth)
  118. endRow = stream.getCurrentTokenRow() - 1;
  119. stackDepth += level == 2 ? 1 : - 1;
  120. if (stackDepth < 0)
  121. break
  122. } else if (level >= startLevel)
  123. break;
  124. }
  125. if (!stackDepth)
  126. endRow = stream.getCurrentTokenRow() - 1;
  127. while (endRow > row && !/\S/.test(session.getLine(endRow)))
  128. endRow--;
  129. return new Range(
  130. row, session.getLine(row).length,
  131. endRow, session.getLine(endRow).length
  132. );
  133. };
  134. }).call(FoldMode.prototype);
  135. });