No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mode-c_cpp.js 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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"
  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"
  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"
  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 : "singleLineComment"
  84. },
  85. DocCommentHighlightRules.getStartRule("doc-start"),
  86. {
  87. token : "comment", // multi line comment
  88. regex : "\\/\\*",
  89. next : "comment"
  90. }, {
  91. token : "string", // single line
  92. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  93. }, {
  94. token : "string", // multi line string start
  95. regex : '["].*\\\\$',
  96. next : "qqstring"
  97. }, {
  98. token : "string", // single line
  99. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  100. }, {
  101. token : "string", // multi line string start
  102. regex : "['].*\\\\$",
  103. next : "qstring"
  104. }, {
  105. token : "constant.numeric", // hex
  106. regex : "0[xX][0-9a-fA-F]+(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\\b"
  107. }, {
  108. token : "constant.numeric", // float
  109. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\\b"
  110. }, {
  111. token : "keyword", // pre-compiler directives
  112. regex : "#\\s*(?:include|import|pragma|line|define|undef|if|ifdef|else|elif|ifndef)\\b",
  113. next : "directive"
  114. }, {
  115. token : "keyword", // special case pre-compiler directive
  116. regex : "(?:#\\s*endif)\\b"
  117. }, {
  118. token : "support.function.C99.c",
  119. regex : cFunctions
  120. }, {
  121. token : keywordMapper,
  122. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b"
  123. }, {
  124. token : "keyword.operator",
  125. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^=|\\b(?:in|new|delete|typeof|void)"
  126. }, {
  127. token : "punctuation.operator",
  128. regex : "\\?|\\:|\\,|\\;|\\."
  129. }, {
  130. token : "paren.lparen",
  131. regex : "[[({]"
  132. }, {
  133. token : "paren.rparen",
  134. regex : "[\\])}]"
  135. }, {
  136. token : "text",
  137. regex : "\\s+"
  138. }
  139. ],
  140. "comment" : [
  141. {
  142. token : "comment", // closing comment
  143. regex : ".*?\\*\\/",
  144. next : "start"
  145. }, {
  146. token : "comment", // comment spanning whole line
  147. regex : ".+"
  148. }
  149. ],
  150. "singleLineComment" : [
  151. {
  152. token : "comment",
  153. regex : /\\$/,
  154. next : "singleLineComment"
  155. }, {
  156. token : "comment",
  157. regex : /$/,
  158. next : "start"
  159. }, {
  160. defaultToken: "comment"
  161. }
  162. ],
  163. "qqstring" : [
  164. {
  165. token : "string",
  166. regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
  167. next : "start"
  168. }, {
  169. defaultToken : "string"
  170. }
  171. ],
  172. "qstring" : [
  173. {
  174. token : "string",
  175. regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
  176. next : "start"
  177. }, {
  178. defaultToken : "string"
  179. }
  180. ],
  181. "directive" : [
  182. {
  183. token : "constant.other.multiline",
  184. regex : /\\/
  185. },
  186. {
  187. token : "constant.other.multiline",
  188. regex : /.*\\/
  189. },
  190. {
  191. token : "constant.other",
  192. regex : "\\s*<.+?>",
  193. next : "start"
  194. },
  195. {
  196. token : "constant.other", // single line
  197. regex : '\\s*["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]',
  198. next : "start"
  199. },
  200. {
  201. token : "constant.other", // single line
  202. regex : "\\s*['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']",
  203. next : "start"
  204. },
  205. {
  206. token : "constant.other",
  207. regex : /[^\\\/]+/,
  208. next : "start"
  209. }
  210. ]
  211. };
  212. this.embedRules(DocCommentHighlightRules, "doc-",
  213. [ DocCommentHighlightRules.getEndRule("start") ]);
  214. };
  215. oop.inherits(c_cppHighlightRules, TextHighlightRules);
  216. exports.c_cppHighlightRules = c_cppHighlightRules;
  217. });
  218. ace.define("ace/mode/matching_brace_outdent",["require","exports","module","ace/range"], function(require, exports, module) {
  219. "use strict";
  220. var Range = require("../range").Range;
  221. var MatchingBraceOutdent = function() {};
  222. (function() {
  223. this.checkOutdent = function(line, input) {
  224. if (! /^\s+$/.test(line))
  225. return false;
  226. return /^\s*\}/.test(input);
  227. };
  228. this.autoOutdent = function(doc, row) {
  229. var line = doc.getLine(row);
  230. var match = line.match(/^(\s*\})/);
  231. if (!match) return 0;
  232. var column = match[1].length;
  233. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  234. if (!openBracePos || openBracePos.row == row) return 0;
  235. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  236. doc.replace(new Range(row, 0, row, column-1), indent);
  237. };
  238. this.$getIndent = function(line) {
  239. return line.match(/^\s*/)[0];
  240. };
  241. }).call(MatchingBraceOutdent.prototype);
  242. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  243. });
  244. 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) {
  245. "use strict";
  246. var oop = require("../../lib/oop");
  247. var Behaviour = require("../behaviour").Behaviour;
  248. var TokenIterator = require("../../token_iterator").TokenIterator;
  249. var lang = require("../../lib/lang");
  250. var SAFE_INSERT_IN_TOKENS =
  251. ["text", "paren.rparen", "punctuation.operator"];
  252. var SAFE_INSERT_BEFORE_TOKENS =
  253. ["text", "paren.rparen", "punctuation.operator", "comment"];
  254. var context;
  255. var contextCache = {};
  256. var initContext = function(editor) {
  257. var id = -1;
  258. if (editor.multiSelect) {
  259. id = editor.selection.index;
  260. if (contextCache.rangeCount != editor.multiSelect.rangeCount)
  261. contextCache = {rangeCount: editor.multiSelect.rangeCount};
  262. }
  263. if (contextCache[id])
  264. return context = contextCache[id];
  265. context = contextCache[id] = {
  266. autoInsertedBrackets: 0,
  267. autoInsertedRow: -1,
  268. autoInsertedLineEnd: "",
  269. maybeInsertedBrackets: 0,
  270. maybeInsertedRow: -1,
  271. maybeInsertedLineStart: "",
  272. maybeInsertedLineEnd: ""
  273. };
  274. };
  275. var CstyleBehaviour = function() {
  276. this.add("braces", "insertion", function(state, action, editor, session, text) {
  277. var cursor = editor.getCursorPosition();
  278. var line = session.doc.getLine(cursor.row);
  279. if (text == '{') {
  280. initContext(editor);
  281. var selection = editor.getSelectionRange();
  282. var selected = session.doc.getTextRange(selection);
  283. if (selected !== "" && selected !== "{" && editor.getWrapBehavioursEnabled()) {
  284. return {
  285. text: '{' + selected + '}',
  286. selection: false
  287. };
  288. } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
  289. if (/[\]\}\)]/.test(line[cursor.column]) || editor.inMultiSelectMode) {
  290. CstyleBehaviour.recordAutoInsert(editor, session, "}");
  291. return {
  292. text: '{}',
  293. selection: [1, 1]
  294. };
  295. } else {
  296. CstyleBehaviour.recordMaybeInsert(editor, session, "{");
  297. return {
  298. text: '{',
  299. selection: [1, 1]
  300. };
  301. }
  302. }
  303. } else if (text == '}') {
  304. initContext(editor);
  305. var rightChar = line.substring(cursor.column, cursor.column + 1);
  306. if (rightChar == '}') {
  307. var matching = session.$findOpeningBracket('}', {column: cursor.column + 1, row: cursor.row});
  308. if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
  309. CstyleBehaviour.popAutoInsertedClosing();
  310. return {
  311. text: '',
  312. selection: [1, 1]
  313. };
  314. }
  315. }
  316. } else if (text == "\n" || text == "\r\n") {
  317. initContext(editor);
  318. var closing = "";
  319. if (CstyleBehaviour.isMaybeInsertedClosing(cursor, line)) {
  320. closing = lang.stringRepeat("}", context.maybeInsertedBrackets);
  321. CstyleBehaviour.clearMaybeInsertedClosing();
  322. }
  323. var rightChar = line.substring(cursor.column, cursor.column + 1);
  324. if (rightChar === '}') {
  325. var openBracePos = session.findMatchingBracket({row: cursor.row, column: cursor.column+1}, '}');
  326. if (!openBracePos)
  327. return null;
  328. var next_indent = this.$getIndent(session.getLine(openBracePos.row));
  329. } else if (closing) {
  330. var next_indent = this.$getIndent(line);
  331. } else {
  332. CstyleBehaviour.clearMaybeInsertedClosing();
  333. return;
  334. }
  335. var indent = next_indent + session.getTabString();
  336. return {
  337. text: '\n' + indent + '\n' + next_indent + closing,
  338. selection: [1, indent.length, 1, indent.length]
  339. };
  340. } else {
  341. CstyleBehaviour.clearMaybeInsertedClosing();
  342. }
  343. });
  344. this.add("braces", "deletion", function(state, action, editor, session, range) {
  345. var selected = session.doc.getTextRange(range);
  346. if (!range.isMultiLine() && selected == '{') {
  347. initContext(editor);
  348. var line = session.doc.getLine(range.start.row);
  349. var rightChar = line.substring(range.end.column, range.end.column + 1);
  350. if (rightChar == '}') {
  351. range.end.column++;
  352. return range;
  353. } else {
  354. context.maybeInsertedBrackets--;
  355. }
  356. }
  357. });
  358. this.add("parens", "insertion", function(state, action, editor, session, text) {
  359. if (text == '(') {
  360. initContext(editor);
  361. var selection = editor.getSelectionRange();
  362. var selected = session.doc.getTextRange(selection);
  363. if (selected !== "" && editor.getWrapBehavioursEnabled()) {
  364. return {
  365. text: '(' + selected + ')',
  366. selection: false
  367. };
  368. } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
  369. CstyleBehaviour.recordAutoInsert(editor, session, ")");
  370. return {
  371. text: '()',
  372. selection: [1, 1]
  373. };
  374. }
  375. } else if (text == ')') {
  376. initContext(editor);
  377. var cursor = editor.getCursorPosition();
  378. var line = session.doc.getLine(cursor.row);
  379. var rightChar = line.substring(cursor.column, cursor.column + 1);
  380. if (rightChar == ')') {
  381. var matching = session.$findOpeningBracket(')', {column: cursor.column + 1, row: cursor.row});
  382. if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
  383. CstyleBehaviour.popAutoInsertedClosing();
  384. return {
  385. text: '',
  386. selection: [1, 1]
  387. };
  388. }
  389. }
  390. }
  391. });
  392. this.add("parens", "deletion", function(state, action, editor, session, range) {
  393. var selected = session.doc.getTextRange(range);
  394. if (!range.isMultiLine() && selected == '(') {
  395. initContext(editor);
  396. var line = session.doc.getLine(range.start.row);
  397. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  398. if (rightChar == ')') {
  399. range.end.column++;
  400. return range;
  401. }
  402. }
  403. });
  404. this.add("brackets", "insertion", function(state, action, editor, session, text) {
  405. if (text == '[') {
  406. initContext(editor);
  407. var selection = editor.getSelectionRange();
  408. var selected = session.doc.getTextRange(selection);
  409. if (selected !== "" && editor.getWrapBehavioursEnabled()) {
  410. return {
  411. text: '[' + selected + ']',
  412. selection: false
  413. };
  414. } else if (CstyleBehaviour.isSaneInsertion(editor, session)) {
  415. CstyleBehaviour.recordAutoInsert(editor, session, "]");
  416. return {
  417. text: '[]',
  418. selection: [1, 1]
  419. };
  420. }
  421. } else if (text == ']') {
  422. initContext(editor);
  423. var cursor = editor.getCursorPosition();
  424. var line = session.doc.getLine(cursor.row);
  425. var rightChar = line.substring(cursor.column, cursor.column + 1);
  426. if (rightChar == ']') {
  427. var matching = session.$findOpeningBracket(']', {column: cursor.column + 1, row: cursor.row});
  428. if (matching !== null && CstyleBehaviour.isAutoInsertedClosing(cursor, line, text)) {
  429. CstyleBehaviour.popAutoInsertedClosing();
  430. return {
  431. text: '',
  432. selection: [1, 1]
  433. };
  434. }
  435. }
  436. }
  437. });
  438. this.add("brackets", "deletion", function(state, action, editor, session, range) {
  439. var selected = session.doc.getTextRange(range);
  440. if (!range.isMultiLine() && selected == '[') {
  441. initContext(editor);
  442. var line = session.doc.getLine(range.start.row);
  443. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  444. if (rightChar == ']') {
  445. range.end.column++;
  446. return range;
  447. }
  448. }
  449. });
  450. this.add("string_dquotes", "insertion", function(state, action, editor, session, text) {
  451. if (text == '"' || text == "'") {
  452. initContext(editor);
  453. var quote = text;
  454. var selection = editor.getSelectionRange();
  455. var selected = session.doc.getTextRange(selection);
  456. if (selected !== "" && selected !== "'" && selected != '"' && editor.getWrapBehavioursEnabled()) {
  457. return {
  458. text: quote + selected + quote,
  459. selection: false
  460. };
  461. } else {
  462. var cursor = editor.getCursorPosition();
  463. var line = session.doc.getLine(cursor.row);
  464. var leftChar = line.substring(cursor.column-1, cursor.column);
  465. var rightChar = line.substring(cursor.column, cursor.column + 1);
  466. var token = session.getTokenAt(cursor.row, cursor.column);
  467. var rightToken = session.getTokenAt(cursor.row, cursor.column + 1);
  468. if (leftChar == "\\" && token && /escape/.test(token.type))
  469. return null;
  470. var stringBefore = token && /string/.test(token.type);
  471. var stringAfter = !rightToken || /string/.test(rightToken.type);
  472. var pair;
  473. if (rightChar == quote) {
  474. pair = stringBefore !== stringAfter;
  475. } else {
  476. if (stringBefore && !stringAfter)
  477. return null; // wrap string with different quote
  478. if (stringBefore && stringAfter)
  479. return null; // do not pair quotes inside strings
  480. var wordRe = session.$mode.tokenRe;
  481. wordRe.lastIndex = 0;
  482. var isWordBefore = wordRe.test(leftChar);
  483. wordRe.lastIndex = 0;
  484. var isWordAfter = wordRe.test(leftChar);
  485. if (isWordBefore || isWordAfter)
  486. return null; // before or after alphanumeric
  487. if (rightChar && !/[\s;,.})\]\\]/.test(rightChar))
  488. return null; // there is rightChar and it isn't closing
  489. pair = true;
  490. }
  491. return {
  492. text: pair ? quote + quote : "",
  493. selection: [1,1]
  494. };
  495. }
  496. }
  497. });
  498. this.add("string_dquotes", "deletion", function(state, action, editor, session, range) {
  499. var selected = session.doc.getTextRange(range);
  500. if (!range.isMultiLine() && (selected == '"' || selected == "'")) {
  501. initContext(editor);
  502. var line = session.doc.getLine(range.start.row);
  503. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  504. if (rightChar == selected) {
  505. range.end.column++;
  506. return range;
  507. }
  508. }
  509. });
  510. };
  511. CstyleBehaviour.isSaneInsertion = function(editor, session) {
  512. var cursor = editor.getCursorPosition();
  513. var iterator = new TokenIterator(session, cursor.row, cursor.column);
  514. if (!this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS)) {
  515. var iterator2 = new TokenIterator(session, cursor.row, cursor.column + 1);
  516. if (!this.$matchTokenType(iterator2.getCurrentToken() || "text", SAFE_INSERT_IN_TOKENS))
  517. return false;
  518. }
  519. iterator.stepForward();
  520. return iterator.getCurrentTokenRow() !== cursor.row ||
  521. this.$matchTokenType(iterator.getCurrentToken() || "text", SAFE_INSERT_BEFORE_TOKENS);
  522. };
  523. CstyleBehaviour.$matchTokenType = function(token, types) {
  524. return types.indexOf(token.type || token) > -1;
  525. };
  526. CstyleBehaviour.recordAutoInsert = function(editor, session, bracket) {
  527. var cursor = editor.getCursorPosition();
  528. var line = session.doc.getLine(cursor.row);
  529. if (!this.isAutoInsertedClosing(cursor, line, context.autoInsertedLineEnd[0]))
  530. context.autoInsertedBrackets = 0;
  531. context.autoInsertedRow = cursor.row;
  532. context.autoInsertedLineEnd = bracket + line.substr(cursor.column);
  533. context.autoInsertedBrackets++;
  534. };
  535. CstyleBehaviour.recordMaybeInsert = function(editor, session, bracket) {
  536. var cursor = editor.getCursorPosition();
  537. var line = session.doc.getLine(cursor.row);
  538. if (!this.isMaybeInsertedClosing(cursor, line))
  539. context.maybeInsertedBrackets = 0;
  540. context.maybeInsertedRow = cursor.row;
  541. context.maybeInsertedLineStart = line.substr(0, cursor.column) + bracket;
  542. context.maybeInsertedLineEnd = line.substr(cursor.column);
  543. context.maybeInsertedBrackets++;
  544. };
  545. CstyleBehaviour.isAutoInsertedClosing = function(cursor, line, bracket) {
  546. return context.autoInsertedBrackets > 0 &&
  547. cursor.row === context.autoInsertedRow &&
  548. bracket === context.autoInsertedLineEnd[0] &&
  549. line.substr(cursor.column) === context.autoInsertedLineEnd;
  550. };
  551. CstyleBehaviour.isMaybeInsertedClosing = function(cursor, line) {
  552. return context.maybeInsertedBrackets > 0 &&
  553. cursor.row === context.maybeInsertedRow &&
  554. line.substr(cursor.column) === context.maybeInsertedLineEnd &&
  555. line.substr(0, cursor.column) == context.maybeInsertedLineStart;
  556. };
  557. CstyleBehaviour.popAutoInsertedClosing = function() {
  558. context.autoInsertedLineEnd = context.autoInsertedLineEnd.substr(1);
  559. context.autoInsertedBrackets--;
  560. };
  561. CstyleBehaviour.clearMaybeInsertedClosing = function() {
  562. if (context) {
  563. context.maybeInsertedBrackets = 0;
  564. context.maybeInsertedRow = -1;
  565. }
  566. };
  567. oop.inherits(CstyleBehaviour, Behaviour);
  568. exports.CstyleBehaviour = CstyleBehaviour;
  569. });
  570. ace.define("ace/mode/folding/cstyle",["require","exports","module","ace/lib/oop","ace/range","ace/mode/folding/fold_mode"], function(require, exports, module) {
  571. "use strict";
  572. var oop = require("../../lib/oop");
  573. var Range = require("../../range").Range;
  574. var BaseFoldMode = require("./fold_mode").FoldMode;
  575. var FoldMode = exports.FoldMode = function(commentRegex) {
  576. if (commentRegex) {
  577. this.foldingStartMarker = new RegExp(
  578. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  579. );
  580. this.foldingStopMarker = new RegExp(
  581. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  582. );
  583. }
  584. };
  585. oop.inherits(FoldMode, BaseFoldMode);
  586. (function() {
  587. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  588. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  589. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  590. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  591. this.startRegionRe = /^\s*(\/\*|\/\/)#region\b/;
  592. this._getFoldWidgetBase = this.getFoldWidget;
  593. this.getFoldWidget = function(session, foldStyle, row) {
  594. var line = session.getLine(row);
  595. if (this.singleLineBlockCommentRe.test(line)) {
  596. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  597. return "";
  598. }
  599. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  600. if (!fw && this.startRegionRe.test(line))
  601. return "start"; // lineCommentRegionStart
  602. return fw;
  603. };
  604. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  605. var line = session.getLine(row);
  606. if (this.startRegionRe.test(line))
  607. return this.getCommentRegionBlock(session, line, row);
  608. var match = line.match(this.foldingStartMarker);
  609. if (match) {
  610. var i = match.index;
  611. if (match[1])
  612. return this.openingBracketBlock(session, match[1], row, i);
  613. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  614. if (range && !range.isMultiLine()) {
  615. if (forceMultiline) {
  616. range = this.getSectionRange(session, row);
  617. } else if (foldStyle != "all")
  618. range = null;
  619. }
  620. return range;
  621. }
  622. if (foldStyle === "markbegin")
  623. return;
  624. var match = line.match(this.foldingStopMarker);
  625. if (match) {
  626. var i = match.index + match[0].length;
  627. if (match[1])
  628. return this.closingBracketBlock(session, match[1], row, i);
  629. return session.getCommentFoldRange(row, i, -1);
  630. }
  631. };
  632. this.getSectionRange = function(session, row) {
  633. var line = session.getLine(row);
  634. var startIndent = line.search(/\S/);
  635. var startRow = row;
  636. var startColumn = line.length;
  637. row = row + 1;
  638. var endRow = row;
  639. var maxRow = session.getLength();
  640. while (++row < maxRow) {
  641. line = session.getLine(row);
  642. var indent = line.search(/\S/);
  643. if (indent === -1)
  644. continue;
  645. if (startIndent > indent)
  646. break;
  647. var subRange = this.getFoldWidgetRange(session, "all", row);
  648. if (subRange) {
  649. if (subRange.start.row <= startRow) {
  650. break;
  651. } else if (subRange.isMultiLine()) {
  652. row = subRange.end.row;
  653. } else if (startIndent == indent) {
  654. break;
  655. }
  656. }
  657. endRow = row;
  658. }
  659. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  660. };
  661. this.getCommentRegionBlock = function(session, line, row) {
  662. var startColumn = line.search(/\s*$/);
  663. var maxRow = session.getLength();
  664. var startRow = row;
  665. var re = /^\s*(?:\/\*|\/\/)#(end)?region\b/;
  666. var depth = 1;
  667. while (++row < maxRow) {
  668. line = session.getLine(row);
  669. var m = re.exec(line);
  670. if (!m) continue;
  671. if (m[1]) depth--;
  672. else depth++;
  673. if (!depth) break;
  674. }
  675. var endRow = row;
  676. if (endRow > startRow) {
  677. return new Range(startRow, startColumn, endRow, line.length);
  678. }
  679. };
  680. }).call(FoldMode.prototype);
  681. });
  682. 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) {
  683. "use strict";
  684. var oop = require("../lib/oop");
  685. var TextMode = require("./text").Mode;
  686. var c_cppHighlightRules = require("./c_cpp_highlight_rules").c_cppHighlightRules;
  687. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  688. var Range = require("../range").Range;
  689. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  690. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  691. var Mode = function() {
  692. this.HighlightRules = c_cppHighlightRules;
  693. this.$outdent = new MatchingBraceOutdent();
  694. this.$behaviour = new CstyleBehaviour();
  695. this.foldingRules = new CStyleFoldMode();
  696. };
  697. oop.inherits(Mode, TextMode);
  698. (function() {
  699. this.lineCommentStart = "//";
  700. this.blockComment = {start: "/*", end: "*/"};
  701. this.getNextLineIndent = function(state, line, tab) {
  702. var indent = this.$getIndent(line);
  703. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  704. var tokens = tokenizedLine.tokens;
  705. var endState = tokenizedLine.state;
  706. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  707. return indent;
  708. }
  709. if (state == "start") {
  710. var match = line.match(/^.*[\{\(\[]\s*$/);
  711. if (match) {
  712. indent += tab;
  713. }
  714. } else if (state == "doc-start") {
  715. if (endState == "start") {
  716. return "";
  717. }
  718. var match = line.match(/^\s*(\/?)\*/);
  719. if (match) {
  720. if (match[1]) {
  721. indent += " ";
  722. }
  723. indent += "* ";
  724. }
  725. }
  726. return indent;
  727. };
  728. this.checkOutdent = function(state, line, input) {
  729. return this.$outdent.checkOutdent(line, input);
  730. };
  731. this.autoOutdent = function(state, doc, row) {
  732. this.$outdent.autoOutdent(doc, row);
  733. };
  734. this.$id = "ace/mode/c_cpp";
  735. }).call(Mode.prototype);
  736. exports.Mode = Mode;
  737. });