summaryrefslogtreecommitdiffstats
path: root/src/go/printer/comment.go
blob: 9012714939406b8a0dd1d0d477e2195b44426a92 (plain)
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
// 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 printer

import (
	"go/ast"
	"go/doc/comment"
	"strings"
)

// formatDocComment reformats the doc comment list,
// returning the canonical formatting.
func formatDocComment(list []*ast.Comment) []*ast.Comment {
	// Extract comment text (removing comment markers).
	var kind, text string
	var directives []*ast.Comment
	if len(list) == 1 && strings.HasPrefix(list[0].Text, "/*") {
		kind = "/*"
		text = list[0].Text
		if !strings.Contains(text, "\n") || allStars(text) {
			// 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 list
		}
		text = text[2 : len(text)-2] // cut /* and */
	} else if strings.HasPrefix(list[0].Text, "//") {
		kind = "//"
		var b strings.Builder
		for _, c := range list {
			after, found := strings.CutPrefix(c.Text, "//")
			if !found {
				return list
			}
			// Accumulate //go:build etc lines separately.
			if isDirective(after) {
				directives = append(directives, c)
				continue
			}
			b.WriteString(strings.TrimPrefix(after, " "))
			b.WriteString("\n")
		}
		text = b.String()
	} else {
		// Not sure what this is, so leave alone.
		return list
	}

	if text == "" {
		return list
	}

	// Parse comment and reformat as text.
	var p comment.Parser
	d := p.Parse(text)

	var pr comment.Printer
	text = string(pr.Comment(d))

	// For /* */ comment, return one big comment with text inside.
	slash := list[0].Slash
	if kind == "/*" {
		c := &ast.Comment{
			Slash: slash,
			Text:  "/*\n" + text + "*/",
		}
		return []*ast.Comment{c}
	}

	// For // comment, return sequence of // lines.
	var out []*ast.Comment
	for text != "" {
		var line string
		line, text, _ = strings.Cut(text, "\n")
		if line == "" {
			line = "//"
		} else if strings.HasPrefix(line, "\t") {
			line = "//" + line
		} else {
			line = "// " + line
		}
		out = append(out, &ast.Comment{
			Slash: slash,
			Text:  line,
		})
	}
	if len(directives) > 0 {
		out = append(out, &ast.Comment{
			Slash: slash,
			Text:  "//",
		})
		for _, c := range directives {
			out = append(out, &ast.Comment{
				Slash: slash,
				Text:  c.Text,
			})
		}
	}
	return out
}

// isDirective reports whether c is a comment directive.
// See go.dev/issue/37974.
// This code is also in go/ast.
func isDirective(c string) bool {
	// "//line " is a line directive.
	// "//extern " is for gccgo.
	// "//export " is for cgo.
	// (The // has been removed.)
	if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") {
		return true
	}

	// "//[a-z0-9]+:[a-z0-9]"
	// (The // has been removed.)
	colon := strings.Index(c, ":")
	if colon <= 0 || colon+1 >= len(c) {
		return false
	}
	for i := 0; i <= colon+1; i++ {
		if i == colon {
			continue
		}
		b := c[i]
		if !('a' <= b && b <= 'z' || '0' <= b && b <= '9') {
			return false
		}
	}
	return true
}

// allStars reports whether text is the interior of an
// old-style /* */ comment with a star at the start of each line.
func allStars(text string) bool {
	for i := 0; i < len(text); i++ {
		if text[i] == '\n' {
			j := i + 1
			for j < len(text) && (text[j] == ' ' || text[j] == '\t') {
				j++
			}
			if j < len(text) && text[j] != '*' {
				return false
			}
		}
	}
	return true
}