diff --git a/config/config_test.go b/config/config_test.go index f50e6c7..cfe67e0 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -59,6 +59,18 @@ func TestReadConfigFile(t *testing.T) { as.Nil(alejandra.Options) as.Equal([]string{"*.nix"}, alejandra.Includes) as.Equal([]string{"examples/nix/sources.nix"}, alejandra.Excludes) + as.Equal("nix", alejandra.Pipeline) + as.Equal(1, alejandra.Priority) + + // deadnix + deadnix, ok := cfg.Formatters["deadnix"] + as.True(ok, "deadnix formatter not found") + as.Equal("deadnix", deadnix.Command) + as.Nil(deadnix.Options) + as.Equal([]string{"*.nix"}, deadnix.Includes) + as.Nil(deadnix.Excludes) + as.Equal("nix", deadnix.Pipeline) + as.Equal(2, deadnix.Priority) // ruby ruby, ok := cfg.Formatters["ruby"] diff --git a/config/formatter.go b/config/formatter.go index 4bc6a76..4fea20b 100644 --- a/config/formatter.go +++ b/config/formatter.go @@ -9,6 +9,8 @@ type Formatter struct { Includes []string // Excludes is an optional list of glob patterns used to exclude certain files from this Formatter. Excludes []string - // + // Indicates this formatter should be executed as part of a group of formatters all sharing the same pipeline key. Pipeline string + // Indicates the order of precedence when executing as part of a pipeline. + Priority int } diff --git a/format/pipeline.go b/format/pipeline.go index 84dbefc..4dc8cd5 100644 --- a/format/pipeline.go +++ b/format/pipeline.go @@ -1,6 +1,9 @@ package format -import "context" +import ( + "context" + "slices" +) type Pipeline struct { sequence []*Formatter @@ -8,6 +11,10 @@ type Pipeline struct { func (p *Pipeline) Add(f *Formatter) { p.sequence = append(p.sequence, f) + // sort by priority in ascending order + slices.SortFunc(p.sequence, func(a, b *Formatter) int { + return a.config.Priority - b.config.Priority + }) } func (p *Pipeline) Wants(path string) bool { diff --git a/test/examples/treefmt.toml b/test/examples/treefmt.toml index 0207c04..8d3ea62 100644 --- a/test/examples/treefmt.toml +++ b/test/examples/treefmt.toml @@ -32,11 +32,13 @@ includes = ["*.nix"] # Act as an example on how to exclude specific files excludes = ["examples/nix/sources.nix"] pipeline = "nix" +priority = 1 [formatter.deadnix] command = "deadnix" includes = ["*.nix"] pipeline = "nix" +priority = 2 [formatter.ruby] command = "rufo"