refactor

package
v0.0.0-...-5c202b9 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: BSD-3-Clause Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StackTypes

func StackTypes(list []ast.Node) string

func Walk

func Walk(n ast.Node, f func(stack []ast.Node))

func WalkPost

func WalkPost(n ast.Node, f func(stack []ast.Node))

func WalkRange

func WalkRange(n ast.Node, lo, hi token.Pos, f func(stack []ast.Node))

func WalkRangePost

func WalkRangePost(n ast.Node, lo, hi token.Pos, f func(stack []ast.Node))

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

A Buffer is a queue of edits to apply to a file text. It's like edit.Buffer but uses token.Pos as coordinate space.

func NewBufferAt

func NewBufferAt(s *Snapshot, pos token.Pos, text []byte) *Buffer

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a new byte slice containing the original data with the queued edits applied.

func (*Buffer) Delete

func (b *Buffer) Delete(pos, end token.Pos)

func (*Buffer) ForceDelete

func (b *Buffer) ForceDelete(pos, end token.Pos)

func (*Buffer) Insert

func (b *Buffer) Insert(pos token.Pos, new string)

func (*Buffer) Replace

func (b *Buffer) Replace(pos, end token.Pos, new string)

func (*Buffer) String

func (b *Buffer) String() string

String returns a string containing the original data with the queued edits applied.

type Config

type Config struct {
	// BuildTags is a list of build tags to set for this configuration.
	//
	// Some build tags are propagated specially:
	//
	// - GOOS and GOARCH build tags control the GOOS/GOARCH environment
	// variables.
	//
	// - The "race" build tag controls the -race flag.
	//
	// - The "cgo" and "!cgo" build tags control the CGO_ENABLED environment
	// variable.
	//
	// TODO: We might need to do something for release tags, too.
	BuildTags []string
}

func (Config) String

func (c Config) String() string

type Configs

type Configs struct {
	// contains filtered or unexported fields
}

func ConfigsAllPlatforms

func ConfigsAllPlatforms() (Configs, error)

ConfigsAllPlatforms returns a Configs for all GOOS/GOARCH combinations, including covering both cgo and non-cgo where possible.

func NewConfigs

func NewConfigs(buildTags ...string) Configs

NewConfigs returns a new Configs consisting of one Config with the given build tags.

func (Configs) Cross

func (cs Configs) Cross(cs2 Configs) Configs

func (Configs) Plus

func (cs Configs) Plus(cs2 Configs) Configs

type DepsGraph

type DepsGraph struct {
	Level DepsGraphLevel

	// G is the graph, where G[p1][n1][p2][n2] is true if p1.n1 references p2.n2.
	G map[*Package]map[string]map[*Package]map[string]bool
}

A DepsGraph records dependency information.

This is a graph over QualName nodes, with edges from each package-level declaration to the identifiers it references. If Level is PkgsRefs, then QualName.Name is always "", so this decays into references between packages.

func (*DepsGraph) Map

func (g *DepsGraph) Map(remap map[QualName]QualName) *DepsGraph

func (*DepsGraph) Reverse

func (g *DepsGraph) Reverse() *DepsGraph

type DepsGraphLevel

type DepsGraphLevel int
const (
	PkgRefs DepsGraphLevel = iota
	CrossPkgSymRefs
	SymRefs
)

type Edit

type Edit struct {
	Name       string
	OldText    []byte
	Create     bool
	Delete     bool
	Buffer     *Buffer
	Package    *Package
	File       *File
	AddImports []NewImport
}

func (*Edit) NewText

func (e *Edit) NewText() ([]byte, error)

type Error

type Error struct {
	Pos token.Position
	Msg string

	Secondary []*Error
}

An Error is an error at a particular source position. It may have attached errors at other positions (but those must not have secondary errors).

func (*Error) Error

func (e *Error) Error() string

type ErrorList

type ErrorList struct {
	// contains filtered or unexported fields
}

ErrorList is a set of Errors. It is also an error itself. The zero value is an empty list, ready to use.

func (*ErrorList) Add

func (l *ErrorList) Add(err error)

Add adds and error to l. If the error is an Error, scanner.Error, or types.Error, it uses the position information from the error. If the error is an ErrorList or a scanner.ErrorList, it merges all errors from that list into this list. Otherwise, it adds the error with no position information. It suppresses duplicate errors (same position and message).

func (*ErrorList) Err

func (l *ErrorList) Err() error

Err returns an error equivalent to this error list. If the list is empty, Err returns nil.

func (*ErrorList) Error

func (l *ErrorList) Error() string

Error sorts, deduplicates, and returns a "\n" separated list of formatted errors. Note that the result does not end in "\n" because the caller is expected to add that.

type File

type File struct {
	Name     string // Short path (either relative to r.dir or absolute)
	Text     []byte
	Syntax   *ast.File // Parsed form of Text
	Imports  []string  // Local import paths, derived from Syntax
	Modified bool      // Modified from on-disk file (or does not exist on disk)
	Deleted  bool      // Tombstone marking a deleted File. Only Name is valid.
	Hash     string    // SHA256(Name+Text)
}

File represents a source file, including both its text and parsed forms. Files are tracked by the buildCache and are deeply immutable.

type Item

type Item struct {
	Kind  ItemKind
	Name  string
	Outer *Item
	Obj   types.Object
	Pos   token.Pos
	End   token.Pos
}

func (*Item) Outermost

func (i *Item) Outermost() *Item

type ItemKind

type ItemKind int
const (
	ItemNotFound ItemKind
	ItemFile
	ItemDir
	ItemConst
	ItemType
	ItemVar
	ItemFunc
	ItemField
	ItemMethod
	ItemPos
	ItemPkg
)

func (ItemKind) String

func (k ItemKind) String() string

type NewImport

type NewImport struct {
	// contains filtered or unexported fields
}

type Package

type Package struct {
	Name            string
	Dir             string
	ID              string
	PkgPath         string
	ForTest         string
	Files           []*File // Sorted by File.Name
	Imports         []string
	InCurrentModule bool
	Export          string
	BuildID         string
	ImportMap       map[string]string // from local import path to package path (used for vendored packages in std)

	Types     *types.Package
	TypesInfo *types.Info
	Sizes     types.Sizes
}

func (*Package) String

func (p *Package) String() string

type QualName

type QualName struct {
	Pkg  *Package
	Name string
}

A QualName is a qualified name: a name and its package path.

func (QualName) Object

func (q QualName) Object() types.Object

func (QualName) String

func (q QualName) String() string

type Refactor

type Refactor struct {
	Stdout   io.Writer
	Stderr   io.Writer
	ShowDiff bool
	Debug    map[string]string // debugging settings

	// Configs is a set of build configurations to refactor.
	// New returns just the default build configuration,
	// but a caller can overwrite this.
	Configs Configs
	// contains filtered or unexported fields
}

A Refactor holds the state for an active refactoring.

func New

func New(dir string) (*Refactor, error)

New returns a new refactoring, editing the package in the given directory (usually ".").

func (*Refactor) Apply

func (r *Refactor) Apply() error

Apply applies edits to all Snapshots, type-checks the resulting files, and creates a new set of current Snapshots. If there are any type errors in the new Snapshots, it returns an ErrorList.

func (*Refactor) MergeSnapshots

func (r *Refactor) MergeSnapshots() (*Snapshot, error)

MergeSnapshots combines all Snapshots. The resulting Snapshot contains only files and cannot be type-checked, but can be written out. If any Snapshots are inconsistent, it prints details to r.Stderr and returns an error.

TODO: Maybe there should be another type representing just the modified file system. Snapshots could use that internally so we can still, e.g., show diffs on errors, and this could return it directly rather than returning a weird Snapshot. That would make it easy to separate out "apply edits" from "build a new Snapshot" in apply, and here we could reuse "apply edits".

func (*Refactor) ModPath

func (r *Refactor) ModPath() string

func (*Refactor) ModRoot

func (r *Refactor) ModRoot() string

func (*Refactor) PkgDir

func (r *Refactor) PkgDir(pkg string) (string, error)

func (*Refactor) Snapshots

func (r *Refactor) Snapshots() ([]*Snapshot, error)

Snapshots returns the latest Snapshot set. The caller should perform the same refactoring on each Snapshot and then call r.Commit(). On the first call, it loads all packages.

type Snapshot

type Snapshot struct {
	Errors *ErrorList
	// contains filtered or unexported fields
}

A Snapshot is a base set of Packages and their parsed source files, plus a set of concurrent edits to be made to those files.

func (*Snapshot) Addr

func (s *Snapshot) Addr(pos token.Pos) string

func (*Snapshot) CheckImportCycle

func (s *Snapshot) CheckImportCycle(g *DepsGraph) error

func (*Snapshot) CreateFile

func (s *Snapshot) CreateFile(p *Package, baseName, text string) *ast.File

func (*Snapshot) CreatePackage

func (s *Snapshot) CreatePackage(pkgpath string) (*Package, error)

func (*Snapshot) DeleteAt

func (s *Snapshot) DeleteAt(pos, end token.Pos)

func (*Snapshot) DeleteFile

func (s *Snapshot) DeleteFile(pos token.Pos)

func (*Snapshot) DepsGraph

func (s *Snapshot) DepsGraph(level DepsGraphLevel) *DepsGraph

func (*Snapshot) Diff

func (s *Snapshot) Diff() ([]byte, error)

func (*Snapshot) ErrorAt

func (s *Snapshot) ErrorAt(pos token.Pos, format string, args ...interface{})

func (*Snapshot) Eval

func (s *Snapshot) Eval(expr string) *Item

func (*Snapshot) EvalList

func (s *Snapshot) EvalList(args string) ([]*Item, []string)

func (*Snapshot) EvalNext

func (s *Snapshot) EvalNext(args string) (*Item, string, string)

func (*Snapshot) File

func (s *Snapshot) File(pos token.Pos) string

func (*Snapshot) FileAt

func (s *Snapshot) FileAt(pos token.Pos) (*Package, *ast.File)

func (*Snapshot) FileByName

func (s *Snapshot) FileByName(name string) (*Package, *ast.File)

func (*Snapshot) FileRange

func (s *Snapshot) FileRange(pos token.Pos) (start, end token.Pos)

func (*Snapshot) ForEachFile

func (s *Snapshot) ForEachFile(f func(pkg *Package, file *ast.File))

func (*Snapshot) ForEachTargetFile

func (s *Snapshot) ForEachTargetFile(f func(pkg *Package, file *ast.File))

func (*Snapshot) ForceDeleteAt

func (s *Snapshot) ForceDeleteAt(pos, end token.Pos)

func (*Snapshot) Fset

func (s *Snapshot) Fset() *token.FileSet

func (*Snapshot) Gofmt

func (s *Snapshot) Gofmt()

func (*Snapshot) InsertAt

func (s *Snapshot) InsertAt(pos token.Pos, repl string)

func (*Snapshot) LookupAt

func (s *Snapshot) LookupAt(name string, pos token.Pos) types.Object

func (*Snapshot) Modified

func (s *Snapshot) Modified() []string

func (*Snapshot) NeedImport

func (s *Snapshot) NeedImport(pos token.Pos, id string, pkg *types.Package) string

func (*Snapshot) PackageAt

func (s *Snapshot) PackageAt(pos token.Pos) *Package

func (*Snapshot) Packages

func (s *Snapshot) Packages() []*Package

func (*Snapshot) Position

func (s *Snapshot) Position(pos token.Pos) token.Position

func (*Snapshot) QualNameOf

func (s *Snapshot) QualNameOf(obj types.Object) QualName

func (*Snapshot) Refactor

func (s *Snapshot) Refactor() *Refactor

func (*Snapshot) ReplaceAt

func (s *Snapshot) ReplaceAt(lo, hi token.Pos, repl string)

func (*Snapshot) ReplaceNode

func (s *Snapshot) ReplaceNode(n ast.Node, repl string)

func (*Snapshot) Reset

func (s *Snapshot) Reset()

Reset undoes any edits to s.

func (*Snapshot) ScopeAt

func (s *Snapshot) ScopeAt(pos token.Pos) *types.Scope

func (*Snapshot) SyntaxAt

func (s *Snapshot) SyntaxAt(pos token.Pos) []ast.Node

func (*Snapshot) Target

func (s *Snapshot) Target() *Package

func (*Snapshot) Text

func (s *Snapshot) Text(lo, hi token.Pos) []byte

func (*Snapshot) Write

func (s *Snapshot) Write() error

Jump to

Keyboard shortcuts

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