// Copyright 2022 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package printerimport ()// formatDocComment reformats the doc comment list,// returning the canonical formatting.func formatDocComment( []*ast.Comment) []*ast.Comment {// Extract comment text (removing comment markers).var , stringvar []*ast.Commentiflen() == 1 && strings.HasPrefix([0].Text, "/*") { = "/*" = [0].Textif !strings.Contains(, "\n") || allStars() {// Single-line /* .. */ comment in doc comment position, // or multiline old-style comment like // /* // * Comment // * text here. // */ // Should not happen, since it will not work well as a // doc comment, but if it does, just ignore: // reformatting it will only make the situation worse.return } = [2 : len()-2] // cut /* and */ } elseifstrings.HasPrefix([0].Text, "//") { = "//"varstrings.Builderfor , := range { , := strings.CutPrefix(.Text, "//")if ! {return }// Accumulate //go:build etc lines separately.ifisDirective() { = append(, )continue } .WriteString(strings.TrimPrefix(, " ")) .WriteString("\n") } = .String() } else {// Not sure what this is, so leave alone.return }if == "" {return }// Parse comment and reformat as text.varcomment.Parser := .Parse()varcomment.Printer = string(.Comment())// For /* */ comment, return one big comment with text inside. := [0].Slashif == "/*" { := &ast.Comment{Slash: ,Text: "/*\n" + + "*/", }return []*ast.Comment{} }// For // comment, return sequence of // lines.var []*ast.Commentfor != "" {varstring , , _ = strings.Cut(, "\n")if == "" { = "//" } elseifstrings.HasPrefix(, "\t") { = "//" + } else { = "// " + } = append(, &ast.Comment{Slash: ,Text: , }) }iflen() > 0 { = append(, &ast.Comment{Slash: ,Text: "//", })for , := range { = append(, &ast.Comment{Slash: ,Text: .Text, }) } }return}// isDirective reports whether c is a comment directive.// See go.dev/issue/37974.// This code is also in go/ast.func isDirective( string) bool {// "//line " is a line directive. // "//extern " is for gccgo. // "//export " is for cgo. // (The // has been removed.)ifstrings.HasPrefix(, "line ") || strings.HasPrefix(, "extern ") || strings.HasPrefix(, "export ") {returntrue }// "//[a-z0-9]+:[a-z0-9]" // (The // has been removed.) := strings.Index(, ":")if <= 0 || +1 >= len() {returnfalse }for := 0; <= +1; ++ {if == {continue } := []if !('a' <= && <= 'z' || '0' <= && <= '9') {returnfalse } }returntrue}// allStars reports whether text is the interior of an// old-style /* */ comment with a star at the start of each line.func allStars( string) bool {for := 0; < len(); ++ {if [] == '\n' { := + 1for < len() && ([] == ' ' || [] == '\t') { ++ }if < len() && [] != '*' {returnfalse } } }returntrue}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.