|
1 | | -import { SnippetDocument, SnippetVariable, parseSnippetFile } from "./SnippetParser"; |
2 | 1 | import type { LanguageFormatterText } from "./registerLanguageFormatters"; |
| 2 | +import { parseSnippetFile, serializeSnippetFile } from "talon-snippets"; |
3 | 3 |
|
4 | 4 | export const snippetFormatter: LanguageFormatterText = { |
5 | | - getText(text: string, ident: string): string { |
6 | | - const formatter = new SnippetFormatter(ident); |
7 | | - return formatter.getText(text); |
| 5 | + getText(text: string, _ident: string): string { |
| 6 | + const documents = parseSnippetFile(text); |
| 7 | + return serializeSnippetFile(documents); |
8 | 8 | } |
9 | 9 | }; |
10 | | - |
11 | | -class SnippetFormatter { |
12 | | - constructor(private ident: string) {} |
13 | | - |
14 | | - getText(text: string): string { |
15 | | - const result = parseSnippetFile(text) |
16 | | - .map((s) => this.getDocumentText(s)) |
17 | | - // Remove empty documents |
18 | | - .filter(Boolean) |
19 | | - .join("\n---\n\n"); |
20 | | - return result ? result + "\n---\n" : ""; |
21 | | - } |
22 | | - |
23 | | - private getDocumentText(document: SnippetDocument): string { |
24 | | - const parts: string[] = [ |
25 | | - this.getOptionalPairString("name", document.name), |
26 | | - this.getOptionalPairString("language", document.languages), |
27 | | - this.getOptionalPairString("phrase", document.phrases), |
28 | | - this.getOptionalPairString("insertionScope", document.insertionScopes) |
29 | | - ].filter(Boolean); |
30 | | - |
31 | | - if (document.variables.length > 0) { |
32 | | - if (parts.length > 0) { |
33 | | - parts.push(""); |
34 | | - } |
35 | | - parts.push(...this.getSortedVariables(document.variables)); |
36 | | - } |
37 | | - |
38 | | - if (document.body != null) { |
39 | | - parts.push("-", ...document.body); |
40 | | - } |
41 | | - |
42 | | - return parts.join("\n"); |
43 | | - } |
44 | | - |
45 | | - private getSortedVariables(variables: SnippetVariable[]): string[] { |
46 | | - const result = [...variables]; |
47 | | - result.sort(compareVariables); |
48 | | - return result |
49 | | - .flatMap((variable) => [ |
50 | | - this.getOptionalPairString( |
51 | | - `$${variable.name}.insertionFormatter`, |
52 | | - variable.insertionFormatters |
53 | | - ), |
54 | | - this.getOptionalPairString( |
55 | | - `$${variable.name}.wrapperPhrase`, |
56 | | - variable.wrapperPhrases |
57 | | - ), |
58 | | - this.getOptionalPairString(`$${variable.name}.wrapperScope`, variable.wrapperScope) |
59 | | - ]) |
60 | | - .filter(Boolean); |
61 | | - } |
62 | | - |
63 | | - private getOptionalPairString(key: string, value: string | string[] | undefined): string { |
64 | | - if (value == null) { |
65 | | - return ""; |
66 | | - } |
67 | | - if (Array.isArray(value)) { |
68 | | - return `${key}: ${value.join(" | ")}`; |
69 | | - } |
70 | | - return `${key}: ${value}`; |
71 | | - } |
72 | | -} |
73 | | - |
74 | | -function compareVariables(a: SnippetVariable, b: SnippetVariable): number { |
75 | | - if (a.name === "0") { |
76 | | - return 1; |
77 | | - } |
78 | | - if (b.name === "0") { |
79 | | - return -1; |
80 | | - } |
81 | | - return a.name.localeCompare(b.name); |
82 | | -} |
0 commit comments