Repository navigation
Expand file tree
/
Copy pathdefault.nix
More file actions
218 lines (198 loc) · 5.55 KB
/
Copy pathdefault.nix
File metadata and controls
218 lines (198 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# my-dotfiles | Copyright (C) 2025-2026 eth-p
# Repository: https://github.com/eth-p/my-dotfiles
#
# Program: https://code.visualstudio.com/
# ==============================================================================
{
lib,
pkgs,
config,
my-dotfiles,
...
}@inputs:
let
inherit (lib) mkIf mkMerge;
inherit (my-dotfiles.lib.programs) vscode;
extensions = pkgs.vscode-extensions;
snippets = (import ./snippet-generators.nix inputs);
vscodeCfg = vscode.getConfig config;
cfg = vscodeCfg.language.go;
in
{
options.my-dotfiles.vscode.language.go = {
enable = lib.mkEnableOption "add Go language support to Visual Studio Code";
compiler.package = lib.mkOption {
default = pkgs.go;
description = "the Go compiler package";
};
debugger = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "install the Go debugger, dlv";
};
package = lib.mkOption {
default = pkgs.delve;
description = "the dlv package";
};
};
linter = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "install golangci-lint for linting Go source code";
};
package = lib.mkOption {
default = pkgs.golangci-lint;
description = "the golangci-lint package";
};
};
lsp = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "install the Go language server, gopls";
};
package = lib.mkOption {
default = pkgs.gopls;
description = "the gopls package";
};
};
snippets = {
general = lib.mkOption {
type = lib.types.bool;
default = true;
description = "add snippets for general language features";
};
};
};
config = mkIf (vscodeCfg.enable && cfg.enable) (mkMerge [
# Install the Go extension and Go compiler.
# https://marketplace.visualstudio.com/items?itemName=golang.go
{
programs.vscode = {
profiles.default.extensions = with extensions; [
golang.go
];
profiles.default.userSettings = {
"go.alternateTools" = {
"go" = cfg.compiler.package + "/bin/go";
};
"go.survey.prompt" = false;
};
};
}
# Install the Go debugger, dlv.
(mkIf cfg.debugger.enable {
programs.vscode = {
profiles.default.userSettings = {
"go.alternateTools" = {
"dlv" = cfg.debugger.package + "/bin/dlv";
};
};
};
})
# Install the Go language server, gopls.
(mkIf cfg.lsp.enable {
programs.vscode = {
profiles.default.userSettings = {
"go.alternateTools" = {
"gopls" = cfg.lsp.package + "/bin/gopls";
};
};
};
})
# Install the Go linter, golangci-lint.
(mkIf cfg.lsp.enable {
programs.vscode = {
profiles.default.userSettings = {
"go.lintTool" = "golangci-lint-v2";
"go.alternateTools" = {
"golangci-lint" = pkgs.golangci-lint-v1 + "/bin/golangci-lint-v1";
"golangci-lint-v2" = cfg.linter.package + "/bin/golangci-lint";
};
};
};
})
# Add snippets for general language features.
(mkIf cfg.lsp.enable {
programs.vscode.profiles.default.languageSnippets.go = {
"New interface type" = {
"prefix" = "interface";
"description" = "Create an interface";
"body" = [
"type \${1:name} interface {"
"\t$0"
"}"
];
};
"New struct type" = {
"prefix" = "struct";
"description" = "Create a struct";
"body" = [
"type \${1:name} struct {"
"\t$0"
"}"
];
};
"Receiver function" = {
"prefix" = "impl";
"description" = "Create a receiver function";
"body" = [
"func (\${1:s} *\${2:ty}) \${3:name}(\${4:params}) \${5:returns} {"
"\t$0"
"}"
];
};
"Assert implements" = {
"prefix" = "assert interface";
"description" = "Asserts that a type implements the given interface";
"body" = [
"var _ \${1:interface} = (*\${2:ty})(nil) // Asserts \${2} implements \${1}"
];
};
"Table test" = {
prefix = "func Test";
description = "Test function with table tests";
body = [
"func Test\${1:Name}(t *testing.T) {"
"\ttestcases := map[string]struct {"
"\t\texpected \${2:type}"
"\t\t\$0"
"\t}{"
"\t\t"
"\t}"
""
"\tt.Parallel()"
"\tfor name, tc := range testcases {"
"\t\tt.Run(name, func(t *testing.T) {"
"\t\t\tt.Parallel()"
"\t\t\t"
"\t\t})"
"\t}"
"}"
];
};
}
// (snippets.mkTableTestSnippet { paramCount = 1; })
// (snippets.mkTableTestSnippet {
paramCount = 1;
returnsError = true;
})
// (snippets.mkTableTestSnippet { paramCount = 2; })
// (snippets.mkTableTestSnippet {
paramCount = 2;
returnsError = true;
})
// (snippets.mkTableTestSnippet {
paramCount = 2;
returnCount = 2;
})
// (snippets.mkTableTestSnippet {
paramCount = 2;
returnCount = 2;
returnsError = true;
});
})
]);
}