mode-c9search.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ace.define("ace/mode/c9search_highlight_rules",[], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var lang = require("../lib/lang");
  5. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  6. function safeCreateRegexp(source, flag) {
  7. try {
  8. return new RegExp(source, flag);
  9. } catch(e) {}
  10. }
  11. var C9SearchHighlightRules = function() {
  12. this.$rules = {
  13. "start" : [
  14. {
  15. tokenNames : ["c9searchresults.constant.numeric", "c9searchresults.text", "c9searchresults.text", "c9searchresults.keyword"],
  16. regex : /(^\s+[0-9]+)(:)(\d*\s?)([^\r\n]+)/,
  17. onMatch : function(val, state, stack) {
  18. var values = this.splitRegex.exec(val);
  19. var types = this.tokenNames;
  20. var tokens = [{
  21. type: types[0],
  22. value: values[1]
  23. }, {
  24. type: types[1],
  25. value: values[2]
  26. }];
  27. if (values[3]) {
  28. if (values[3] == " ")
  29. tokens[1] = { type: types[1], value: values[2] + " " };
  30. else
  31. tokens.push({ type: types[1], value: values[3] });
  32. }
  33. var regex = stack[1];
  34. var str = values[4];
  35. var m;
  36. var last = 0;
  37. if (regex && regex.exec) {
  38. regex.lastIndex = 0;
  39. while (m = regex.exec(str)) {
  40. var skipped = str.substring(last, m.index);
  41. last = regex.lastIndex;
  42. if (skipped)
  43. tokens.push({type: types[2], value: skipped});
  44. if (m[0])
  45. tokens.push({type: types[3], value: m[0]});
  46. else if (!skipped)
  47. break;
  48. }
  49. }
  50. if (last < str.length)
  51. tokens.push({type: types[2], value: str.substr(last)});
  52. return tokens;
  53. }
  54. },
  55. {
  56. regex : "^Searching for [^\\r\\n]*$",
  57. onMatch: function(val, state, stack) {
  58. var parts = val.split("\x01");
  59. if (parts.length < 3)
  60. return "text";
  61. var options, search;
  62. var i = 0;
  63. var tokens = [{
  64. value: parts[i++] + "'",
  65. type: "text"
  66. }, {
  67. value: search = parts[i++],
  68. type: "text" // "c9searchresults.keyword"
  69. }, {
  70. value: "'" + parts[i++],
  71. type: "text"
  72. }];
  73. if (parts[2] !== " in") {
  74. tokens.push({
  75. value: "'" + parts[i++] + "'",
  76. type: "text"
  77. }, {
  78. value: parts[i++],
  79. type: "text"
  80. });
  81. }
  82. tokens.push({
  83. value: " " + parts[i++] + " ",
  84. type: "text"
  85. });
  86. if (parts[i+1]) {
  87. options = parts[i+1];
  88. tokens.push({
  89. value: "(" + parts[i+1] + ")",
  90. type: "text"
  91. });
  92. i += 1;
  93. } else {
  94. i -= 1;
  95. }
  96. while (i++ < parts.length) {
  97. parts[i] && tokens.push({
  98. value: parts[i],
  99. type: "text"
  100. });
  101. }
  102. if (search) {
  103. if (!/regex/.test(options))
  104. search = lang.escapeRegExp(search);
  105. if (/whole/.test(options))
  106. search = "\\b" + search + "\\b";
  107. }
  108. var regex = search && safeCreateRegexp(
  109. "(" + search + ")",
  110. / sensitive/.test(options) ? "g" : "ig"
  111. );
  112. if (regex) {
  113. stack[0] = state;
  114. stack[1] = regex;
  115. }
  116. return tokens;
  117. }
  118. },
  119. {
  120. regex : "^(?=Found \\d+ matches)",
  121. token : "text",
  122. next : "numbers"
  123. },
  124. {
  125. token : "string", // single line
  126. regex : "^\\S:?[^:]+",
  127. next : "numbers"
  128. }
  129. ],
  130. numbers:[{
  131. regex : "\\d+",
  132. token : "constant.numeric"
  133. }, {
  134. regex : "$",
  135. token : "text",
  136. next : "start"
  137. }]
  138. };
  139. this.normalizeRules();
  140. };
  141. oop.inherits(C9SearchHighlightRules, TextHighlightRules);
  142. exports.C9SearchHighlightRules = C9SearchHighlightRules;
  143. });
  144. ace.define("ace/mode/matching_brace_outdent",[], function(require, exports, module) {
  145. "use strict";
  146. var Range = require("../range").Range;
  147. var MatchingBraceOutdent = function() {};
  148. (function() {
  149. this.checkOutdent = function(line, input) {
  150. if (! /^\s+$/.test(line))
  151. return false;
  152. return /^\s*\}/.test(input);
  153. };
  154. this.autoOutdent = function(doc, row) {
  155. var line = doc.getLine(row);
  156. var match = line.match(/^(\s*\})/);
  157. if (!match) return 0;
  158. var column = match[1].length;
  159. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  160. if (!openBracePos || openBracePos.row == row) return 0;
  161. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  162. doc.replace(new Range(row, 0, row, column-1), indent);
  163. };
  164. this.$getIndent = function(line) {
  165. return line.match(/^\s*/)[0];
  166. };
  167. }).call(MatchingBraceOutdent.prototype);
  168. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  169. });
  170. ace.define("ace/mode/folding/c9search",[], function(require, exports, module) {
  171. "use strict";
  172. var oop = require("../../lib/oop");
  173. var Range = require("../../range").Range;
  174. var BaseFoldMode = require("./fold_mode").FoldMode;
  175. var FoldMode = exports.FoldMode = function() {};
  176. oop.inherits(FoldMode, BaseFoldMode);
  177. (function() {
  178. this.foldingStartMarker = /^(\S.*:|Searching for.*)$/;
  179. this.foldingStopMarker = /^(\s+|Found.*)$/;
  180. this.getFoldWidgetRange = function(session, foldStyle, row) {
  181. var lines = session.doc.getAllLines(row);
  182. var line = lines[row];
  183. var level1 = /^(Found.*|Searching for.*)$/;
  184. var level2 = /^(\S.*:|\s*)$/;
  185. var re = level1.test(line) ? level1 : level2;
  186. var startRow = row;
  187. var endRow = row;
  188. if (this.foldingStartMarker.test(line)) {
  189. for (var i = row + 1, l = session.getLength(); i < l; i++) {
  190. if (re.test(lines[i]))
  191. break;
  192. }
  193. endRow = i;
  194. }
  195. else if (this.foldingStopMarker.test(line)) {
  196. for (var i = row - 1; i >= 0; i--) {
  197. line = lines[i];
  198. if (re.test(line))
  199. break;
  200. }
  201. startRow = i;
  202. }
  203. if (startRow != endRow) {
  204. var col = line.length;
  205. if (re === level1)
  206. col = line.search(/\(Found[^)]+\)$|$/);
  207. return new Range(startRow, col, endRow, 0);
  208. }
  209. };
  210. }).call(FoldMode.prototype);
  211. });
  212. ace.define("ace/mode/c9search",[], function(require, exports, module) {
  213. "use strict";
  214. var oop = require("../lib/oop");
  215. var TextMode = require("./text").Mode;
  216. var C9SearchHighlightRules = require("./c9search_highlight_rules").C9SearchHighlightRules;
  217. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  218. var C9StyleFoldMode = require("./folding/c9search").FoldMode;
  219. var Mode = function() {
  220. this.HighlightRules = C9SearchHighlightRules;
  221. this.$outdent = new MatchingBraceOutdent();
  222. this.foldingRules = new C9StyleFoldMode();
  223. };
  224. oop.inherits(Mode, TextMode);
  225. (function() {
  226. this.getNextLineIndent = function(state, line, tab) {
  227. var indent = this.$getIndent(line);
  228. return indent;
  229. };
  230. this.checkOutdent = function(state, line, input) {
  231. return this.$outdent.checkOutdent(line, input);
  232. };
  233. this.autoOutdent = function(state, doc, row) {
  234. this.$outdent.autoOutdent(doc, row);
  235. };
  236. this.$id = "ace/mode/c9search";
  237. }).call(Mode.prototype);
  238. exports.Mode = Mode;
  239. });
  240. (function() {
  241. ace.require(["ace/mode/c9search"], function(m) {
  242. if (typeof module == "object" && typeof exports == "object" && module) {
  243. module.exports = m;
  244. }
  245. });
  246. })();