|
| 1 | +import { completeFromList } from "@codemirror/autocomplete"; |
| 2 | +import { |
| 3 | + delimitedIndent, |
| 4 | + foldInside, |
| 5 | + foldNodeProp, |
| 6 | + indentNodeProp, |
| 7 | + LanguageSupport, |
| 8 | + LRLanguage, |
| 9 | +} from "@codemirror/language"; |
| 10 | +import { styleTags, tags as t } from "@lezer/highlight"; |
| 11 | +import { parser } from "./parser"; |
| 12 | + |
| 13 | +const configuredParser = parser.configure({ |
| 14 | + props: [ |
| 15 | + styleTags({ |
| 16 | + ModuleComment: t.docComment, |
| 17 | + DocComment: t.docComment, |
| 18 | + LineComment: t.lineComment, |
| 19 | + String: t.string, |
| 20 | + Float: t.number, |
| 21 | + Integer: t.number, |
| 22 | + Discard: t.comment, |
| 23 | + "FunctionDefinition/Identifier": t.function(t.definition(t.variableName)), |
| 24 | + "FunctionCall/Identifier": t.function(t.variableName), |
| 25 | + TypeName: t.typeName, |
| 26 | + ModulePath: t.namespace, |
| 27 | + Identifier: t.variableName, |
| 28 | + "True False": t.bool, |
| 29 | + Nil: t.null, |
| 30 | + "As Assert Case Else If Import Let Pub Type Use Const Opaque": t.keyword, |
| 31 | + Fn: t.definitionKeyword, |
| 32 | + "Panic Todo Echo": t.controlKeyword, |
| 33 | + Operator: t.operator, |
| 34 | + Attribute: t.meta, |
| 35 | + "( )": t.paren, |
| 36 | + "[ ]": t.squareBracket, |
| 37 | + "{ }": t.brace, |
| 38 | + "<< >>": t.special(t.brace), |
| 39 | + Punctuation: t.punctuation, |
| 40 | + }), |
| 41 | + indentNodeProp.add({ |
| 42 | + Block: delimitedIndent({ closing: "}" }), |
| 43 | + Bracketed: delimitedIndent({ closing: "]" }), |
| 44 | + Parenthesized: delimitedIndent({ closing: ")" }), |
| 45 | + BitArray: delimitedIndent({ closing: ">>" }), |
| 46 | + }), |
| 47 | + foldNodeProp.add({ |
| 48 | + Block: foldInside, |
| 49 | + Bracketed: foldInside, |
| 50 | + Parenthesized: foldInside, |
| 51 | + BitArray: foldInside, |
| 52 | + }), |
| 53 | + ], |
| 54 | +}); |
| 55 | + |
| 56 | +export const gleamLanguage = LRLanguage.define({ |
| 57 | + name: "gleam", |
| 58 | + parser: configuredParser, |
| 59 | + languageData: { |
| 60 | + commentTokens: { line: "//" }, |
| 61 | + closeBrackets: { brackets: ["(", "[", "{", '"', "<<"] }, |
| 62 | + indentOnInput: /^\s*}$/, |
| 63 | + }, |
| 64 | +}); |
| 65 | + |
| 66 | +const keywords = [ |
| 67 | + "as", "assert", "case", "const", "echo", "else", "fn", "if", "import", |
| 68 | + "let", "opaque", "panic", "pub", "todo", "type", "use", |
| 69 | +]; |
| 70 | + |
| 71 | +const builtinTypes = [ |
| 72 | + "Int", "Float", "String", "Bool", "Nil", "UtfCodepoint", "BitArray", "Result", "List", |
| 73 | +]; |
| 74 | + |
| 75 | +const gleamCompletion = gleamLanguage.data.of({ |
| 76 | + autocomplete: completeFromList([ |
| 77 | + ...keywords.map((label) => ({ label, type: "keyword" })), |
| 78 | + ...builtinTypes.map((label) => ({ label, type: "type" })), |
| 79 | + { label: "True", type: "constant" }, |
| 80 | + { label: "False", type: "constant" }, |
| 81 | + { label: "Nil", type: "constant" }, |
| 82 | + ]), |
| 83 | +}); |
| 84 | + |
| 85 | +export function gleam() { |
| 86 | + return new LanguageSupport(gleamLanguage, [gleamCompletion]); |
| 87 | +} |
| 88 | + |
| 89 | +export const gleamMode = { |
| 90 | + name: "gleam", |
| 91 | + caption: "Gleam", |
| 92 | + extensions: ["gleam"], |
| 93 | + load: gleam, |
| 94 | +}; |
0 commit comments