| 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 commentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.20 bytes/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.20 fmt/" |
| 10 | "sortscip-go gomod github.com/golang/go/src go1.20 sort/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 12 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.20 `unicode/utf8`/" |
| 13 | ) |
| 14 | |
| 15 | // A textPrinter holds the state needed for printing a Doc as plain text. |
| 16 | type textPrinterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter# struct { |
| 17 | *Printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#Printer. |
| 18 | longscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#long. stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Builderscip-go gomod github.com/golang/go/src go1.20 strings/Builder# |
| 19 | prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix. string |
| 20 | codePrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#codePrefix. string |
| 21 | widthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#width. int |
| 22 | } |
| 23 | |
| 24 | // Text returns a textual formatting of the Doc. |
| 25 | // See the [Printer] documentation for ways to customize the text output. |
| 26 | func (p *Printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#) Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#Text().(d *Docscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Doc#) []byte { |
| 27 | tp := &textPrinterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#{ |
| 28 | Printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#Printer.: p, |
| 29 | prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.: p.TextPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#TextPrefix., |
| 30 | codePrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#codePrefix.: p.TextCodePrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#TextCodePrefix., |
| 31 | widthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#width.: p.TextWidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#TextWidth., |
| 32 | } |
| 33 | if tp.codePrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#codePrefix. == "" { |
| 34 | tp.codePrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#codePrefix. = p.TextPrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Printer#TextPrefix. + "\t" |
| 35 | } |
| 36 | if tp.widthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#width. == 0 { |
| 37 | tp.widthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#width. = 80 - utf8scip-go gomod github.com/golang/go/src go1.20 `unicode/utf8`/.RuneCountInStringscip-go gomod github.com/golang/go/src go1.20 `unicode/utf8`/RuneCountInString().(tp.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 38 | } |
| 39 | |
| 40 | var out bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer# |
| 41 | for i, x := range d.Contentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Doc#Content. { |
| 42 | if i > 0 && blankBeforescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/blankBefore().(x) { |
| 43 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(tp.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 44 | writeNLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/writeNL().(&out) |
| 45 | } |
| 46 | tp.blockscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#block().(&out, x) |
| 47 | } |
| 48 | anyUsed := false |
| 49 | for _, def := range d.Linksscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Doc#Links. { |
| 50 | if def.Usedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/LinkDef#Used. { |
| 51 | anyUsed = true |
| 52 | break |
| 53 | } |
| 54 | } |
| 55 | if anyUsed { |
| 56 | writeNLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/writeNL().(&out) |
| 57 | for _, def := range d.Linksscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Doc#Links. { |
| 58 | if def.Usedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/LinkDef#Used. { |
| 59 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(&out, "[%s]: %s\n", def.Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/LinkDef#Text., def.URLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/LinkDef#URL.) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return out.Bytesscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#Bytes().() |
| 64 | } |
| 65 | |
| 66 | // writeNL calls out.WriteByte('\n') |
| 67 | // but first trims trailing spaces on the previous line. |
| 68 | func writeNLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/writeNL().(out *bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#) { |
| 69 | // Trim trailing spaces. |
| 70 | data := out.Bytesscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#Bytes().() |
| 71 | n := 0 |
| 72 | for n < len(data) && (data[len(data)-n-1] == ' ' || data[len(data)-n-1] == '\t') { |
| 73 | n++ |
| 74 | } |
| 75 | if n > 0 { |
| 76 | out.Truncatescip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#Truncate().(len(data) - n) |
| 77 | } |
| 78 | out.WriteBytescip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteByte().('\n') |
| 79 | } |
| 80 | |
| 81 | // block prints the block x to out. |
| 82 | func (p *textPrinterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#) blockscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#block().(out *bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#, x Blockscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Block#) { |
| 83 | switch x := x.(type) { |
| 84 | default: |
| 85 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(out, "?%T\n", x) |
| 86 | |
| 87 | case *Paragraphscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Paragraph#: |
| 88 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(p.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 89 | p.textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#text().(out, "", x.Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Paragraph#Text.) |
| 90 | |
| 91 | case *Headingscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Heading#: |
| 92 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(p.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 93 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().("# ") |
| 94 | p.textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#text().(out, "", x.Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Heading#Text.) |
| 95 | |
| 96 | case *Codescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Code#: |
| 97 | text := x.Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Code#Text. |
| 98 | for text != "" { |
| 99 | var line string |
| 100 | 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") |
| 101 | if line != "" { |
| 102 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(p.codePrefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#codePrefix.) |
| 103 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(line) |
| 104 | } |
| 105 | writeNLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/writeNL().(out) |
| 106 | } |
| 107 | |
| 108 | case *Listscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/List#: |
| 109 | loose := x.BlankBetweenscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/List#BlankBetween().() |
| 110 | for i, item := range x.Itemsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/List#Items. { |
| 111 | if i > 0 && loose { |
| 112 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(p.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 113 | writeNLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/writeNL().(out) |
| 114 | } |
| 115 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(p.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 116 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(" ") |
| 117 | if item.Numberscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/ListItem#Number. == "" { |
| 118 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(" - ") |
| 119 | } else { |
| 120 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(item.Numberscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/ListItem#Number.) |
| 121 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(". ") |
| 122 | } |
| 123 | for i, blk := range item.Contentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/ListItem#Content. { |
| 124 | const fourSpace = " " |
| 125 | if i > 0 { |
| 126 | writeNLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/writeNL().(out) |
| 127 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(p.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 128 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(fourSpace) |
| 129 | } |
| 130 | p.textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#text().(out, fourSpace, blk.(*Paragraphscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Paragraph#).Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Paragraph#Text.) |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // text prints the text sequence x to out. |
| 137 | func (p *textPrinterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#) textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#text().(out *bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#, indent string, x []Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Text#) { |
| 138 | p.oneLongLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#oneLongLine().(&p.longscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#long., x) |
| 139 | words := stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Fieldsscip-go gomod github.com/golang/go/src go1.20 strings/Fields().(p.longscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#long..Stringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#String().()) |
| 140 | p.longscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#long..Resetscip-go gomod github.com/golang/go/src go1.20 strings/Builder#Reset().() |
| 141 | |
| 142 | var seq []int |
| 143 | if p.widthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#width. < 0 || len(words) == 0 { |
| 144 | seq = []int{0, len(words)} // one long line |
| 145 | } else { |
| 146 | seq = wrapscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/wrap().(words, p.widthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#width.-utf8scip-go gomod github.com/golang/go/src go1.20 `unicode/utf8`/.RuneCountInStringscip-go gomod github.com/golang/go/src go1.20 `unicode/utf8`/RuneCountInString().(indent)) |
| 147 | } |
| 148 | for i := 0; i+1 < len(seq); i++ { |
| 149 | if i > 0 { |
| 150 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(p.prefixscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#prefix.) |
| 151 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(indent) |
| 152 | } |
| 153 | for j, w := range words[seq[i]:seq[i+1]] { |
| 154 | if j > 0 { |
| 155 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(" ") |
| 156 | } |
| 157 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 bytes/Buffer#WriteString().(w) |
| 158 | } |
| 159 | writeNLscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/writeNL().(out) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // oneLongLine prints the text sequence x to out as one long line, |
| 164 | // without worrying about line wrapping. |
| 165 | // Explicit links have the [ ] dropped to improve readability. |
| 166 | func (p *textPrinterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#) oneLongLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#oneLongLine().(out *stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Builderscip-go gomod github.com/golang/go/src go1.20 strings/Builder#, x []Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Text#) { |
| 167 | for _, t := range x { |
| 168 | switch t := t.(type) { |
| 169 | case Plainscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Plain#: |
| 170 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteString().(string(t)) |
| 171 | case Italicscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Italic#: |
| 172 | out.WriteStringscip-go gomod github.com/golang/go/src go1.20 strings/Builder#WriteString().(string(t)) |
| 173 | case *Linkscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Link#: |
| 174 | p.oneLongLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#oneLongLine().(out, t.Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/Link#Text.) |
| 175 | case *DocLinkscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/DocLink#: |
| 176 | p.oneLongLinescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/textPrinter#oneLongLine().(out, t.Textscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/DocLink#Text.) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // wrap wraps words into lines of at most max runes, |
| 182 | // minimizing the sum of the squares of the leftover lengths |
| 183 | // at the end of each line (except the last, of course), |
| 184 | // with a preference for ending lines at punctuation (.,:;). |
| 185 | // |
| 186 | // The returned slice gives the indexes of the first words |
| 187 | // on each line in the wrapped text with a final entry of len(words). |
| 188 | // Thus the lines are words[seq[0]:seq[1]], words[seq[1]:seq[2]], |
| 189 | // ..., words[seq[len(seq)-2]:seq[len(seq)-1]]. |
| 190 | // |
| 191 | // The implementation runs in O(n log n) time, where n = len(words), |
| 192 | // using the algorithm described in D. S. Hirschberg and L. L. Larmore, |
| 193 | // “[The least weight subsequence problem],” FOCS 1985, pp. 137-143. |
| 194 | // |
| 195 | // [The least weight subsequence problem]: https://doi.org/10.1109/SFCS.1985.60 |
| 196 | func wrapscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/wrap().(words []string, max int) (seq []int) { |
| 197 | // The algorithm requires that our scoring function be concave, |
| 198 | // meaning that for all i₀ ≤ i₁ < j₀ ≤ j₁, |
| 199 | // weight(i₀, j₀) + weight(i₁, j₁) ≤ weight(i₀, j₁) + weight(i₁, j₀). |
| 200 | // |
| 201 | // Our weights are two-element pairs [hi, lo] |
| 202 | // ordered by elementwise comparison. |
| 203 | // The hi entry counts the weight for lines that are longer than max, |
| 204 | // and the lo entry counts the weight for lines that are not. |
| 205 | // This forces the algorithm to first minimize the number of lines |
| 206 | // that are longer than max, which correspond to lines with |
| 207 | // single very long words. Having done that, it can move on to |
| 208 | // minimizing the lo score, which is more interesting. |
| 209 | // |
| 210 | // The lo score is the sum for each line of the square of the |
| 211 | // number of spaces remaining at the end of the line and a |
| 212 | // penalty of 64 given out for not ending the line in a |
| 213 | // punctuation character (.,:;). |
| 214 | // The penalty is somewhat arbitrarily chosen by trying |
| 215 | // different amounts and judging how nice the wrapped text looks. |
| 216 | // Roughly speaking, using 64 means that we are willing to |
| 217 | // end a line with eight blank spaces in order to end at a |
| 218 | // punctuation character, even if the next word would fit in |
| 219 | // those spaces. |
| 220 | // |
| 221 | // We care about ending in punctuation characters because |
| 222 | // it makes the text easier to skim if not too many sentences |
| 223 | // or phrases begin with a single word on the previous line. |
| 224 | |
| 225 | // A score is the score (also called weight) for a given line. |
| 226 | // add and cmp add and compare scores. |
| 227 | type score struct { |
| 228 | hi int64 |
| 229 | lo int64 |
| 230 | } |
| 231 | add := func(s, t score) score { return score{s.hi + t.hi, s.lo + t.lo} } |
| 232 | cmp := func(s, t score) int { |
| 233 | switch { |
| 234 | case s.hi < t.hi: |
| 235 | return -1 |
| 236 | case s.hi > t.hi: |
| 237 | return +1 |
| 238 | case s.lo < t.lo: |
| 239 | return -1 |
| 240 | case s.lo > t.lo: |
| 241 | return +1 |
| 242 | } |
| 243 | return 0 |
| 244 | } |
| 245 | |
| 246 | // total[j] is the total number of runes |
| 247 | // (including separating spaces) in words[:j]. |
| 248 | total := make([]int, len(words)+1) |
| 249 | total[0] = 0 |
| 250 | for i, s := range words { |
| 251 | total[1+i] = total[i] + utf8scip-go gomod github.com/golang/go/src go1.20 `unicode/utf8`/.RuneCountInStringscip-go gomod github.com/golang/go/src go1.20 `unicode/utf8`/RuneCountInString().(s) + 1 |
| 252 | } |
| 253 | |
| 254 | // weight returns weight(i, j). |
| 255 | weight := func(i, j int) score { |
| 256 | // On the last line, there is zero weight for being too short. |
| 257 | n := total[j] - 1 - total[i] |
| 258 | if j == len(words) && n <= max { |
| 259 | return score{0, 0} |
| 260 | } |
| 261 | |
| 262 | // Otherwise the weight is the penalty plus the square of the number of |
| 263 | // characters remaining on the line or by which the line goes over. |
| 264 | // In the latter case, that value goes in the hi part of the score. |
| 265 | // (See note above.) |
| 266 | p := wrapPenaltyscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/wrapPenalty().(words[j-1]) |
| 267 | v := int64(max-n) * int64(max-n) |
| 268 | if n > max { |
| 269 | return score{v, p} |
| 270 | } |
| 271 | return score{0, v + p} |
| 272 | } |
| 273 | |
| 274 | // The rest of this function is “The Basic Algorithm” from |
| 275 | // Hirschberg and Larmore's conference paper, |
| 276 | // using the same names as in the paper. |
| 277 | f := []score{{0, 0}} |
| 278 | g := func(i, j int) score { return add(f[i], weight(i, j)) } |
| 279 | |
| 280 | bridge := func(a, b, c int) bool { |
| 281 | k := c + sortscip-go gomod github.com/golang/go/src go1.20 sort/.Searchscip-go gomod github.com/golang/go/src go1.20 sort/Search().(len(words)+1-c, func(k int) bool { |
| 282 | k += c |
| 283 | return cmp(g(a, k), g(b, k)) > 0 |
| 284 | }) |
| 285 | if k > len(words) { |
| 286 | return true |
| 287 | } |
| 288 | return cmp(g(c, k), g(b, k)) <= 0 |
| 289 | } |
| 290 | |
| 291 | // d is a one-ended deque implemented as a slice. |
| 292 | d := make([]int, 1, len(words)) |
| 293 | d[0] = 0 |
| 294 | bestleft := make([]int, 1, len(words)) |
| 295 | bestleft[0] = -1 |
| 296 | for m := 1; m < len(words); m++ { |
| 297 | f = append(f, g(d[0], m)) |
| 298 | bestleft = append(bestleft, d[0]) |
| 299 | for len(d) > 1 && cmp(g(d[1], m+1), g(d[0], m+1)) <= 0 { |
| 300 | d = d[1:] // “Retire” |
| 301 | } |
| 302 | for len(d) > 1 && bridge(d[len(d)-2], d[len(d)-1], m) { |
| 303 | d = d[:len(d)-1] // “Fire” |
| 304 | } |
| 305 | if cmp(g(m, len(words)), g(d[len(d)-1], len(words))) < 0 { |
| 306 | d = append(d, m) // “Hire” |
| 307 | // The next few lines are not in the paper but are necessary |
| 308 | // to handle two-word inputs correctly. It appears to be |
| 309 | // just a bug in the paper's pseudocode. |
| 310 | if len(d) == 2 && cmp(g(d[1], m+1), g(d[0], m+1)) <= 0 { |
| 311 | d = d[1:] |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | bestleft = append(bestleft, d[0]) |
| 316 | |
| 317 | // Recover least weight sequence from bestleft. |
| 318 | n := 1 |
| 319 | for m := len(words); m > 0; m = bestleft[m] { |
| 320 | n++ |
| 321 | } |
| 322 | seq = make([]int, n) |
| 323 | for m := len(words); m > 0; m = bestleft[m] { |
| 324 | n-- |
| 325 | seq[n] = m |
| 326 | } |
| 327 | return seq |
| 328 | } |
| 329 | |
| 330 | // wrapPenalty is the penalty for inserting a line break after word s. |
| 331 | func wrapPenaltyscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/doc/comment`/wrapPenalty().(s string) int64 { |
| 332 | switch s[len(s)-1] { |
| 333 | case '.', ',', ':', ';': |
| 334 | return 0 |
| 335 | } |
| 336 | return 64 |
| 337 | } |