mode-gherkin.js 4.6 KB

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