index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { parser } from "./syntax.grammar"
  2. import {
  3. LRLanguage,
  4. LanguageSupport,
  5. indentNodeProp,
  6. foldNodeProp,
  7. foldInside,
  8. delimitedIndent,
  9. } from "@codemirror/language"
  10. import { styleTags, tags as t } from "@lezer/highlight"
  11. export const GQLLanguage = LRLanguage.define({
  12. parser: parser.configure({
  13. props: [
  14. indentNodeProp.add({
  15. "SelectionSet FieldsDefinition ObjectValue SchemaDefinition RootTypeDef":
  16. delimitedIndent({ closing: "}", align: true }),
  17. }),
  18. foldNodeProp.add({
  19. Application: foldInside,
  20. "SelectionSet FieldsDefinition ObjectValue RootOperationTypeDefinition RootTypeDef":
  21. (node) => {
  22. return {
  23. from: node.from,
  24. to: node.to,
  25. }
  26. },
  27. }),
  28. styleTags({
  29. Comment: t.lineComment,
  30. Name: t.propertyName,
  31. StringValue: t.string,
  32. IntValue: t.integer,
  33. FloatValue: t.float,
  34. NullValue: t.null,
  35. BooleanValue: t.bool,
  36. Comma: t.separator,
  37. "OperationDefinition/Name": t.definition(t.function(t.variableName)),
  38. "OperationType TypeKeyword SchemaKeyword FragmentKeyword OnKeyword DirectiveKeyword RepeatableKeyword SchemaKeyword ExtendKeyword ScalarKeyword InterfaceKeyword UnionKeyword EnumKeyword InputKeyword ImplementsKeyword": t.keyword,
  39. "ExecutableDirectiveLocation TypeSystemDirectiveLocation": t.atom,
  40. "DirectiveName!": t.annotation,
  41. "\"{\" \"}\"": t.brace,
  42. "\"(\" \")\"": t.paren,
  43. "\"[\" \"]\"": t.squareBracket,
  44. "Type! NamedType": t.typeName,
  45. }),
  46. ],
  47. }),
  48. languageData: {
  49. commentTokens: { line: "#" },
  50. closeBrackets: { brackets: ["(", "[", "{", '"', '"""'] },
  51. },
  52. })
  53. export function GQL() {
  54. return new LanguageSupport(GQLLanguage)
  55. }