mode-golang.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. ace.define("ace/mode/doc_comment_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/golang_highlight_rules",[], function(require, exports, module) {
  42. var oop = require("../lib/oop");
  43. var DocCommentHighlightRules = require("./doc_comment_highlight_rules").DocCommentHighlightRules;
  44. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  45. var GolangHighlightRules = function() {
  46. var keywords = (
  47. "else|break|case|return|goto|if|const|select|" +
  48. "continue|struct|default|switch|for|range|" +
  49. "func|import|package|chan|defer|fallthrough|go|interface|map|range|" +
  50. "select|type|var"
  51. );
  52. var builtinTypes = (
  53. "string|uint8|uint16|uint32|uint64|int8|int16|int32|int64|float32|" +
  54. "float64|complex64|complex128|byte|rune|uint|int|uintptr|bool|error"
  55. );
  56. var builtinFunctions = (
  57. "new|close|cap|copy|panic|panicln|print|println|len|make|delete|real|recover|imag|append"
  58. );
  59. var builtinConstants = ("nil|true|false|iota");
  60. var keywordMapper = this.createKeywordMapper({
  61. "keyword": keywords,
  62. "constant.language": builtinConstants,
  63. "support.function": builtinFunctions,
  64. "support.type": builtinTypes
  65. }, "");
  66. var stringEscapeRe = "\\\\(?:[0-7]{3}|x\\h{2}|u{4}|U\\h{6}|[abfnrtv'\"\\\\])".replace(/\\h/g, "[a-fA-F\\d]");
  67. this.$rules = {
  68. "start" : [
  69. {
  70. token : "comment",
  71. regex : "\\/\\/.*$"
  72. },
  73. DocCommentHighlightRules.getStartRule("doc-start"),
  74. {
  75. token : "comment.start", // multi line comment
  76. regex : "\\/\\*",
  77. next : "comment"
  78. }, {
  79. token : "string", // single line
  80. regex : /"(?:[^"\\]|\\.)*?"/
  81. }, {
  82. token : "string", // raw
  83. regex : '`',
  84. next : "bqstring"
  85. }, {
  86. token : "constant.numeric", // rune
  87. regex : "'(?:[^\\'\uD800-\uDBFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|" + stringEscapeRe.replace('"', '') + ")'"
  88. }, {
  89. token : "constant.numeric", // hex
  90. regex : "0[xX][0-9a-fA-F]+\\b"
  91. }, {
  92. token : "constant.numeric", // float
  93. regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b"
  94. }, {
  95. token : ["keyword", "text", "entity.name.function"],
  96. regex : "(func)(\\s+)([a-zA-Z_$][a-zA-Z0-9_$]*)\\b"
  97. }, {
  98. token : function(val) {
  99. if (val[val.length - 1] == "(") {
  100. return [{
  101. type: keywordMapper(val.slice(0, -1)) || "support.function",
  102. value: val.slice(0, -1)
  103. }, {
  104. type: "paren.lparen",
  105. value: val.slice(-1)
  106. }];
  107. }
  108. return keywordMapper(val) || "identifier";
  109. },
  110. regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b\\(?"
  111. }, {
  112. token : "keyword.operator",
  113. regex : "!|\\$|%|&|\\*|\\-\\-|\\-|\\+\\+|\\+|~|==|=|!=|<=|>=|<<=|>>=|>>>=|<>|<|>|!|&&|\\|\\||\\?\\:|\\*=|%=|\\+=|\\-=|&=|\\^="
  114. }, {
  115. token : "punctuation.operator",
  116. regex : "\\?|\\:|\\,|\\;|\\."
  117. }, {
  118. token : "paren.lparen",
  119. regex : "[[({]"
  120. }, {
  121. token : "paren.rparen",
  122. regex : "[\\])}]"
  123. }, {
  124. token : "text",
  125. regex : "\\s+"
  126. }
  127. ],
  128. "comment" : [
  129. {
  130. token : "comment.end",
  131. regex : "\\*\\/",
  132. next : "start"
  133. }, {
  134. defaultToken : "comment"
  135. }
  136. ],
  137. "bqstring" : [
  138. {
  139. token : "string",
  140. regex : '`',
  141. next : "start"
  142. }, {
  143. defaultToken : "string"
  144. }
  145. ]
  146. };
  147. this.embedRules(DocCommentHighlightRules, "doc-",
  148. [ DocCommentHighlightRules.getEndRule("start") ]);
  149. };
  150. oop.inherits(GolangHighlightRules, TextHighlightRules);
  151. exports.GolangHighlightRules = GolangHighlightRules;
  152. });
  153. ace.define("ace/mode/matching_brace_outdent",[], function(require, exports, module) {
  154. "use strict";
  155. var Range = require("../range").Range;
  156. var MatchingBraceOutdent = function() {};
  157. (function() {
  158. this.checkOutdent = function(line, input) {
  159. if (! /^\s+$/.test(line))
  160. return false;
  161. return /^\s*\}/.test(input);
  162. };
  163. this.autoOutdent = function(doc, row) {
  164. var line = doc.getLine(row);
  165. var match = line.match(/^(\s*\})/);
  166. if (!match) return 0;
  167. var column = match[1].length;
  168. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  169. if (!openBracePos || openBracePos.row == row) return 0;
  170. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  171. doc.replace(new Range(row, 0, row, column-1), indent);
  172. };
  173. this.$getIndent = function(line) {
  174. return line.match(/^\s*/)[0];
  175. };
  176. }).call(MatchingBraceOutdent.prototype);
  177. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  178. });
  179. ace.define("ace/mode/folding/cstyle",[], function(require, exports, module) {
  180. "use strict";
  181. var oop = require("../../lib/oop");
  182. var Range = require("../../range").Range;
  183. var BaseFoldMode = require("./fold_mode").FoldMode;
  184. var FoldMode = exports.FoldMode = function(commentRegex) {
  185. if (commentRegex) {
  186. this.foldingStartMarker = new RegExp(
  187. this.foldingStartMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.start)
  188. );
  189. this.foldingStopMarker = new RegExp(
  190. this.foldingStopMarker.source.replace(/\|[^|]*?$/, "|" + commentRegex.end)
  191. );
  192. }
  193. };
  194. oop.inherits(FoldMode, BaseFoldMode);
  195. (function() {
  196. this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/;
  197. this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/;
  198. this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/;
  199. this.tripleStarBlockCommentRe = /^\s*(\/\*\*\*).*\*\/\s*$/;
  200. this.startRegionRe = /^\s*(\/\*|\/\/)#?region\b/;
  201. this._getFoldWidgetBase = this.getFoldWidget;
  202. this.getFoldWidget = function(session, foldStyle, row) {
  203. var line = session.getLine(row);
  204. if (this.singleLineBlockCommentRe.test(line)) {
  205. if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line))
  206. return "";
  207. }
  208. var fw = this._getFoldWidgetBase(session, foldStyle, row);
  209. if (!fw && this.startRegionRe.test(line))
  210. return "start"; // lineCommentRegionStart
  211. return fw;
  212. };
  213. this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) {
  214. var line = session.getLine(row);
  215. if (this.startRegionRe.test(line))
  216. return this.getCommentRegionBlock(session, line, row);
  217. var match = line.match(this.foldingStartMarker);
  218. if (match) {
  219. var i = match.index;
  220. if (match[1])
  221. return this.openingBracketBlock(session, match[1], row, i);
  222. var range = session.getCommentFoldRange(row, i + match[0].length, 1);
  223. if (range && !range.isMultiLine()) {
  224. if (forceMultiline) {
  225. range = this.getSectionRange(session, row);
  226. } else if (foldStyle != "all")
  227. range = null;
  228. }
  229. return range;
  230. }
  231. if (foldStyle === "markbegin")
  232. return;
  233. var match = line.match(this.foldingStopMarker);
  234. if (match) {
  235. var i = match.index + match[0].length;
  236. if (match[1])
  237. return this.closingBracketBlock(session, match[1], row, i);
  238. return session.getCommentFoldRange(row, i, -1);
  239. }
  240. };
  241. this.getSectionRange = function(session, row) {
  242. var line = session.getLine(row);
  243. var startIndent = line.search(/\S/);
  244. var startRow = row;
  245. var startColumn = line.length;
  246. row = row + 1;
  247. var endRow = row;
  248. var maxRow = session.getLength();
  249. while (++row < maxRow) {
  250. line = session.getLine(row);
  251. var indent = line.search(/\S/);
  252. if (indent === -1)
  253. continue;
  254. if (startIndent > indent)
  255. break;
  256. var subRange = this.getFoldWidgetRange(session, "all", row);
  257. if (subRange) {
  258. if (subRange.start.row <= startRow) {
  259. break;
  260. } else if (subRange.isMultiLine()) {
  261. row = subRange.end.row;
  262. } else if (startIndent == indent) {
  263. break;
  264. }
  265. }
  266. endRow = row;
  267. }
  268. return new Range(startRow, startColumn, endRow, session.getLine(endRow).length);
  269. };
  270. this.getCommentRegionBlock = function(session, line, row) {
  271. var startColumn = line.search(/\s*$/);
  272. var maxRow = session.getLength();
  273. var startRow = row;
  274. var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/;
  275. var depth = 1;
  276. while (++row < maxRow) {
  277. line = session.getLine(row);
  278. var m = re.exec(line);
  279. if (!m) continue;
  280. if (m[1]) depth--;
  281. else depth++;
  282. if (!depth) break;
  283. }
  284. var endRow = row;
  285. if (endRow > startRow) {
  286. return new Range(startRow, startColumn, endRow, line.length);
  287. }
  288. };
  289. }).call(FoldMode.prototype);
  290. });
  291. ace.define("ace/mode/golang",[], function(require, exports, module) {
  292. var oop = require("../lib/oop");
  293. var TextMode = require("./text").Mode;
  294. var GolangHighlightRules = require("./golang_highlight_rules").GolangHighlightRules;
  295. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  296. var CstyleBehaviour = require("./behaviour/cstyle").CstyleBehaviour;
  297. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  298. var Mode = function() {
  299. this.HighlightRules = GolangHighlightRules;
  300. this.$outdent = new MatchingBraceOutdent();
  301. this.foldingRules = new CStyleFoldMode();
  302. this.$behaviour = new CstyleBehaviour();
  303. };
  304. oop.inherits(Mode, TextMode);
  305. (function() {
  306. this.lineCommentStart = "//";
  307. this.blockComment = {start: "/*", end: "*/"};
  308. this.getNextLineIndent = function(state, line, tab) {
  309. var indent = this.$getIndent(line);
  310. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  311. var tokens = tokenizedLine.tokens;
  312. var endState = tokenizedLine.state;
  313. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  314. return indent;
  315. }
  316. if (state == "start") {
  317. var match = line.match(/^.*[\{\(\[]\s*$/);
  318. if (match) {
  319. indent += tab;
  320. }
  321. }
  322. return indent;
  323. };//end getNextLineIndent
  324. this.checkOutdent = function(state, line, input) {
  325. return this.$outdent.checkOutdent(line, input);
  326. };
  327. this.autoOutdent = function(state, doc, row) {
  328. this.$outdent.autoOutdent(doc, row);
  329. };
  330. this.$id = "ace/mode/golang";
  331. }).call(Mode.prototype);
  332. exports.Mode = Mode;
  333. });
  334. (function() {
  335. ace.require(["ace/mode/golang"], function(m) {
  336. if (typeof module == "object" && typeof exports == "object" && module) {
  337. module.exports = m;
  338. }
  339. });
  340. })();