formatx

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FmtNode

func FmtNode(fset *token.FileSet, node ast.Node) []byte

func Format

func Format(file, mod string, src []byte, procs ...Proc) ([]byte, error)
Example
package main

import (
	"fmt"
	"os"
	"path"
	"path/filepath"

	. "github.com/machinefi/w3bstream/pkg/depends/gen/codegen"
)

func CreateDemoFile(filename string) *File {
	f := NewFile("main", filename)

	f.WriteSnippet(Func().Named("main").Do(
		Call(f.Use("fmt", "Println"), f.Value("Hello, 世界")),
		Call(f.Use("github.com/some/pkg", "Println"), f.Value("Hello World!")),
		Call(f.Use("github.com/another/pkg", "Println"), f.Value("Hello World!")),
		Call(f.Use("github.com/one_more/pkg", "Println"), f.Value("Hello World!")),

		Assign(AnonymousIdent).By(Call(f.Use("bytes", "NewBuffer"), f.Value(nil))),
	))

	return f
}

func main() {
	cwd, _ := os.Getwd()
	filename := path.Join(cwd, "hello/hello.go")

	f := CreateDemoFile(filename)

	defer os.RemoveAll(filepath.Dir(f.Name))

	_, err := f.Write()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(string(f.Formatted()))

}
Output:

// This is a generated source file. DO NOT EDIT
// Source: main/hello.go

package main

import (
	"bytes"
	"fmt"

	gen_pkg_1 "github.com/another/pkg"
	gen_pkg_2 "github.com/one_more/pkg"
	"github.com/some/pkg"
)

func main() {
	fmt.Println("Hello, 世界")
	pkg.Println("Hello World!")
	gen_pkg_1.Println("Hello World!")
	gen_pkg_2.Println("Hello World!")
	_ = bytes.NewBuffer(nil)
}

func MustFormat

func MustFormat(file, mod string, src []byte, procs ...Proc) []byte

func Parse

func Parse(file string, src []byte) (*token.FileSet, *ast.File, error)

func SortImports

func SortImports(fset *token.FileSet, f *ast.File, file, group string) error

Types

type GroupSet

type GroupSet [4][]*dep

func (*GroupSet) AppendGroup

func (g *GroupSet) AppendGroup(pkg string, spec *ast.ImportSpec)

func (*GroupSet) AppendLocal

func (g *GroupSet) AppendLocal(pkg string, spec *ast.ImportSpec)

func (*GroupSet) AppendStd

func (g *GroupSet) AppendStd(pkg string, spec *ast.ImportSpec)

func (*GroupSet) AppendVendor

func (g *GroupSet) AppendVendor(pkg string, spec *ast.ImportSpec)

func (GroupSet) Bytes

func (g GroupSet) Bytes() []byte

func (*GroupSet) Len

func (g *GroupSet) Len() int

type Proc

type Proc func(fset *token.FileSet, f *ast.File, filename, group string) error

type StdLibSet

type StdLibSet map[string]bool
var Stds StdLibSet

func (StdLibSet) WalkInit

func (s StdLibSet) WalkInit(root, prefix string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL