@@ -20,22 +20,14 @@ import (
20
20
21
21
const listItemPrefix = "list-item-"
22
22
23
- func getExampleSnippets () (* SettingSnippets , error ) {
24
- reference , err := os .ReadFile (".golangci.reference.yml" )
25
- if err != nil {
26
- return nil , fmt .Errorf ("can't read .golangci.reference.yml: %w" , err )
27
- }
28
-
29
- snippets , err := extractExampleSnippets (reference )
30
- if err != nil {
31
- return nil , fmt .Errorf ("can't extract example snippets from .golangci.reference.yml: %w" , err )
32
- }
33
-
34
- return snippets , nil
35
- }
23
+ const (
24
+ keyLinters = "linters"
25
+ keyFormatters = "formatters"
26
+ keySettings = "settings"
27
+ )
36
28
37
- func getLintersListMarkdown (enabled bool ) string {
38
- linters , err := readJSONFile [[]* types.LinterWrapper ](filepath . Join ( "assets" , "linters-info.json" ) )
29
+ func getLintersListMarkdown (enabled bool , src string ) string {
30
+ linters , err := readJSONFile [[]* types.LinterWrapper ](src )
39
31
if err != nil {
40
32
panic (err )
41
33
}
@@ -172,14 +164,63 @@ func spanWithID(id, title, icon string) string {
172
164
}
173
165
174
166
type SettingSnippets struct {
175
- ConfigurationFile string
176
- LintersSettings string
167
+ ConfigurationFile string
168
+ LintersSettings string
169
+ FormattersSettings string
177
170
}
178
171
179
- func extractExampleSnippets (example []byte ) (* SettingSnippets , error ) {
180
- var data yaml.Node
181
- err := yaml .Unmarshal (example , & data )
172
+ func marshallSnippet (node * yaml.Node ) (string , error ) {
173
+ builder := & strings.Builder {}
174
+
175
+ if node .Value != "" {
176
+ _ , _ = fmt .Fprintf (builder , "### %s\n \n " , node .Value )
177
+ }
178
+ _ , _ = fmt .Fprintln (builder , "```yaml" )
179
+
180
+ encoder := yaml .NewEncoder (builder )
181
+ encoder .SetIndent (2 )
182
+
183
+ err := encoder .Encode (node )
184
+ if err != nil {
185
+ return "" , err
186
+ }
187
+
188
+ _ , _ = fmt .Fprintln (builder , "```" )
189
+ _ , _ = fmt .Fprintln (builder )
190
+
191
+ return builder .String (), nil
192
+ }
193
+
194
+ type ExampleSnippetsExtractor struct {
195
+ referencePath string
196
+ assetsPath string
197
+ }
198
+
199
+ func NewExampleSnippetsExtractor () * ExampleSnippetsExtractor {
200
+ return & ExampleSnippetsExtractor {
201
+ // TODO(ldez) replace .golangci.next.reference.yml by .golangci.reference.yml
202
+ referencePath : ".golangci.next.reference.yml" ,
203
+ assetsPath : "assets" ,
204
+ }
205
+ }
206
+
207
+ func (e * ExampleSnippetsExtractor ) GetExampleSnippets () (* SettingSnippets , error ) {
208
+ reference , err := os .ReadFile (e .referencePath )
209
+ if err != nil {
210
+ return nil , fmt .Errorf ("can't read .golangci.reference.yml: %w" , err )
211
+ }
212
+
213
+ snippets , err := e .extractExampleSnippets (reference )
182
214
if err != nil {
215
+ return nil , fmt .Errorf ("can't extract example snippets from .golangci.reference.yml: %w" , err )
216
+ }
217
+
218
+ return snippets , nil
219
+ }
220
+
221
+ func (e * ExampleSnippetsExtractor ) extractExampleSnippets (example []byte ) (* SettingSnippets , error ) {
222
+ var data yaml.Node
223
+ if err := yaml .Unmarshal (example , & data ); err != nil {
183
224
return nil , err
184
225
}
185
226
@@ -205,7 +246,7 @@ func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
205
246
206
247
for j , node := range root .Content {
207
248
switch node .Value {
208
- case "run" , "output" , "linters" , "linters-settings" , "issues" , "severity" : // TODO(ldez) documentation
249
+ case "run" , "output" , keyLinters , keyFormatters , "issues" , "severity" :
209
250
default :
210
251
continue
211
252
}
@@ -233,20 +274,43 @@ func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
233
274
234
275
globalNode .Content = append (globalNode .Content , node , newNode )
235
276
236
- if node .Value == "linters-settings" { // TODO(ldez) documentation
237
- snippets .LintersSettings , err = getLintersSettingSections (node , nextNode )
238
- if err != nil {
239
- return nil , err
277
+ if node .Value == keyLinters || node .Value == keyFormatters {
278
+ for i := 0 ; i < len (nextNode .Content ); i ++ {
279
+ if nextNode .Content [i ].Value != keySettings {
280
+ continue
281
+ }
282
+
283
+ settingSections , err := e .getSettingSections (node , nextNode .Content [i + 1 ])
284
+ if err != nil {
285
+ return nil , err
286
+ }
287
+
288
+ switch node .Value {
289
+ case keyLinters :
290
+ snippets .LintersSettings = settingSections
291
+
292
+ case keyFormatters :
293
+ snippets .FormattersSettings = settingSections
294
+ }
295
+
296
+ nextNode .Content [i + 1 ].Content = []* yaml.Node {
297
+ {
298
+ HeadComment : fmt .Sprintf (`See the dedicated "%s.%s" documentation section.` , node .Value , nextNode .Content [i ].Value ),
299
+ Kind : node .Kind ,
300
+ Style : node .Style ,
301
+ Tag : node .Tag ,
302
+ Value : "option" ,
303
+ },
304
+ {
305
+ Kind : node .Kind ,
306
+ Style : node .Style ,
307
+ Tag : node .Tag ,
308
+ Value : "value" ,
309
+ },
310
+ }
311
+
312
+ i ++
240
313
}
241
-
242
- _ , _ = builder .WriteString (
243
- // TODO(ldez) documentation
244
- fmt .Sprintf (
245
- "### `%s` configuration\n \n See the dedicated [linters-settings](/usage/linters) documentation section.\n \n " ,
246
- node .Value ,
247
- ),
248
- )
249
- continue
250
314
}
251
315
252
316
nodeSection := & yaml.Node {
@@ -275,8 +339,8 @@ func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
275
339
return & snippets , nil
276
340
}
277
341
278
- func getLintersSettingSections (node , nextNode * yaml.Node ) (string , error ) {
279
- linters , err := readJSONFile [[]* types.LinterWrapper ](filepath .Join ("assets" , "linters -info.json" ))
342
+ func ( e * ExampleSnippetsExtractor ) getSettingSections (node , nextNode * yaml.Node ) (string , error ) {
343
+ linters , err := readJSONFile [[]* types.LinterWrapper ](filepath .Join (e . assetsPath , fmt . Sprintf ( "%s -info.json", node . Value ) ))
280
344
if err != nil {
281
345
return "" , err
282
346
}
@@ -331,25 +395,3 @@ func getLintersSettingSections(node, nextNode *yaml.Node) (string, error) {
331
395
332
396
return builder .String (), nil
333
397
}
334
-
335
- func marshallSnippet (node * yaml.Node ) (string , error ) {
336
- builder := & strings.Builder {}
337
-
338
- if node .Value != "" {
339
- _ , _ = fmt .Fprintf (builder , "### %s\n \n " , node .Value )
340
- }
341
- _ , _ = fmt .Fprintln (builder , "```yaml" )
342
-
343
- encoder := yaml .NewEncoder (builder )
344
- encoder .SetIndent (2 )
345
-
346
- err := encoder .Encode (node )
347
- if err != nil {
348
- return "" , err
349
- }
350
-
351
- _ , _ = fmt .Fprintln (builder , "```" )
352
- _ , _ = fmt .Fprintln (builder )
353
-
354
- return builder .String (), nil
355
- }
0 commit comments