Debian Codegraph @

frontend/src/codemirror/beancount.ts

raw
1 import {
2 defineLanguageFacet,
3 Language,
4 languageDataProp,
5 LanguageSupport,
6 syntaxHighlighting,
7 } from "@codemirror/language";
8 import { highlightTrailingWhitespace, keymap } from "@codemirror/view";
9 import { styleTags, tags } from "@lezer/highlight";
10 import { Language as TSLanguage, Parser as TSParser } from "web-tree-sitter";
11
12 import ts_wasm from "../../node_modules/web-tree-sitter/tree-sitter.wasm";
13 import { beancountCompletion } from "./beancount-autocomplete";
14 import { beancountFold } from "./beancount-fold";
15 import { beancountFormat } from "./beancount-format";
16 import { beancountEditorHighlight } from "./beancount-highlight";
17 import { beancountIndent } from "./beancount-indent";
18 // WASM build of tree-sitter grammar from https://github.com/yagebu/tree-sitter-beancount
19 import ts_beancount_wasm from "./tree-sitter-beancount.wasm";
20 import { LezerTSParser } from "./tree-sitter-parser";
21
22 /** Import the tree-sitter and Beancount language WASM files and initialise the parser. */
23 async function loadBeancountParser(): Promise<TSParser> {
24 const ts = import.meta.resolve(ts_wasm);
25 const ts_beancount = import.meta.resolve(ts_beancount_wasm);
26 await TSParser.init({ locateFile: () => ts });
27 const lang = await TSLanguage.load(ts_beancount);
28 const parser = new TSParser();
29 parser.setLanguage(lang);
30 return parser;
31 }
32
33 const beancountLanguageFacet = defineLanguageFacet();
34 const beancountLanguageSupportExtensions = [
35 beancountFold,
36 syntaxHighlighting(beancountEditorHighlight),
37 beancountIndent,
38 keymap.of([{ key: "Control-d", mac: "Meta-d", run: beancountFormat }]),
39 beancountLanguageFacet.of({
40 autocomplete: beancountCompletion,
41 commentTokens: { line: ";" },
42 indentOnInput: /^\s+\d\d\d\d/,
43 }),
44 highlightTrailingWhitespace(),
45 ];
46
47 /** The node props that allow for highlighting/coloring of the code. */
48 const props = [
49 styleTags({
50 account: tags.className,
51 currency: tags.unit,
52 date: tags.special(tags.number),
53 string: tags.string,
54 "BALANCE CLOSE COMMODITY CUSTOM DOCUMENT EVENT NOTE OPEN PAD PRICE TRANSACTION QUERY":
55 tags.keyword,
56 "tag link": tags.labelName,
57 number: tags.number,
58 key: tags.propertyName,
59 bool: tags.bool,
60 "PUSHTAG POPTAG PUSHMETA POPMETA OPTION PLUGIN INCLUDE": tags.standard(
61 tags.string,
62 ),
63 }),
64 languageDataProp.add((type) =>
65 type.isTop ? beancountLanguageFacet : undefined,
66 ),
67 ];
68
69 /** Only load the TSParser once. */
70 let load_parser: Promise<TSParser> | null = null;
71
72 /**
73 * Get the LanguageSupport for Beancount.
74 *
75 * Since this might need to load the tree-sitter parser, this is async.
76 */
77 export async function getBeancountLanguageSupport(): Promise<LanguageSupport> {
78 load_parser ??= loadBeancountParser();
79 const ts_parser = await load_parser;
80 return new LanguageSupport(
81 new Language(
82 beancountLanguageFacet,
83 new LezerTSParser(ts_parser, props, "beancount_file"),
84 [],
85 "beancount",
86 ),
87 beancountLanguageSupportExtensions,
88 );
89 }