Debian Codegraph @

internal/govendor/go/printer/comment.go

raw symbols SCIP
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 printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/
6
7 import (
8 "go/astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/"
9 "stringsscip-go gomod github.com/golang/go/src go1.20 strings/"
10
11 "mvdan.cc/gofumpt/internal/govendor/go/doc/commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/"
12 )
13
14 // formatDocComment reformats the doc comment list,
15 // returning the canonical formatting.
16 func formatDocCommentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/formatDocComment().(list []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#) []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment# {
17 // Extract comment text (removing comment markers).
18 var kind, text string
19 var directives []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#
20 if len(list) == 1 && stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(list[0].Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text., "/*") {
21 kind = "/*"
22 text = list[0].Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.
23 if !stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Containsscip-go gomod github.com/golang/go/src go1.20 strings/Contains().(text, "\n") || allStarsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/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 stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(list[0].Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text., "//") {
37 kind = "//"
38 var b stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Builderscip-go gomod github.com/golang/go/src go1.20 strings/Builder#
39 for _, c := range list {
40 after, found := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.CutPrefixscip-go gomod github.com/golang/go/src go1.20 strings/CutPrefix().(c.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text., "//")
41 if !found {
42 return list
43 }
44 // Accumulate //go:build etc lines separately.
45 if isDirectivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/isDirective().(after) {
46 directives = append(directives, c)
47 continue
48 }
49 b.WriteStringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteString().(stringsscip-go gomod github.com/golang/go/src go1.20 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.20 strings/TrimPrefix().(after, " "))
50 b.WriteStringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteString().("\n")
51 }
52 text = b.Stringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#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 commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/.Parserscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Parser#
64 d := p.Parsescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Parser#Parse().(text)
65
66 var pr commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/.Printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#
67 text = string(pr.Commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#Comment().(d))
68
69 // For /* */ comment, return one big comment with text inside.
70 slash := list[0].Slashscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Slash.
71 if kind == "/*" {
72 c := &astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#{
73 Slashscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Slash.: slash,
74 Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.: "/*\n" + text + "*/",
75 }
76 return []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#{c}
77 }
78
79 // For // comment, return sequence of // lines.
80 var out []*astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#
81 for text != "" {
82 var line string
83 line, text, _ = stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Cutscip-go gomod github.com/golang/go/src go1.20 strings/Cut().(text, "\n")
84 if line == "" {
85 line = "//"
86 } else if stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(line, "\t") {
87 line = "//" + line
88 } else {
89 line = "// " + line
90 }
91 out = append(out, &astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#{
92 Slashscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Slash.: slash,
93 Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.: line,
94 })
95 }
96 if len(directives) > 0 {
97 out = append(out, &astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#{
98 Slashscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Slash.: slash,
99 Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.: "//",
100 })
101 for _, c := range directives {
102 out = append(out, &astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Commentscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#{
103 Slashscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Slash.: slash,
104 Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.: c.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#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 isDirectivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/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 stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(c, "line ") || stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(c, "extern ") || stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(c, "export ") {
120 return true
121 }
122
123 // "//[a-z0-9]+:[a-z0-9]"
124 // (The // has been removed.)
125 colon := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Indexscip-go gomod github.com/golang/go/src go1.20 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 allStarsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/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 }