@@ -165,7 +165,7 @@ func buildTemplateContext() (map[string]string, error) {
165165 return nil , fmt .Errorf ("can't read .golangci.example.yml: %s" , err )
166166 }
167167
168- lintersCfg , err := getLintersConfiguration (golangciYamlExample )
168+ snippets , err := extractExampleSnippets (golangciYamlExample )
169169 if err != nil {
170170 return nil , fmt .Errorf ("can't read .golangci.example.yml: %s" , err )
171171 }
@@ -202,8 +202,8 @@ func buildTemplateContext() (map[string]string, error) {
202202 }
203203
204204 return map [string ]string {
205- "LintersExample" : lintersCfg ,
206- "GolangciYamlExample " : strings . TrimSpace ( string ( golangciYamlExample )) ,
205+ "LintersExample" : snippets . LintersSettings ,
206+ "ConfigurationExample " : snippets . ConfigurationFile ,
207207 "LintersCommandOutputEnabledOnly" : string (lintersOutParts [0 ]),
208208 "LintersCommandOutputDisabledOnly" : string (lintersOutParts [1 ]),
209209 "EnabledByDefaultLinters" : getLintersListMarkdown (true ),
@@ -317,58 +317,168 @@ func getThanksList() string {
317317 return strings .Join (lines , "\n " )
318318}
319319
320- func getLintersConfiguration (example []byte ) (string , error ) {
321- builder := & strings.Builder {}
320+ type SettingSnippets struct {
321+ ConfigurationFile string
322+ LintersSettings string
323+ }
322324
325+ func extractExampleSnippets (example []byte ) (* SettingSnippets , error ) {
323326 var data yaml.Node
324327 err := yaml .Unmarshal (example , & data )
325328 if err != nil {
326- return "" , err
329+ return nil , err
327330 }
328331
329332 root := data .Content [0 ]
330333
334+ globalNode := & yaml.Node {
335+ Kind : root .Kind ,
336+ Style : root .Style ,
337+ Tag : root .Tag ,
338+ Value : root .Value ,
339+ Anchor : root .Anchor ,
340+ Alias : root .Alias ,
341+ HeadComment : root .HeadComment ,
342+ LineComment : root .LineComment ,
343+ FootComment : root .FootComment ,
344+ Line : root .Line ,
345+ Column : root .Column ,
346+ }
347+
348+ snippets := SettingSnippets {}
349+
350+ buffer := bytes .NewBufferString ("" )
351+
331352 for j , node := range root .Content {
332- if node .Value != "linters-settings" {
353+ switch node .Value {
354+ case "run" , "output" , "linters" , "linters-settings" , "issues" , "severity" :
355+ default :
333356 continue
334357 }
335358
336- nodes := root .Content [j + 1 ]
337-
338- for i := 0 ; i < len (nodes .Content ); i += 2 {
339- r := & yaml.Node {
340- Kind : nodes .Kind ,
341- Style : nodes .Style ,
342- Tag : nodes .Tag ,
343- Value : node .Value ,
344- Content : []* yaml.Node {
345- {
346- Kind : root .Content [j ].Kind ,
347- Value : root .Content [j ].Value ,
348- },
349- {
350- Kind : nodes .Kind ,
351- Content : []* yaml.Node {nodes .Content [i ], nodes .Content [i + 1 ]},
352- },
359+ nextNode := root .Content [j + 1 ]
360+
361+ newNode := & yaml.Node {
362+ Kind : nextNode .Kind ,
363+ Content : []* yaml.Node {
364+ {
365+ HeadComment : fmt .Sprintf ("See the dedicated %q documentation section." , node .Value ),
366+ Kind : node .Kind ,
367+ Style : node .Style ,
368+ Tag : node .Tag ,
369+ Value : "option" ,
353370 },
354- }
355-
356- _ , _ = fmt .Fprintf (builder , "### %s\n \n " , nodes .Content [i ].Value )
357- _ , _ = fmt .Fprintln (builder , "```yaml" )
371+ {
372+ Kind : node .Kind ,
373+ Style : node .Style ,
374+ Tag : node .Tag ,
375+ Value : "value" ,
376+ },
377+ },
378+ }
358379
359- const ident = 2
360- encoder := yaml .NewEncoder (builder )
361- encoder .SetIndent (ident )
380+ globalNode .Content = append (globalNode .Content , node , newNode )
362381
363- err = encoder .Encode (r )
382+ if node .Value == "linters-settings" {
383+ snippets .LintersSettings , err = getLintersSettingSnippets (node , nextNode )
364384 if err != nil {
365- return "" , err
385+ return nil , err
366386 }
367387
368- _ , _ = fmt .Fprintln (builder , "```" )
369- _ , _ = fmt .Fprintln (builder )
388+ _ , _ = buffer .WriteString (
389+ fmt .Sprintf (
390+ "### `%s` configuration\n \n See the dedicated [linters-settings](/usage/linters) documentation section.\n \n " ,
391+ node .Value ,
392+ ),
393+ )
394+ continue
395+ }
396+
397+ nodeSection := & yaml.Node {
398+ Kind : root .Kind ,
399+ Style : root .Style ,
400+ Tag : root .Tag ,
401+ Value : root .Value ,
402+ Content : []* yaml.Node {node , nextNode },
403+ }
404+
405+ snippet , errSnip := marshallSnippet (nodeSection )
406+ if errSnip != nil {
407+ return nil , errSnip
408+ }
409+
410+ _ , _ = buffer .WriteString (fmt .Sprintf ("### `%s` configuration\n \n %s" , node .Value , snippet ))
411+ }
412+
413+ overview , err := marshallSnippet (globalNode )
414+ if err != nil {
415+ return nil , err
416+ }
417+
418+ snippets .ConfigurationFile = overview + buffer .String ()
419+
420+ return & snippets , nil
421+ }
422+
423+ func getLintersSettingSnippets (node , nextNode * yaml.Node ) (string , error ) {
424+ builder := & strings.Builder {}
425+
426+ for i := 0 ; i < len (nextNode .Content ); i += 2 {
427+ r := & yaml.Node {
428+ Kind : nextNode .Kind ,
429+ Style : nextNode .Style ,
430+ Tag : nextNode .Tag ,
431+ Value : node .Value ,
432+ Content : []* yaml.Node {
433+ {
434+ Kind : node .Kind ,
435+ Value : node .Value ,
436+ },
437+ {
438+ Kind : nextNode .Kind ,
439+ Content : []* yaml.Node {nextNode .Content [i ], nextNode .Content [i + 1 ]},
440+ },
441+ },
370442 }
443+
444+ _ , _ = fmt .Fprintf (builder , "### %s\n \n " , nextNode .Content [i ].Value )
445+ _ , _ = fmt .Fprintln (builder , "```yaml" )
446+
447+ const ident = 2
448+ encoder := yaml .NewEncoder (builder )
449+ encoder .SetIndent (ident )
450+
451+ err := encoder .Encode (r )
452+ if err != nil {
453+ return "" , err
454+ }
455+
456+ _ , _ = fmt .Fprintln (builder , "```" )
457+ _ , _ = fmt .Fprintln (builder )
371458 }
372459
373460 return builder .String (), nil
374461}
462+
463+ func marshallSnippet (node * yaml.Node ) (string , error ) {
464+ builder := & strings.Builder {}
465+
466+ if node .Value != "" {
467+ _ , _ = fmt .Fprintf (builder , "### %s\n \n " , node .Value )
468+ }
469+ _ , _ = fmt .Fprintln (builder , "```yaml" )
470+
471+ const ident = 2
472+ encoder := yaml .NewEncoder (builder )
473+ encoder .SetIndent (ident )
474+
475+ err := encoder .Encode (node )
476+ if err != nil {
477+ return "" , err
478+ }
479+
480+ _ , _ = fmt .Fprintln (builder , "```" )
481+ _ , _ = fmt .Fprintln (builder )
482+
483+ return builder .String (), nil
484+ }
0 commit comments