mirror of
https://github.com/numtide/treefmt.git
synced 2024-09-11 17:07:44 +03:00
ce14ee828f
For each path we determine the list of formatters that are interested in formatting it. From there, we sort the list of formatters first by priority (lower value, higher priority) and then by name (lexicographically). With this information we create a batch key which is based on the unique sequence of formatters. When enough paths with the same sequence is ready we apply them in order to each formatter. By default, with no special configuration, this model guarantees that a given path will only be processed by one formatter at a time. If a user wishes to influence the order in which formatters are applied they can use the priority field. Signed-off-by: Brian McGee <brian@bmcgee.ie>
43 lines
897 B
Go
43 lines
897 B
Go
package format
|
|
|
|
import (
|
|
"cmp"
|
|
"slices"
|
|
|
|
"git.numtide.com/numtide/treefmt/walk"
|
|
)
|
|
|
|
type Task struct {
|
|
File *walk.File
|
|
Formatters []*Formatter
|
|
BatchKey string
|
|
}
|
|
|
|
func NewTask(file *walk.File, formatters []*Formatter) Task {
|
|
// sort by priority in ascending order
|
|
slices.SortFunc(formatters, func(a, b *Formatter) int {
|
|
priorityA := a.Priority()
|
|
priorityB := b.Priority()
|
|
|
|
result := priorityA - priorityB
|
|
if result == 0 {
|
|
// formatters with the same priority are sorted lexicographically to ensure a deterministic outcome
|
|
result = cmp.Compare(a.Name(), b.Name())
|
|
}
|
|
return result
|
|
})
|
|
|
|
// construct a batch key which represents the unique sequence of formatters to be applied to file
|
|
var key string
|
|
for _, f := range formatters {
|
|
key += f.name + ":"
|
|
}
|
|
key = key[:len(key)-1]
|
|
|
|
return Task{
|
|
File: file,
|
|
Formatters: formatters,
|
|
BatchKey: key,
|
|
}
|
|
}
|