forked from numtide/treefmt-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstylua.nix
193 lines (173 loc) · 4.64 KB
/
stylua.nix
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
{
lib,
pkgs,
config,
mkFormatterModule,
...
}:
let
inherit (lib.types)
bool
int
enum
nullOr
;
cfg = config.programs.stylua;
configFormat = pkgs.formats.toml { };
/*
The schema and descriptions were taken from the StyLua README
on the project's GitHub page:
<https://github.com/JohnnyMorganz/StyLua/blob/main/README.md>
*/
settingsSchema = {
column_width = lib.mkOption {
description = ''
Approximate line length for printing.
Used as a guide for line wrapping -
this is not a hard requirement:
lines may fall under or over the limit.
'';
type = nullOr int;
example = 120;
default = null;
};
line_endings = lib.mkOption {
description = ''
Line endings type.
'';
type = nullOr (enum [
"Unix"
"Windows"
]);
example = "Unix";
default = null;
};
indent_type = lib.mkOption {
description = ''
Indent type.
'';
type = nullOr (enum [
"Tabs"
"Spaces"
]);
example = "Tabs";
default = null;
};
indent_width = lib.mkOption {
description = ''
Character size of single indentation.
If `indent_type` is set to `Tabs`,
this option is used as a heuristic to
determine column width only.
'';
type = nullOr int;
example = 4;
default = null;
};
quote_style = lib.mkOption {
description = ''
Quote style for string literals.
`AutoPrefer` styles will prefer the
specified quote style, but fall back to
the alternative if it has fewer string
escapes. `Force` styles always use the
specified style regardless of escapes.
'';
type = nullOr (enum [
"AutoPreferDouble"
"AutoPreferSingle"
"ForceDouble"
"ForceSingle"
]);
example = "AutoPreferDouble";
default = null;
};
call_parentheses = lib.mkOption {
description = ''
Whether parentheses should be applied on
function calls with a single string/table
argument. `Always` applies parentheses in
all cases. `NoSingleString` omits
parentheses on calls with a single string
argument. Similarly, `NoSingleTable` omits
parentheses on calls with a single table
argument. `None` omits parentheses in both
cases.
Note: parentheses are still kept in situations
where removal can lead to obscurity
(e.g. `foo "bar".setup -> foo("bar").setup`,
since the index is on the call result, not the string).
`Input` removes all automation and preserves
parentheses only if they were present in input code:
consistency is not enforced.
'';
type = nullOr (enum [
"Always"
"NoSingleString"
"NoSingleTable"
"None"
"Input"
]);
example = "Always";
default = null;
};
collapse_simple_statement = lib.mkOption {
description = ''
Specify whether to collapse simple statements.
'';
type = nullOr (enum [
"Never"
"FunctionOnly"
"ConditionalOnly"
"Always"
]);
example = "Never";
default = null;
};
sort_requires.enabled = lib.mkOption {
description = ''
StyLua has built-in support for sorting
require statements. We group consecutive
require statements into a single "block",
and then requires are sorted only within
that block. Blocks of requires do not
move around the file.
We only include requires of the form
`local NAME = require(EXPR)`, and sort
lexicographically based on `NAME`.
(We also sort Roblox services of the form
`local NAME = game:GetService(EXPR)`)
'';
type = nullOr bool;
example = false;
default = null;
};
};
settingsFile =
let
filterOutNull = lib.filterAttrsRecursive (_: v: v != null);
filterOutEmptyAttrs = lib.filterAttrsRecursive (_: v: v != { });
settings = filterOutEmptyAttrs (filterOutNull cfg.settings);
in
if settings != { } then configFormat.generate "stylua.toml" settings else null;
in
{
meta.maintainers = [ "sebaszv" ];
imports = [
(mkFormatterModule {
name = "stylua";
includes = [ "*.lua" ];
})
];
options.programs.stylua = {
settings = settingsSchema;
};
config = lib.mkIf cfg.enable {
settings.formatter.stylua = {
options = lib.mkIf (settingsFile != null) [
"--config-path"
(toString settingsFile)
];
};
};
}