package refactor
import "rsc.io/rf/refactor"
Index ¶
- func StackTypes(list []ast.Node) string
- func Walk(n ast.Node, f func(stack []ast.Node))
- func WalkPost(n ast.Node, f func(stack []ast.Node))
- func WalkRange(n ast.Node, lo, hi token.Pos, f func(stack []ast.Node))
- func WalkRangePost(n ast.Node, lo, hi token.Pos, f func(stack []ast.Node))
- type Buffer
- func NewBufferAt(s *Snapshot, pos token.Pos, text []byte) *Buffer
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Delete(pos, end token.Pos)
- func (b *Buffer) ForceDelete(pos, end token.Pos)
- func (b *Buffer) Insert(pos token.Pos, new string)
- func (b *Buffer) Replace(pos, end token.Pos, new string)
- func (b *Buffer) String() string
- type Config
- type Configs
- func ConfigsAllPlatforms() (Configs, error)
- func NewConfigs(buildTags ...string) Configs
- func (cs Configs) Cross(cs2 Configs) Configs
- func (cs Configs) Plus(cs2 Configs) Configs
- type DepsGraph
- func (g *DepsGraph) Map(remap map[QualName]QualName) *DepsGraph
- func (g *DepsGraph) Reverse() *DepsGraph
- type DepsGraphLevel
- type Edit
- type Error
- type ErrorList
- func (l *ErrorList) Add(err error)
- func (l *ErrorList) Err() error
- func (l *ErrorList) Error() string
- type File
- type Item
- type ItemKind
- type NewImport
- type Package
- type QualName
- type Refactor
- func New(dir string) (*Refactor, error)
- func (r *Refactor) Apply() error
- func (r *Refactor) MergeSnapshots() (*Snapshot, error)
- func (r *Refactor) ModPath() string
- func (r *Refactor) ModRoot() string
- func (r *Refactor) PkgDir(pkg string) (string, error)
- func (r *Refactor) Snapshots() ([]*Snapshot, error)
- type Snapshot
- func (s *Snapshot) Addr(pos token.Pos) string
- func (s *Snapshot) CheckImportCycle(g *DepsGraph) error
- func (s *Snapshot) CreateFile(p *Package, baseName, text string) *ast.File
- func (s *Snapshot) CreatePackage(pkgpath string) (*Package, error)
- func (s *Snapshot) DeleteAt(pos, end token.Pos)
- func (s *Snapshot) DeleteFile(pos token.Pos)
- func (s *Snapshot) DepsGraph(level DepsGraphLevel) *DepsGraph
- func (s *Snapshot) Diff() ([]byte, error)
- func (s *Snapshot) ErrorAt(pos token.Pos, format string, args ...interface{})
- func (s *Snapshot) Eval(expr string) *Item
- func (s *Snapshot) EvalList(args string) ([]*Item, []string)
- func (s *Snapshot) EvalNext(args string) (*Item, string, string)
- func (s *Snapshot) File(pos token.Pos) string
- func (s *Snapshot) FileAt(pos token.Pos) (*Package, *ast.File)
- func (s *Snapshot) FileByName(name string) (*Package, *ast.File)
- func (s *Snapshot) FileRange(pos token.Pos) (start, end token.Pos)
- func (s *Snapshot) ForEachFile(f func(pkg *Package, file *ast.File))
- func (s *Snapshot) ForEachTargetFile(f func(pkg *Package, file *ast.File))
- func (s *Snapshot) ForceDeleteAt(pos, end token.Pos)
- func (s *Snapshot) Fset() *token.FileSet
- func (s *Snapshot) Gofmt()
- func (s *Snapshot) InsertAt(pos token.Pos, repl string)
- func (s *Snapshot) LookupAt(name string, pos token.Pos) types.Object
- func (s *Snapshot) Modified() []string
- func (s *Snapshot) NeedImport(pos token.Pos, id string, pkg *types.Package) string
- func (s *Snapshot) PackageAt(pos token.Pos) *Package
- func (s *Snapshot) Packages() []*Package
- func (s *Snapshot) Position(pos token.Pos) token.Position
- func (s *Snapshot) QualNameOf(obj types.Object) QualName
- func (s *Snapshot) Refactor() *Refactor
- func (s *Snapshot) ReplaceAt(lo, hi token.Pos, repl string)
- func (s *Snapshot) ReplaceNode(n ast.Node, repl string)
- func (s *Snapshot) Reset()
- func (s *Snapshot) ScopeAt(pos token.Pos) *types.Scope
- func (s *Snapshot) SyntaxAt(pos token.Pos) []ast.Node
- func (s *Snapshot) Target() *Package
- func (s *Snapshot) Text(lo, hi token.Pos) []byte
- func (s *Snapshot) Write() error
Functions ¶
func StackTypes ¶
func Walk ¶
func WalkPost ¶
func WalkRange ¶
func WalkRangePost ¶
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 (*Buffer) Bytes ¶
Bytes returns a new byte slice containing the original data with the queued edits applied.
func (*Buffer) Delete ¶
func (*Buffer) ForceDelete ¶
func (*Buffer) Insert ¶
func (*Buffer) Replace ¶
func (*Buffer) 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 ¶
type Configs ¶
type Configs struct {
// contains filtered or unexported fields
}
func ConfigsAllPlatforms ¶
ConfigsAllPlatforms returns a Configs for all GOOS/GOARCH combinations, including covering both cgo and non-cgo where possible.
func NewConfigs ¶
NewConfigs returns a new Configs consisting of one Config with the given build tags.
func (Configs) Cross ¶
func (Configs) Plus ¶
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 (*DepsGraph) Reverse ¶
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 ¶
type 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 ¶
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 ¶
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 ¶
Err returns an error equivalent to this error list. If the list is empty, Err returns nil.
func (*ErrorList) Error ¶
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 ¶
type ItemKind ¶
type ItemKind int
const ( ItemNotFound ItemKind ItemFile ItemDir ItemConst ItemType ItemVar ItemFunc ItemField ItemMethod ItemPos ItemPkg )
func (ItemKind) 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 ¶
type QualName ¶
A QualName is a qualified name: a name and its package path.
func (QualName) Object ¶
func (QualName) 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 ¶
New returns a new refactoring, editing the package in the given directory (usually ".").
func (*Refactor) Apply ¶
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 ¶
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 (*Refactor) ModRoot ¶
func (*Refactor) PkgDir ¶
func (*Refactor) Snapshots ¶
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 (*Snapshot) CheckImportCycle ¶
func (*Snapshot) CreateFile ¶
func (*Snapshot) CreatePackage ¶
func (*Snapshot) DeleteAt ¶
func (*Snapshot) DeleteFile ¶
func (*Snapshot) DepsGraph ¶
func (s *Snapshot) DepsGraph(level DepsGraphLevel) *DepsGraph
func (*Snapshot) Diff ¶
func (*Snapshot) ErrorAt ¶
func (*Snapshot) Eval ¶
func (*Snapshot) EvalList ¶
func (*Snapshot) EvalNext ¶
func (*Snapshot) File ¶
func (*Snapshot) FileAt ¶
func (*Snapshot) FileByName ¶
func (*Snapshot) FileRange ¶
func (*Snapshot) ForEachFile ¶
func (*Snapshot) ForEachTargetFile ¶
func (*Snapshot) ForceDeleteAt ¶
func (*Snapshot) Fset ¶
func (*Snapshot) Gofmt ¶
func (s *Snapshot) Gofmt()
func (*Snapshot) InsertAt ¶
func (*Snapshot) LookupAt ¶
func (*Snapshot) Modified ¶
func (*Snapshot) NeedImport ¶
func (*Snapshot) PackageAt ¶
func (*Snapshot) Packages ¶
func (*Snapshot) Position ¶
func (*Snapshot) QualNameOf ¶
func (*Snapshot) Refactor ¶
func (*Snapshot) ReplaceAt ¶
func (*Snapshot) ReplaceNode ¶
func (*Snapshot) Reset ¶
func (s *Snapshot) Reset()
Reset undoes any edits to s.
func (*Snapshot) ScopeAt ¶
func (*Snapshot) SyntaxAt ¶
func (*Snapshot) Target ¶
func (*Snapshot) Text ¶
func (*Snapshot) Write ¶
Source Files ¶
addr.go config.go edit.go errors.go imports.go pkggraph.go pkgref.go refactor.go snap.go syntax.go
- Version
- v0.0.0-20250314175034-c71790db1500 (latest)
- Published
- Mar 14, 2025
- Platform
- linux/amd64
- Imports
- 28 packages
- Last checked
- 3 weeks ago –
Tools for package owners.