| 1 | // Copyright 2022 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package printer |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "strings" |
| 10 | |
| 11 | "mvdan.cc/gofumpt/internal/govendor/go/doc/comment" |
| 12 | ) |
| 13 | |
| 14 | // formatDocComment reformats the doc comment list, |
| 15 | // returning the canonical formatting. |
| 16 | func formatDocComment(list []*ast.Comment) []*ast.Comment { |
| 17 | // Extract comment text (removing comment markers). |
| 18 | var kind, text string |
| 19 | var directives []*ast.Comment |
| 20 | if len(list) == 1 && strings.HasPrefix(list[0].Text, "/*") { |
| 21 | kind = "/*" |
| 22 | text = list[0].Text |
| 23 | if !strings.Contains(text, "\n") || allStars(text) { |
| 24 | // Single-line /* .. */ comment in doc comment position, |
| 25 | // or multiline old-style comment like |
| 26 | // /* |
| 27 | // * Comment |
| 28 | // * text here. |
| 29 | // */ |
| 30 | // Should not happen, since it will not work well as a |
| 31 | // doc comment, but if it does, just ignore: |
| 32 | // reformatting it will only make the situation worse. |
| 33 | return list |
| 34 | } |
| 35 | text = text[2 : len(text)-2] // cut /* and */ |
| 36 | } else if strings.HasPrefix(list[0].Text, "//") { |
| 37 | kind = "//" |
| 38 | var b strings.Builder |
| 39 | for _, c := range list { |
| 40 | after, found := strings.CutPrefix(c.Text, "//") |
| 41 | if !found { |
| 42 | return list |
| 43 | } |
| 44 | // Accumulate //go:build etc lines separately. |
| 45 | if isDirective(after) { |
| 46 | directives = append(directives, c) |
| 47 | continue |
| 48 | } |
| 49 | b.WriteString(strings.TrimPrefix(after, " ")) |
| 50 | b.WriteString("\n") |
| 51 | } |
| 52 | text = b.String() |
| 53 | } else { |
| 54 | // Not sure what this is, so leave alone. |
| 55 | return list |
| 56 | } |
| 57 | |
| 58 | if text == "" { |
| 59 | return list |
| 60 | } |
| 61 | |
| 62 | // Parse comment and reformat as text. |
| 63 | var p comment.Parser |
| 64 | d := p.Parse(text) |
| 65 | |
| 66 | var pr comment.Printer |
| 67 | text = string(pr.Comment(d)) |
| 68 | |
| 69 | // For /* */ comment, return one big comment with text inside. |
| 70 | slash := list[0].Slash |
| 71 | if kind == "/*" { |
| 72 | c := &ast.Comment{ |
| 73 | Slash: slash, |
| 74 | Text: "/*\n" + text + "*/", |
| 75 | } |
| 76 | return []*ast.Comment{c} |
| 77 | } |
| 78 | |
| 79 | // For // comment, return sequence of // lines. |
| 80 | var out []*ast.Comment |
| 81 | for text != "" { |
| 82 | var line string |
| 83 | line, text, _ = strings.Cut(text, "\n") |
| 84 | if line == "" { |
| 85 | line = "//" |
| 86 | } else if strings.HasPrefix(line, "\t") { |
| 87 | line = "//" + line |
| 88 | } else { |
| 89 | line = "// " + line |
| 90 | } |
| 91 | out = append(out, &ast.Comment{ |
| 92 | Slash: slash, |
| 93 | Text: line, |
| 94 | }) |
| 95 | } |
| 96 | if len(directives) > 0 { |
| 97 | out = append(out, &ast.Comment{ |
| 98 | Slash: slash, |
| 99 | Text: "//", |
| 100 | }) |
| 101 | for _, c := range directives { |
| 102 | out = append(out, &ast.Comment{ |
| 103 | Slash: slash, |
| 104 | Text: c.Text, |
| 105 | }) |
| 106 | } |
| 107 | } |
| 108 | return out |
| 109 | } |
| 110 | |
| 111 | // isDirective reports whether c is a comment directive. |
| 112 | // See go.dev/issue/37974. |
| 113 | // This code is also in go/ast. |
| 114 | func isDirective(c string) bool { |
| 115 | // "//line " is a line directive. |
| 116 | // "//extern " is for gccgo. |
| 117 | // "//export " is for cgo. |
| 118 | // (The // has been removed.) |
| 119 | if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") { |
| 120 | return true |
| 121 | } |
| 122 | |
| 123 | // "//[a-z0-9]+:[a-z0-9]" |
| 124 | // (The // has been removed.) |
| 125 | colon := strings.Index(c, ":") |
| 126 | if colon <= 0 || colon+1 >= len(c) { |
| 127 | return false |
| 128 | } |
| 129 | for i := 0; i <= colon+1; i++ { |
| 130 | if i == colon { |
| 131 | continue |
| 132 | } |
| 133 | b := c[i] |
| 134 | if !('a' <= b && b <= 'z' || '0' <= b && b <= '9') { |
| 135 | return false |
| 136 | } |
| 137 | } |
| 138 | return true |
| 139 | } |
| 140 | |
| 141 | // allStars reports whether text is the interior of an |
| 142 | // old-style /* */ comment with a star at the start of each line. |
| 143 | func allStars(text string) bool { |
| 144 | for i := 0; i < len(text); i++ { |
| 145 | if text[i] == '\n' { |
| 146 | j := i + 1 |
| 147 | for j < len(text) && (text[j] == ' ' || text[j] == '\t') { |
| 148 | j++ |
| 149 | } |
| 150 | if j < len(text) && text[j] != '*' { |
| 151 | return false |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | return true |
| 156 | } |