mode-gherkin.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ace.define("ace/mode/gherkin_highlight_rules",[], function(require, exports, module) {
  2. var oop = require("../lib/oop");
  3. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  4. var stringEscape = "\\\\(x[0-9A-Fa-f]{2}|[0-7]{3}|[\\\\abfnrtv'\"]|U[0-9A-Fa-f]{8}|u[0-9A-Fa-f]{4})";
  5. var GherkinHighlightRules = function() {
  6. var languages = [{
  7. name: "en",
  8. labels: "Feature|Background|Scenario(?: Outline)?|Examples",
  9. keywords: "Given|When|Then|And|But"
  10. }
  11. ];
  12. var labels = languages.map(function(l) {
  13. return l.labels;
  14. }).join("|");
  15. var keywords = languages.map(function(l) {
  16. return l.keywords;
  17. }).join("|");
  18. this.$rules = {
  19. start : [{
  20. token: "constant.numeric",
  21. regex: "(?:(?:[1-9]\\d*)|(?:0))"
  22. }, {
  23. token : "comment",
  24. regex : "#.*$"
  25. }, {
  26. token : "keyword",
  27. regex : "(?:" + labels + "):|(?:" + keywords + ")\\b"
  28. }, {
  29. token : "keyword",
  30. regex : "\\*"
  31. }, {
  32. token : "string", // multi line """ string start
  33. regex : '"{3}',
  34. next : "qqstring3"
  35. }, {
  36. token : "string", // " string
  37. regex : '"',
  38. next : "qqstring"
  39. }, {
  40. token : "text",
  41. regex : "^\\s*(?=@[\\w])",
  42. next : [{
  43. token : "text",
  44. regex : "\\s+"
  45. }, {
  46. token : "variable.parameter",
  47. regex : "@[\\w]+"
  48. }, {
  49. token : "empty",
  50. regex : "",
  51. next : "start"
  52. }]
  53. }, {
  54. token : "comment",
  55. regex : "<[^>]+>"
  56. }, {
  57. token : "comment",
  58. regex : "\\|(?=.)",
  59. next : "table-item"
  60. }, {
  61. token : "comment",
  62. regex : "\\|$",
  63. next : "start"
  64. }],
  65. "qqstring3" : [ {
  66. token : "constant.language.escape",
  67. regex : stringEscape
  68. }, {
  69. token : "string", // multi line """ string end
  70. regex : '"{3}',
  71. next : "start"
  72. }, {
  73. defaultToken : "string"
  74. }],
  75. "qqstring" : [{
  76. token : "constant.language.escape",
  77. regex : stringEscape
  78. }, {
  79. token : "string",
  80. regex : "\\\\$",
  81. next : "qqstring"
  82. }, {
  83. token : "string",
  84. regex : '"|$',
  85. next : "start"
  86. }, {
  87. defaultToken: "string"
  88. }],
  89. "table-item" : [{
  90. token : "comment",
  91. regex : /$/,
  92. next : "start"
  93. }, {
  94. token : "comment",
  95. regex : /\|/
  96. }, {
  97. token : "string",
  98. regex : /\\./
  99. }, {
  100. defaultToken : "string"
  101. }]
  102. };
  103. this.normalizeRules();
  104. };
  105. oop.inherits(GherkinHighlightRules, TextHighlightRules);
  106. exports.GherkinHighlightRules = GherkinHighlightRules;
  107. });
  108. ace.define("ace/mode/gherkin",[], function(require, exports, module) {
  109. var oop = require("../lib/oop");
  110. var TextMode = require("./text").Mode;
  111. var GherkinHighlightRules = require("./gherkin_highlight_rules").GherkinHighlightRules;
  112. var Mode = function() {
  113. this.HighlightRules = GherkinHighlightRules;
  114. this.$behaviour = this.$defaultBehaviour;
  115. };
  116. oop.inherits(Mode, TextMode);
  117. (function() {
  118. this.lineCommentStart = "#";
  119. this.$id = "ace/mode/gherkin";
  120. this.getNextLineIndent = function(state, line, tab) {
  121. var indent = this.$getIndent(line);
  122. var space2 = " ";
  123. var tokenizedLine = this.getTokenizer().getLineTokens(line, state);
  124. var tokens = tokenizedLine.tokens;
  125. console.log(state);
  126. if(line.match("[ ]*\\|")) {
  127. indent += "| ";
  128. }
  129. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  130. return indent;
  131. }
  132. if (state == "start") {
  133. if (line.match("Scenario:|Feature:|Scenario Outline:|Background:")) {
  134. indent += space2;
  135. } else if(line.match("(Given|Then).+(:)$|Examples:")) {
  136. indent += space2;
  137. } else if(line.match("\\*.+")) {
  138. indent += "* ";
  139. }
  140. }
  141. return indent;
  142. };
  143. }).call(Mode.prototype);
  144. exports.Mode = Mode;
  145. });
  146. (function() {
  147. ace.require(["ace/mode/gherkin"], function(m) {
  148. if (typeof module == "object" && typeof exports == "object" && module) {
  149. module.exports = m;
  150. }
  151. });
  152. })();