8000 GitHub - loov/gorelo: A command-line tool for relocating Go declarations between files and packages. · GitHub
[go: up one dir, main page]

Skip to content
loovPublic

About

A command-line tool for relocating Go declarations between files and packages.

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

479 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gorelo

Move and rename Go declarations across files and packages.

Gorelo loads all Go packages in the current module (respecting all build constraints simultaneously), applies the requested moves and renames, and rewrites every file that references the affected declarations.

Install

go install github.com/loov/gorelo@latest

Usage

gorelo <command> [flags] [rule-or-file ...]
Command Description
apply Apply rules from files and/or inline arguments
check Print the plan without writing (dry-run of apply)

Each positional argument is either a path to a .rules file or an inline rule string. An argument naming an existing file is loaded as a file. Otherwise, arguments containing rule syntax (->, <-, =, #) or starting with @ are treated as inline rules; everything else is loaded as a file. With no arguments, gorelo.rules is loaded by default.

apply and check share these flags:

Flag Default Description
-v, --verbose false apply: print the plan to stderr before writing
--no-verify false Skip type-checking the edited packages before write

Before writing, apply and check type-check the edited packages and the packages that import them, and refuse a plan that adds type errors.

gorelo apply                                       # apply gorelo.rules
gorelo apply refactor.rules                        # different rules file
gorelo apply "Server -> server.go"                 # inline rule
gorelo apply refactor.rules "X=Y -> target.go"     # file plus inline rule
gorelo apply "@stubs" "Server -> server.go"        # with stubs directive
gorelo check                                       # preview without writing
gorelo check refactor.rules                        # preview specific file

See EXAMPLE.md for a walkthrough of splitting a flat package into subpackages (server/, db/, service/) with private-to-public renames.

Rules syntax

Moving declarations

Forward notation is compact and works well for inline rules:

Server ServerOption -> server.go

Reverse notation reads better in rules files since grouping by destination is the common case:

server.go <- Server ServerOption

Multiline reverse block (indented continuation):

server.go <-
    Server
    ServerOption
    Config

Methods follow their type. To move a method on its own, name it with its type: Widget.Render -> render.go.

Renaming

Rename a declaration at the destination with =:

OldName=NewName -> target.go

Rename a struct field or a method with Type.Name:

ServerOptions.Listen=Address
Server.handle=Handle

Rename a nested anonymous struct field with a dotted path:

ServerOptions.Limits.Min=MinValue

Rename a parameter, result, receiver or local variable with Func.name or Type.Method.name, when the function has no field or method of that name:

Serve.w=writer
Server.Start.s=srv

Gorelo refuses the rename when the new name is already declared in that scope, or when either name would shadow the other at a use. A local cannot be moved, and a function that declares the name twice (for example in two loops) cannot be targeted.

Attaching and detaching methods

Turn a standalone function into a method by writing the target method on the right of the rename:

start=Server.Start                # attach 'start' as (*Server).Start
StartServer=Server.Start          # attach 'StartServer' as (*Server).Start
start=Server.Start -> server.go   # attach and move in one rule

Turn a method into a standalone function with =! ("cut"). Leave the name after ! empty to keep the method's name:

Server.Start=!                    # detach, keep the name "Start"
Server.Start=!startServer         # detach and rename to startServer
Server.Start=! -> util.go         # detach and move

Removing declarations

Remove a declaration with =-:

Helper=-
Server.debug=-

Removing a type also removes its methods. Gorelo refuses the removal while code that stays still refers to the declaration. It cannot see a method that is only needed to satisfy an interface.

Moving whole files

Point the arrow from a .go source path to a .go destination path to move an entire file. The source file's content is transferred verbatim — declaration order, doc comments, and file-level layout are preserved:

old.go -> new.go                       # file rename within the same package
src/greet.go -> dst/greet.go           # move into a different package

The destination file must not already exist; per-declaration rules are the way to merge into an existing file. Cross-package moves reject unexported declarations with external references — add an explicit rename rule on another line to export them first.

Source specifiers

A name that matches declarations in more than one package, or methods on more than one type, is an error that lists the qualified forms to pick from (./pkg.Name, T.M).

Disambiguate by source file:

server_linux.go:Server -> server/core_linux.go

A file source picks which build-tag copy of a declaration moves. A rename applies to every copy: server_linux.go:Server=Core also renames Server in server_windows.go, because files without a build tag refer to both under one name.

By relative or full package path. The first . after the last / separates the package from the name; later dots name a field or method:

./util.Helper -> helpers.go
./util.Server.Listen=Address
github.com/loov/gorelo.Server -> server.go

A package whose last path element contains a dot (gopkg.in/yaml.v3) is found automatically: each dot is tried as the end of the package path, and the split whose package defines the named declaration wins. ./yaml.v3.Node.Kind works even when ./yaml also exists, as long as ./yaml has no type v3. When both splits define it, gorelo reports the item as ambiguous. Then write the field or method with #, which makes the last dot before it end the package path:

./yaml.v3.Node#Kind=Tag     # package ./yaml.v3, type Node
./yaml.v3#Node.Kind=Tag     # package ./yaml, type v3

Directives

Directives start with @ and configure processing behavior:

@fmt goimports          # run formatter on modified files
@stubs=true             # generate //go:fix backward-compat stubs

@stubs alone means @stubs=true. Any other directive, a @stubs value that is not a boolean, or an @fmt without a command is an error.

How it works

Gorelo uses a multi-AST approach: it discovers all .go files (including test files and platform-specific variants like _linux.go, _windows.go) and type-checks each build-constraint group. This lets it track identifiers across all platforms in a single run.

Gorelo loads every package of the module that holds the working directory, or every module of the go.work workspace when one is in effect, so running it from a subdirectory still updates importers elsewhere. File paths and ./pkg qualifiers in rules resolve against the working directory.

The compilation pipeline:

  1. Parse rules into relocation instructions
  2. Load and type-check all packages (mast.Load)
  3. Resolve declarations and synthesize related moves (e.g. methods follow their type)
  4. Compute source/target spans respecting block semantics (const, var, import groups)
  5. Check build-constraint compatibility and detect conflicts
  6. Compute rename and import edits
  7. Rewrite consumer files that reference moved/renamed declarations
  8. Assemble and apply file edits

mast-browser

A companion web UI for browsing Go packages with identifier rename-group highlighting.

go install github.com/loov/gorelo/cmd/mast-browser@latest
mast-browser -dir . -listen 127.0.0.1:8080

Click any identifier to highlight all references in the same rename group.

Contributing

See CONTRIBUTING.md.

About

A command-line tool for relocating Go declarations between files and packages.

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0