Skip to content

Commit 8259788

Browse files
committed
languages/beancount: init
Create the `vim.languages.beancount` module using `bean-format` and `beancount-language-server`.
1 parent cf58d88 commit 8259788

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

configuration.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ isMaximal: {
7575
# Language modules that are not as common.
7676
assembly.enable = false;
7777
astro.enable = false;
78+
beancount.enable = false;
7879
nu.enable = false;
7980
csharp.enable = false;
8081
julia.enable = false;

docs/release-notes/rl-0.8.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,11 @@
538538
[gmvar](https://github.com/gmvar):
539539

540540
[harper-ls]: https://github.com/Automattic/harper
541+
[beancount-language-server]: https://github.com/polarmutex/beancount-language-server
542+
[bean-format]: https://github.com/beancount/beancount
541543

542544
- Add [harper-ls] to the `vim.lsp` module.
545+
- Add beancount support under `vim.languages.beancount` using [beancount-language-server] and [bean-format].
543546

544547
[derethil](https://github.com/derethil):
545548

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
config,
3+
pkgs,
4+
lib,
5+
...
6+
}: let
7+
inherit (builtins) attrNames;
8+
inherit (lib.options) mkOption mkEnableOption literalExpression;
9+
inherit (lib.meta) getExe';
10+
inherit (lib.modules) mkIf mkMerge;
11+
inherit (lib.lists) isList;
12+
inherit (lib.types) anything attrsOf either enum listOf package str;
13+
inherit (lib.nvim.types) mkGrammarOption;
14+
15+
cfg = config.vim.languages.beancount;
16+
17+
defaultServer = "beancount-language-server";
18+
servers = {
19+
beancount-language-server = {
20+
rootmarkers = [".git"];
21+
filetypes = ["beancount" "bean"];
22+
init_options = cfg.lsp.initOptions;
23+
cmd =
24+
if (isList cfg.lsp.package)
25+
then cfg.lsp.package
26+
else [(getExe' cfg.lsp.package cfg.lsp.server)];
27+
};
28+
};
29+
30+
defaultFormat = "bean-format";
31+
formats = {
32+
bean-format = {
33+
package = pkgs.beancount;
34+
};
35+
};
36+
in {
37+
options.vim.languages.beancount = {
38+
enable = mkEnableOption "Beancount language support";
39+
40+
treesitter = {
41+
enable = mkEnableOption "Beancount treesitter support" // {default = config.vim.languages.enableTreesitter;};
42+
package = mkGrammarOption pkgs "beancount";
43+
};
44+
45+
lsp = {
46+
enable = mkEnableOption "Beancount LSP support" // {default = config.vim.lsp.enable;};
47+
48+
server = mkOption {
49+
type = enum (attrNames servers);
50+
default = defaultServer;
51+
description = ''
52+
Beancount LSP server to use.
53+
54+
::: {.note}
55+
'beancount-language-server' requires 'bean-check' and 'bean-format'.
56+
Both are provided by 'pkgs.beancount'. These binaries must be in
57+
your PATH or in 'vim.extraPackages'. There are no additional checks
58+
to verify if this requirement is met.
59+
:::
60+
'';
61+
};
62+
63+
package = mkOption {
64+
type = either package (listOf str);
65+
default = pkgs.beancount-language-server;
66+
example = literalExpression ''[lib.getExe pkgs.beancount-language-server]'';
67+
description = "Beancount LSP package, or the command to run as a list of strings";
68+
};
69+
70+
initOptions = mkOption {
71+
type = attrsOf anything;
72+
default = {};
73+
example = {
74+
journal_file = "/path/to/main.beancount";
75+
formatting = {
76+
prefix_width = 30;
77+
currency_column = 60;
78+
number_currency_spacing = 1;
79+
};
80+
};
81+
description = "Init options to pass to beancount-language-server";
82+
};
83+
};
84+
85+
format = {
86+
enable = mkEnableOption "Beancount formatting" // {default = config.vim.languages.enableFormat;};
87+
88+
type = mkOption {
89+
type = enum (attrNames formats);
90+
default = defaultFormat;
91+
description = "Beancount formatter to use";
92+
};
93+
94+
package = mkOption {
95+
type = package;
96+
default = formats.${cfg.format.type}.package;
97+
description = "Beancount formatter package";
98+
};
99+
};
100+
};
101+
102+
config = mkIf cfg.enable (mkMerge [
103+
(mkIf cfg.treesitter.enable {
104+
vim.treesitter.enable = true;
105+
vim.treesitter.grammars = [cfg.treesitter.package];
106+
})
107+
108+
(mkIf cfg.lsp.enable {
109+
vim.lsp.servers.${cfg.lsp.server} = servers.${cfg.lsp.server};
110+
})
111+
112+
(mkIf cfg.format.enable {
113+
vim.formatter.conform-nvim = {
114+
enable = true;
115+
setupOpts.formatters_by_ft.beancount = [cfg.format.type];
116+
setupOpts.formatters.${cfg.format.type} = {
117+
command = getExe' cfg.format.package cfg.format.type;
118+
};
119+
};
120+
})
121+
]);
122+
}

modules/plugins/languages/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ in {
66
./asm.nix
77
./astro.nix
88
./bash.nix
9+
./beancount.nix
910
./cue.nix
1011
./dart.nix
1112
./clang.nix

0 commit comments

Comments
 (0)