mode-c_cpp.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. ace.define("ace/mode/doc_comment_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_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 DocCommentHighlightRules = function() {
  6. this.$rules = {
  7. "start" : [ {
  8. token : "comment.doc.tag",
  9. regex : "@[\\w\\d_]+" // TODO: fix email addresses
  10. },
  11. DocCommentHighlightRules.getTagRule(),
  12. {
  13. defaultToken : "comment.doc",
  14. caseInsensitive: true
  15. }]
  16. };
  17. };
  18. oop.inherits(DocCommentHighlightRules, TextHighlightRules);
  19. DocCommentHighlightRules.getTagRule = function(start) {
  20. return {
  21. token : "comment.doc.tag.storage.type",
  22. regex : "\\b(?:TODO|FIXME|XXX|HACK)\\b"
  23. };
  24. }
  25. DocCommentHighlightRules.getStartRule = function(start) {
  26. return {
  27. token : "comment.doc", // doc comment
  28. regex : "\\/\\*(?=\\*)",
  29. next : start
  30. };
  31. };
  32. DocCommentHighlightRules.getEndRule = function (start) {
  33. return {
  34. token : "comment.doc", // closing comment
  35. regex : "\\*\\/",
  36. next : start
  37. };
  38. };
  39. exports.DocCommentHighlightRules = DocCommentHighlightRules;
  40. });
  41. ace.define("ace/mode/c_cpp_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/doc_comment_highlight_rules","ace/mode/text_highlight_rules"], function(require, exports, module) {
  42. "use strict";
  43. var oop = require("../lib/oop");
  44. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  45. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  46. var cFunctions = exports.cFunctions = "\\b(?:hypot(?:f|l)?|s(?:scanf|ystem|nprintf|ca(?:nf|lb(?:n(?:f|l)?|ln(?:f|l)?))|i(?:n(?:h(?:f|l)?|f|l)?|gn(?:al|bit))|tr(?:s(?:tr|pn)|nc(?:py|at|mp)|c(?:spn|hr|oll|py|at|mp)|to(?:imax|d|u(?:l(?:l)?|max)|k|f|l(?:d|l)?)|error|pbrk|ftime|len|rchr|xfrm)|printf|et(?:jmp|vbuf|locale|buf)|qrt(?:f|l)?|w(?:scanf|printf)|rand)|n(?:e(?:arbyint(?:f|l)?|xt(?:toward(?:f|l)?|after(?:f|l)?))|an(?:f|l)?)|c(?:s(?:in(?:h(?:f|l)?|f|l)?|qrt(?:f|l)?)|cos(?:h(?:f)?|f|l)?|imag(?:f|l)?|t(?:ime|an(?:h(?:f|l)?|f|l)?)|o(?:s(?:h(?:f|l)?|f|l)?|nj(?:f|l)?|pysign(?:f|l)?)|p(?:ow(?:f|l)?|roj(?:f|l)?)|e(?:il(?:f|l)?|xp(?:f|l)?)|l(?:o(?:ck|g(?:f|l)?)|earerr)|a(?:sin(?:h(?:f|l)?|f|l)?|cos(?:h(?:f|l)?|f|l)?|tan(?:h(?:f|l)?|f|l)?|lloc|rg(?:f|l)?|bs(?:f|l)?)|real(?:f|l)?|brt(?:f|l)?)|t(?:ime|o(?:upper|lower)|an(?:h(?:f|l)?|f|l)?|runc(?:f|l)?|gamma(?:f|l)?|mp(?:nam|file))|i(?:s(?:space|n(?:ormal|an)|cntrl|inf|digit|u(?:nordered|pper)|p(?:unct|rint)|finite|w(?:space|c(?:ntrl|type)|digit|upper|p(?:unct|rint)|lower|al(?:num|pha)|graph|xdigit|blank)|l(?:ower|ess(?:equal|greater)?)|al(?:num|pha)|gr(?:eater(?:equal)?|aph)|xdigit|blank)|logb(?:f|l)?|max(?:div|abs))|di(?:v|fftime)|_Exit|unget(?:c|wc)|p(?:ow(?:f|l)?|ut(?:s|c(?:har)?|wc(?:har)?)|error|rintf)|e(?:rf(?:c(?:f|l)?|f|l)?|x(?:it|p(?:2(?:f|l)?|f|l|m1(?:f|l)?)?))|v(?:s(?:scanf|nprintf|canf|printf|w(?:scanf|printf))|printf|f(?:scanf|printf|w(?:scanf|printf))|w(?:scanf|printf)|a_(?:start|copy|end|arg))|qsort|f(?:s(?:canf|e(?:tpos|ek))|close|tell|open|dim(?:f|l)?|p(?:classify|ut(?:s|c|w(?:s|c))|rintf)|e(?:holdexcept|set(?:e(?:nv|xceptflag)|round)|clearexcept|testexcept|of|updateenv|r(?:aiseexcept|ror)|get(?:e(?:nv|xceptflag)|round))|flush|w(?:scanf|ide|printf|rite)|loor(?:f|l)?|abs(?:f|l)?|get(?:s|c|pos|w(?:s|c))|re(?:open|e|ad|xp(?:f|l)?)|m(?:in(?:f|l)?|od(?:f|l)?|a(?:f|l|x(?:f|l)?)?))|l(?:d(?:iv|exp(?:f|l)?)|o(?:ngjmp|cal(?:time|econv)|g(?:1(?:p(?:f|l)?|0(?:f|l)?)|2(?:f|l)?|f|l|b(?:f|l)?)?)|abs|l(?:div|abs|r(?:int(?:f|l)?|ound(?:f|l)?))|r(?:int(?:f|l)?|ound(?:f|l)?)|gamma(?:f|l)?)|w(?:scanf|c(?:s(?:s(?:tr|pn)|nc(?:py|at|mp)|c(?:spn|hr|oll|py|at|mp)|to(?:imax|d|u(?:l(?:l)?|max)|k|f|l(?:d|l)?|mbs)|pbrk|ftime|len|r(?:chr|tombs)|xfrm)|to(?:b|mb)|rtomb)|printf|mem(?:set|c(?:hr|py|mp)|move))|a(?:s(?:sert|ctime|in(?:h(?:f|l)?|f|l)?)|cos(?:h(?:f|l)?|f|l)?|t(?:o(?:i|f|l(?:l)?)|exit|an(?:h(?:f|l)?|2(?:f|l)?|f|l)?)|b(?:s|ort))|g(?:et(?:s|c(?:har)?|env|wc(?:har)?)|mtime)|r(?:int(?:f|l)?|ound(?:f|l)?|e(?:name|alloc|wind|m(?:ove|quo(?:f|l)?|ainder(?:f|l)?))|a(?:nd|ise))|b(?:search|towc)|m(?:odf(?:f|l)?|em(?:set|c(?:hr|py|mp)|move)|ktime|alloc|b(?:s(?:init|towcs|rtowcs)|towc|len|r(?:towc|len))))\\b"
  47. var c_cppHighlightRules = function() {
  48. var keywordControls = (
  49. "break|case|continue|default|do|else|for|goto|if|_Pragma|" +
  50. "return|switch|while|catch|operator|try|throw|using"
  51. );
  52. var storageType = (
  53. "asm|__asm__|auto|bool|_Bool|char|_Complex|double|enum|float|" +
  54. "_Imaginary|int|long|short|signed|struct|typedef|union|unsigned|void|" +
  55. "class|wchar_t|template|char16_t|char32_t"
  56. );
  57. var storageModifiers = (
  58. "const|extern|register|restrict|static|volatile|inline|private|" +
  59. "protected|public|friend|explicit|virtual|export|mutable|typename|" +
  60. "constexpr|new|delete|alignas|alignof|decltype|noexcept|thread_local"
  61. );
  62. var keywordOperators = (
  63. "and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|typeid|xor|xor_eq" +
  64. "const_cast|dynamic_cast|reinterpret_cast|static_cast|sizeof|namespace"
  65. );
  66. var builtinConstants = (
  67. "NULL|true|false|TRUE|FALSE|nullptr"
  68. );
  69. var keywordMapper = this.$keywords = this.createKeywordMapper({
  70. "keyword.control" : keywordControls,
  71. "storage.type" : storageType,
  72. "storage.modifier" : storageModifiers,
  73. "keyword.operator" : keywordOperators,
  74. "variable.language": "this",
  75. "constant.language": builtinConstants
  76. }, "identifier");
  77. var identifierRe = "[a-zA-Z\\$_\u00a1-\uffff][a-zA-Z\d\\$_\u00a1-\uffff]*\\b";
  78. this.$rules = {
  79. "start" : [
  80. {
  81. token : "comment",
  82. regex : "//$",
  83. next : "start"
  84. }, {
  85. token : "comment",
  86. regex : "//",
  87. next : "singleLineComment"
  88. },
  89. DocCommentHighlightRules.getStartRule("doc-start"),
  90. {
  91. token : "comment", // multi line comment
  92. regex : "\\/\\*",
  93. next : "comment"
  94. }, {
  95. token : "string", // single line
  96. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  97. }, {
  98. token : "string", // multi line string start
  99. regex : '["].*\\\\$',
  100. next : "qqstring"
  101. }, {
  102. token : "string", // single line
  103. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  104. }, {
  105. token : "string", // multi line string start
  106. regex : "['].*\\\\$",
  107. next : "qstring"
  108. }, {
  109. token : "constant.numeric", // hex
  110. regex : "0[xX][0-9a-fA-F]+(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\\b"
  111. }, {
  112. token : "constant.numeric", // float
  113. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\\b"
  114. }, {
  115. token : "keyword", // pre-compiler directives
  116. regex : "#\\s*(?:include|import|pragma|line|define|undef)\\b",
  117. next : "directive"
  118. }, {
  119. token : "keyword", // special case pre-compiler directive
  120. regex : "#\\s*(?:endif|if|ifdef|else|elif|ifndef)\\b"
  121. }, {
  122. token : "support.function.C99.c",
  123. regex : cFunctions
  124. }, {
  125. token : keywordMapper,
  126. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  127. }, {
  128. token : "keyword.operator",
  129. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|new|delete|typeof|void)"
  130. }, {
  131. token : "punctuation.operator",
  132. regex : "\\?|\\:|\\,|\\;|\\."
  133. }, {
  134. token : "paren.lparen",
  135. regex : "[[({]"
  136. }, {
  137. token : "paren.rparen",
  138. regex : "[\\])}]"
  139. }, {
  140. token : "text",
  141. regex : "\\s+"
  142. }
  143. ],
  144. "comment" : [
  145. {
  146. token : "comment", // closing comment
  147. regex : ".*?\\*\\/",
  148. next : "start"
  149. }, {
  150. token : "comment", // comment spanning whole line
  151. regex : ".+"
  152. }
  153. ],
  154. "singleLineComment" : [
  155. {
  156. token : "comment",
  157. regex : /\\$/,
  158. next : "singleLineComment"
  159. }, {
  160. token : "comment",
  161. regex : /$/,
  162. next : "start"
  163. }, {
  164. defaultToken: "comment"
  165. }
  166. ],
  167. "qqstring" : [
  168. {
  169. token : "string",
  170. regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
  171. next : "start"
  172. }, {
  173. defaultToken : "string"
  174. }
  175. ],
  176. "qstring" : [
  177. {
  178. token : "string",
  179. regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
  180. next : "start"
  181. }, {
  182. defaultToken : "string"
  183. }
  184. ],
  185. "directive" : [
  186. {
  187. token : "constant.other.multiline",
  188. regex : /\\/
  189. },
  190. {
  191. token : "constant.other.multiline",
  192. regex : /.*\\/
  193. },
  194. {
  195. token : "constant.other",
  196. regex : "\\s*<.+?>",
  197. next : "start"
  198. },
  199. {
  200. token : "constant.other", // single line
  201. regex : '\\s*["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]',
  202. next : "start"
  203. },
  204. {
  205. token : "constant.other", // single line
  206. regex : "\\s*['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']",
  207. next : "start"
  208. },
  209. {
  210. token : "constant.other",
  211. regex : /[^\\\/]+/,
  212. next : "start"
  213. }
  214. ]
  215. };
  216. this.embedRules(DocCommentHighlightRules, "doc-",
  217. [ DocCommentHighlightRules.getEndRule("start") ]);
  218. };
  219. oop.inherits(c_cppHighlightRules, TextHighlightRules);
  220. exports.c_cppHighlightRules = c_cppHighlightRules;
  221. });
  222. ace.define("ace/mode/matching_brace_outdent",["require","exports","module","ace/range"], function(require, exports, module) {
  223. "use strict";
  224. var Range = require("../range").Range;
  225. var MatchingBraceOutdent = function() {};
  226. (function() {
  227. this.checkOutdent = function(line, input) {
  228. if (! /^\s+$/.test(line))
  229. return false;
  230. return /^\s*\}/.test(input);
  231. };
  232. this.autoOutdent = function(doc, row) {
  233. var line = doc.getLine(row);
  234. var match = line.match(/^(\s*\})/);
  235. if (!match) return 0;
  236. var column = match[1].length;
  237. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  238. if (!openBracePos || openBracePos.row == row) return 0;
  239. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  240. doc.replace(new Range(row, 0, row, column-1), indent);
  241. };
  242. this.$getIndent = function(line) {
  243. return line.match(/^\s*/)[0];
  244. };
  245. }).call(MatchingBraceOutdent.prototype);
  246. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  247. });
  248. ace.define("ace/mode/behaviour/cstyle",["require","exports","module","ace/lib/oop","ace/mode/behaviour","ace/token_iterator","ace/lib/lang"], function(require, exports, module) {
  249. "use strict";
  250. var oop = require("../../lib/oop");
  251. var Behaviour = require("../behaviour").Behaviour;
  252. var TokenIterator = require("../../token_iterator").TokenIterator;
  253. var lang = require("../../lib/lang");
  254. var SAFE_INSERT_IN_TOKENS =
  255. ["text", "paren.rparen", "punctuation.operator"];
  256. var SAFE_INSERT_BEFORE_TOKENS =
  257. ["text", "paren.rparen", "punctuation.operator", "comment"];
  258. var context;
  259. var contextCache = {};
  260. var initContext = function(editor) {
  261. var id = -1;
  262. if (editor.multiSelect) {
  263. id = editor.selection.index;
  264. if (contextCache.rangeCount != editor.multiSelect.rangeCount)
  265. contextCache = {rangeCount: editor.multiSelect.rangeCount};
  266. }
  267. if (contextCache[id])
  268. return context = contextCache[id];
  269. context = contextCache[id] = {
  270. autoInsertedBrackets: 0,
  271. autoInsertedRow: -1,
  272. autoInsertedLineEnd: "",
  273. maybeInsertedBrackets: 0,
  274. maybeInsertedRow: -1,
  275. maybeInsertedLineStart: "",
  276. maybeInsertedLineEnd: ""
  277. };
  278. };
  279. var getWrapped = function(selection, selected, opening, closing) {
  280. var rowDiff = selection.end.row - selection.start.row;
  281. return {
  282. text: opening + selected + closing,
  283. selection: [
  284. 0,
  285. selection.start.column + 1,
  286. rowDiff,
  287. selection.end.column + (rowDiff ? 0 : 1)
  288. ]
  289. };
  290. };
  291. var CstyleBehaviour = function() {
  292. this.add("braces", "insertion", function(state, action, editor, session, text) {
  293. var cursor = editor.getCursorPosition();
  294. var line = session.doc.getLine(cursor.row);
  295. if (text == '{') {
  296. initContext(editor);
  297. var selection = editor.getSelectionRange();
  298. var selected = session.doc.getTextRange(selection);
  299. if (selected !== "" && selected !== "{" && editor.getWrapBehavioursEnabled()) {
  300. return getWrapped(selection, selected, '{', '}');
  301. } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
  302. if (/[\]\}\)]/.test(line[cursor.column]) || editor.inMultiSelectMode) {
  303. CstyleBehaviour.recordAutoInsert(editor, session, "}");
  304. return {
  305. text: '{}',
  306. selection: [1, 1]
  307. };
  308. } else {
  309. CstyleBehaviour.recordMaybeInsert(editor, session, "{");
  310. return {
  311. text: '{',
  312. selection: [1, 1]
  313. };
  314. }
  315. }
  316. } else if (text == '}') {
  317. initContext(editor);
  318. var rightChar = line.substring(cursor.column, cursor.column + 1);
  319. if (rightChar == '}') {
  320. var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
  321. if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
  322. CstyleBehaviour.popAutoInsertedClosing();
  323. return {
  324. text: '',
  325. selection: [1, 1]
  326. };
  327. }
  328. }
  329. } else if (text == "\n" || text == "\r\n") {
  330. initContext(editor);
  331. var closing = "";
  332. if (CstyleBehaviour.isMaybeInsertedClosing(cursor, line)) {
  333. closing = lang.stringRepeat("}", context.maybeInsertedBrackets);
  334. CstyleBehaviour.clearMaybeInsertedClosing();
  335. }
  336. var rightChar = line.substring(cursor.column, cursor.column + 1);
  337. if (rightChar === '}') {
  338. var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column+1}, '}');
  339. if (!openBracePos)
  340. return null;
  341. var next_indent = this.$getIndent(session.getLine(openBracePos.row));
  342. } else if (closing) {
  343. var next_indent = this.$getIndent(line);
  344. } else {
  345. CstyleBehaviour.clearMaybeInsertedClosing();
  346. return;
  347. }
  348. var indent = next_indent + session.getTabString();
  349. return {
  350. text: '\n' + indent + '\n' + next_indent + closing,
  351. selection: [1, indent.length, 1, indent.length]
  352. };
  353. } else {
  354. CstyleBehaviour.clearMaybeInsertedClosing();
  355. }
  356. });
  357. this.add("braces", "deletion", function(state, action, editor, session, range) {
  358. var selected = session.doc.getTextRange(range);
  359. if (!range.isMultiLine() && selected == '{') {
  360. initContext(editor);
  361. var line = session.doc.getLine(range.start.row);
  362. var rightChar = line.substring(range.end.column, range.end.column + 1);
  363. if (rightChar == '}') {
  364. range.end.column++;
  365. return range;
  366. } else {
  367. context.maybeInsertedBrackets--;
  368. }
  369. }
  370. });
  371. this.add("parens", "insertion", function(state, action, editor, session, text) {
  372. if (text == '(') {
  373. initContext(editor);
  374. var selection = editor.getSelectionRange();
  375. var selected = session.doc.getTextRange(selection);
  376. if (selected !== "" && editor.getWrapBehavioursEnabled()) {
  377. return getWrapped(selection, selected, '(', ')');
  378. } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
  379. CstyleBehaviour.recordAutoInsert(editor, session, ")");
  380. return {
  381. text: '()',
  382. selection: [1, 1]
  383. };
  384. }
  385. } else if (text == ')') {
  386. initContext(editor);
  387. var cursor = editor.getCursorPosition();
  388. var line = session.doc.getLine(cursor.row);
  389. var rightChar = line.substring(cursor.column, cursor.column + 1);
  390. if (rightChar == ')') {
  391. var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row});
  392. if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
  393. CstyleBehaviour.popAutoInsertedClosing();
  394. return {
  395. text: '',
  396. selection: [1, 1]
  397. };
  398. }
  399. }
  400. }
  401. });
  402. this.add("parens", "deletion", function(state, action, editor, session, range) {
  403. var selected = session.doc.getTextRange(range);
  404. if (!range.isMultiLine() && selected == '(') {
  405. initContext(editor);
  406. var line = session.doc.getLine(range.start.row);
  407. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  408. if (rightChar == ')') {
  409. range.end.column++;
  410. return range;
  411. }
  412. }
  413. });
  414. this.add("brackets", "insertion", function(state, action, editor, session, text) {
  415. if (text == '[') {
  416. initContext(editor);
  417. var selection = editor.getSelectionRange();
  418. var selected = session.doc.getTextRange(selection);
  419. if (selected !== "" && editor.getWrapBehavioursEnabled()) {
  420. return getWrapped(selection, selected, '[', ']');
  421. } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
  422. CstyleBehaviour.recordAutoInsert(editor, session, "]");
  423. return {
  424. text: '[]',
  425. selection: [1, 1]
  426. };
  427. }
  428. } else if (text == ']') {
  429. initContext(editor);
  430. var cursor = editor.getCursorPosition();
  431. var line = session.doc.getLine(cursor.row);
  432. var rightChar = line.substring(cursor.column, cursor.column + 1);
  433. if (rightChar == ']') {
  434. var matching = session.$findOpeningBracket(']', {column: cursor.column + 1, row: cursor.row});
  435. if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
  436. CstyleBehaviour.popAutoInsertedClosing();
  437. return {
  438. text: '',
  439. selection: [1, 1]
  440. };
  441. }
  442. }
  443. }
  444. });
  445. this.add("brackets", "deletion", function(state, action, editor, session, range) {
  446. var selected = session.doc.getTextRange(range);
  447. if (!range.isMultiLine() && selected == '[') {
  448. initContext(editor);
  449. var line = session.doc.getLine(range.start.row);
  450. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  451. if (rightChar == ']') {
  452. range.end.column++;
  453. return range;
  454. }
  455. }
  456. });
  457. this.add("string_dquotes", "insertion", function(state, action, editor, session, text) {
  458. if (text == '"' || text == "'") {
  459. initContext(editor);
  460. var quote = text;
  461. var selection = editor.getSelectionRange();
  462. var selected = session.doc.getTextRange(selection);
  463. if (selected !== "" && selected !== "'" && selected != '"' && editor.getWrapBehavioursEnabled()) {
  464. return getWrapped(selection, selected, quote, quote);
  465. } else if (!selected) {
  466. var cursor = editor.getCursorPosition();
  467. var line = session.doc.getLine(cursor.row);
  468. var leftChar = line.substring(cursor.column-1, cursor.column);
  469. var rightChar = line.substring(cursor.column, cursor.column + 1);
  470. var token = session.getTokenAt(cursor.row, cursor.column);
  471. var rightToken = session.getTokenAt(cursor.row, cursor.column + 1);
  472. if (leftChar == "\\" && token && /escape/.test(token.type))
  473. return null;
  474. var stringBefore = token && /string|escape/.test(token.type);
  475. var stringAfter = !rightToken || /string|escape/.test(rightToken.type);
  476. var pair;
  477. if (rightChar == quote) {
  478. pair = stringBefore !== stringAfter;
  479. } else {
  480. if (stringBefore && !stringAfter)
  481. return null; // wrap string with different quote
  482. if (stringBefore && stringAfter)
  483. return null; // do not pair quotes inside strings
  484. var wordRe = session.$mode.tokenRe;
  485. wordRe.lastIndex = 0;
  486. var isWordBefore = wordRe.test(leftChar);
  487. wordRe.lastIndex = 0;
  488. var isWordAfter = wordRe.test(leftChar);
  489. if (isWordBefore || isWordAfter)
  490. return null; // before or after alphanumeric
  491. if (rightChar && !/[\s;,.})\]\\]/.test(rightChar))
  492. return null; // there is rightChar and it isn't closing
  493. pair = true;
  494. }
  495. return {
  496. text: pair ? quote + quote : "",
  497. selection: [1,1]
  498. };
  499. }
  500. }
  501. });
  502. this.add("string_dquotes", "deletion", function(state, action, editor, session, range) {
  503. var selected = session.doc.getTextRange(range);
  504. if (!range.isMultiLine() && (selected == '"' || selected == "'")) {
  505. initContext(editor);
  506. var line = session.doc.getLine(range.start.row);
  507. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  508. if (rightChar == selected) {
  509. range.end.column++;
  510. return range;
  511. }
  512. }
  513. });
  514. };
  515. CstyleBehaviour.isSaneInsertion = function(editor, session) {
  516. var cursor = editor.getCursorPosition();
  517. var iterator = new TokenIterator(session, cursor.row, cursor.column);
  518. if (!this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS)) {
  519. var iterator2 = new TokenIterator(session, cursor.row, cursor.column + 1);
  520. if (!this.$matchTokenType(iterator2.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS))
  521. return false;
  522. }
  523. iterator.stepForward();
  524. return iterator.getCurrentTokenRow() !== cursor.row ||
  525. this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_BEFORE_TOKENS);
  526. };
  527. CstyleBehaviour.$matchTokenType = function(token, types) {
  528. return types.indexOf(token.type || token) > -1;
  529. };
  530. CstyleBehaviour.recordAutoInsert = function(editor, session, bracket) {
  531. var cursor = editor.getCursorPosition();
  532. var line = session.doc.getLine(cursor.row);
  533. if (!this.isAutoInsertedClosing(cursor, line, context.autoInsertedLineEnd[0]))
  534. context.autoInsertedBrackets = 0;
  535. context.autoInsertedRow = cursor.row;
  536. context.autoInsertedLineEnd = bracket + line.substr(cursor.column);
  537. context.autoInsertedBrackets++;
  538. };
  539. CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
  540. var cursor = editor.getCursorPosition();
  541. var line = session.doc.getLine(cursor.row);
  542. if (!this.isMaybeInsertedClosing(cursor, line))
  543. context.maybeInsertedBrackets = 0;
  544. context.maybeInsertedRow = cursor.row;
  545. context.maybeInsertedLineStart = line.substr(0, cursor.column) + bracket;
  546. context.maybeInsertedLineEnd = line.substr(cursor.column);
  547. context.maybeInsertedBrackets++;
  548. };
  549. CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
  550. return context.autoInsertedBrackets > 0 &&
  551. cursor.row === context.autoInsertedRow &&
  552. bracket === context.autoInsertedLineEnd[0] &&
  553. line.substr(cursor.column) === context.autoInsertedLineEnd;
  554. };
  555. CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
  556. return context.maybeInsertedBrackets > 0 &&
  557. cursor.row === context.maybeInsertedRow &&
  558. line.substr(cursor.column) === context.maybeInsertedLineEnd &&
  559. line.substr(0, cursor.column) == context.maybeInsertedLineStart;
  560. };
  561. CstyleBehaviour.popAutoInsertedClosing = function() {
  562. context.autoInsertedLineEnd = context.autoInsertedLineEnd.substr(1);
  563. context.autoInsertedBrackets--;
  564. };
  565. CstyleBehaviour.clearMaybeInsertedClosing = function() {
  566. if (context) {
  567. context.maybeInsertedBrackets = 0;
  568. context.maybeInsertedRow = -1;
  569. }
  570. };
  571. oop.inherits(CstyleBehaviour, Behaviour);
  572. exports.CstyleBehaviour = CstyleBehaviour;
  573. });
  574. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  575. "use strict";
  576. var oop = require("../../lib/oop");
  577. var Range = require("../../range").Range;
  578. var BaseFoldMode = require("./fold_mode").FoldMode;
  579. var FoldMode = exports.FoldMode = function(commentRegex) {
  580. if (commentRegex) {
  581. this.foldingStartMarker = new RegExp(
  582. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  583. );
  584. this.foldingStopMarker = new RegExp(
  585. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  586. );
  587. }
  588. };
  589. oop.inherits(FoldMode, BaseFoldMode);
  590. (function() {
  591. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  592. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  593. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  594. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  595. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  596. this._getFoldWidgetBase = this.getFoldWidget;
  597. this.getFoldWidget = function(session, foldStyle, row) {
  598. var line = session.getLine(row);
  599. if (this.singleLineBlockCommentRe.test(line)) {
  600. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  601. return "";
  602. }
  603. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  604. if (!fw && this.startRegionRe.test(line))
  605. return "start"; // lineCommentRegionStart
  606. return fw;
  607. };
  608. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  609. var line = session.getLine(row);
  610. if (this.startRegionRe.test(line))
  611. return this.getCommentRegionBlock(session, line, row);
  612. var match = line.match(this.foldingStartMarker);
  613. if (match) {
  614. var i = match.index;
  615. if (match[1])
  616. return this.openingBracketBlock(session, match[1], row, i);
  617. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  618. if (range && !range.isMultiLine()) {
  619. if (forceMultiline) {
  620. range = this.getSectionRange(session, row);
  621. } else if (foldStyle != "all")
  622. range = null;
  623. }
  624. return range;
  625. }
  626. if (foldStyle === "markbegin")
  627. return;
  628. var match = line.match(this.foldingStopMarker);
  629. if (match) {
  630. var i = match.index + match[0].length;
  631. if (match[1])
  632. return this.closingBracketBlock(session, match[1], row, i);
  633. return session.getCommentFoldRange(row, i, -1);
  634. }
  635. };
  636. this.getSectionRange = function(session, row) {
  637. var line = session.getLine(row);
  638. var startIndent = line.search(/\S/);
  639. var startRow = row;
  640. var startColumn = line.length;
  641. row = row + 1;
  642. var endRow = row;
  643. var maxRow = session.getLength();
  644. while (++row < maxRow) {
  645. line = session.getLine(row);
  646. var indent = line.search(/\S/);
  647. if (indent === -1)
  648. continue;
  649. if (startIndent > indent)
  650. break;
  651. var subRange = this.getFoldWidgetRange(session, "all", row);
  652. if (subRange) {
  653. if (subRange.start.row <= startRow) {
  654. break;
  655. } else if (subRange.isMultiLine()) {
  656. row = subRange.end.row;
  657. } else if (startIndent == indent) {
  658. break;
  659. }
  660. }
  661. endRow = row;
  662. }
  663. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  664. };
  665. this.getCommentRegionBlock = function(session, line, row) {
  666. var startColumn = line.search(/\s*$/);
  667. var maxRow = session.getLength();
  668. var startRow = row;
  669. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  670. var depth = 1;
  671. while (++row < maxRow) {
  672. line = session.getLine(row);
  673. var m = re.exec(line);
  674. if (!m) continue;
  675. if (m[1]) depth--;
  676. else depth++;
  677. if (!depth) break;
  678. }
  679. var endRow = row;
  680. if (endRow > startRow) {
  681. return new Range(startRow, startColumn, endRow, line.length);
  682. }
  683. };
  684. }).call(FoldMode.prototype);
  685. });
  686. ace.define("ace/mode/c_cpp",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/c_cpp_highlight_rules","ace/mode/matching_brace_outdent","ace/range","ace/mode/behaviour/cstyle","ace/mode/folding/cstyle"], function(require, exports, module) {
  687. "use strict";
  688. var oop = require("../lib/oop");
  689. var TextMode = require("./text").Mode;
  690. var c_cppHighlightRules = require("./c_cpp_highlight_rules").c_cppHighlightRules;
  691. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  692. var Range = require("../range").Range;
  693. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  694. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  695. var Mode = function() {
  696. this.HighlightRules = c_cppHighlightRules;
  697. this.$outdent = new MatchingBraceOutdent();
  698. this.$behaviour = new CstyleBehaviour();
  699. this.foldingRules = new CStyleFoldMode();
  700. };
  701. oop.inherits(Mode, TextMode);
  702. (function() {
  703. this.lineCommentStart = "//";
  704. this.blockComment = {start: "/*", end: "*/"};
  705. this.getNextLineIndent = function(state, line, tab) {
  706. var indent = this.$getIndent(line);
  707. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  708. var tokens = tokenizedLine.tokens;
  709. var endState = tokenizedLine.state;
  710. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  711. return indent;
  712. }
  713. if (state == "start") {
  714. var match = line.match(/^.*[\{\(\[]\s*$/);
  715. if (match) {
  716. indent += tab;
  717. }
  718. } else if (state == "doc-start") {
  719. if (endState == "start") {
  720. return "";
  721. }
  722. var match = line.match(/^\s*(\/?)\*/);
  723. if (match) {
  724. if (match[1]) {
  725. indent += " ";
  726. }
  727. indent += "* ";
  728. }
  729. }
  730. return indent;
  731. };
  732. this.checkOutdent = function(state, line, input) {
  733. return this.$outdent.checkOutdent(line, input);
  734. };
  735. this.autoOutdent = function(state, doc, row) {
  736. this.$outdent.autoOutdent(doc, row);
  737. };
  738. this.$id = "ace/mode/c_cpp";
  739. }).call(Mode.prototype);
  740. exports.Mode = Mode;
  741. });