Skip to content

Commit

Permalink
Allow formatting of an individual ast.FileNode (#2547)
Browse files Browse the repository at this point in the history
Avoiding the need to materializing the .proto file being formatted, or a
module to contain the file (or a readbucket to contain the result). If a
file can parse, it can be formatted. For example, the current vscode-buf
extension writes a temp file to format the in memory contents:
https://github.com/bufbuild/vscode-buf/blob/6a083e4c4d21235643da65ddbed585d7a391da55/src/formatter.ts#L47
  • Loading branch information
Alfus authored Nov 8, 2023
1 parent 2eb772c commit 6befbe4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions private/buf/bufformat/bufformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ package bufformat

import (
"context"
"io"

"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/pkg/storage"
"github.com/bufbuild/buf/private/pkg/storage/storagemem"
"github.com/bufbuild/buf/private/pkg/thread"
"github.com/bufbuild/protocompile/ast"
"github.com/bufbuild/protocompile/parser"
"github.com/bufbuild/protocompile/reporter"
"go.uber.org/multierr"
)

// Format formats and writes the target module files into a read bucket.
func Format(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket, retErr error) {
// FormatModule formats and writes the target module files into a read bucket.
func FormatModule(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket, retErr error) {
fileInfos, err := module.TargetFileInfos(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -55,7 +57,7 @@ func Format(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket,
defer func() {
retErr = multierr.Append(retErr, writeObjectCloser.Close())
}()
if err := newFormatter(writeObjectCloser, fileNode).Run(); err != nil {
if err := FormatFileNode(writeObjectCloser, fileNode); err != nil {
return err
}
return writeObjectCloser.SetExternalPath(moduleFile.ExternalPath())
Expand All @@ -66,3 +68,9 @@ func Format(ctx context.Context, module bufmodule.Module) (_ storage.ReadBucket,
}
return readWriteBucket, nil
}

// FormatFileNode formats the given file node and writ the result to dest.
func FormatFileNode(dest io.Writer, fileNode *ast.FileNode) error {
formatter := newFormatter(dest, fileNode)
return formatter.Run()
}
2 changes: 1 addition & 1 deletion private/buf/bufformat/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func testFormatNoDiff(t *testing.T, path string) {
require.NoError(t, err)
module, err := bufmodule.NewModuleForBucket(ctx, moduleBucket)
require.NoError(t, err)
readBucket, err := Format(ctx, module)
readBucket, err := FormatModule(ctx, module)
require.NoError(t, err)
require.NoError(
t,
Expand Down
2 changes: 1 addition & 1 deletion private/buf/cmd/buf/command/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func formatModule(
return false, err
}
// Note that external paths are set properly for the files in this read bucket.
formattedReadBucket, err := bufformat.Format(ctx, module)
formattedReadBucket, err := bufformat.FormatModule(ctx, module)
if err != nil {
return false, err
}
Expand Down

0 comments on commit 6befbe4

Please sign in to comment.