Skip to content

Commit 54476b5

Browse files
authored
languages/go: add gofmt, golines and gofumpt formatter (#654)
1 parent e42bcbe commit 54476b5

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

docs/release-notes/rl-0.8.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,4 @@
170170
[Libadoxon](https://github.com/Libadoxon)
171171

172172
- Add [git-conflict](https://github.com/akinsho/git-conflict.nvim) plugin for resolving git conflicts
173+
- Add formatters for go: [gofmt](https://go.dev/blog/gofmt), [golines](https://github.com/segmentio/golines) and [gofumpt](https://github.com/mvdan/gofumpt)

modules/plugins/languages/go.nix

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,43 @@
3434
};
3535
};
3636

37+
defaultFormat = "gofmt";
38+
formats = {
39+
gofmt = {
40+
package = pkgs.go;
41+
nullConfig = ''
42+
table.insert(
43+
ls_sources,
44+
null_ls.builtins.formatting.gofmt.with({
45+
command = "${cfg.format.package}/bin/gofmt",
46+
})
47+
)
48+
'';
49+
};
50+
gofumpt = {
51+
package = pkgs.gofumpt;
52+
nullConfig = ''
53+
table.insert(
54+
ls_sources,
55+
null_ls.builtins.formatting.gofumpt.with({
56+
command = "${cfg.format.package}/bin/gofumpt",
57+
})
58+
)
59+
'';
60+
};
61+
golines = {
62+
package = pkgs.golines;
63+
nullConfig = ''
64+
table.insert(
65+
ls_sources,
66+
null_ls.builtins.formatting.golines.with({
67+
command = "${cfg.format.package}/bin/golines",
68+
})
69+
)
70+
'';
71+
};
72+
};
73+
3774
defaultDebugger = "delve";
3875
debuggers = {
3976
delve = {
@@ -67,6 +104,22 @@ in {
67104
};
68105
};
69106

107+
format = {
108+
enable = mkEnableOption "Go formatting" // {default = config.vim.languages.enableFormat;};
109+
110+
type = mkOption {
111+
description = "Go formatter to use";
112+
type = enum (attrNames formats);
113+
default = defaultFormat;
114+
};
115+
116+
package = mkOption {
117+
description = "Go formatter package";
118+
type = package;
119+
default = formats.${cfg.format.type}.package;
120+
};
121+
};
122+
70123
dap = {
71124
enable = mkOption {
72125
description = "Enable Go Debug Adapter via nvim-dap-go plugin";
@@ -99,6 +152,11 @@ in {
99152
vim.lsp.lspconfig.sources.go-lsp = servers.${cfg.lsp.server}.lspConfig;
100153
})
101154

155+
(mkIf cfg.format.enable {
156+
vim.lsp.null-ls.enable = true;
157+
vim.lsp.null-ls.sources.go-format = formats.${cfg.format.type}.nullConfig;
158+
})
159+
102160
(mkIf cfg.dap.enable {
103161
vim = {
104162
startPlugins = ["nvim-dap-go"];

0 commit comments

Comments
 (0)