mode-clojure.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. ace.define("ace/mode/clojure_highlight_rules",[], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var ClojureHighlightRules = function() {
  6. var builtinFunctions = (
  7. '* *1 *2 *3 *agent* *allow-unresolved-vars* *assert* *clojure-version* ' +
  8. '*command-line-args* *compile-files* *compile-path* *e *err* *file* ' +
  9. '*flush-on-newline* *in* *macro-meta* *math-context* *ns* *out* ' +
  10. '*print-dup* *print-length* *print-level* *print-meta* *print-readably* ' +
  11. '*read-eval* *source-path* *use-context-classloader* ' +
  12. '*warn-on-reflection* + - -> ->> .. / < <= = ' +
  13. '== > &gt; >= &gt;= accessor aclone ' +
  14. 'add-classpath add-watch agent agent-errors aget alength alias all-ns ' +
  15. 'alter alter-meta! alter-var-root amap ancestors and apply areduce ' +
  16. 'array-map aset aset-boolean aset-byte aset-char aset-double aset-float ' +
  17. 'aset-int aset-long aset-short assert assoc assoc! assoc-in associative? ' +
  18. 'atom await await-for await1 bases bean bigdec bigint binding bit-and ' +
  19. 'bit-and-not bit-clear bit-flip bit-not bit-or bit-set bit-shift-left ' +
  20. 'bit-shift-right bit-test bit-xor boolean boolean-array booleans ' +
  21. 'bound-fn bound-fn* butlast byte byte-array bytes cast char char-array ' +
  22. 'char-escape-string char-name-string char? chars chunk chunk-append ' +
  23. 'chunk-buffer chunk-cons chunk-first chunk-next chunk-rest chunked-seq? ' +
  24. 'class class? clear-agent-errors clojure-version coll? comment commute ' +
  25. 'comp comparator compare compare-and-set! compile complement concat cond ' +
  26. 'condp conj conj! cons constantly construct-proxy contains? count ' +
  27. 'counted? create-ns create-struct cycle dec decimal? declare definline ' +
  28. 'defmacro defmethod defmulti defn defn- defonce defstruct delay delay? ' +
  29. 'deliver deref derive descendants destructure disj disj! dissoc dissoc! ' +
  30. 'distinct distinct? doall doc dorun doseq dosync dotimes doto double ' +
  31. 'double-array doubles drop drop-last drop-while empty empty? ensure ' +
  32. 'enumeration-seq eval even? every? false? ffirst file-seq filter find ' +
  33. 'find-doc find-ns find-var first float float-array float? floats flush ' +
  34. 'fn fn? fnext for force format future future-call future-cancel ' +
  35. 'future-cancelled? future-done? future? gen-class gen-interface gensym ' +
  36. 'get get-in get-method get-proxy-class get-thread-bindings get-validator ' +
  37. 'hash hash-map hash-set identical? identity if-let if-not ifn? import ' +
  38. 'in-ns inc init-proxy instance? int int-array integer? interleave intern ' +
  39. 'interpose into into-array ints io! isa? iterate iterator-seq juxt key ' +
  40. 'keys keyword keyword? last lazy-cat lazy-seq let letfn line-seq list ' +
  41. 'list* list? load load-file load-reader load-string loaded-libs locking ' +
  42. 'long long-array longs loop macroexpand macroexpand-1 make-array ' +
  43. 'make-hierarchy map map? mapcat max max-key memfn memoize merge ' +
  44. 'merge-with meta method-sig methods min min-key mod name namespace neg? ' +
  45. 'newline next nfirst nil? nnext not not-any? not-empty not-every? not= ' +
  46. 'ns ns-aliases ns-imports ns-interns ns-map ns-name ns-publics ' +
  47. 'ns-refers ns-resolve ns-unalias ns-unmap nth nthnext num number? odd? ' +
  48. 'or parents partial partition pcalls peek persistent! pmap pop pop! ' +
  49. 'pop-thread-bindings pos? pr pr-str prefer-method prefers ' +
  50. 'primitives-classnames print print-ctor print-doc print-dup print-method ' +
  51. 'print-namespace-doc print-simple print-special-doc print-str printf ' +
  52. 'println println-str prn prn-str promise proxy proxy-call-with-super ' +
  53. 'proxy-mappings proxy-name proxy-super push-thread-bindings pvalues quot ' +
  54. 'rand rand-int range ratio? rational? rationalize re-find re-groups ' +
  55. 're-matcher re-matches re-pattern re-seq read read-line read-string ' +
  56. 'reduce ref ref-history-count ref-max-history ref-min-history ref-set ' +
  57. 'refer refer-clojure release-pending-sends rem remove remove-method ' +
  58. 'remove-ns remove-watch repeat repeatedly replace replicate require ' +
  59. 'reset! reset-meta! resolve rest resultset-seq reverse reversible? rseq ' +
  60. 'rsubseq second select-keys send send-off seq seq? seque sequence ' +
  61. 'sequential? set set-validator! set? short short-array shorts ' +
  62. 'shutdown-agents slurp some sort sort-by sorted-map sorted-map-by ' +
  63. 'sorted-set sorted-set-by sorted? special-form-anchor special-symbol? ' +
  64. 'split-at split-with str stream? string? struct struct-map subs subseq ' +
  65. 'subvec supers swap! symbol symbol? sync syntax-symbol-anchor take ' +
  66. 'take-last take-nth take-while test the-ns time to-array to-array-2d ' +
  67. 'trampoline transient tree-seq true? type unchecked-add unchecked-dec ' +
  68. 'unchecked-divide unchecked-inc unchecked-multiply unchecked-negate ' +
  69. 'unchecked-remainder unchecked-subtract underive unquote ' +
  70. 'unquote-splicing update-in update-proxy use val vals var-get var-set ' +
  71. 'var? vary-meta vec vector vector? when when-first when-let when-not ' +
  72. 'while with-bindings with-bindings* with-in-str with-loading-context ' +
  73. 'with-local-vars with-meta with-open with-out-str with-precision xml-seq ' +
  74. 'zero? zipmap'
  75. );
  76. var keywords = ('throw try var ' +
  77. 'def do fn if let loop monitor-enter monitor-exit new quote recur set!'
  78. );
  79. var buildinConstants = ("true false nil");
  80. var keywordMapper = this.createKeywordMapper({
  81. "keyword": keywords,
  82. "constant.language": buildinConstants,
  83. "support.function": builtinFunctions
  84. }, "identifier", false, " ");
  85. this.$rules = {
  86. "start" : [
  87. {
  88. token : "comment",
  89. regex : ";.*$"
  90. }, {
  91. token : "keyword", //parens
  92. regex : "[\\(|\\)]"
  93. }, {
  94. token : "keyword", //lists
  95. regex : "[\\'\\(]"
  96. }, {
  97. token : "keyword", //vectors
  98. regex : "[\\[|\\]]"
  99. }, {
  100. token : "keyword", //sets and maps
  101. regex : "[\\{|\\}|\\#\\{|\\#\\}]"
  102. }, {
  103. token : "keyword", // ampersands
  104. regex : '[\\&]'
  105. }, {
  106. token : "keyword", // metadata
  107. regex : '[\\#\\^\\{]'
  108. }, {
  109. token : "keyword", // anonymous fn syntactic sugar
  110. regex : '[\\%]'
  111. }, {
  112. token : "keyword", // deref reader macro
  113. regex : '[@]'
  114. }, {
  115. token : "constant.numeric", // hex
  116. regex : "0[xX][0-9a-fA-F]+\\b"
  117. }, {
  118. token : "constant.numeric", // float
  119. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  120. }, {
  121. token : "constant.language",
  122. regex : '[!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+||=|!=|<=|>=|<>|<|>|!|&&]'
  123. }, {
  124. token : keywordMapper,
  125. regex : "[a-zA-Z_$][a-zA-Z0-9_$\\-]*\\b"
  126. }, {
  127. token : "string", // single line
  128. regex : '"',
  129. next: "string"
  130. }, {
  131. token : "constant", // symbol
  132. regex : /:[^()\[\]{}'"\^%`,;\s]+/
  133. }, {
  134. token : "string.regexp", //Regular Expressions
  135. regex : '/#"(?:\\.|(?:\\")|[^""\n])*"/g'
  136. }
  137. ],
  138. "string" : [
  139. {
  140. token : "constant.language.escape",
  141. regex : "\\\\.|\\\\$"
  142. }, {
  143. token : "string",
  144. regex : '[^"\\\\]+'
  145. }, {
  146. token : "string",
  147. regex : '"',
  148. next : "start"
  149. }
  150. ]
  151. };
  152. };
  153. oop.inherits(ClojureHighlightRules, TextHighlightRules);
  154. exports.ClojureHighlightRules = ClojureHighlightRules;
  155. });
  156. ace.define("ace/mode/matching_parens_outdent",[], function(require, exports, module) {
  157. "use strict";
  158. var Range = require("../range").Range;
  159. var MatchingParensOutdent = function() {};
  160. (function() {
  161. this.checkOutdent = function(line, input) {
  162. if (! /^\s+$/.test(line))
  163. return false;
  164. return /^\s*\)/.test(input);
  165. };
  166. this.autoOutdent = function(doc, row) {
  167. var line = doc.getLine(row);
  168. var match = line.match(/^(\s*\))/);
  169. if (!match) return 0;
  170. var column = match[1].length;
  171. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  172. if (!openBracePos || openBracePos.row == row) return 0;
  173. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  174. doc.replace(new Range(row, 0, row, column-1), indent);
  175. };
  176. this.$getIndent = function(line) {
  177. var match = line.match(/^(\s+)/);
  178. if (match) {
  179. return match[1];
  180. }
  181. return "";
  182. };
  183. }).call(MatchingParensOutdent.prototype);
  184. exports.MatchingParensOutdent = MatchingParensOutdent;
  185. });
  186. ace.define("ace/mode/clojure",[], function(require, exports, module) {
  187. "use strict";
  188. var oop = require("../lib/oop");
  189. var TextMode = require("./text").Mode;
  190. var ClojureHighlightRules = require("./clojure_highlight_rules").ClojureHighlightRules;
  191. var MatchingParensOutdent = require("./matching_parens_outdent").MatchingParensOutdent;
  192. var Mode = function() {
  193. this.HighlightRules = ClojureHighlightRules;
  194. this.$outdent = new MatchingParensOutdent();
  195. this.$behaviour = this.$defaultBehaviour;
  196. };
  197. oop.inherits(Mode, TextMode);
  198. (function() {
  199. this.lineCommentStart = ";";
  200. this.minorIndentFunctions = ["defn", "defn-", "defmacro", "def", "deftest", "testing"];
  201. this.$toIndent = function(str) {
  202. return str.split('').map(function(ch) {
  203. if (/\s/.exec(ch)) {
  204. return ch;
  205. } else {
  206. return ' ';
  207. }
  208. }).join('');
  209. };
  210. this.$calculateIndent = function(line, tab) {
  211. var baseIndent = this.$getIndent(line);
  212. var delta = 0;
  213. var isParen, ch;
  214. for (var i = line.length - 1; i >= 0; i--) {
  215. ch = line[i];
  216. if (ch === '(') {
  217. delta--;
  218. isParen = true;
  219. } else if (ch === '(' || ch === '[' || ch === '{') {
  220. delta--;
  221. isParen = false;
  222. } else if (ch === ')' || ch === ']' || ch === '}') {
  223. delta++;
  224. }
  225. if (delta < 0) {
  226. break;
  227. }
  228. }
  229. if (delta < 0 && isParen) {
  230. i += 1;
  231. var iBefore = i;
  232. var fn = '';
  233. while (true) {
  234. ch = line[i];
  235. if (ch === ' ' || ch === '\t') {
  236. if(this.minorIndentFunctions.indexOf(fn) !== -1) {
  237. return this.$toIndent(line.substring(0, iBefore - 1) + tab);
  238. } else {
  239. return this.$toIndent(line.substring(0, i + 1));
  240. }
  241. } else if (ch === undefined) {
  242. return this.$toIndent(line.substring(0, iBefore - 1) + tab);
  243. }
  244. fn += line[i];
  245. i++;
  246. }
  247. } else if(delta < 0 && !isParen) {
  248. return this.$toIndent(line.substring(0, i+1));
  249. } else if(delta > 0) {
  250. baseIndent = baseIndent.substring(0, baseIndent.length - tab.length);
  251. return baseIndent;
  252. } else {
  253. return baseIndent;
  254. }
  255. };
  256. this.getNextLineIndent = function(state, line, tab) {
  257. return this.$calculateIndent(line, tab);
  258. };
  259. this.checkOutdent = function(state, line, input) {
  260. return this.$outdent.checkOutdent(line, input);
  261. };
  262. this.autoOutdent = function(state, doc, row) {
  263. this.$outdent.autoOutdent(doc, row);
  264. };
  265. this.$id = "ace/mode/clojure";
  266. }).call(Mode.prototype);
  267. exports.Mode = Mode;
  268. });
  269. (function() {
  270. ace.require(["ace/mode/clojure"], function(m) {
  271. if (typeof module == "object" && typeof exports == "object" && module) {
  272. module.exports = m;
  273. }
  274. });
  275. })();