mode-c9search.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ace.define("ace/mode/c9search_highlight_rules",["require","exports","module","ace/lib/oop","ace/lib/lang","ace/mode/text_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, replace;
  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. replace = parts[i];
  75. tokens.push({
  76. value: "'" + parts[i++] + "'",
  77. type: "text"
  78. }, {
  79. value: parts[i++],
  80. type: "text"
  81. });
  82. }
  83. tokens.push({
  84. value: " " + parts[i++] + " ",
  85. type: "text"
  86. });
  87. if (parts[i+1]) {
  88. options = parts[i+1];
  89. tokens.push({
  90. value: "(" + parts[i+1] + ")",
  91. type: "text"
  92. });
  93. i += 1;
  94. } else {
  95. i -= 1;
  96. }
  97. while (i++ < parts.length) {
  98. parts[i] && tokens.push({
  99. value: parts[i],
  100. type: "text"
  101. });
  102. }
  103. if (replace) {
  104. search = replace;
  105. options = "";
  106. }
  107. if (search) {
  108. if (!/regex/.test(options))
  109. search = lang.escapeRegExp(search);
  110. if (/whole/.test(options))
  111. search = "\\b" + search + "\\b";
  112. }
  113. var regex = search && safeCreateRegexp(
  114. "(" + search + ")",
  115. / sensitive/.test(options) ? "g" : "ig"
  116. );
  117. if (regex) {
  118. stack[0] = state;
  119. stack[1] = regex;
  120. }
  121. return tokens;
  122. }
  123. },
  124. {
  125. regex : "^(?=Found \\d+ matches)",
  126. token : "text",
  127. next : "numbers"
  128. },
  129. {
  130. token : "string", // single line
  131. regex : "^\\S:?[^:]+",
  132. next : "numbers"
  133. }
  134. ],
  135. numbers:[{
  136. regex : "\\d+",
  137. token : "constant.numeric"
  138. }, {
  139. regex : "$",
  140. token : "text",
  141. next : "start"
  142. }]
  143. };
  144. this.normalizeRules();
  145. };
  146. oop.inherits(C9SearchHighlightRules, TextHighlightRules);
  147. exports.C9SearchHighlightRules = C9SearchHighlightRules;
  148. });
  149. ace.define("ace/mode/matching_brace_outdent",["require","exports","module","ace/range"], function(require, exports, module) {
  150. "use strict";
  151. var Range = require("../range").Range;
  152. var MatchingBraceOutdent = function() {};
  153. (function() {
  154. this.checkOutdent = function(line, input) {
  155. if (! /^\s+$/.test(line))
  156. return false;
  157. return /^\s*\}/.test(input);
  158. };
  159. this.autoOutdent = function(doc, row) {
  160. var line = doc.getLine(row);
  161. var match = line.match(/^(\s*\})/);
  162. if (!match) return 0;
  163. var column = match[1].length;
  164. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  165. if (!openBracePos || openBracePos.row == row) return 0;
  166. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  167. doc.replace(new Range(row, 0, row, column-1), indent);
  168. };
  169. this.$getIndent = function(line) {
  170. return line.match(/^\s*/)[0];
  171. };
  172. }).call(MatchingBraceOutdent.prototype);
  173. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  174. });
  175. ace.define("ace/mode/folding/c9search",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  176. "use strict";
  177. var oop = require("../../lib/oop");
  178. var Range = require("../../range").Range;
  179. var BaseFoldMode = require("./fold_mode").FoldMode;
  180. var FoldMode = exports.FoldMode = function() {};
  181. oop.inherits(FoldMode, BaseFoldMode);
  182. (function() {
  183. this.foldingStartMarker = /^(\S.*:|Searching for.*)$/;
  184. this.foldingStopMarker = /^(\s+|Found.*)$/;
  185. this.getFoldWidgetRange = function(session, foldStyle, row) {
  186. var lines = session.doc.getAllLines(row);
  187. var line = lines[row];
  188. var level1 = /^(Found.*|Searching for.*)$/;
  189. var level2 = /^(\S.*:|\s*)$/;
  190. var re = level1.test(line) ? level1 : level2;
  191. var startRow = row;
  192. var endRow = row;
  193. if (this.foldingStartMarker.test(line)) {
  194. for (var i = row + 1, l = session.getLength(); i < l; i++) {
  195. if (re.test(lines[i]))
  196. break;
  197. }
  198. endRow = i;
  199. }
  200. else if (this.foldingStopMarker.test(line)) {
  201. for (var i = row - 1; i >= 0; i--) {
  202. line = lines[i];
  203. if (re.test(line))
  204. break;
  205. }
  206. startRow = i;
  207. }
  208. if (startRow != endRow) {
  209. var col = line.length;
  210. if (re === level1)
  211. col = line.search(/\(Found[^)]+\)$|$/);
  212. return new Range(startRow, col, endRow, 0);
  213. }
  214. };
  215. }).call(FoldMode.prototype);
  216. });
  217. ace.define("ace/mode/c9search",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/c9search_highlight_rules","ace/mode/matching_brace_outdent","ace/mode/folding/c9search"], function(require, exports, module) {
  218. "use strict";
  219. var oop = require("../lib/oop");
  220. var TextMode = require("./text").Mode;
  221. var C9SearchHighlightRules = require("./c9search_highlight_rules").C9SearchHighlightRules;
  222. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  223. var C9StyleFoldMode = require("./folding/c9search").FoldMode;
  224. var Mode = function() {
  225. this.HighlightRules = C9SearchHighlightRules;
  226. this.$outdent = new MatchingBraceOutdent();
  227. this.foldingRules = new C9StyleFoldMode();
  228. };
  229. oop.inherits(Mode, TextMode);
  230. (function() {
  231. this.getNextLineIndent = function(state, line, tab) {
  232. var indent = this.$getIndent(line);
  233. return indent;
  234. };
  235. this.checkOutdent = function(state, line, input) {
  236. return this.$outdent.checkOutdent(line, input);
  237. };
  238. this.autoOutdent = function(state, doc, row) {
  239. this.$outdent.autoOutdent(doc, row);
  240. };
  241. this.$id = "ace/mode/c9search";
  242. }).call(Mode.prototype);
  243. exports.Mode = Mode;
  244. });