// Copyright 2018 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.// Module file printer.package modfileimport ()// Format returns a go.mod file as a byte slice, formatted in standard style.func ( *FileSyntax) []byte { := &printer{} .file()// remove trailing blank lines := .Bytes()forlen() > 0 && [len()-1] == '\n' && (len() == 1 || [len()-2] == '\n') { = [:len()-1] }return}// A printer collects the state during printing of a file or expression.typeprinterstruct {bytes.Buffer// output buffercomment []Comment// pending end-of-line commentsmarginint// left margin (indent), a number of tabs}// printf prints to the buffer.func ( *printer) ( string, ...any) {fmt.Fprintf(, , ...)}// indent returns the position on the current line, in bytes, 0-indexed.func ( *printer) () int { := .Bytes() := 0for < len() && [len()-1-] != '\n' { ++ }return}// newline ends the current line, flushing end-of-line comments.func ( *printer) () {iflen(.comment) > 0 { .printf(" ")for , := range .comment {if > 0 { .trim() .printf("\n")for := 0; < .margin; ++ { .printf("\t") } } .printf("%s", strings.TrimSpace(.Token)) } .comment = .comment[:0] } .trim()if := .Bytes(); len() == 0 || (len() >= 2 && [len()-1] == '\n' && [len()-2] == '\n') {// skip the blank line at top of file or after a blank line } else { .printf("\n") }for := 0; < .margin; ++ { .printf("\t") }}// trim removes trailing spaces and tabs from the current line.func ( *printer) () {// Remove trailing spaces and tabs from line we're about to end. := .Bytes() := len()for > 0 && ([-1] == '\t' || [-1] == ' ') { -- } .Truncate()}// file formats the given file into the print buffer.func ( *printer) ( *FileSyntax) {for , := range .Before { .printf("%s", strings.TrimSpace(.Token)) .newline() }for , := range .Stmt {switch x := .(type) {case *CommentBlock:// comments already handled .expr()default: .expr() .newline() }for , := range .Comment().After { .printf("%s", strings.TrimSpace(.Token)) .newline() }if +1 < len(.Stmt) { .newline() } }}func ( *printer) ( Expr) {// Emit line-comments preceding this expression.if := .Comment().Before; len() > 0 {// Want to print a line comment. // Line comments must be at the current margin. .trim()if .indent() > 0 {// There's other text on the line. Start a new line. .printf("\n") }// Re-indent to margin.for := 0; < .margin; ++ { .printf("\t") }for , := range { .printf("%s", strings.TrimSpace(.Token)) .newline() } }switch x := .(type) {default:panic(fmt.Errorf("printer: unexpected type %T", ))case *CommentBlock:// donecase *LParen: .printf("(")case *RParen: .printf(")")case *Line: .tokens(.Token)case *LineBlock: .tokens(.Token) .printf(" ") .(&.LParen) .margin++for , := range .Line { .newline() .() } .margin-- .newline() .(&.RParen) }// Queue end-of-line comments for printing when we // reach the end of the line. .comment = append(.comment, .Comment().Suffix...)}func ( *printer) ( []string) { := ""for , := range {if == "," || == ")" || == "]" || == "}" { = "" } .printf("%s%s", , ) = " "if == "(" || == "[" || == "{" { = "" } }}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)