|
| 1 | +// CommentToString is a modified version of ast.CommentGroup.Text() that doesn't skip directives. |
| 2 | +// https://github.com/golang/go/blob/b70244ff7a043786c211775b68259de6104ff91c/src/go/ast/ast.go#L97 |
| 3 | +// |
| 4 | +// Copyright 2009 The Go Authors. |
| 5 | +// |
| 6 | +// Redistribution and use in source and binary forms, with or without |
| 7 | +// modification, are permitted provided that the following conditions are |
| 8 | +// met: |
| 9 | +// |
| 10 | +// * Redistributions of source code must retain the above copyright |
| 11 | +// notice, this list of conditions and the following disclaimer. |
| 12 | +// * Redistributions in binary form must reproduce the above |
| 13 | +// copyright notice, this list of conditions and the following disclaimer |
| 14 | +// in the documentation and/or other materials provided with the |
| 15 | +// distribution. |
| 16 | +// * Neither the name of Google LLC nor the names of its |
| 17 | +// contributors may be used to endorse or promote products derived from |
| 18 | +// this software without specific prior written permission. |
| 19 | +// |
| 20 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + |
| 32 | +package parse |
| 33 | + |
| 34 | +import ( |
| 35 | + "go/ast" |
| 36 | + "strings" |
| 37 | +) |
| 38 | + |
| 39 | +func CommentToString(g *ast.CommentGroup) string { |
| 40 | + if g == nil { |
| 41 | + return "" |
| 42 | + } |
| 43 | + comments := make([]string, len(g.List)) |
| 44 | + for i, c := range g.List { |
| 45 | + comments[i] = c.Text |
| 46 | + } |
| 47 | + |
| 48 | + lines := make([]string, 0, 10) // most comments are less than 10 lines |
| 49 | + for _, c := range comments { |
| 50 | + // Remove comment markers. |
| 51 | + // The parser has given us exactly the comment text. |
| 52 | + switch c[1] { |
| 53 | + case '/': |
| 54 | + //-style comment (no newline at the end) |
| 55 | + c = c[2:] |
| 56 | + if len(c) == 0 { |
| 57 | + // empty line |
| 58 | + break |
| 59 | + } |
| 60 | + if c[0] == ' ' { |
| 61 | + // strip first space - required for Example tests |
| 62 | + c = c[1:] |
| 63 | + break |
| 64 | + } |
| 65 | + |
| 66 | + // This is where [*ast.CommentGroup.Text] detects and skips directives. |
| 67 | + |
| 68 | + case '*': |
| 69 | + /*-style comment */ |
| 70 | + c = c[2 : len(c)-2] |
| 71 | + } |
| 72 | + |
| 73 | + // Split on newlines. |
| 74 | + cl := strings.Split(c, "\n") |
| 75 | + |
| 76 | + // Walk lines, stripping trailing white space and adding to list. |
| 77 | + for _, l := range cl { |
| 78 | + lines = append(lines, stripTrailingWhitespace(l)) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Remove leading blank lines; convert runs of |
| 83 | + // interior blank lines to a single blank line. |
| 84 | + n := 0 |
| 85 | + for _, line := range lines { |
| 86 | + if line != "" || n > 0 && lines[n-1] != "" { |
| 87 | + lines[n] = line |
| 88 | + n++ |
| 89 | + } |
| 90 | + } |
| 91 | + lines = lines[0:n] |
| 92 | + |
| 93 | + // Add final "" entry to get trailing newline from Join. |
| 94 | + if n > 0 && lines[n-1] != "" { |
| 95 | + lines = append(lines, "") |
| 96 | + } |
| 97 | + |
| 98 | + return strings.Join(lines, "\n") |
| 99 | +} |
| 100 | + |
| 101 | +func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' } |
| 102 | + |
| 103 | +func stripTrailingWhitespace(s string) string { |
| 104 | + i := len(s) |
| 105 | + for i > 0 && isWhitespace(s[i-1]) { |
| 106 | + i-- |
| 107 | + } |
| 108 | + return s[0:i] |
| 109 | +} |
0 commit comments