gqlQueryLangMode.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. export function defineGQLLanguageMode(ace) {
  2. // Highlighting
  3. ace.define(
  4. "ace/mode/gql-query-highlight",
  5. ["require", "exports", "ace/lib/oop", "ace/mode/text_highlight_rules"],
  6. (aceRequire, exports) => {
  7. const oop = aceRequire("ace/lib/oop")
  8. const TextHighlightRules = aceRequire(
  9. "ace/mode/text_highlight_rules"
  10. ).TextHighlightRules
  11. const GQLQueryTextHighlightRules = function () {
  12. const keywords =
  13. "type|interface|union|enum|schema|input|implements|extends|scalar|fragment|query|mutation|subscription"
  14. const dataTypes = "Int|Float|String|ID|Boolean"
  15. const literalValues = "true|false|null"
  16. const escapeRe = /\\(?:u[\da-fA-f]{4}|.)/
  17. const keywordMapper = this.createKeywordMapper(
  18. {
  19. keyword: keywords,
  20. "storage.type": dataTypes,
  21. "constant.language": literalValues,
  22. },
  23. "identifier"
  24. )
  25. this.$rules = {
  26. start: [
  27. {
  28. token: "comment",
  29. regex: "#.*$",
  30. },
  31. {
  32. token: "paren.lparen",
  33. regex: /[[({]/,
  34. next: "start",
  35. },
  36. {
  37. token: "paren.rparen",
  38. regex: /[\])}]/,
  39. },
  40. {
  41. token: keywordMapper,
  42. regex: "[a-zA-Z_][a-zA-Z0-9_$]*\\b",
  43. },
  44. {
  45. token: "string", // character
  46. regex: `'(?:${escapeRe}|.)?'`,
  47. },
  48. {
  49. token: "string.start",
  50. regex: '"',
  51. stateName: "qqstring",
  52. next: [
  53. { token: "string", regex: /\\\s*$/, next: "qqstring" },
  54. { token: "constant.language.escape", regex: escapeRe },
  55. { token: "string.end", regex: '"|$', next: "start" },
  56. { defaultToken: "string" },
  57. ],
  58. },
  59. {
  60. token: "string.start",
  61. regex: "'",
  62. stateName: "singleQuoteString",
  63. next: [
  64. { token: "string", regex: /\\\s*$/, next: "singleQuoteString" },
  65. { token: "constant.language.escape", regex: escapeRe },
  66. { token: "string.end", regex: "'|$", next: "start" },
  67. { defaultToken: "string" },
  68. ],
  69. },
  70. {
  71. token: "constant.numeric",
  72. regex: /\d+\.?\d*[eE]?[+-]?\d*/,
  73. },
  74. {
  75. token: "variable",
  76. regex: /\$[_A-Za-z][_0-9A-Za-z]*/,
  77. },
  78. ],
  79. }
  80. this.normalizeRules()
  81. }
  82. oop.inherits(GQLQueryTextHighlightRules, TextHighlightRules)
  83. exports.GQLQueryTextHighlightRules = GQLQueryTextHighlightRules
  84. }
  85. )
  86. // Language Mode Definition
  87. ace.define(
  88. "ace/mode/gql-query",
  89. ["require", "exports", "ace/mode/text", "ace/mode/gql-query-highlight"],
  90. (aceRequire, exports) => {
  91. const oop = aceRequire("ace/lib/oop")
  92. const TextMode = aceRequire("ace/mode/text").Mode
  93. const GQLQueryTextHighlightRules = aceRequire(
  94. "ace/mode/gql-query-highlight"
  95. ).GQLQueryTextHighlightRules
  96. const FoldMode = aceRequire("ace/mode/folding/cstyle").FoldMode
  97. const Mode = function () {
  98. this.HighlightRules = GQLQueryTextHighlightRules
  99. this.foldingRules = new FoldMode()
  100. }
  101. oop.inherits(Mode, TextMode)
  102. exports.Mode = Mode
  103. }
  104. )
  105. }