forked from numtide/treefmt-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocorrect.nix
More file actions
238 lines (216 loc) · 6.43 KB
/
autocorrect.nix
File metadata and controls
238 lines (216 loc) · 6.43 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{
lib,
pkgs,
config,
mkFormatterModule,
...
}:
let
cfg = config.programs.autocorrect;
configFormat = pkgs.formats.json { };
# 0 - off, 1 - error, 2 - warning
ruleType = lib.types.nullOr (
lib.types.enum [
"off"
"error"
"warning"
]
);
# Configuration schema for Autocorrect, which we generate .autocorrectrc with.
# Definition taken from: https://github.com/huacnlee/autocorrect/raw/refs/heads/main/.autocorrectrc.template
settingsSchema = {
rules = lib.mkOption {
description = "Configure rules for autocorrect formatting.";
type = lib.types.nullOr (
lib.types.submodule {
options = {
space-word = lib.mkOption {
description = "Auto add spacing between CJK (Chinese, Japanese, Korean) and English words.";
type = ruleType;
example = "off";
default = null;
};
space-punctuation = lib.mkOption {
description = "Add space between some punctuations.";
type = ruleType;
example = "error";
default = null;
};
space-bracket = lib.mkOption {
description = "Add space between brackets (), [] when near the CJK.";
type = ruleType;
example = "error";
default = null;
};
space-backticks = lib.mkOption {
description = "Add space between ``, when near the CJK.";
type = ruleType;
example = "error";
default = null;
};
space-dash = lib.mkOption {
description = "Add space between dash `-`.";
type = ruleType;
example = "off";
default = null;
};
space-dollar = lib.mkOption {
description = "Add space between dollar $ when near the CJK.";
type = ruleType;
example = "off";
default = null;
};
fullwidth = lib.mkOption {
description = "Convert to fullwidth.";
type = ruleType;
example = "error";
default = null;
};
no-space-fullwidth = lib.mkOption {
description = "Remove space near the fullwidth.";
type = ruleType;
example = "error";
default = null;
};
halfwidth-word = lib.mkOption {
description = "Fullwidth alphanumeric characters to halfwidth.";
type = ruleType;
example = "error";
default = null;
};
halfwidth-punctuation = lib.mkOption {
description = "Fullwidth punctuations to halfwidth in english.";
type = ruleType;
example = "error";
default = null;
};
spellcheck = lib.mkOption {
description = "Spellcheck.";
type = ruleType;
example = "warning";
default = null;
};
};
}
);
default = null;
};
context = lib.mkOption {
description = "Enable or disable in a specific context.";
type = lib.types.nullOr (
lib.types.submodule {
options = {
codeblock = lib.mkOption {
description = "Enable or disable to format codeblock in Markdown or AsciiDoc etc.";
type = ruleType;
example = "error";
default = null;
};
};
}
);
default = null;
};
textRules = lib.mkOption {
description = "Configure special rules for some texts.";
type = lib.types.nullOr (lib.types.attrsOf ruleType);
example = {
"Hello你好" = "warning";
"Hi你好" = "off";
};
default = null;
};
fileTypes = lib.mkOption {
description = "Configure the files associations, you config is higher priority than default.";
type = lib.types.nullOr (lib.types.attrsOf lib.types.str);
example = {
"rb" = "ruby";
"Rakefile" = "ruby";
"*.js" = "javascript";
".mdx" = "markdown";
};
default = null;
};
spellcheck = lib.mkOption {
description = "Spellcheck configuration.";
type = lib.types.nullOr (
lib.types.submodule {
options = {
words = lib.mkOption {
description = "Correct Words (Case insensitive) for by Spellcheck.";
type = lib.types.nullOr (lib.types.listOf lib.types.str);
example = [
"GitHub"
"App Store"
"AppStore = App Store"
"Git"
"Node.js"
"nodejs = Node.js"
"VIM"
"DNS"
"HTTP"
"SSL"
];
default = null;
};
};
}
);
default = null;
};
};
settingsFile =
let
# Convert rule lib.types from strings to numeric values
convertRuleTypes =
attrs:
lib.mapAttrsRecursive (
_path: value:
if isNull value then
null
else
{
"off" = 0;
"error" = 1;
"warning" = 2;
}
.${value} or value
) attrs;
# remove all null values and convert rule lib.types
settings = lib.filterAttrsRecursive (_n: v: v != null) (convertRuleTypes cfg.settings);
in
if settings != { } then configFormat.generate ".autocorrectrc" settings else null;
in
{
meta.maintainers = [ "definfo" ];
imports = [
(mkFormatterModule {
name = "autocorrect";
args = [ "--fix" ];
includes = [ "*" ];
})
];
options.programs.autocorrect = {
threads = lib.mkOption {
description = "Number of threads, 0 - use number of CPU. [default: 0]";
type = lib.types.nullOr lib.types.int;
example = 2;
default = null;
};
# Represents the .autocorrectrc config schema
settings = settingsSchema;
};
config = lib.mkIf cfg.enable {
settings.formatter.autocorrect = {
options =
(lib.optionals (!isNull cfg.threads) [
"--threads"
(toString cfg.threads)
])
++ (lib.optionals (settingsFile != null) [
"--config"
(toString settingsFile)
]);
};
};
}