-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
formatter: split lines significantly longer than others into separate…
… groups
- Loading branch information
Showing
4 changed files
with
593 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2009 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. | ||
|
||
package format | ||
|
||
import ( | ||
"iter" | ||
"math" | ||
) | ||
|
||
// Splits fields into sub-slices based on their length to isolate fields or | ||
// groups of fields that are significantly longer than others in the group. | ||
// | ||
// The algorithm itself and the constants used in this function are from gofmt: | ||
// https://github.com/golang/go/blob/go1.23.0/src/go/printer/nodes.go#L126 | ||
func splitSegmentedFields(fields []segmentedField) iter.Seq[[]segmentedField] { | ||
return func(yield func([]segmentedField) bool) { | ||
const r = 2.5 | ||
const smallSize = 40 | ||
var count, lower, size int | ||
var lnsum float64 | ||
for i := 0; i < len(fields); i++ { | ||
f := fields[i] | ||
prevSize := size | ||
size = len(f.typeName) + len(f.fieldName) | ||
if size > 0 && prevSize > 0 && count > 0 && (prevSize > smallSize || size > smallSize) { | ||
mean := math.Exp(lnsum / float64(count)) | ||
ratio := float64(size) / mean | ||
if r*ratio <= 1 || r <= ratio { | ||
// split the group | ||
yield(fields[lower:i]) | ||
lower = i | ||
count = 0 | ||
lnsum = 0 | ||
} | ||
} | ||
if size > 0 { | ||
count++ | ||
lnsum += math.Log(float64(size)) | ||
} | ||
} | ||
yield(fields[lower:]) | ||
} | ||
} |
Oops, something went wrong.