| 1 |
import type { |
| 2 |
Completion, |
| 3 |
CompletionResult, |
| 4 |
CompletionSource, |
| 5 |
} from "@codemirror/autocomplete"; |
| 6 |
import { syntaxTree } from "@codemirror/language"; |
| 7 |
import { get as store_get } from "svelte/store"; |
| 8 |
|
| 9 |
import { accounts, currencies, links, payees, tags } from "../stores"; |
| 10 |
import { beancountSnippets } from "./beancount-snippets"; |
| 11 |
|
| 12 |
const undatedDirectives = ["option", "plugin", "include"]; |
| 13 |
const datedDirectives = [ |
| 14 |
"*", |
| 15 |
"open", |
| 16 |
"close", |
| 17 |
"custom", |
| 18 |
"commodity", |
| 19 |
"balance", |
| 20 |
"pad", |
| 21 |
"note", |
| 22 |
"document", |
| 23 |
"price", |
| 24 |
"event", |
| 25 |
"query", |
| 26 |
]; |
| 27 |
|
| 28 |
/** Get Completion objects from strings. */ |
| 29 |
const opts = (s: readonly string[]): Completion[] => |
| 30 |
s.map((label) => ({ label })); |
| 31 |
|
| 32 |
/** Generate completion result list for codemirror from strings. */ |
| 33 |
const res = (s: readonly string[], from: number): CompletionResult => ({ |
| 34 |
options: opts(s), |
| 35 |
from, |
| 36 |
}); |
| 37 |
|
| 38 |
export const beancountCompletion: CompletionSource = (context) => { |
| 39 |
const tag = context.matchBefore(/#[A-Za-z0-9\-_/.]*/); |
| 40 |
if (tag) { |
| 41 |
return { |
| 42 |
options: opts(store_get(tags)), |
| 43 |
from: tag.from + 1, |
| 44 |
validFor: /\S+/, |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
const link = context.matchBefore(/\^[A-Za-z0-9\-_/.]*/); |
| 49 |
if (link) { |
| 50 |
return { |
| 51 |
options: opts(store_get(links)), |
| 52 |
from: link.from + 1, |
| 53 |
validFor: /\S+/, |
| 54 |
}; |
| 55 |
} |
| 56 |
|
| 57 |
const indented = context.matchBefore(/^\s+[A-Z]\S*/); |
| 58 |
if (indented) { |
| 59 |
const indentation = indented.text.length - indented.text.trimStart().length; |
| 60 |
return { |
| 61 |
options: opts(store_get(accounts)), |
| 62 |
from: indented.from + indentation, |
| 63 |
validFor: /\S+/, |
| 64 |
}; |
| 65 |
} |
| 66 |
|
| 67 |
const line = context.state.doc.lineAt(context.pos); |
| 68 |
if (context.matchBefore(/\d+/)) { |
| 69 |
return { options: beancountSnippets(), from: line.from }; |
| 70 |
} |
| 71 |
|
| 72 |
const currentWord = context.matchBefore(/\S*/); |
| 73 |
if (currentWord?.from === line.from && line.length > 0) { |
| 74 |
return { |
| 75 |
options: opts(undatedDirectives), |
| 76 |
from: line.from, |
| 77 |
validFor: /\S+/, |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
const tree = syntaxTree(context.state); |
| 82 |
const before = tree.resolve(context.pos, -1); |
| 83 |
// Node types of the last 4 nodes. |
| 84 |
const nodeTypesBefore = [ |
| 85 |
before.name, |
| 86 |
before.prevSibling?.name, |
| 87 |
before.prevSibling?.prevSibling?.name, |
| 88 |
before.prevSibling?.prevSibling?.prevSibling?.name, |
| 89 |
]; |
| 90 |
type T = string | string[]; |
| 91 |
// Check whether the previous nodes (up to 4) match the given types. |
| 92 |
const match = (...types: [T] | [T, T] | [T, T, T] | [T, T, T, T]): boolean => |
| 93 |
types.every((t, i) => { |
| 94 |
const nodeType = nodeTypesBefore[i]; |
| 95 |
return typeof t === "string" |
| 96 |
? nodeType === t |
| 97 |
: t.some((n) => nodeType === n); |
| 98 |
}); |
| 99 |
|
| 100 |
// complete payee after transaction flag. |
| 101 |
if (match("string", "flag")) { |
| 102 |
return res(store_get(payees), before.from + 1); |
| 103 |
} |
| 104 |
|
| 105 |
// complete directive names after a date. |
| 106 |
if (match("keyword", "date")) { |
| 107 |
return res(datedDirectives, before.from); |
| 108 |
} |
| 109 |
|
| 110 |
if ( |
| 111 |
// account directly after one of these directives: |
| 112 |
match( |
| 113 |
["ERROR", "account"], |
| 114 |
["BALANCE", "CLOSE", "OPEN", "PAD", "NOTE", "DOCUMENT"], |
| 115 |
"date", |
| 116 |
) || |
| 117 |
// padding account |
| 118 |
match(["ERROR", "account"], "account", "PAD", "date") |
| 119 |
) { |
| 120 |
return res(store_get(accounts), before.from); |
| 121 |
} |
| 122 |
|
| 123 |
if ( |
| 124 |
// complete currencies after a number. |
| 125 |
match("ERROR", "number") || |
| 126 |
// account currency |
| 127 |
match(["ERROR", "currency"], "account", "OPEN", "date") || |
| 128 |
// price or commodity currency |
| 129 |
match(["ERROR", "currency"], ["COMMODITY", "PRICE"], "date") |
| 130 |
) { |
| 131 |
return res(store_get(currencies), before.from); |
| 132 |
} |
| 133 |
|
| 134 |
return null; |
| 135 |
}; |