github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/worker/format.go (about)

     1  // Copyright 2022 Edward McFarlane. 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 worker
     6  
     7  import (
     8  	"context"
     9  
    10  	"github.com/bazelbuild/buildtools/build"
    11  	"github.com/bazelbuild/buildtools/convertast"
    12  	"go.starlark.net/syntax"
    13  )
    14  
    15  // Format starlark code.
    16  func Format(_ context.Context, filename string, src interface{}) ([]byte, error) {
    17  	ast, err := syntax.Parse(filename, src, syntax.RetainComments)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	newAst := convertast.ConvFile(ast)
    22  	return build.Format(newAst), nil
    23  }