| 1 | // Copyright 2009 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 mainscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.20 bytes/" |
| 9 | "contextscip-go gomod github.com/golang/go/src go1.20 context/" |
| 10 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/" |
| 11 | "flagscip-go gomod github.com/golang/go/src go1.20 flag/" |
| 12 | "fmtscip-go gomod github.com/golang/go/src go1.20 fmt/" |
| 13 | "go/astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/" |
| 14 | "go/parserscip-go gomod github.com/golang/go/src go1.20 `go/parser`/" |
| 15 | "go/scannerscip-go gomod github.com/golang/go/src go1.20 `go/scanner`/" |
| 16 | "go/tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/" |
| 17 | "ioscip-go gomod github.com/golang/go/src go1.20 io/" |
| 18 | "io/fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/" |
| 19 | "osscip-go gomod github.com/golang/go/src go1.20 os/" |
| 20 | "os/execscip-go gomod github.com/golang/go/src go1.20 `os/exec`/" |
| 21 | "path/filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/" |
| 22 | "regexpscip-go gomod github.com/golang/go/src go1.20 regexp/" |
| 23 | "runtimescip-go gomod github.com/golang/go/src go1.20 runtime/" |
| 24 | "runtime/pprofscip-go gomod github.com/golang/go/src go1.20 `runtime/pprof`/" |
| 25 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 26 | "syncscip-go gomod github.com/golang/go/src go1.20 sync/" |
| 27 | |
| 28 | "golang.org/x/sync/semaphorescip-go gomod golang.org/x/sync v0.6.0 `golang.org/x/sync/semaphore`/" |
| 29 | |
| 30 | gformatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/ "mvdan.cc/gofumpt/formatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/" |
| 31 | "mvdan.cc/gofumpt/internal/govendor/diffscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/diff`/" |
| 32 | "mvdan.cc/gofumpt/internal/govendor/go/printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/" |
| 33 | gversionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/version`/ "mvdan.cc/gofumpt/internal/versionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/version`/" |
| 34 | ) |
| 35 | |
| 36 | //go:generate go run gen_govendor.go |
| 37 | //go:generate go run . -w internal/govendor |
| 38 | |
| 39 | var ( |
| 40 | // main operation modes |
| 41 | listscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/list. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Boolscip-go gomod github.com/golang/go/src go1.20 flag/Bool().("l", false, "") |
| 42 | writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/write. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Boolscip-go gomod github.com/golang/go/src go1.20 flag/Bool().("w", false, "") |
| 43 | doDiffscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/doDiff. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Boolscip-go gomod github.com/golang/go/src go1.20 flag/Bool().("d", false, "") |
| 44 | allErrorsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/allErrors. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Boolscip-go gomod github.com/golang/go/src go1.20 flag/Bool().("e", false, "") |
| 45 | |
| 46 | // debugging |
| 47 | cpuprofilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/cpuprofile. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Stringscip-go gomod github.com/golang/go/src go1.20 flag/String().("cpuprofile", "", "") |
| 48 | |
| 49 | // gofumpt's own flags |
| 50 | langVersionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/langVersion. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Stringscip-go gomod github.com/golang/go/src go1.20 flag/String().("lang", "", "") |
| 51 | modulePathscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/modulePath. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Stringscip-go gomod github.com/golang/go/src go1.20 flag/String().("modpath", "", "") |
| 52 | extraRulesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/extraRules. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Boolscip-go gomod github.com/golang/go/src go1.20 flag/Bool().("extra", false, "") |
| 53 | showVersionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/showVersion. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Boolscip-go gomod github.com/golang/go/src go1.20 flag/Bool().("version", false, "") |
| 54 | |
| 55 | // DEPRECATED |
| 56 | rewriteRulescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/rewriteRule. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Stringscip-go gomod github.com/golang/go/src go1.20 flag/String().("r", "", "") |
| 57 | simplifyASTscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/simplifyAST. = flagscip-go gomod github.com/golang/go/src go1.20 flag/.Boolscip-go gomod github.com/golang/go/src go1.20 flag/Bool().("s", false, "") |
| 58 | ) |
| 59 | |
| 60 | var versionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/version. = "" |
| 61 | |
| 62 | // Keep these in sync with go/format/format.go. |
| 63 | const ( |
| 64 | tabWidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/tabWidth. = 8 |
| 65 | printerModescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/printerMode. = printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/.UseSpacesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/UseSpaces. | printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/.TabIndentscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/TabIndent. | printerNormalizeNumbersscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/printerNormalizeNumbers. |
| 66 | |
| 67 | // printerNormalizeNumbers means to canonicalize number literal prefixes |
| 68 | // and exponents while printing. See https://golang.org/doc/go1.13#gofmt. |
| 69 | // |
| 70 | // This value is defined in go/printer specifically for go/format and cmd/gofmt. |
| 71 | printerNormalizeNumbersscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/printerNormalizeNumbers. = 1 << 30 |
| 72 | ) |
| 73 | |
| 74 | // fdSem guards the number of concurrently-open file descriptors. |
| 75 | // |
| 76 | // For now, this is arbitrarily set to 200, based on the observation that many |
| 77 | // platforms default to a kernel limit of 256. Ideally, perhaps we should derive |
| 78 | // it from rlimit on platforms that support that system call. |
| 79 | // |
| 80 | // File descriptors opened from outside of this package are not tracked, |
| 81 | // so this limit may be approximate. |
| 82 | var fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. = make(chan bool, 200) |
| 83 | |
| 84 | var ( |
| 85 | fileSetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fileSet. = tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.20 `go/token`/NewFileSet().() // per process FileSet |
| 86 | parserModescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/parserMode. parserscip-go gomod github.com/golang/go/src go1.20 `go/parser`/.Modescip-go gomod github.com/golang/go/src go1.20 `go/parser`/Mode# |
| 87 | ) |
| 88 | |
| 89 | func usagescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/usage().() { |
| 90 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.20 os/.Stderrscip-go gomod github.com/golang/go/src go1.20 os/Stderr., `usage: gofumpt [flags] [path ...] |
| 91 | -version show version and exit |
| 92 | |
| 93 | -d display diffs instead of rewriting files |
| 94 | -e report all errors (not just the first 10 on different lines) |
| 95 | -l list files whose formatting differs from gofumpt's |
| 96 | -w write result to (source) file instead of stdout |
| 97 | -extra enable extra rules which should be vetted by a human |
| 98 | |
| 99 | -lang str target Go version in the form "1.X" (default from go.mod) |
| 100 | -modpath str Go module path containing the source file (default from go.mod) |
| 101 | `) |
| 102 | } |
| 103 | |
| 104 | func initParserModescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/initParserMode().() { |
| 105 | parserModescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/parserMode. = parserscip-go gomod github.com/golang/go/src go1.20 `go/parser`/.ParseCommentsscip-go gomod github.com/golang/go/src go1.20 `go/parser`/ParseComments. | parserscip-go gomod github.com/golang/go/src go1.20 `go/parser`/.SkipObjectResolutionscip-go gomod github.com/golang/go/src go1.20 `go/parser`/SkipObjectResolution. |
| 106 | if *allErrorsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/allErrors. { |
| 107 | parserModescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/parserMode. |= parserscip-go gomod github.com/golang/go/src go1.20 `go/parser`/.AllErrorsscip-go gomod github.com/golang/go/src go1.20 `go/parser`/AllErrors. |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func isGoFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/isGoFile().(f fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.20 `io/fs`/DirEntry#) bool { |
| 112 | // ignore non-Go files |
| 113 | name := f.Namescip-go gomod github.com/golang/go/src go1.20 `io/fs`/DirEntry#Name().() |
| 114 | return !stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.20 strings/HasPrefix().(name, ".") && stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.20 strings/HasSuffix().(name, ".go") && !f.IsDirscip-go gomod github.com/golang/go/src go1.20 `io/fs`/DirEntry#IsDir().() |
| 115 | } |
| 116 | |
| 117 | var rxCodeGeneratedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/rxCodeGenerated. = regexpscip-go gomod github.com/golang/go/src go1.20 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.20 regexp/MustCompile().(`^// Code generated .* DO NOT EDIT\.$`) |
| 118 | |
| 119 | func isGeneratedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/isGenerated().(file *astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#) bool { |
| 120 | for _, cg := range file.Commentsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#Comments. { |
| 121 | if cg.Posscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#Pos().() > file.Packagescip-go gomod github.com/golang/go/src go1.20 `go/ast`/File#Package. { |
| 122 | return false |
| 123 | } |
| 124 | for _, line := range cg.Listscip-go gomod github.com/golang/go/src go1.20 `go/ast`/CommentGroup#List. { |
| 125 | if rxCodeGeneratedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/rxCodeGenerated..MatchStringscip-go gomod github.com/golang/go/src go1.20 regexp/Regexp#MatchString().(line.Textscip-go gomod github.com/golang/go/src go1.20 `go/ast`/Comment#Text.) { |
| 126 | return true |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | return false |
| 131 | } |
| 132 | |
| 133 | // A sequencer performs concurrent tasks that may write output, but emits that |
| 134 | // output in a deterministic order. |
| 135 | type sequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer# struct { |
| 136 | maxWeightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#maxWeight. int64 |
| 137 | semscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#sem. *semaphorescip-go gomod golang.org/x/sync v0.6.0 `golang.org/x/sync/semaphore`/.Weightedscip-go gomod golang.org/x/sync v0.6.0 `golang.org/x/sync/semaphore`/Weighted# // weighted by input bytes (an approximate proxy for memory overhead) |
| 138 | prevscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#prev. <-chan *reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState# // 1-buffered |
| 139 | } |
| 140 | |
| 141 | // newSequencer returns a sequencer that allows concurrent tasks up to maxWeight |
| 142 | // and writes tasks' output to out and err. |
| 143 | func newSequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/newSequencer().(maxWeight int64, out, err ioscip-go gomod github.com/golang/go/src go1.20 io/.Writerscip-go gomod github.com/golang/go/src go1.20 io/Writer#) *sequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer# { |
| 144 | sem := semaphorescip-go gomod golang.org/x/sync v0.6.0 `golang.org/x/sync/semaphore`/.NewWeightedscip-go gomod golang.org/x/sync v0.6.0 `golang.org/x/sync/semaphore`/NewWeighted().(maxWeight) |
| 145 | prev := make(chan *reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#, 1) |
| 146 | prev <- &reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#{outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#out.: out, errscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#err.: err} |
| 147 | return &sequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#{ |
| 148 | maxWeightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#maxWeight.: maxWeight, |
| 149 | semscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#sem.: sem, |
| 150 | prevscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#prev.: prev, |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // exclusive is a weight that can be passed to a sequencer to cause |
| 155 | // a task to be executed without any other concurrent tasks. |
| 156 | const exclusivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/exclusive. = -1 |
| 157 | |
| 158 | // Add blocks until the sequencer has enough weight to spare, then adds f as a |
| 159 | // task to be executed concurrently. |
| 160 | // |
| 161 | // If the weight is either negative or larger than the sequencer's maximum |
| 162 | // weight, Add blocks until all other tasks have completed, then the task |
| 163 | // executes exclusively (blocking all other calls to Add until it completes). |
| 164 | // |
| 165 | // f may run concurrently in a goroutine, but its output to the passed-in |
| 166 | // reporter will be sequential relative to the other tasks in the sequencer. |
| 167 | // |
| 168 | // If f invokes a method on the reporter, execution of that method may block |
| 169 | // until the previous task has finished. (To maximize concurrency, f should |
| 170 | // avoid invoking the reporter until it has finished any parallelizable work.) |
| 171 | // |
| 172 | // If f returns a non-nil error, that error will be reported after f's output |
| 173 | // (if any) and will cause a nonzero final exit code. |
| 174 | func (s *sequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#) Addscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#Add().(weight int64, f func(*reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) error) { |
| 175 | if weight < 0 || weight > s.maxWeightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#maxWeight. { |
| 176 | weight = s.maxWeightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#maxWeight. |
| 177 | } |
| 178 | if err := s.semscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#sem..Acquirescip-go gomod golang.org/x/sync v0.6.0 `golang.org/x/sync/semaphore`/Weighted#Acquire().(contextscip-go gomod github.com/golang/go/src go1.20 context/.TODOscip-go gomod github.com/golang/go/src go1.20 context/TODO().(), weight); err != nil { |
| 179 | // Change the task from "execute f" to "report err". |
| 180 | weight = 0 |
| 181 | f = func(*reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) error { return err } |
| 182 | } |
| 183 | |
| 184 | r := &reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#{prevscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#prev.: s.prevscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#prev.} |
| 185 | next := make(chan *reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#, 1) |
| 186 | s.prevscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#prev. = next |
| 187 | |
| 188 | // Start f in parallel: it can run until it invokes a method on r, at which |
| 189 | // point it will block until the previous task releases the output state. |
| 190 | go func() { |
| 191 | if err := f(r); err != nil { |
| 192 | r.Reportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#Report().(err) |
| 193 | } |
| 194 | next <- r.getStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#getState().() // Release the next task. |
| 195 | s.semscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#sem..Releasescip-go gomod golang.org/x/sync v0.6.0 `golang.org/x/sync/semaphore`/Weighted#Release().(weight) |
| 196 | }() |
| 197 | } |
| 198 | |
| 199 | // AddReport prints an error to s after the output of any previously-added |
| 200 | // tasks, causing the final exit code to be nonzero. |
| 201 | func (s *sequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#) AddReportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#AddReport().(err error) { |
| 202 | s.Addscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#Add().(0, func(*reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) error { return err }) |
| 203 | } |
| 204 | |
| 205 | // GetExitCode waits for all previously-added tasks to complete, then returns an |
| 206 | // exit code for the sequence suitable for passing to os.Exit. |
| 207 | func (s *sequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#) GetExitCodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#GetExitCode().() int { |
| 208 | c := make(chan int, 1) |
| 209 | s.Addscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#Add().(0, func(r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) error { |
| 210 | c <- r.ExitCodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#ExitCode().() |
| 211 | return nil |
| 212 | }) |
| 213 | return <-c |
| 214 | } |
| 215 | |
| 216 | // A reporter reports output, warnings, and errors. |
| 217 | type reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter# struct { |
| 218 | prevscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#prev. <-chan *reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState# |
| 219 | statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#state. *reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState# |
| 220 | } |
| 221 | |
| 222 | // reporterState carries the state of a reporter instance. |
| 223 | // |
| 224 | // Only one reporter at a time may have access to a reporterState. |
| 225 | type reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState# struct { |
| 226 | outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#out., errscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#err. ioscip-go gomod github.com/golang/go/src go1.20 io/.Writerscip-go gomod github.com/golang/go/src go1.20 io/Writer# |
| 227 | exitCodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#exitCode. int |
| 228 | } |
| 229 | |
| 230 | // getState blocks until any prior reporters are finished with the reporter |
| 231 | // state, then returns the state for manipulation. |
| 232 | func (r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) getStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#getState().() *reporterStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState# { |
| 233 | if r.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#state. == nil { |
| 234 | r.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#state. = <-r.prevscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#prev. |
| 235 | } |
| 236 | return r.statescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#state. |
| 237 | } |
| 238 | |
| 239 | // Warnf emits a warning message to the reporter's error stream, |
| 240 | // without changing its exit code. |
| 241 | func (r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) Warnfscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#Warnf().(format string, args ...any) { |
| 242 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(r.getStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#getState().().errscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#err., format, args...) |
| 243 | } |
| 244 | |
| 245 | // Write emits a slice to the reporter's output stream. |
| 246 | // |
| 247 | // Any error is returned to the caller, and does not otherwise affect the |
| 248 | // reporter's exit code. |
| 249 | func (r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) Writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#Write().(p []byte) (int, error) { |
| 250 | return r.getStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#getState().().outscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#out..Writescip-go gomod github.com/golang/go/src go1.20 io/Writer#Write().(p) |
| 251 | } |
| 252 | |
| 253 | // Report emits a non-nil error to the reporter's error stream, |
| 254 | // changing its exit code to a nonzero value. |
| 255 | func (r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) Reportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#Report().(err error) { |
| 256 | if err == nil { |
| 257 | panic("Report with nil error") |
| 258 | } |
| 259 | st := r.getStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#getState().() |
| 260 | scannerscip-go gomod github.com/golang/go/src go1.20 `go/scanner`/.PrintErrorscip-go gomod github.com/golang/go/src go1.20 `go/scanner`/PrintError().(st.errscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#err., err) |
| 261 | st.exitCodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#exitCode. = 2 |
| 262 | } |
| 263 | |
| 264 | func (r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) ExitCodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#ExitCode().() int { |
| 265 | return r.getStatescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#getState().().exitCodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporterState#exitCode. |
| 266 | } |
| 267 | |
| 268 | // If info == nil, we are formatting stdin instead of a file. |
| 269 | // If in == nil, the source is the contents of the file with the given filename. |
| 270 | func processFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/processFile().(filename string, info fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#, in ioscip-go gomod github.com/golang/go/src go1.20 io/.Readerscip-go gomod github.com/golang/go/src go1.20 io/Reader#, r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#, explicit bool) error { |
| 271 | src, err := readFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/readFile().(filename, info, in) |
| 272 | if err != nil { |
| 273 | return err |
| 274 | } |
| 275 | |
| 276 | fileSet := tokenscip-go gomod github.com/golang/go/src go1.20 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.20 `go/token`/NewFileSet().() |
| 277 | fragmentOk := false |
| 278 | if info == nil { |
| 279 | // If we are formatting stdin, we accept a program fragment in lieu of a |
| 280 | // complete source file. |
| 281 | fragmentOk = true |
| 282 | } |
| 283 | file, sourceAdj, indentAdj, err := parsescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/parse().(fileSet, filename, src, fragmentOk) |
| 284 | if err != nil { |
| 285 | return err |
| 286 | } |
| 287 | |
| 288 | astscip-go gomod github.com/golang/go/src go1.20 `go/ast`/.SortImportsscip-go gomod github.com/golang/go/src go1.20 `go/ast`/SortImports().(fileSet, file) |
| 289 | |
| 290 | // Apply gofumpt's changes before we print the code in gofumpt's format. |
| 291 | |
| 292 | // If either -lang or -modpath aren't set, fetch them from go.mod. |
| 293 | lang := *langVersionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/langVersion. |
| 294 | modpath := *modulePathscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/modulePath. |
| 295 | if lang == "" || modpath == "" { |
| 296 | dir := filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Dir().(filename) |
| 297 | mod, ok := moduleCacheByDirscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/moduleCacheByDir..Loadscip-go gomod github.com/golang/go/src go1.20 sync/Map#Load().(dir) |
| 298 | if ok && mod != nil { |
| 299 | mod := mod.(*modulescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#) |
| 300 | if lang == "" { |
| 301 | lang = mod.Goscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#Go. |
| 302 | } |
| 303 | if modpath == "" { |
| 304 | modpath = mod.Modulescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#Module..Pathscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#$anon_7924d3d9624588aa#Path. |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // We always apply the gofumpt formatting rules to explicit files, including stdin. |
| 310 | // Otherwise, we don't apply them on generated files. |
| 311 | // We also skip walking vendor directories entirely, but that happens elsewhere. |
| 312 | if explicit || !isGeneratedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/isGenerated().(file) { |
| 313 | gformatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/.Filescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/File().(fileSet, file, gformatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/.Optionsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Options#{ |
| 314 | LangVersionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Options#LangVersion.: lang, |
| 315 | ModulePathscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Options#ModulePath.: modpath, |
| 316 | ExtraRulesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Options#ExtraRules.: *extraRulesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/extraRules., |
| 317 | }) |
| 318 | } |
| 319 | |
| 320 | res, err := formatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/format().(fileSet, file, sourceAdj, indentAdj, src, printerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/.Configscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#{Modescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Mode.: printerModescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/printerMode., Tabwidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/go/printer`/Config#Tabwidth.: tabWidthscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/tabWidth.}) |
| 321 | if err != nil { |
| 322 | return err |
| 323 | } |
| 324 | |
| 325 | if !bytesscip-go gomod github.com/golang/go/src go1.20 bytes/.Equalscip-go gomod github.com/golang/go/src go1.20 bytes/Equal().(src, res) { |
| 326 | // formatting has changed |
| 327 | if *listscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/list. { |
| 328 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintln().(r, filename) |
| 329 | } |
| 330 | if *writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/write. { |
| 331 | if info == nil { |
| 332 | panic("-w should not have been allowed with stdin") |
| 333 | } |
| 334 | // make a temporary backup before overwriting original |
| 335 | perm := info.Modescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Mode().().Permscip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileMode#Perm().() |
| 336 | bakname, err := backupFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/backupFile().(filename+".", src, perm) |
| 337 | if err != nil { |
| 338 | return err |
| 339 | } |
| 340 | fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. <- true |
| 341 | err = osscip-go gomod github.com/golang/go/src go1.20 os/.WriteFilescip-go gomod github.com/golang/go/src go1.20 os/WriteFile().(filename, res, perm) |
| 342 | <-fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. |
| 343 | if err != nil { |
| 344 | osscip-go gomod github.com/golang/go/src go1.20 os/.Renamescip-go gomod github.com/golang/go/src go1.20 os/Rename().(bakname, filename) |
| 345 | return err |
| 346 | } |
| 347 | err = osscip-go gomod github.com/golang/go/src go1.20 os/.Removescip-go gomod github.com/golang/go/src go1.20 os/Remove().(bakname) |
| 348 | if err != nil { |
| 349 | return err |
| 350 | } |
| 351 | } |
| 352 | if *doDiffscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/doDiff. { |
| 353 | newName := filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/ToSlash().(filename) |
| 354 | oldName := newName + ".orig" |
| 355 | r.Writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#Write().(diffscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/diff`/.Diffscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/govendor/diff`/Diff().(oldName, src, newName, res)) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if !*listscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/list. && !*writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/write. && !*doDiffscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/doDiff. { |
| 360 | _, err = r.Writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#Write().(res) |
| 361 | } |
| 362 | |
| 363 | return err |
| 364 | } |
| 365 | |
| 366 | // readFile reads the contents of filename, described by info. |
| 367 | // If in is non-nil, readFile reads directly from it. |
| 368 | // Otherwise, readFile opens and reads the file itself, |
| 369 | // with the number of concurrently-open files limited by fdSem. |
| 370 | func readFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/readFile().(filename string, info fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#, in ioscip-go gomod github.com/golang/go/src go1.20 io/.Readerscip-go gomod github.com/golang/go/src go1.20 io/Reader#) ([]byte, error) { |
| 371 | if in == nil { |
| 372 | fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. <- true |
| 373 | var err error |
| 374 | f, err := osscip-go gomod github.com/golang/go/src go1.20 os/.Openscip-go gomod github.com/golang/go/src go1.20 os/Open().(filename) |
| 375 | if err != nil { |
| 376 | return nil, err |
| 377 | } |
| 378 | in = f |
| 379 | defer func() { |
| 380 | f.Closescip-go gomod github.com/golang/go/src go1.20 os/File#Close().() |
| 381 | <-fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. |
| 382 | }() |
| 383 | } |
| 384 | |
| 385 | // Compute the file's size and read its contents with minimal allocations. |
| 386 | // |
| 387 | // If we have the FileInfo from filepath.WalkDir, use it to make |
| 388 | // a buffer of the right size and avoid ReadAll's reallocations. |
| 389 | // |
| 390 | // If the size is unknown (or bogus, or overflows an int), fall back to |
| 391 | // a size-independent ReadAll. |
| 392 | size := -1 |
| 393 | if info != nil && info.Modescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Mode().().IsRegularscip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileMode#IsRegular().() && int64(int(info.Sizescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Size().())) == info.Sizescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Size().() { |
| 394 | size = int(info.Sizescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Size().()) |
| 395 | } |
| 396 | if size+1 <= 0 { |
| 397 | // The file is not known to be regular, so we don't have a reliable size for it. |
| 398 | var err error |
| 399 | src, err := ioscip-go gomod github.com/golang/go/src go1.20 io/.ReadAllscip-go gomod github.com/golang/go/src go1.20 io/ReadAll().(in) |
| 400 | if err != nil { |
| 401 | return nil, err |
| 402 | } |
| 403 | return src, nil |
| 404 | } |
| 405 | |
| 406 | // We try to read size+1 bytes so that we can detect modifications: if we |
| 407 | // read more than size bytes, then the file was modified concurrently. |
| 408 | // (If that happens, we could, say, append to src to finish the read, or |
| 409 | // proceed with a truncated buffer — but the fact that it changed at all |
| 410 | // indicates a possible race with someone editing the file, so we prefer to |
| 411 | // stop to avoid corrupting it.) |
| 412 | src := make([]byte, size+1) |
| 413 | n, err := ioscip-go gomod github.com/golang/go/src go1.20 io/.ReadFullscip-go gomod github.com/golang/go/src go1.20 io/ReadFull().(in, src) |
| 414 | switch err { |
| 415 | case nil, ioscip-go gomod github.com/golang/go/src go1.20 io/.EOFscip-go gomod github.com/golang/go/src go1.20 io/EOF., ioscip-go gomod github.com/golang/go/src go1.20 io/.ErrUnexpectedEOFscip-go gomod github.com/golang/go/src go1.20 io/ErrUnexpectedEOF.: |
| 416 | // io.ReadFull returns io.EOF (for an empty file) or io.ErrUnexpectedEOF |
| 417 | // (for a non-empty file) if the file was changed unexpectedly. Continue |
| 418 | // with comparing file sizes in those cases. |
| 419 | default: |
| 420 | return nil, err |
| 421 | } |
| 422 | if n < size { |
| 423 | return nil, fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("error: size of %s changed during reading (from %d to %d bytes)", filename, size, n) |
| 424 | } else if n > size { |
| 425 | return nil, fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("error: size of %s changed during reading (from %d to >=%d bytes)", filename, size, len(src)) |
| 426 | } |
| 427 | return src[:n], nil |
| 428 | } |
| 429 | |
| 430 | func mainscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/main().() { osscip-go gomod github.com/golang/go/src go1.20 os/.Exitscip-go gomod github.com/golang/go/src go1.20 os/Exit().(main1scip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/main1().()) } |
| 431 | |
| 432 | func main1scip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/main1().() int { |
| 433 | // Arbitrarily limit in-flight work to 2MiB times the number of threads. |
| 434 | // |
| 435 | // The actual overhead for the parse tree and output will depend on the |
| 436 | // specifics of the file, but this at least keeps the footprint of the process |
| 437 | // roughly proportional to GOMAXPROCS. |
| 438 | maxWeight := (2 << 20) * int64(runtimescip-go gomod github.com/golang/go/src go1.20 runtime/.GOMAXPROCSscip-go gomod github.com/golang/go/src go1.20 runtime/GOMAXPROCS().(0)) |
| 439 | s := newSequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/newSequencer().(maxWeight, osscip-go gomod github.com/golang/go/src go1.20 os/.Stdoutscip-go gomod github.com/golang/go/src go1.20 os/Stdout., osscip-go gomod github.com/golang/go/src go1.20 os/.Stderrscip-go gomod github.com/golang/go/src go1.20 os/Stderr.) |
| 440 | |
| 441 | // call gofmtMain in a separate function |
| 442 | // so that it can use defer and have them |
| 443 | // run before the exit. |
| 444 | gofmtMainscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/gofmtMain().(s) |
| 445 | return s.GetExitCodescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#GetExitCode().() |
| 446 | } |
| 447 | |
| 448 | func gofmtMainscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/gofmtMain().(s *sequencerscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#) { |
| 449 | // Ensure our parsed files never start with base 1, |
| 450 | // to ensure that using token.NoPos+1 will panic. |
| 451 | fileSetscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fileSet..AddFilescip-go gomod github.com/golang/go/src go1.20 `go/token`/FileSet#AddFile().("gofumpt_base.go", 1, 10) |
| 452 | |
| 453 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.Usagescip-go gomod github.com/golang/go/src go1.20 flag/Usage. = usagescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/usage(). |
| 454 | flagscip-go gomod github.com/golang/go/src go1.20 flag/.Parsescip-go gomod github.com/golang/go/src go1.20 flag/Parse().() |
| 455 | |
| 456 | if *simplifyASTscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/simplifyAST. { |
| 457 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.20 os/.Stderrscip-go gomod github.com/golang/go/src go1.20 os/Stderr., "warning: -s is deprecated as it is always enabled\n") |
| 458 | } |
| 459 | if *rewriteRulescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/rewriteRule. != "" { |
| 460 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.20 os/.Stderrscip-go gomod github.com/golang/go/src go1.20 os/Stderr., `the rewrite flag is no longer available; use "gofmt -r" instead`+"\n") |
| 461 | osscip-go gomod github.com/golang/go/src go1.20 os/.Exitscip-go gomod github.com/golang/go/src go1.20 os/Exit().(2) |
| 462 | } |
| 463 | |
| 464 | // Print the gofumpt version if the user asks for it. |
| 465 | if *showVersionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/showVersion. { |
| 466 | fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.20 fmt/Println().(gversionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/version`/.Stringscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/internal/version`/String().(versionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/version.)) |
| 467 | return |
| 468 | } |
| 469 | |
| 470 | if *cpuprofilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/cpuprofile. != "" { |
| 471 | fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. <- true |
| 472 | f, err := osscip-go gomod github.com/golang/go/src go1.20 os/.Createscip-go gomod github.com/golang/go/src go1.20 os/Create().(*cpuprofilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/cpuprofile.) |
| 473 | if err != nil { |
| 474 | s.AddReportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#AddReport().(fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("creating cpu profile: %s", err)) |
| 475 | return |
| 476 | } |
| 477 | defer func() { |
| 478 | f.Closescip-go gomod github.com/golang/go/src go1.20 os/File#Close().() |
| 479 | <-fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. |
| 480 | }() |
| 481 | pprofscip-go gomod github.com/golang/go/src go1.20 `runtime/pprof`/.StartCPUProfilescip-go gomod github.com/golang/go/src go1.20 `runtime/pprof`/StartCPUProfile().(f) |
| 482 | defer pprofscip-go gomod github.com/golang/go/src go1.20 `runtime/pprof`/.StopCPUProfilescip-go gomod github.com/golang/go/src go1.20 `runtime/pprof`/StopCPUProfile().() |
| 483 | } |
| 484 | |
| 485 | initParserModescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/initParserMode().() |
| 486 | |
| 487 | args := flagscip-go gomod github.com/golang/go/src go1.20 flag/.Argsscip-go gomod github.com/golang/go/src go1.20 flag/Args().() |
| 488 | if len(args) == 0 { |
| 489 | if *writescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/write. { |
| 490 | s.AddReportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#AddReport().(fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.20 fmt/Errorf().("error: cannot use -w with standard input")) |
| 491 | return |
| 492 | } |
| 493 | s.Addscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#Add().(0, func(r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) error { |
| 494 | // TODO: test explicit==true |
| 495 | return processFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/processFile().("<standard input>", nil, osscip-go gomod github.com/golang/go/src go1.20 os/.Stdinscip-go gomod github.com/golang/go/src go1.20 os/Stdin., r, true) |
| 496 | }) |
| 497 | return |
| 498 | } |
| 499 | |
| 500 | for _, arg := range args { |
| 501 | switch info, err := osscip-go gomod github.com/golang/go/src go1.20 os/.Statscip-go gomod github.com/golang/go/src go1.20 os/Stat().(arg); { |
| 502 | case err != nil: |
| 503 | s.AddReportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#AddReport().(err) |
| 504 | case !info.IsDirscip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#IsDir().(): |
| 505 | // Non-directory arguments are always formatted. |
| 506 | arg := arg |
| 507 | s.Addscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#Add().(fileWeightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fileWeight().(arg, info), func(r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) error { |
| 508 | return processFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/processFile().(arg, info, nil, r, true) |
| 509 | }) |
| 510 | default: |
| 511 | // Directories are walked, ignoring non-Go files. |
| 512 | err := filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.WalkDirscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/WalkDir().(arg, func(path string, f fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.20 `io/fs`/DirEntry#, err error) error { |
| 513 | // vendor and testdata directories are skipped, |
| 514 | // unless they are explicitly passed as an argument. |
| 515 | base := filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Base().(path) |
| 516 | if path != arg && (base == "vendor" || base == "testdata") { |
| 517 | return filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.SkipDirscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/SkipDir. |
| 518 | } |
| 519 | |
| 520 | if err != nil || !isGoFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/isGoFile().(f) { |
| 521 | return err |
| 522 | } |
| 523 | info, err := f.Infoscip-go gomod github.com/golang/go/src go1.20 `io/fs`/DirEntry#Info().() |
| 524 | if err != nil { |
| 525 | s.AddReportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#AddReport().(err) |
| 526 | return nil |
| 527 | } |
| 528 | s.Addscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#Add().(fileWeightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fileWeight().(path, info), func(r *reporterscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/reporter#) error { |
| 529 | return processFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/processFile().(path, info, nil, r, false) |
| 530 | }) |
| 531 | return nil |
| 532 | }) |
| 533 | if err != nil { |
| 534 | s.AddReportscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/sequencer#AddReport().(err) |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | type modulescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module# struct { |
| 541 | Goscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#Go. string |
| 542 | Modulescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#Module. struct { |
| 543 | Pathscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#$anon_7924d3d9624588aa#Path. string |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | func loadModuleInfoscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/loadModuleInfo().(dir string) any { |
| 548 | cmd := execscip-go gomod github.com/golang/go/src go1.20 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.20 `os/exec`/Command().("go", "mod", "edit", "-json") |
| 549 | cmd.Dirscip-go gomod github.com/golang/go/src go1.20 `os/exec`/Cmd#Dir. = dir |
| 550 | |
| 551 | // Spawning "go mod edit" will open files by design, |
| 552 | // such as the named pipe to obtain stdout. |
| 553 | // TODO(mvdan): if we run into "too many open files" errors again in the |
| 554 | // future, we probably need to turn fdSem into a weighted semaphore so this |
| 555 | // operation can acquire a weight larger than 1. |
| 556 | fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. <- true |
| 557 | out, err := cmd.Outputscip-go gomod github.com/golang/go/src go1.20 `os/exec`/Cmd#Output().() |
| 558 | defer func() { <-fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. }() |
| 559 | |
| 560 | if err != nil || len(out) == 0 { |
| 561 | return nil |
| 562 | } |
| 563 | mod := new(modulescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/module#) |
| 564 | if err := jsonscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.20 `encoding/json`/Unmarshal().(out, mod); err != nil { |
| 565 | return nil |
| 566 | } |
| 567 | return mod |
| 568 | } |
| 569 | |
| 570 | // Written to by fileWeight, read from fileWeight and processFile. |
| 571 | // A present but nil value means that loading the module info failed. |
| 572 | // Note that we don't require the keys to be absolute directories, |
| 573 | // so duplicates are possible. The same can happen with symlinks. |
| 574 | var moduleCacheByDirscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/moduleCacheByDir. syncscip-go gomod github.com/golang/go/src go1.20 sync/.Mapscip-go gomod github.com/golang/go/src go1.20 sync/Map# // map[dirString]*module |
| 575 | |
| 576 | func fileWeightscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fileWeight().(path string, info fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/.FileInfoscip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#) int64 { |
| 577 | dir := filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Dir().(path) |
| 578 | if _, ok := moduleCacheByDirscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/moduleCacheByDir..Loadscip-go gomod github.com/golang/go/src go1.20 sync/Map#Load().(dir); !ok { |
| 579 | moduleCacheByDirscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/moduleCacheByDir..Storescip-go gomod github.com/golang/go/src go1.20 sync/Map#Store().(dir, loadModuleInfoscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/loadModuleInfo().(dir)) |
| 580 | } |
| 581 | if info == nil { |
| 582 | return exclusivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/exclusive. |
| 583 | } |
| 584 | if info.Modescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Mode().().Typescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileMode#Type().() == fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/.ModeSymlinkscip-go gomod github.com/golang/go/src go1.20 `io/fs`/ModeSymlink. { |
| 585 | var err error |
| 586 | info, err = osscip-go gomod github.com/golang/go/src go1.20 os/.Statscip-go gomod github.com/golang/go/src go1.20 os/Stat().(path) |
| 587 | if err != nil { |
| 588 | return exclusivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/exclusive. |
| 589 | } |
| 590 | } |
| 591 | if !info.Modescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Mode().().IsRegularscip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileMode#IsRegular().() { |
| 592 | // For non-regular files, FileInfo.Size is system-dependent and thus not a |
| 593 | // reliable indicator of weight. |
| 594 | return exclusivescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/exclusive. |
| 595 | } |
| 596 | return info.Sizescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileInfo#Size().() |
| 597 | } |
| 598 | |
| 599 | const chmodSupportedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/chmodSupported. = runtimescip-go gomod github.com/golang/go/src go1.20 runtime/.GOOSscip-go gomod github.com/golang/go/src go1.20 runtime/GOOS. != "windows" |
| 600 | |
| 601 | // backupFile writes data to a new file named filename<number> with permissions perm, |
| 602 | // with <number randomly chosen such that the file name is unique. backupFile returns |
| 603 | // the chosen file name. |
| 604 | func backupFilescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/backupFile().(filename string, data []byte, perm fsscip-go gomod github.com/golang/go/src go1.20 `io/fs`/.FileModescip-go gomod github.com/golang/go/src go1.20 `io/fs`/FileMode#) (string, error) { |
| 605 | fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. <- true |
| 606 | defer func() { <-fdSemscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/fdSem. }() |
| 607 | |
| 608 | // create backup file |
| 609 | f, err := osscip-go gomod github.com/golang/go/src go1.20 os/.CreateTempscip-go gomod github.com/golang/go/src go1.20 os/CreateTemp().(filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Dir().(filename), filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Base().(filename)) |
| 610 | if err != nil { |
| 611 | return "", err |
| 612 | } |
| 613 | bakname := f.Namescip-go gomod github.com/golang/go/src go1.20 os/File#Name().() |
| 614 | if chmodSupportedscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt`/chmodSupported. { |
| 615 | err = f.Chmodscip-go gomod github.com/golang/go/src go1.20 os/File#Chmod().(perm) |
| 616 | if err != nil { |
| 617 | f.Closescip-go gomod github.com/golang/go/src go1.20 os/File#Close().() |
| 618 | osscip-go gomod github.com/golang/go/src go1.20 os/.Removescip-go gomod github.com/golang/go/src go1.20 os/Remove().(bakname) |
| 619 | return bakname, err |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // write data to backup file |
| 624 | _, err = f.Writescip-go gomod github.com/golang/go/src go1.20 os/File#Write().(data) |
| 625 | if err1 := f.Closescip-go gomod github.com/golang/go/src go1.20 os/File#Close().(); err == nil { |
| 626 | err = err1 |
| 627 | } |
| 628 | |
| 629 | return bakname, err |
| 630 | } |